Removed indexedNotes from stores
Since the components don't need to access it anymore
This commit is contained in:
@@ -1,18 +1,25 @@
|
|||||||
import { Notice, TFile, type TAbstractFile } from 'obsidian'
|
import { Notice, TFile, type TAbstractFile } from 'obsidian'
|
||||||
import MiniSearch, { type SearchResult } from 'minisearch'
|
import MiniSearch, { type SearchResult } from 'minisearch'
|
||||||
import { SPACE_OR_PUNCTUATION, type IndexedNote, type ResultNote, type SearchMatch } from './globals'
|
import {
|
||||||
import { indexedNotes, plugin } from './stores'
|
SPACE_OR_PUNCTUATION,
|
||||||
|
type IndexedNote,
|
||||||
|
type ResultNote,
|
||||||
|
type SearchMatch,
|
||||||
|
} from './globals'
|
||||||
|
import { plugin } from './stores'
|
||||||
import { get } from 'svelte/store'
|
import { get } from 'svelte/store'
|
||||||
import { extractHeadingsFromCache, stringsToRegex, wait } from './utils'
|
import { extractHeadingsFromCache, stringsToRegex, wait } from './utils'
|
||||||
|
|
||||||
let minisearchInstance: MiniSearch<IndexedNote>
|
let minisearchInstance: MiniSearch<IndexedNote>
|
||||||
|
|
||||||
|
let indexedNotes: Record<string, IndexedNote> = {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the MiniSearch instance,
|
* Initializes the MiniSearch instance,
|
||||||
* and adds all the notes to the index
|
* and adds all the notes to the index
|
||||||
*/
|
*/
|
||||||
export async function initGlobalSearchIndex(): Promise<void> {
|
export async function initGlobalSearchIndex(): Promise<void> {
|
||||||
indexedNotes.set({})
|
indexedNotes = {}
|
||||||
minisearchInstance = new MiniSearch({
|
minisearchInstance = new MiniSearch({
|
||||||
tokenize: text => text.split(SPACE_OR_PUNCTUATION),
|
tokenize: text => text.split(SPACE_OR_PUNCTUATION),
|
||||||
idField: 'path',
|
idField: 'path',
|
||||||
@@ -112,7 +119,7 @@ export function getSuggestions(
|
|||||||
|
|
||||||
// Map the raw results to get usable suggestions
|
// Map the raw results to get usable suggestions
|
||||||
const suggestions = results.map(result => {
|
const suggestions = results.map(result => {
|
||||||
const note = indexedNotes.get(result.id)
|
const note = indexedNotes[result.id]
|
||||||
if (!note) {
|
if (!note) {
|
||||||
throw new Error(`Note "${result.id}" not indexed`)
|
throw new Error(`Note "${result.id}" not indexed`)
|
||||||
}
|
}
|
||||||
@@ -145,7 +152,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
|
|||||||
const fileCache = app.metadataCache.getFileCache(file)
|
const fileCache = app.metadataCache.getFileCache(file)
|
||||||
// console.log(fileCache)
|
// console.log(fileCache)
|
||||||
|
|
||||||
if (indexedNotes.get(file.path)) {
|
if (indexedNotes[file.path]) {
|
||||||
throw new Error(`${file.basename} is already indexed`)
|
throw new Error(`${file.basename} is already indexed`)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,7 +175,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
|
|||||||
: '',
|
: '',
|
||||||
}
|
}
|
||||||
minisearchInstance.add(note)
|
minisearchInstance.add(note)
|
||||||
indexedNotes.add(note)
|
indexedNotes[note.path] = note
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
console.trace('Error while indexing ' + file.basename)
|
console.trace('Error while indexing ' + file.basename)
|
||||||
@@ -193,9 +200,9 @@ export function removeFromIndex(file: TAbstractFile): void {
|
|||||||
* @param path
|
* @param path
|
||||||
*/
|
*/
|
||||||
export function removeFromIndexByPath(path: string): void {
|
export function removeFromIndexByPath(path: string): void {
|
||||||
const note = indexedNotes.get(path)
|
const note = indexedNotes[path]
|
||||||
if (note) {
|
if (note) {
|
||||||
minisearchInstance.remove(note)
|
minisearchInstance.remove(note)
|
||||||
indexedNotes.remove(path)
|
delete indexedNotes[path]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,36 +1,7 @@
|
|||||||
import { get, writable } from 'svelte/store'
|
import { writable } from 'svelte/store'
|
||||||
import type { IndexedNote } from './globals'
|
|
||||||
import type OmnisearchPlugin from './main'
|
import type OmnisearchPlugin from './main'
|
||||||
|
|
||||||
function createIndexedNotes() {
|
|
||||||
const { subscribe, set, update } = writable<Record<string, IndexedNote>>({})
|
|
||||||
return {
|
|
||||||
subscribe,
|
|
||||||
set,
|
|
||||||
add(note: IndexedNote) {
|
|
||||||
update(notes => {
|
|
||||||
notes[note.path] = note
|
|
||||||
return notes
|
|
||||||
})
|
|
||||||
},
|
|
||||||
remove(path: string) {
|
|
||||||
update(notes => {
|
|
||||||
delete notes[path]
|
|
||||||
return notes
|
|
||||||
})
|
|
||||||
},
|
|
||||||
get(path: string): IndexedNote | undefined {
|
|
||||||
return get(indexedNotes)[path]
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A reference to the plugin instance
|
* A reference to the plugin instance
|
||||||
*/
|
*/
|
||||||
export const plugin = writable<OmnisearchPlugin>()
|
export const plugin = writable<OmnisearchPlugin>()
|
||||||
|
|
||||||
/**
|
|
||||||
* The entire list of indexed notes, constantly kept up-to-date.
|
|
||||||
*/
|
|
||||||
export const indexedNotes = createIndexedNotes()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user