Chore - code cleaning

This commit is contained in:
Simon Cambier
2022-09-21 17:49:07 +02:00
parent 80ce347a85
commit 8dfa1810f0
6 changed files with 19 additions and 20 deletions

View File

@@ -153,7 +153,7 @@
{note} {note}
index={i} index={i}
selected={i === selectedIndex} selected={i === selectedIndex}
on:mousemove={e => (selectedIndex = i)} on:mousemove={_e => (selectedIndex = i)}
on:click={openSelection} /> on:click={openSelection} />
{/each} {/each}
{:else} {:else}

View File

@@ -55,7 +55,7 @@ export class EventBus {
public emit(event: string, ...args: any[]): void { public emit(event: string, ...args: any[]): void {
const entries = [...this.handlers.entries()].filter( const entries = [...this.handlers.entries()].filter(
([k, h]) => !this.disabled.includes(k.split('@')[0]) ([k, _]) => !this.disabled.includes(k.split('@')[0])
) )
for (const [key, handler] of entries) { for (const [key, handler] of entries) {
if (key.endsWith(`@${event}`)) { if (key.endsWith(`@${event}`)) {

View File

@@ -1,7 +1,5 @@
import { EventBus } from './event-bus' import { EventBus } from './event-bus'
// Matches a wikiling that begins a string
export const regexWikilink = /^!?\[\[(?<name>.+?)(\|(?<alias>.+?))?\]\]/
export const regexLineSplit = /\r?\n|\r|((\.|\?|!)( |\r?\n|\r))/g export const regexLineSplit = /\r?\n|\r|((\.|\?|!)( |\r?\n|\r))/g
export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms export const regexYaml = /^---\s*\n(.*?)\n?^---\s?/ms
export const regexStripQuotes = /^"|"$|^'|'$/g export const regexStripQuotes = /^"|"$|^'|'$/g
@@ -18,12 +16,6 @@ export const searchIndexFilePath = `${app.vault.configDir}/plugins/omnisearch/se
export const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json` export const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json`
export const historyFilePath = `${app.vault.configDir}/plugins/omnisearch/historyCache.json` export const historyFilePath = `${app.vault.configDir}/plugins/omnisearch/historyCache.json`
export type SearchNote = {
path: string
basename: string
content: string
}
export type IndexedNote = { export type IndexedNote = {
path: string path: string
basename: string basename: string

View File

@@ -101,7 +101,7 @@ export default class OmnisearchPlugin extends Plugin {
onunload(): void {} onunload(): void {}
addRibbonButton(): void { addRibbonButton(): void {
this.addRibbonIcon('search', 'Omnisearch', evt => { this.addRibbonIcon('search', 'Omnisearch', _evt => {
new OmnisearchVaultModal(app).open() new OmnisearchVaultModal(app).open()
}) })
} }

View File

@@ -43,15 +43,19 @@ export async function loadNotesCache(): Promise<void> {
notesCache = {} notesCache = {}
} }
} }
export function getNoteFromCache(key: string): IndexedNote | undefined { export function getNoteFromCache(key: string): IndexedNote | undefined {
return notesCache[key] return notesCache[key]
} }
export function getNonExistingNotesFromCache(): IndexedNote[] { export function getNonExistingNotesFromCache(): IndexedNote[] {
return Object.values(notesCache).filter(note => note.doesNotExist) return Object.values(notesCache).filter(note => note.doesNotExist)
} }
export function addNoteToCache(filename: string, note: IndexedNote): void { export function addNoteToCache(filename: string, note: IndexedNote): void {
notesCache[filename] = note notesCache[filename] = note
} }
export function removeNoteFromCache(key: string): void { export function removeNoteFromCache(key: string): void {
delete notesCache[key] delete notesCache[key]
} }
@@ -74,7 +78,7 @@ export async function openNote(
leaf.getViewState().state?.file === item.path && leaf.getViewState().state?.file === item.path &&
leaf.getViewState()?.pinned leaf.getViewState()?.pinned
) { ) {
app.workspace.setActiveLeaf(leaf, false, true) app.workspace.setActiveLeaf(leaf, { focus: true })
alreadyOpenAndPinned = true alreadyOpenAndPinned = true
} }
} }
@@ -101,7 +105,7 @@ export async function openNote(
export async function createNote(name: string, newLeaf = false): Promise<void> { export async function createNote(name: string, newLeaf = false): Promise<void> {
try { try {
let pathPrefix = '' let pathPrefix: string
switch (app.vault.getConfig('newFileLocation')) { switch (app.vault.getConfig('newFileLocation')) {
case 'current': case 'current':
pathPrefix = (app.workspace.getActiveFile()?.parent.path ?? '') + '/' pathPrefix = (app.workspace.getActiveFile()?.parent.path ?? '') + '/'
@@ -113,7 +117,7 @@ export async function createNote(name: string, newLeaf = false): Promise<void> {
pathPrefix = '' pathPrefix = ''
break break
} }
app.workspace.openLinkText(`${pathPrefix}${name}.md`, '', newLeaf) await app.workspace.openLinkText(`${pathPrefix}${name}.md`, '', newLeaf)
} catch (e) { } catch (e) {
;(e as any).message = ;(e as any).message =
'OmniSearch - Could not create note: ' + (e as any).message 'OmniSearch - Could not create note: ' + (e as any).message

View File

@@ -80,7 +80,9 @@ export async function initGlobalSearchIndex(): Promise<void> {
console.log('Omnisearch - MiniSearch index loaded from the file') console.log('Omnisearch - MiniSearch index loaded from the file')
await loadNotesCache() await loadNotesCache()
} catch (e) { } catch (e) {
console.trace('Omnisearch - Could not load MiniSearch index from the file') console.trace(
'Omnisearch - Could not load MiniSearch index from the file'
)
console.error(e) console.error(e)
} }
} }
@@ -138,12 +140,12 @@ export async function initGlobalSearchIndex(): Promise<void> {
/** /**
* Searches the index for the given query, * Searches the index for the given query,
* and returns an array of raw results * and returns an array of raw results
* @param text * @param query
* @returns * @returns
*/ */
async function search(query: Query): Promise<SearchResult[]> { async function search(query: Query): Promise<SearchResult[]> {
if (!query.segmentsToStr()) return [] if (!query.segmentsToStr()) return []
let results = minisearchInstance.search(query.segmentsToStr(), { let results = minisearchInstance.search(query.segmentsToStr(), {
prefix: true, prefix: true,
fuzzy: term => (term.length > 4 ? 0.2 : false), fuzzy: term => (term.length > 4 ? 0.2 : false),
@@ -253,7 +255,7 @@ export async function getSuggestions(
} }
// Map the raw results to get usable suggestions // Map the raw results to get usable suggestions
const suggestions = results.map(result => { return results.map(result => {
const note = getNoteFromCache(result.id) const note = getNoteFromCache(result.id)
if (!note) { if (!note) {
throw new Error(`Note "${result.id}" not indexed`) throw new Error(`Note "${result.id}" not indexed`)
@@ -292,8 +294,6 @@ export async function getSuggestions(
} }
return resultNote return resultNote
}) })
return suggestions
} }
/** /**
@@ -365,6 +365,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
* Index a non-existing note. * Index a non-existing note.
* Useful to find internal links that lead (yet) to nowhere * Useful to find internal links that lead (yet) to nowhere
* @param name * @param name
* @param parent The note referencing the
*/ */
export function addNonExistingToIndex(name: string, parent: string): void { export function addNonExistingToIndex(name: string, parent: string): void {
name = removeAnchors(name) name = removeAnchors(name)
@@ -415,9 +416,11 @@ export function removeFromIndex(path: string): void {
} }
const notesToReindex = new Set<TAbstractFile>() const notesToReindex = new Set<TAbstractFile>()
export function addNoteToReindex(note: TAbstractFile): void { 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) { if (settings.showIndexingNotices && notesToReindex.size > 0) {
new Notice(`Omnisearch - Reindexing ${notesToReindex.size} notes`, 2000) new Notice(`Omnisearch - Reindexing ${notesToReindex.size} notes`, 2000)