From 359292de5c045cb382067674762defec3ba0f9c3 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Wed, 20 Apr 2022 08:38:16 +0200 Subject: [PATCH] Removed modal from stores --- src/CmpModalInFile.svelte | 17 ++++++++++++----- src/CmpModalVault.svelte | 15 ++++++++------- src/modal.ts | 15 +++++++++++---- src/stores.ts | 9 +-------- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/CmpModalInFile.svelte b/src/CmpModalInFile.svelte index 38f9dcb..34c130c 100644 --- a/src/CmpModalInFile.svelte +++ b/src/CmpModalInFile.svelte @@ -2,17 +2,21 @@ import CmpInput from "./CmpInput.svelte" import CmpResultInFile from "./CmpResultInFile.svelte" import { excerptAfter, type ResultNote, type SearchMatch } from "./globals" -import { modal, plugin } from "./stores" +import { plugin } from "./stores" import { loopIndex } from "./utils" import { tick } from "svelte" import { MarkdownView } from "obsidian" import { getSuggestions } from "./search" +import type { OmnisearchModal } from "./modal" +export let modal: OmnisearchModal +export let parent: OmnisearchModal | null = null export let canGoBack = false export let singleFilePath = "" +let searchQuery: string + let groupedOffsets: number[] = [] let selectedIndex = 0 -let searchQuery: string let note: ResultNote | null = null $: { @@ -71,14 +75,18 @@ function scrollIntoView(): void { }) } -function openSelection(): void { +async function openSelection(): Promise { // TODO: clean me, merge with notes.openNote() if (note) { - $plugin.app.workspace.openLinkText(note.path, "") + modal.close() + if (parent) parent.close() + + await $plugin.app.workspace.openLinkText(note.path, "") const view = $plugin.app.workspace.getActiveViewOfType(MarkdownView) if (!view) { throw new Error("OmniSearch - No active MarkdownView") } + const offset = groupedOffsets[selectedIndex] ?? 0 const pos = view.editor.offsetToPos(offset) pos.ch = 0 @@ -87,7 +95,6 @@ function openSelection(): void { from: { line: pos.line - 10, ch: 0 }, to: { line: pos.line + 10, ch: 0 }, }) - $modal.close() } } diff --git a/src/CmpModalVault.svelte b/src/CmpModalVault.svelte index d89ad24..f74ac48 100644 --- a/src/CmpModalVault.svelte +++ b/src/CmpModalVault.svelte @@ -11,9 +11,10 @@ import type { ResultNote } from "./globals" import { OmnisearchModal } from "./modal" import { openNote } from "./notes" import { getSuggestions } from "./search" -import { modal, plugin } from "./stores" +import { plugin } from "./stores" import { loopIndex } from "./utils" +export let modal: OmnisearchModal let selectedIndex = 0 let searchQuery: string let resultNotes: ResultNote[] = [] @@ -53,34 +54,34 @@ async function createOrOpenNote(item: ResultNote): Promise { function onClick() { if (!selectedNote) return openNote(selectedNote) - $modal.close() + modal.close() } function onInputEnter(): void { // console.log(event.detail) if (!selectedNote) return openNote(selectedNote) - $modal.close() + modal.close() } function onInputCtrlEnter(): void { if (!selectedNote) return openNote(selectedNote, true) - $modal.close() + modal.close() } function onInputShiftEnter(): void { if (!selectedNote) return createOrOpenNote(selectedNote) - $modal.close() + modal.close() } function onInputAltEnter(): void { if (selectedNote) { const file = $plugin.app.vault.getAbstractFileByPath(selectedNote.path) if (file && file instanceof TFile) { - // $modal.close() - new OmnisearchModal($plugin, file, true).open() + // modal.close() + new OmnisearchModal($plugin, file, true, modal).open() } } } diff --git a/src/modal.ts b/src/modal.ts index 908588b..c05b73c 100644 --- a/src/modal.ts +++ b/src/modal.ts @@ -2,10 +2,14 @@ import { Modal, TFile } from 'obsidian' import type OmnisearchPlugin from './main' import CmpModalVault from './CmpModalVault.svelte' import CmpModalInFile from './CmpModalInFile.svelte' -import { modal } from './stores' export class OmnisearchModal extends Modal { - constructor(plugin: OmnisearchPlugin, file?: TFile, canGoBack = false) { + constructor( + plugin: OmnisearchPlugin, + file?: TFile, + canGoBack = false, + parent?: OmnisearchModal, + ) { super(plugin.app) // Remove all the default modal's children (except the close button) @@ -15,20 +19,23 @@ export class OmnisearchModal extends Modal { this.modalEl.append(closeEl) this.modalEl.addClass('omnisearch-modal', 'prompt') - modal.set(this) - if (file) { new CmpModalInFile({ target: this.modalEl, props: { + modal: this, canGoBack, singleFilePath: file.path, + parent: parent, }, }) } else { new CmpModalVault({ target: this.modalEl, + props: { + modal: this, + }, }) } } diff --git a/src/stores.ts b/src/stores.ts index 41321cc..2c34629 100644 --- a/src/stores.ts +++ b/src/stores.ts @@ -1,8 +1,6 @@ -import type { TFile } from 'obsidian' import { get, writable } from 'svelte/store' -import type { IndexedNote, ResultNote } from './globals' +import type { IndexedNote } from './globals' import type OmnisearchPlugin from './main' -import type { OmnisearchModal } from './modal' function createIndexedNotes() { const { subscribe, set, update } = writable>({}) @@ -32,11 +30,6 @@ function createIndexedNotes() { */ export const plugin = writable() -/** - * A reference to the modal instance - */ -export const modal = writable() - /** * The entire list of indexed notes, constantly kept up-to-date. */