#57 - only update the index when Omnisearch is loaded
This commit is contained in:
@@ -9,7 +9,7 @@ import InputSearch from "./InputSearch.svelte"
|
|||||||
import ModalContainer from "./ModalContainer.svelte"
|
import ModalContainer from "./ModalContainer.svelte"
|
||||||
import { eventBus, type ResultNote } from "src/globals"
|
import { eventBus, type ResultNote } from "src/globals"
|
||||||
import { createNote, openNote } from "src/notes"
|
import { createNote, openNote } from "src/notes"
|
||||||
import { getSuggestions } from "src/search"
|
import { getSuggestions, reindexNotes } from "src/search"
|
||||||
import { loopIndex } from "src/utils"
|
import { loopIndex } from "src/utils"
|
||||||
import { OmnisearchInFileModal, type OmnisearchVaultModal } from "src/modals"
|
import { OmnisearchInFileModal, type OmnisearchVaultModal } from "src/modals"
|
||||||
import ResultItemVault from "./ResultItemVault.svelte"
|
import ResultItemVault from "./ResultItemVault.svelte"
|
||||||
@@ -29,6 +29,7 @@ $: if (searchQuery) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
reindexNotes()
|
||||||
searchQuery = lastSearch
|
searchQuery = lastSearch
|
||||||
eventBus.on("vault", "enter", onInputEnter)
|
eventBus.on("vault", "enter", onInputEnter)
|
||||||
eventBus.on("vault", "shift-enter", onInputShiftEnter)
|
eventBus.on("vault", "shift-enter", onInputShiftEnter)
|
||||||
|
|||||||
39
src/main.ts
39
src/main.ts
@@ -3,30 +3,21 @@ import {
|
|||||||
addNoteToReindex,
|
addNoteToReindex,
|
||||||
addToIndex,
|
addToIndex,
|
||||||
initGlobalSearchIndex,
|
initGlobalSearchIndex,
|
||||||
reindexNotes,
|
|
||||||
removeFromIndex,
|
removeFromIndex,
|
||||||
} from './search'
|
} from './search'
|
||||||
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
|
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
|
||||||
import { loadSettings, settings, SettingsTab } from './settings'
|
import { loadSettings, SettingsTab } from './settings'
|
||||||
|
|
||||||
let mainWindow: { on: any; off: any } | null = null
|
// let mainWindow: { on: any; off: any } | null = null
|
||||||
try {
|
// try {
|
||||||
mainWindow = require('electron').remote.getCurrentWindow()
|
// mainWindow = require('electron').remote.getCurrentWindow()
|
||||||
}
|
// }
|
||||||
catch (e) {
|
// catch (e) {
|
||||||
console.log("Can't load electron, mobile platform")
|
// console.log("Can't load electron, mobile platform")
|
||||||
}
|
// }
|
||||||
|
|
||||||
const onBlur = (): void => {
|
|
||||||
reindexNotes()
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class OmnisearchPlugin extends Plugin {
|
export default class OmnisearchPlugin extends Plugin {
|
||||||
async onload(): Promise<void> {
|
async onload(): Promise<void> {
|
||||||
if (mainWindow) {
|
|
||||||
mainWindow.on('blur', onBlur)
|
|
||||||
}
|
|
||||||
|
|
||||||
await loadSettings(this)
|
await loadSettings(this)
|
||||||
this.addSettingTab(new SettingsTab(this))
|
this.addSettingTab(new SettingsTab(this))
|
||||||
|
|
||||||
@@ -61,13 +52,7 @@ 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) {
|
addNoteToReindex(file)
|
||||||
removeFromIndex(file.path)
|
|
||||||
await addToIndex(file)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
addNoteToReindex(file)
|
|
||||||
}
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
this.registerEvent(
|
this.registerEvent(
|
||||||
@@ -83,9 +68,5 @@ export default class OmnisearchPlugin extends Plugin {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
onunload(): void {
|
onunload(): void {}
|
||||||
if (mainWindow) {
|
|
||||||
mainWindow.off('blur', onBlur)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -388,9 +388,13 @@ export function addNoteToReindex(note: TAbstractFile): void {
|
|||||||
notesToReindex.add(note)
|
notesToReindex.add(note)
|
||||||
}
|
}
|
||||||
export async function reindexNotes(): Promise<void> {
|
export async function reindexNotes(): Promise<void> {
|
||||||
|
if (settings.showIndexingNotices && notesToReindex.size > 0) {
|
||||||
|
new Notice(`Omnisearch - Reindexing ${notesToReindex.size} notes`, 2000)
|
||||||
|
}
|
||||||
for (const note of notesToReindex) {
|
for (const note of notesToReindex) {
|
||||||
removeFromIndex(note.path)
|
removeFromIndex(note.path)
|
||||||
await addToIndex(note)
|
await addToIndex(note)
|
||||||
|
await wait(0)
|
||||||
}
|
}
|
||||||
notesToReindex.clear()
|
notesToReindex.clear()
|
||||||
|
|
||||||
@@ -401,7 +405,7 @@ async function saveIndexToFile(): Promise<void> {
|
|||||||
if (settings.storeIndexInFile && minisearchInstance && isIndexChanged) {
|
if (settings.storeIndexInFile && minisearchInstance && isIndexChanged) {
|
||||||
const json = JSON.stringify(minisearchInstance)
|
const json = JSON.stringify(minisearchInstance)
|
||||||
await app.vault.adapter.write(searchIndexFilePath, json)
|
await app.vault.adapter.write(searchIndexFilePath, json)
|
||||||
console.log('MiniSearch index saved to the file')
|
console.log('Omnisearch - Index saved on disk')
|
||||||
|
|
||||||
await saveNotesCacheToFile()
|
await saveNotesCacheToFile()
|
||||||
isIndexChanged = false
|
isIndexChanged = false
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ interface WeightingSettings {
|
|||||||
|
|
||||||
export interface OmnisearchSettings extends WeightingSettings {
|
export interface OmnisearchSettings extends WeightingSettings {
|
||||||
respectExcluded: boolean
|
respectExcluded: boolean
|
||||||
reindexInRealTime: boolean
|
|
||||||
ignoreDiacritics: boolean
|
ignoreDiacritics: boolean
|
||||||
showIndexingNotices: boolean
|
showIndexingNotices: boolean
|
||||||
showShortName: boolean
|
showShortName: boolean
|
||||||
@@ -38,25 +37,6 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
|
|
||||||
new Setting(containerEl).setName('Behavior').setHeading()
|
new Setting(containerEl).setName('Behavior').setHeading()
|
||||||
|
|
||||||
// Index in real-time, desktop only
|
|
||||||
if (require('electron')) {
|
|
||||||
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 Obsidian's performances when editing large notes.",
|
|
||||||
)
|
|
||||||
.addToggle(toggle =>
|
|
||||||
toggle.setValue(settings.reindexInRealTime).onChange(async v => {
|
|
||||||
settings.reindexInRealTime = v
|
|
||||||
await saveSettings(this.plugin)
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// No real time indexing on mobile
|
|
||||||
settings.reindexInRealTime = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Respect excluded files
|
// Respect excluded files
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName('Respect Obsidian\'s "Excluded Files"')
|
.setName('Respect Obsidian\'s "Excluded Files"')
|
||||||
@@ -193,7 +173,6 @@ export class SettingsTab extends PluginSettingTab {
|
|||||||
|
|
||||||
export const DEFAULT_SETTINGS: OmnisearchSettings = {
|
export const DEFAULT_SETTINGS: OmnisearchSettings = {
|
||||||
respectExcluded: true,
|
respectExcluded: true,
|
||||||
reindexInRealTime: false,
|
|
||||||
ignoreDiacritics: false,
|
ignoreDiacritics: false,
|
||||||
|
|
||||||
showIndexingNotices: false,
|
showIndexingNotices: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user