Merge branch 'develop'

This commit is contained in:
Simon Cambier
2024-05-29 18:58:58 +02:00
27 changed files with 1874 additions and 1832 deletions

View File

@@ -4,10 +4,12 @@ import {
OmnisearchVaultModal,
} from './components/modals'
import {
getDefaultSettings,
isCacheEnabled,
isPluginDisabled,
loadSettings,
type OmnisearchSettings,
saveSettings,
settings,
SettingsTab,
showExcerpt,
} from './settings'
@@ -16,46 +18,57 @@ import {
EventNames,
indexingStep,
IndexingStepType,
isCacheEnabled,
type TextExtractorApi,
} from './globals'
import api, { notifyOnIndexed } from './tools/api'
import { isFileIndexable, logDebug } from './tools/utils'
import { OmnisearchCache, database } from './database'
import * as NotesIndex from './notes-index'
import { searchEngine } from './search/omnisearch'
import { cacheManager } from './cache-manager'
import { setObsidianApp } from './stores/obsidian-app'
import { notifyOnIndexed, registerAPI } from './tools/api'
import { Database } from './database'
import { SearchEngine } from './search/search-engine'
import { CacheManager } from './cache-manager'
import { logDebug } from './tools/utils'
import { NotesIndexer } from './notes-indexer'
import { TextProcessor } from './tools/text-processing'
export default class OmnisearchPlugin extends Plugin {
// FIXME: fix the type
public apiHttpServer: null | any = null
public settings: OmnisearchSettings = getDefaultSettings(this.app)
// FIXME: merge cache and cacheManager, or find other names
public readonly cacheManager: CacheManager
public readonly database = new Database(this)
public readonly notesIndexer = new NotesIndexer(this)
public readonly textProcessor = new TextProcessor(this)
public readonly searchEngine = new SearchEngine(this)
private ribbonButton?: HTMLElement
constructor(app: App, manifest: PluginManifest) {
super(app, manifest)
setObsidianApp(this.app)
this.cacheManager = new CacheManager(this)
}
async onload(): Promise<void> {
await loadSettings(this)
this.settings = await loadSettings(this)
this.addSettingTab(new SettingsTab(this))
if (!Platform.isMobile) {
import('./tools/api-server').then(
m => (this.apiHttpServer = m.getServer())
m => (this.apiHttpServer = m.getServer(this))
)
}
if (isPluginDisabled()) {
if (isPluginDisabled(this.app)) {
console.log('Omnisearch - Plugin disabled')
return
}
await cleanOldCacheFiles(this.app)
await OmnisearchCache.clearOldDatabases()
await this.database.clearOldDatabases()
registerAPI(this)
const settings = this.settings
if (settings.ribbonIcon) {
this.addRibbonButton()
}
@@ -71,7 +84,7 @@ export default class OmnisearchPlugin extends Plugin {
id: 'show-modal',
name: 'Vault search',
callback: () => {
new OmnisearchVaultModal(this.app).open()
new OmnisearchVaultModal(this).open()
},
})
@@ -80,16 +93,18 @@ export default class OmnisearchPlugin extends Plugin {
name: 'In-file search',
editorCallback: (_editor, view) => {
if (view.file) {
new OmnisearchInFileModal(this.app, view.file).open()
new OmnisearchInFileModal(this, view.file).open()
}
},
})
const searchEngine = this.searchEngine
this.app.workspace.onLayoutReady(async () => {
// Listeners to keep the search index up-to-date
this.registerEvent(
this.app.vault.on('create', file => {
if (isFileIndexable(file.path)) {
if (this.notesIndexer.isFileIndexable(file.path)) {
logDebug('Indexing new file', file.path)
// await cacheManager.addToLiveCache(file.path)
searchEngine.addFromPaths([file.path])
@@ -99,25 +114,25 @@ export default class OmnisearchPlugin extends Plugin {
this.registerEvent(
this.app.vault.on('delete', file => {
logDebug('Removing file', file.path)
cacheManager.removeFromLiveCache(file.path)
this.cacheManager.removeFromLiveCache(file.path)
searchEngine.removeFromPaths([file.path])
})
)
this.registerEvent(
this.app.vault.on('modify', async file => {
if (isFileIndexable(file.path)) {
if (this.notesIndexer.isFileIndexable(file.path)) {
logDebug('Updating file', file.path)
await cacheManager.addToLiveCache(file.path)
NotesIndex.markNoteForReindex(file)
await this.cacheManager.addToLiveCache(file.path)
this.notesIndexer.flagNoteForReindex(file)
}
})
)
this.registerEvent(
this.app.vault.on('rename', async (file, oldPath) => {
if (isFileIndexable(file.path)) {
if (this.notesIndexer.isFileIndexable(file.path)) {
logDebug('Renaming file', file.path)
cacheManager.removeFromLiveCache(oldPath)
await cacheManager.addToLiveCache(file.path)
this.cacheManager.removeFromLiveCache(oldPath)
await this.cacheManager.addToLiveCache(file.path)
searchEngine.removeFromPaths([oldPath])
await searchEngine.addFromPaths([file.path])
}
@@ -142,8 +157,8 @@ export default class OmnisearchPlugin extends Plugin {
// })
// new Notice(welcome, 20_000)
// }
settings.welcomeMessage = code
await this.saveData(settings)
this.settings.welcomeMessage = code
await this.saveData(this.settings)
}
async onunload(): Promise<void> {
@@ -152,14 +167,14 @@ export default class OmnisearchPlugin extends Plugin {
// Clear cache when disabling Omnisearch
if (process.env.NODE_ENV === 'production') {
await database.clearCache()
await this.database.clearCache()
}
this.apiHttpServer.close()
}
addRibbonButton(): void {
this.ribbonButton = this.addRibbonIcon('search', 'Omnisearch', _evt => {
new OmnisearchVaultModal(this.app).open()
new OmnisearchVaultModal(this).open()
})
}
@@ -169,10 +184,28 @@ export default class OmnisearchPlugin extends Plugin {
}
}
/**
* Plugin dependency - Chs Patch for Chinese word segmentation
* @returns
*/
public getChsSegmenter(): any | undefined {
return (this.app as any).plugins.plugins['cm-chs-patch']
}
/**
* Plugin dependency - Text Extractor
* @returns
*/
public getTextExtractor(): TextExtractorApi | undefined {
return (this.app as any).plugins?.plugins?.['text-extractor']?.api
}
private async populateIndex(): Promise<void> {
console.time('Omnisearch - Indexing total time')
indexingStep.set(IndexingStepType.ReadingFiles)
const files = this.app.vault.getFiles().filter(f => isFileIndexable(f.path))
const files = this.app.vault
.getFiles()
.filter(f => this.notesIndexer.isFileIndexable(f.path))
console.log(`Omnisearch - ${files.length} files total`)
console.log(
`Omnisearch - Cache is ${isCacheEnabled() ? 'enabled' : 'disabled'}`
@@ -180,6 +213,7 @@ export default class OmnisearchPlugin extends Plugin {
// Map documents in the background
// Promise.all(files.map(f => cacheManager.addToLiveCache(f.path)))
const searchEngine = this.searchEngine
if (isCacheEnabled()) {
console.time('Omnisearch - Loading index from cache')
indexingStep.set(IndexingStepType.LoadingCache)
@@ -223,14 +257,14 @@ export default class OmnisearchPlugin extends Plugin {
indexingStep.set(IndexingStepType.WritingCache)
// Disable settings.useCache while writing the cache, in case it freezes
settings.useCache = false
this.settings.useCache = false
await saveSettings(this)
// Write the cache
await searchEngine.writeToCache()
// Re-enable settings.caching
settings.useCache = true
this.settings.useCache = true
await saveSettings(this)
}
@@ -264,16 +298,3 @@ async function cleanOldCacheFiles(app: App) {
}
}
}
function registerAPI(plugin: OmnisearchPlugin): void {
// Url scheme for obsidian://omnisearch?query=foobar
plugin.registerObsidianProtocolHandler('omnisearch', params => {
new OmnisearchVaultModal(plugin.app, params.query).open()
})
// Public api
// @ts-ignore
globalThis['omnisearch'] = api
// Deprecated
;(plugin.app as any).plugins.plugins.omnisearch.api = api
}