#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:
@@ -11,6 +11,7 @@
|
||||
import ResultItemVault from './ResultItemVault.svelte'
|
||||
import { Query } from 'src/query'
|
||||
import { saveSearchHistory, searchHistory } from 'src/search-history'
|
||||
import { SearchContextType, settings } from 'src/settings'
|
||||
|
||||
export let modal: OmnisearchVaultModal
|
||||
let selectedIndex = 0
|
||||
@@ -28,6 +29,7 @@ import { saveSearchHistory, searchHistory } from 'src/search-history'
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
console.log('mount')
|
||||
await reindexNotes()
|
||||
searchQuery = searchHistory[historySearchIndex]
|
||||
eventBus.enable('vault')
|
||||
@@ -38,8 +40,9 @@ import { saveSearchHistory, searchHistory } from 'src/search-history'
|
||||
eventBus.on('vault', 'tab', switchToInFileModal)
|
||||
eventBus.on('vault', 'arrow-up', () => moveIndex(-1))
|
||||
eventBus.on('vault', 'arrow-down', () => moveIndex(1))
|
||||
eventBus.on('vault', 'prev-search-history', () => prevSearchHistory())
|
||||
eventBus.on('vault', 'next-search-history', () => nextSearchHistory())
|
||||
eventBus.on('vault', 'prev-search-history', prevSearchHistory)
|
||||
eventBus.on('vault', 'next-search-history', nextSearchHistory)
|
||||
eventBus.on('vault', 'toggle-context', toggleContext)
|
||||
})
|
||||
|
||||
onDestroy(() => {
|
||||
@@ -59,6 +62,16 @@ import { saveSearchHistory, searchHistory } from 'src/search-history'
|
||||
searchQuery = searchHistory[historySearchIndex]
|
||||
}
|
||||
|
||||
function toggleContext() {
|
||||
settings.update(s => {
|
||||
s.showContext =
|
||||
s.showContext === SearchContextType.None
|
||||
? SearchContextType.Simple
|
||||
: SearchContextType.None
|
||||
return s
|
||||
})
|
||||
}
|
||||
|
||||
async function updateResults() {
|
||||
query = new Query(searchQuery)
|
||||
resultNotes = (await getSuggestions(query)).sort(
|
||||
@@ -212,4 +225,10 @@ import { saveSearchHistory, searchHistory } from 'src/search-history'
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">esc</span><span>to close</span>
|
||||
</div>
|
||||
<br />
|
||||
|
||||
<div class="prompt-instruction">
|
||||
<span class="prompt-instruction-command">ctrl+h</span><span
|
||||
>to toggle context</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { getNoteFromCache } from 'src/notes'
|
||||
import { settings } from 'src/settings'
|
||||
import { SearchContextType, settings } from 'src/settings'
|
||||
import type { ResultNote } from '../globals'
|
||||
import { getMatches } from '../search'
|
||||
import { highlighter, makeExcerpt, stringsToRegex } from '../utils'
|
||||
@@ -13,7 +13,7 @@
|
||||
$: matches = getMatches(note.content, reg)
|
||||
$: cleanedContent = makeExcerpt(note.content, note.matches[0]?.offset ?? -1)
|
||||
$: glyph = getNoteFromCache(note.path)?.doesNotExist
|
||||
$: title = settings.showShortName ? note.basename : note.path
|
||||
$: title = $settings.showShortName ? note.basename : note.path
|
||||
</script>
|
||||
|
||||
<ResultItemContainer id={note.path} {selected} on:mousemove on:click {glyph}>
|
||||
@@ -28,7 +28,10 @@
|
||||
</span>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if $settings.showContext !== SearchContextType.None}
|
||||
<div class="omnisearch-result__body">
|
||||
{@html cleanedContent.replace(reg, highlighter)}
|
||||
</div>
|
||||
{/if}
|
||||
</ResultItemContainer>
|
||||
|
||||
@@ -3,6 +3,7 @@ import ModalVault from './components/ModalVault.svelte'
|
||||
import ModalInFile from './components/ModalInFile.svelte'
|
||||
import { eventBus, isInputComposition } from './globals'
|
||||
import { settings } from './settings'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
abstract class OmnisearchModal extends Modal {
|
||||
constructor(app: App) {
|
||||
@@ -37,7 +38,7 @@ abstract class OmnisearchModal extends Modal {
|
||||
] as const) {
|
||||
for (const modifier of ['Ctrl', 'Meta'] as const) {
|
||||
this.scope.register([modifier], key.k, e => {
|
||||
if (settings.CtrlJK && this.app.vault.getConfig('vimMode')) {
|
||||
if (get(settings).CtrlJK && this.app.vault.getConfig('vimMode')) {
|
||||
e.preventDefault()
|
||||
eventBus.emit('arrow-' + key.dir)
|
||||
}
|
||||
@@ -52,7 +53,7 @@ abstract class OmnisearchModal extends Modal {
|
||||
] as const) {
|
||||
for (const modifier of ['Ctrl', 'Meta'] as const) {
|
||||
this.scope.register([modifier], key.k, e => {
|
||||
if (settings.CtrlNP && this.app.vault.getConfig('vimMode')) {
|
||||
if (get(settings).CtrlNP && this.app.vault.getConfig('vimMode')) {
|
||||
e.preventDefault()
|
||||
eventBus.emit('arrow-' + key.dir)
|
||||
}
|
||||
@@ -98,6 +99,12 @@ abstract class OmnisearchModal extends Modal {
|
||||
e.preventDefault()
|
||||
eventBus.emit('prev-search-history')
|
||||
})
|
||||
|
||||
// Context
|
||||
this.scope.register(['Ctrl'], 'h', e => {
|
||||
e.preventDefault()
|
||||
eventBus.emit('toggle-context')
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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