Massive refactor to get rid of the global app.
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import type { ResultNote } from '../globals'
|
||||
import { Query } from '../search/query'
|
||||
import { makeExcerpt } from './text-processing'
|
||||
import { refreshIndex } from '../notes-index'
|
||||
import { getObsidianApp } from '../stores/obsidian-app'
|
||||
import { Omnisearch } from 'src/search/omnisearch'
|
||||
import type OmnisearchPlugin from '../main'
|
||||
import { OmnisearchVaultModal } from '../components/modals'
|
||||
|
||||
type ResultNoteApi = {
|
||||
score: number
|
||||
@@ -20,7 +18,6 @@ export type SearchMatchApi = {
|
||||
offset: number
|
||||
}
|
||||
|
||||
|
||||
let notified = false
|
||||
|
||||
/**
|
||||
@@ -28,15 +25,21 @@ let notified = false
|
||||
*/
|
||||
let onIndexedCallbacks: Array<() => void> = []
|
||||
|
||||
function mapResults(results: ResultNote[]): ResultNoteApi[] {
|
||||
function mapResults(
|
||||
plugin: OmnisearchPlugin,
|
||||
results: ResultNote[]
|
||||
): ResultNoteApi[] {
|
||||
return results.map(result => {
|
||||
const { score, path, basename, foundWords, matches, content } = result
|
||||
|
||||
const excerpt = makeExcerpt(content, matches[0]?.offset ?? -1)
|
||||
const excerpt = plugin.textProcessor.makeExcerpt(
|
||||
content,
|
||||
matches[0]?.offset ?? -1
|
||||
)
|
||||
|
||||
const res: ResultNoteApi = {
|
||||
score,
|
||||
vault: getObsidianApp().vault.getName(),
|
||||
vault: plugin.app.vault.getName(),
|
||||
path,
|
||||
basename,
|
||||
foundWords,
|
||||
@@ -53,27 +56,52 @@ function mapResults(results: ResultNote[]): ResultNoteApi[] {
|
||||
})
|
||||
}
|
||||
|
||||
async function search(q: string): Promise<ResultNoteApi[]> {
|
||||
const query = new Query(q)
|
||||
const raw = await Omnisearch.getInstance().getSuggestions(query)
|
||||
return mapResults(raw)
|
||||
}
|
||||
|
||||
function registerOnIndexed(cb: () => void): void {
|
||||
onIndexedCallbacks.push(cb)
|
||||
// Immediately call the callback if the indexing is already ready done
|
||||
if (notified) {
|
||||
cb()
|
||||
}
|
||||
}
|
||||
|
||||
function unregisterOnIndexed(cb: () => void): void {
|
||||
onIndexedCallbacks = onIndexedCallbacks.filter(o => o !== cb)
|
||||
}
|
||||
|
||||
export function notifyOnIndexed(): void {
|
||||
notified = true
|
||||
onIndexedCallbacks.forEach(cb => cb())
|
||||
}
|
||||
|
||||
export default { search, registerOnIndexed, unregisterOnIndexed, refreshIndex }
|
||||
let registed = false
|
||||
|
||||
export function registerAPI(plugin: OmnisearchPlugin): void {
|
||||
if (registed) {
|
||||
return
|
||||
}
|
||||
registed = true
|
||||
|
||||
// Url scheme for obsidian://omnisearch?query=foobar
|
||||
plugin.registerObsidianProtocolHandler('omnisearch', params => {
|
||||
new OmnisearchVaultModal(plugin, params.query).open()
|
||||
})
|
||||
|
||||
const api = getApi(plugin)
|
||||
|
||||
// Public api
|
||||
// @ts-ignore
|
||||
globalThis['omnisearch'] = api
|
||||
// Deprecated
|
||||
;(plugin.app as any).plugins.plugins.omnisearch.api = api
|
||||
}
|
||||
|
||||
export function getApi(plugin: OmnisearchPlugin) {
|
||||
return {
|
||||
async search(q: string): Promise<ResultNoteApi[]> {
|
||||
const query = new Query(q, {
|
||||
ignoreDiacritics: plugin.settings.ignoreDiacritics,
|
||||
})
|
||||
const raw = await plugin.omnisearch.getSuggestions(query)
|
||||
return mapResults(plugin, raw)
|
||||
},
|
||||
registerOnIndexed(cb: () => void): void {
|
||||
onIndexedCallbacks.push(cb)
|
||||
// Immediately call the callback if the indexing is already ready done
|
||||
if (notified) {
|
||||
cb()
|
||||
}
|
||||
},
|
||||
unregisterOnIndexed(cb: () => void): void {
|
||||
onIndexedCallbacks = onIndexedCallbacks.filter(o => o !== cb)
|
||||
},
|
||||
refreshIndex: plugin.notesIndexer.refreshIndex,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user