Removed modal from stores

This commit is contained in:
Simon Cambier
2022-04-20 08:38:16 +02:00
parent 035d4ff957
commit 359292de5c
4 changed files with 32 additions and 24 deletions

View File

@@ -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<void> {
// 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()
}
}
</script>

View File

@@ -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<void> {
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()
}
}
}

View File

@@ -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,
},
})
}
}

View File

@@ -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<Record<string, IndexedNote>>({})
@@ -32,11 +30,6 @@ function createIndexedNotes() {
*/
export const plugin = writable<OmnisearchPlugin>()
/**
* A reference to the modal instance
*/
export const modal = writable<OmnisearchModal>()
/**
* The entire list of indexed notes, constantly kept up-to-date.
*/