Taking advantage of app as a global var

This commit is contained in:
Simon Cambier
2022-04-21 21:39:33 +02:00
parent 4238bc510e
commit 244211bd69
9 changed files with 132 additions and 125 deletions

View File

@@ -6,7 +6,6 @@ let lastSearch = ""
import CmpInput from "./CmpInput.svelte"
import CmpResultInFile from "./CmpResultInFile.svelte"
import { excerptAfter, type ResultNote, type SearchMatch } from "./globals"
import { plugin } from "./stores"
import { loopIndex } from "./utils"
import { onMount, tick } from "svelte"
import { MarkdownView } from "obsidian"
@@ -91,8 +90,8 @@ async function openSelection(): Promise<void> {
modal.close()
if (parent) parent.close()
await $plugin.app.workspace.openLinkText(note.path, "")
const view = $plugin.app.workspace.getActiveViewOfType(MarkdownView)
await app.workspace.openLinkText(note.path, "")
const view = app.workspace.getActiveViewOfType(MarkdownView)
if (!view) {
throw new Error("OmniSearch - No active MarkdownView")
}

View File

@@ -11,7 +11,6 @@ import type { ResultNote } from "./globals"
import { ModalInFile, type ModalVault } from "./modal"
import { openNote } from "./notes"
import { getSuggestions } from "./search"
import { plugin } from "./stores"
import { loopIndex } from "./utils"
export let modal: ModalVault
@@ -35,11 +34,8 @@ onMount(() => {
async function createOrOpenNote(item: ResultNote): Promise<void> {
try {
const file = await $plugin.app.vault.create(
searchQuery + ".md",
"# " + searchQuery
)
await $plugin.app.workspace.openLinkText(file.path, "")
const file = await app.vault.create(searchQuery + ".md", "# " + searchQuery)
await app.workspace.openLinkText(file.path, "")
} catch (e) {
if (e instanceof Error && e.message === "File already exists.") {
// Open the existing file instead of creating it
@@ -77,10 +73,10 @@ function onInputShiftEnter(): void {
function onInputAltEnter(): void {
if (selectedNote) {
const file = $plugin.app.vault.getAbstractFileByPath(selectedNote.path)
const file = app.vault.getAbstractFileByPath(selectedNote.path)
if (file && file instanceof TFile) {
// modal.close()
new ModalInFile($plugin, file, searchQuery, modal).open()
new ModalInFile(app, file, searchQuery, modal).open()
}
}
}

View File

@@ -1,5 +1,5 @@
/* global app */ // make eslint happy with Obisidian's global app reference
import { MarkdownView, Plugin, TFile } from 'obsidian'
import { plugin } from './stores'
import {
addToIndex,
initGlobalSearchIndex,
@@ -10,14 +10,12 @@ import { ModalInFile, ModalVault } from './modal'
export default class OmnisearchPlugin extends Plugin {
async onload(): Promise<void> {
plugin.set(this)
// Commands to display Omnisearch modals
this.addCommand({
id: 'show-modal',
name: 'Vault search',
callback: () => {
new ModalVault(this).open()
new ModalVault(app).open()
},
})
@@ -25,10 +23,10 @@ export default class OmnisearchPlugin extends Plugin {
id: 'show-modal-infile',
name: 'In-file search',
checkCallback: (checking: boolean) => {
const view = this.app.workspace.getActiveViewOfType(MarkdownView)
const view = app.workspace.getActiveViewOfType(MarkdownView)
if (view) {
if (!checking) {
new ModalInFile(this, view.file).open()
new ModalInFile(app, view.file).open()
}
return true
}

View File

@@ -1,11 +1,10 @@
import { Modal, TFile } from 'obsidian'
import type OmnisearchPlugin from './main'
import { App, Modal, TFile } from 'obsidian'
import CmpModalVault from './CmpModalVault.svelte'
import CmpModalInFile from './CmpModalInFile.svelte'
abstract class ModalOmnisearch extends Modal {
constructor(plugin: OmnisearchPlugin) {
super(plugin.app)
constructor(app: App) {
super(app)
// Remove all the default modal's children (except the close button)
// so that we can more easily customize it
@@ -17,8 +16,8 @@ abstract class ModalOmnisearch extends Modal {
}
export class ModalVault extends ModalOmnisearch {
constructor(plugin: OmnisearchPlugin) {
super(plugin)
constructor(app: App) {
super(app)
new CmpModalVault({
target: this.modalEl,
@@ -31,12 +30,12 @@ export class ModalVault extends ModalOmnisearch {
export class ModalInFile extends ModalOmnisearch {
constructor(
plugin: OmnisearchPlugin,
app: App,
file: TFile,
searchQuery: string = '',
parent?: ModalOmnisearch,
) {
super(plugin)
super(app)
if (parent) {
// Hide the parent modal

View File

@@ -1,14 +1,11 @@
import { MarkdownView } from 'obsidian'
import { get } from 'svelte/store'
import type { ResultNote } from './globals'
import { plugin } from './stores'
import { stringsToRegex } from './utils'
export async function openNote(
item: ResultNote,
newPane = false,
): Promise<void> {
const app = get(plugin).app
const reg = stringsToRegex(item.foundWords)
reg.exec(item.content)
const offset = reg.lastIndex

View File

@@ -6,7 +6,6 @@ import {
type ResultNote,
type SearchMatch,
} from './globals'
import { plugin } from './stores'
import { get } from 'svelte/store'
import { extractHeadingsFromCache, stringsToRegex, wait } from './utils'
@@ -28,7 +27,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
// Index files that are already present
const start = new Date().getTime()
const files = get(plugin).app.vault.getMarkdownFiles()
const files = app.vault.getMarkdownFiles()
// This is basically the same behavior as MiniSearch's `addAllAsync()`.
// We index files by batches of 10
@@ -147,7 +146,6 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
return
}
try {
const app = get(plugin).app
// console.log(`Omnisearch - adding ${file.path} to index`)
const fileCache = app.metadataCache.getFileCache(file)
// console.log(fileCache)

View File

@@ -1,7 +1,2 @@
import { writable } from 'svelte/store'
import type OmnisearchPlugin from './main'
/**
* A reference to the plugin instance
*/
export const plugin = writable<OmnisearchPlugin>()