#41 - Settings page

This commit is contained in:
Simon Cambier
2022-05-06 21:04:56 +02:00
parent abff5ecaca
commit 5eb75363bd
3 changed files with 125 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
import { Notice, Plugin, TFile } from 'obsidian' import { Plugin, TFile } from 'obsidian'
import { import {
addToIndex, addToIndex,
initGlobalSearchIndex, initGlobalSearchIndex,
@@ -6,10 +6,12 @@ import {
removeFromIndexByPath, removeFromIndexByPath,
} from './search' } from './search'
import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals' import { OmnisearchInFileModal, OmnisearchVaultModal } from './modals'
import { loadSettings, SettingsTab } from './settings'
export default class OmnisearchPlugin extends Plugin { export default class OmnisearchPlugin extends Plugin {
async onload(): Promise<void> { async onload(): Promise<void> {
warningOldVersion() await loadSettings(this)
this.addSettingTab(new SettingsTab(this))
// Commands to display Omnisearch modals // Commands to display Omnisearch modals
this.addCommand({ this.addCommand({
@@ -59,16 +61,3 @@ export default class OmnisearchPlugin extends Plugin {
}) })
} }
} }
function warningOldVersion(): void {
const plugins = ((app as any).plugins?.plugins ?? {}) as Record<string, any>
if (plugins['scambier.omnisearch']) {
new Notice(
`OMNISEARCH
It looks like you have 2 versions of Omnisearch installed.
Please uninstall the old one (up to 0.2.5) and keep the new one (1.0.0+)
(Click to dismiss)`,
0,
)
}
}

View File

@@ -14,6 +14,7 @@ import {
wait, wait,
} from './utils' } from './utils'
import type { Query } from './query' import type { Query } from './query'
import { settings } from './settings'
let minisearchInstance: MiniSearch<IndexedNote> let minisearchInstance: MiniSearch<IndexedNote>
let indexedNotes: Record<string, IndexedNote> = {} let indexedNotes: Record<string, IndexedNote> = {}
@@ -57,7 +58,7 @@ export async function initGlobalSearchIndex(): Promise<void> {
if (file) await addToIndex(file) if (file) await addToIndex(file)
} }
if (files.length > 0) { if (files.length > 0 && settings.showIndexingNotices) {
new Notice( new Notice(
`Omnisearch - Indexed ${files.length} notes in ${ `Omnisearch - Indexed ${files.length} notes in ${
new Date().getTime() - start new Date().getTime() - start
@@ -82,19 +83,24 @@ async function search(query: Query): Promise<SearchResult[]> {
fuzzy: term => (term.length > 4 ? 0.2 : false), fuzzy: term => (term.length > 4 ? 0.2 : false),
combineWith: 'AND', combineWith: 'AND',
boost: { boost: {
basename: 2, basename: settings.weightBasename,
headings1: 1.5, headings1: settings.weightH1,
headings2: 1.3, headings2: settings.weightH2,
headings3: 1.1, headings3: settings.weightH3,
}, },
}) })
// Half the score for files that are in Obsidian's excluded list // Downrank files that are in Obsidian's excluded list
results.forEach(result => { if (settings.respectExcluded) {
if (app.metadataCache.isUserIgnored && app.metadataCache.isUserIgnored(result.id)) { results.forEach(result => {
result.score /= 3 // TODO: make this value configurable or toggleable? if (
} app.metadataCache.isUserIgnored &&
}) app.metadataCache.isUserIgnored(result.id)
) {
result.score /= 3 // TODO: make this value configurable or toggleable?
}
})
}
// If the search query contains quotes, filter out results that don't have the exact match // If the search query contains quotes, filter out results that don't have the exact match
const exactTerms = query.getExactTerms() const exactTerms = query.getExactTerms()

104
src/settings.ts Normal file
View File

@@ -0,0 +1,104 @@
import { Plugin, PluginSettingTab, Setting, SliderComponent } from 'obsidian'
import type OmnisearchPlugin from './main'
interface WeightingSettings {
weightBasename: number
weightH1: number
weightH2: number
weightH3: number
}
export interface OmnisearchSettings extends WeightingSettings {
showIndexingNotices: boolean
respectExcluded: boolean
}
export class SettingsTab extends PluginSettingTab {
plugin: OmnisearchPlugin
constructor(plugin: OmnisearchPlugin) {
super(app, plugin)
this.plugin = plugin
}
display(): void {
const { containerEl } = this
containerEl.empty()
// Title
const title = document.createElement('h2')
title.textContent = 'Omnisearch settings'
containerEl.appendChild(title)
// Show notices
new Setting(containerEl)
.setName('Show indexing notices')
.setDesc('Show a notice when indexing is done, usually at startup.')
.addToggle(toggle =>
toggle.setValue(settings.showIndexingNotices).onChange(async v => {
settings.showIndexingNotices = v
await saveSettings(this.plugin)
}),
)
// Respect excluded files
new Setting(containerEl)
.setName('Respect Obsidian\'s "Excluded Files"')
.setDesc(
'Files that are in Obsidian\'s "Options > Files & Links > Excluded Files" list will be downranked in results.',
)
.addToggle(toggle =>
toggle.setValue(settings.respectExcluded).onChange(async v => {
settings.respectExcluded = v
await saveSettings(this.plugin)
}),
)
new Setting(containerEl).setName('Results weighting').setHeading()
new Setting(containerEl)
.setName(`File name (default: ${DEFAULT_SETTINGS.weightBasename})`)
.addSlider(cb => this.weightSlider(cb, 'weightBasename'))
new Setting(containerEl)
.setName(`Headings level 1 (default: ${DEFAULT_SETTINGS.weightH1})`)
.addSlider(cb => this.weightSlider(cb, 'weightH1'))
new Setting(containerEl)
.setName(`Headings level 2 (default: ${DEFAULT_SETTINGS.weightH2})`)
.addSlider(cb => this.weightSlider(cb, 'weightH2'))
new Setting(containerEl)
.setName(`Headings level 3 (default: ${DEFAULT_SETTINGS.weightH3})`)
.addSlider(cb => this.weightSlider(cb, 'weightH3'))
}
weightSlider(cb: SliderComponent, key: keyof WeightingSettings): void {
cb.setLimits(1, 3, 0.1)
cb.setValue(settings[key])
cb.setDynamicTooltip()
cb.onChange(v => {
settings[key] = v
saveSettings(this.plugin)
})
}
}
export const DEFAULT_SETTINGS: OmnisearchSettings = {
showIndexingNotices: true,
respectExcluded: true,
weightBasename: 2,
weightH1: 1.5,
weightH2: 1.3,
weightH3: 1.1,
} as const
export let settings: OmnisearchSettings = Object.assign({}, DEFAULT_SETTINGS)
export async function loadSettings(plugin: Plugin): Promise<void> {
settings = Object.assign({}, DEFAULT_SETTINGS, await plugin.loadData())
}
export async function saveSettings(plugin: Plugin): Promise<void> {
await plugin.saveData(settings)
}