#22 #69 - Public API

This commit is contained in:
Simon Cambier
2022-06-25 13:54:25 +02:00
parent b718f8e5cb
commit a1e305d0c4
5 changed files with 246 additions and 53 deletions

37
src/api.ts Normal file
View File

@@ -0,0 +1,37 @@
import type { ResultNote, SearchMatch } from './globals'
import { Query } from './query'
import { getSuggestions } from './search'
type ResultNoteApi = {
score: number
path: string
basename: string
foundWords: string[]
matches: SearchMatch[]
}
function mapResults(results: ResultNote[]): ResultNoteApi[] {
return results.map(result => {
const { score, path, basename, foundWords, matches } = result
return {
score,
path,
basename,
foundWords,
matches: matches.map(match => {
return {
match: match.match,
offset: match.offset,
}
}),
}
})
}
async function search(q: string): Promise<ResultNoteApi[]> {
const query = new Query(q)
const raw = await getSuggestions(query)
return mapResults(raw)
}
export default { search }

View File

@@ -8,6 +8,8 @@ import {
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
import { loadSettings, SettingsTab } from './settings'
import { eventBus } from './globals'
import { registerAPI } from '@vanakat/plugin-api'
import api from './api'
// let mainWindow: { on: any; off: any } | null = null
// try {
@@ -17,9 +19,20 @@ import { eventBus } from './globals'
// console.log("Can't load electron, mobile platform")
// }
function _registerAPI(plugin: OmnisearchPlugin): void {
registerAPI('omnisearch', api, plugin)
;(app as any).plugins.plugins.omnisearch.api = api
plugin.register(() => {
delete (app as any).plugins.plugins.omnisearch.api
})
}
export default class OmnisearchPlugin extends Plugin {
async onload(): Promise<void> {
await loadSettings(this)
_registerAPI(this)
this.addSettingTab(new SettingsTab(this))
eventBus.disable('vault')
eventBus.disable('infile')