#70 - Added a "toggle context" setting + settings is now a Svelte store
A bit more verbose, but allows for proper reactivity
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Plugin, PluginSettingTab, Setting, SliderComponent } from 'obsidian'
|
||||
import { notesCacheFilePath, searchIndexFilePath } from './globals'
|
||||
import type OmnisearchPlugin from './main'
|
||||
import { get, readable, writable } from 'svelte/store'
|
||||
|
||||
interface WeightingSettings {
|
||||
weightBasename: number
|
||||
@@ -15,11 +16,17 @@ export interface OmnisearchSettings extends WeightingSettings {
|
||||
showIndexingNotices: boolean
|
||||
ribbonIcon: boolean
|
||||
showShortName: boolean
|
||||
showContext: SearchContextType
|
||||
CtrlJK: boolean
|
||||
CtrlNP: boolean
|
||||
storeIndexInFile: boolean
|
||||
}
|
||||
|
||||
export enum SearchContextType {
|
||||
None,
|
||||
Simple,
|
||||
}
|
||||
|
||||
export class SettingsTab extends PluginSettingTab {
|
||||
plugin: OmnisearchPlugin
|
||||
|
||||
@@ -46,8 +53,11 @@ export class SettingsTab extends PluginSettingTab {
|
||||
'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
|
||||
toggle.setValue(get(settings).respectExcluded).onChange(async v => {
|
||||
settings.update(s => {
|
||||
s.respectExcluded = v
|
||||
return s
|
||||
})
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
@@ -63,8 +73,11 @@ export class SettingsTab extends PluginSettingTab {
|
||||
.setName('Ignore diacritics')
|
||||
.setDesc(diacriticsDesc)
|
||||
.addToggle(toggle =>
|
||||
toggle.setValue(settings.ignoreDiacritics).onChange(async v => {
|
||||
settings.ignoreDiacritics = v
|
||||
toggle.setValue(get(settings).ignoreDiacritics).onChange(async v => {
|
||||
settings.update(s => {
|
||||
s.ignoreDiacritics = v
|
||||
return s
|
||||
})
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
@@ -82,10 +95,13 @@ export class SettingsTab extends PluginSettingTab {
|
||||
.setName('EXPERIMENTAL - Store index in file')
|
||||
.setDesc(serializedIndexDesc)
|
||||
.addToggle(toggle =>
|
||||
toggle.setValue(settings.storeIndexInFile).onChange(async v => {
|
||||
toggle.setValue(get(settings).storeIndexInFile).onChange(async v => {
|
||||
app.vault.adapter.remove(notesCacheFilePath)
|
||||
app.vault.adapter.remove(searchIndexFilePath)
|
||||
settings.storeIndexInFile = v
|
||||
settings.update(s => {
|
||||
s.storeIndexInFile = v
|
||||
return s
|
||||
})
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
@@ -103,8 +119,11 @@ export class SettingsTab extends PluginSettingTab {
|
||||
'Add a button on the sidebar to open the Vault search modal. Needs a restart to remove the button.'
|
||||
)
|
||||
.addToggle(toggle =>
|
||||
toggle.setValue(settings.ribbonIcon).onChange(async v => {
|
||||
settings.ribbonIcon = v
|
||||
toggle.setValue(get(settings).ribbonIcon).onChange(async v => {
|
||||
settings.update(s => {
|
||||
s.ribbonIcon = v
|
||||
return s
|
||||
})
|
||||
await saveSettings(this.plugin)
|
||||
if (v) {
|
||||
this.plugin.addRibbonButton()
|
||||
@@ -117,8 +136,11 @@ export class SettingsTab extends PluginSettingTab {
|
||||
.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
|
||||
toggle.setValue(get(settings).showIndexingNotices).onChange(async v => {
|
||||
settings.update(s => {
|
||||
s.showIndexingNotices = v
|
||||
return s
|
||||
})
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
@@ -130,8 +152,11 @@ export class SettingsTab extends PluginSettingTab {
|
||||
'In the search results, only show the note name, without the full path.'
|
||||
)
|
||||
.addToggle(toggle =>
|
||||
toggle.setValue(settings.showShortName).onChange(async v => {
|
||||
settings.showShortName = v
|
||||
toggle.setValue(get(settings).showShortName).onChange(async v => {
|
||||
settings.update(s => {
|
||||
s.showShortName = v
|
||||
return s
|
||||
})
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
@@ -171,8 +196,11 @@ export class SettingsTab extends PluginSettingTab {
|
||||
'Use [Ctrl/Cmd]+j/k to navigate up/down in the results, if Vim mode is enabled'
|
||||
)
|
||||
.addToggle(toggle =>
|
||||
toggle.setValue(settings.CtrlJK).onChange(async v => {
|
||||
settings.CtrlJK = v
|
||||
toggle.setValue(get(settings).CtrlJK).onChange(async v => {
|
||||
settings.update(s => {
|
||||
s.CtrlJK = v
|
||||
return s
|
||||
})
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
@@ -182,8 +210,11 @@ export class SettingsTab extends PluginSettingTab {
|
||||
'Use [Ctrl/Cmd]+n/p to navigate up/down in the results, if Vim mode is enabled'
|
||||
)
|
||||
.addToggle(toggle =>
|
||||
toggle.setValue(settings.CtrlNP).onChange(async v => {
|
||||
settings.CtrlNP = v
|
||||
toggle.setValue(get(settings).CtrlNP).onChange(async v => {
|
||||
settings.update(s => {
|
||||
s.CtrlNP = v
|
||||
return s
|
||||
})
|
||||
await saveSettings(this.plugin)
|
||||
})
|
||||
)
|
||||
@@ -193,10 +224,13 @@ export class SettingsTab extends PluginSettingTab {
|
||||
|
||||
weightSlider(cb: SliderComponent, key: keyof WeightingSettings): void {
|
||||
cb.setLimits(1, 3, 0.1)
|
||||
cb.setValue(settings[key])
|
||||
cb.setValue(get(settings)[key])
|
||||
cb.setDynamicTooltip()
|
||||
cb.onChange(v => {
|
||||
settings[key] = v
|
||||
settings.update(s => {
|
||||
s[key] = v
|
||||
return s
|
||||
})
|
||||
saveSettings(this.plugin)
|
||||
})
|
||||
}
|
||||
@@ -209,6 +243,7 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
|
||||
showIndexingNotices: false,
|
||||
showShortName: false,
|
||||
ribbonIcon: true,
|
||||
showContext: SearchContextType.Simple,
|
||||
|
||||
weightBasename: 2,
|
||||
weightH1: 1.5,
|
||||
@@ -221,12 +256,14 @@ export const DEFAULT_SETTINGS: OmnisearchSettings = {
|
||||
storeIndexInFile: false,
|
||||
} as const
|
||||
|
||||
export let settings: OmnisearchSettings = Object.assign({}, DEFAULT_SETTINGS)
|
||||
export const settings = writable(
|
||||
Object.assign({}, DEFAULT_SETTINGS) as OmnisearchSettings
|
||||
)
|
||||
|
||||
export async function loadSettings(plugin: Plugin): Promise<void> {
|
||||
settings = Object.assign({}, DEFAULT_SETTINGS, await plugin.loadData())
|
||||
settings.set(Object.assign({}, DEFAULT_SETTINGS, await plugin.loadData()))
|
||||
}
|
||||
|
||||
export async function saveSettings(plugin: Plugin): Promise<void> {
|
||||
await plugin.saveData(settings)
|
||||
await plugin.saveData(get(settings))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user