Fixed #57
This commit is contained in:
20
src/main.ts
20
src/main.ts
@@ -1,14 +1,23 @@
|
|||||||
import { Plugin, TFile } from 'obsidian'
|
import { Plugin, TFile } from 'obsidian'
|
||||||
import {
|
import {
|
||||||
|
addNoteToReindex,
|
||||||
addToIndex,
|
addToIndex,
|
||||||
initGlobalSearchIndex,
|
initGlobalSearchIndex,
|
||||||
|
reindexNotes,
|
||||||
removeFromIndex,
|
removeFromIndex,
|
||||||
} from './search'
|
} from './search'
|
||||||
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
|
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
|
||||||
import { loadSettings, SettingsTab } from './settings'
|
import { loadSettings, settings, SettingsTab } from './settings'
|
||||||
|
|
||||||
|
const mainWindow = require('electron').remote.getCurrentWindow()
|
||||||
|
const onBlur = (): void => {
|
||||||
|
reindexNotes()
|
||||||
|
}
|
||||||
|
|
||||||
export default class OmnisearchPlugin extends Plugin {
|
export default class OmnisearchPlugin extends Plugin {
|
||||||
async onload(): Promise<void> {
|
async onload(): Promise<void> {
|
||||||
|
mainWindow.on('blur', onBlur)
|
||||||
|
|
||||||
await loadSettings(this)
|
await loadSettings(this)
|
||||||
this.addSettingTab(new SettingsTab(this))
|
this.addSettingTab(new SettingsTab(this))
|
||||||
|
|
||||||
@@ -43,8 +52,13 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
)
|
)
|
||||||
this.registerEvent(
|
this.registerEvent(
|
||||||
this.app.vault.on('modify', async file => {
|
this.app.vault.on('modify', async file => {
|
||||||
|
if (settings.reindexInRealTime) {
|
||||||
removeFromIndex(file.path)
|
removeFromIndex(file.path)
|
||||||
await addToIndex(file)
|
await addToIndex(file)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
addNoteToReindex(file)
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
this.registerEvent(
|
this.registerEvent(
|
||||||
@@ -59,4 +73,8 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
await initGlobalSearchIndex()
|
await initGlobalSearchIndex()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onunload(): void {
|
||||||
|
mainWindow.off('blur', onBlur)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Notice, TFile, type TAbstractFile } from 'obsidian'
|
import { Notice, TAbstractFile, TFile } from 'obsidian'
|
||||||
import MiniSearch, { type SearchResult } from 'minisearch'
|
import MiniSearch, { type SearchResult } from 'minisearch'
|
||||||
import {
|
import {
|
||||||
chsRegex,
|
chsRegex,
|
||||||
@@ -315,7 +315,9 @@ export function removeFromIndex(path: string): void {
|
|||||||
if (note) {
|
if (note) {
|
||||||
minisearchInstance.remove(note)
|
minisearchInstance.remove(note)
|
||||||
removeNoteFromCache(path)
|
removeNoteFromCache(path)
|
||||||
getNonExistingNotesFromCache().filter(n => n.parent === path).forEach(n => {
|
getNonExistingNotesFromCache()
|
||||||
|
.filter(n => n.parent === path)
|
||||||
|
.forEach(n => {
|
||||||
removeFromIndex(n.path)
|
removeFromIndex(n.path)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -323,3 +325,15 @@ export function removeFromIndex(path: string): void {
|
|||||||
console.warn(`not not found under path ${path}`)
|
console.warn(`not not found under path ${path}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const notesToReindex = new Set<TAbstractFile>()
|
||||||
|
export function addNoteToReindex(note: TAbstractFile): void {
|
||||||
|
notesToReindex.add(note)
|
||||||
|
}
|
||||||
|
export async function reindexNotes(): Promise<void> {
|
||||||
|
for (const note of notesToReindex) {
|
||||||
|
removeFromIndex(note.path)
|
||||||
|
await addToIndex(note)
|
||||||
|
}
|
||||||
|
notesToReindex.clear()
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ interface WeightingSettings {
|
|||||||
export interface OmnisearchSettings extends WeightingSettings {
|
export interface OmnisearchSettings extends WeightingSettings {
|
||||||
showIndexingNotices: boolean
|
showIndexingNotices: boolean
|
||||||
respectExcluded: boolean
|
respectExcluded: boolean
|
||||||
|
reindexInRealTime: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SettingsTab extends PluginSettingTab {
|
export class SettingsTab extends PluginSettingTab {
|
||||||
@@ -52,10 +53,23 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Index in real-time
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName('Reindex in real-time')
|
||||||
|
.setDesc('By default, notes a reindexed when Obsidian focus is lost. Enable this to reindex in real-time. May affect performances.')
|
||||||
|
.addToggle(toggle =>
|
||||||
|
toggle.setValue(settings.reindexInRealTime).onChange(async v => {
|
||||||
|
settings.reindexInRealTime = v
|
||||||
|
await saveSettings(this.plugin)
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
new Setting(containerEl).setName('Results weighting').setHeading()
|
new Setting(containerEl).setName('Results weighting').setHeading()
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName(`File name & declared aliases (default: ${DEFAULT_SETTINGS.weightBasename})`)
|
.setName(
|
||||||
|
`File name & declared aliases (default: ${DEFAULT_SETTINGS.weightBasename})`,
|
||||||
|
)
|
||||||
.addSlider(cb => this.weightSlider(cb, 'weightBasename'))
|
.addSlider(cb => this.weightSlider(cb, 'weightBasename'))
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
@@ -85,6 +99,7 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
export const DEFAULT_SETTINGS: OmnisearchSettings = {
|
export const DEFAULT_SETTINGS: OmnisearchSettings = {
|
||||||
showIndexingNotices: false,
|
showIndexingNotices: false,
|
||||||
respectExcluded: true,
|
respectExcluded: true,
|
||||||
|
reindexInRealTime: false,
|
||||||
weightBasename: 2,
|
weightBasename: 2,
|
||||||
weightH1: 1.5,
|
weightH1: 1.5,
|
||||||
weightH2: 1.3,
|
weightH2: 1.3,
|
||||||
|
|||||||
Reference in New Issue
Block a user