37
src/api.ts
Normal file
37
src/api.ts
Normal 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 }
|
||||
13
src/main.ts
13
src/main.ts
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user