diff --git a/src/globals.ts b/src/globals.ts index 49ce0be..db14f9b 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -16,6 +16,7 @@ export const excerptAfter = 300 export const highlightClass = `suggestion-highlight omnisearch-highlight ${ settings.highlight ? 'omnisearch-default-highlight' : '' }` +export const K_DISABLE_OMNISEARCH = 'omnisearch-disabled' export const eventBus = new EventBus() diff --git a/src/main.ts b/src/main.ts index 0f1e655..4e9566d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,7 @@ import { OmnisearchVaultModal, } from './components/modals' import { + isPluginDisabled, loadSettings, saveSettings, settings, @@ -29,6 +30,13 @@ export default class OmnisearchPlugin extends Plugin { async onload(): Promise { await loadSettings(this) + this.addSettingTab(new SettingsTab(this)) + + if (isPluginDisabled()) { + console.log('Omnisearch - Plugin disabled') + return + } + await cleanOldCacheFiles() await OmnisearchCache.clearOldDatabases() @@ -38,7 +46,6 @@ export default class OmnisearchPlugin extends Plugin { this.addRibbonButton() } - this.addSettingTab(new SettingsTab(this)) eventBus.disable('vault') eventBus.disable('infile') eventBus.on('global', EventNames.ToggleExcerpts, () => { @@ -252,3 +259,4 @@ function registerAPI(plugin: OmnisearchPlugin): void { // Deprecated ;(app as any).plugins.plugins.omnisearch.api = api } + diff --git a/src/settings.ts b/src/settings.ts index 9f8b536..0d2548f 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -7,7 +7,7 @@ import { } from 'obsidian' import { writable } from 'svelte/store' import { database } from './database' -import { getTextExtractor, isCacheEnabled } from './globals' +import { K_DISABLE_OMNISEARCH, getTextExtractor, isCacheEnabled } from './globals' import type OmnisearchPlugin from './main' interface WeightingSettings { @@ -55,6 +55,8 @@ export interface OmnisearchSettings extends WeightingSettings { */ export const showExcerpt = writable(false) +const needsARestart = `Needs a restart to fully take effect.` + export class SettingsTab extends PluginSettingTab { plugin: OmnisearchPlugin @@ -72,6 +74,11 @@ export class SettingsTab extends PluginSettingTab { const { containerEl } = this containerEl.empty() + if (app.loadLocalStorage(K_DISABLE_OMNISEARCH) == '1') { + const span = containerEl.createEl('span') + span.innerHTML = `⚠️ OMNISEARCH IS DISABLED ⚠️` + } + // Settings main title containerEl.createEl('h2', { text: 'Omnisearch' }) @@ -138,7 +145,7 @@ export class SettingsTab extends PluginSettingTab { Add extensions separated by a space, without the dot. Example: "txt org".
⚠️ Using extensions of non-plaintext files (like .docx or .pptx) WILL cause crashes, because Omnisearch will try to index their content.
- Needs a restart to fully take effect.` + ${needsARestart}` }) new Setting(containerEl) .setName('Additional files to index') @@ -192,7 +199,7 @@ export class SettingsTab extends PluginSettingTab { span.innerHTML = `Normalize diacritics in search terms. Words like "brûlée" or "žluťoučký" will be indexed as "brulee" and "zlutoucky".
⚠️ You probably should NOT disable this.
⚠️ Changing this setting will clear the cache.
- Needs a restart to fully take effect. + ${needsARestart} ` }) new Setting(containerEl) @@ -211,7 +218,7 @@ export class SettingsTab extends PluginSettingTab { camelCaseDesc.createSpan({}, span => { span.innerHTML = `Enable this if you want to be able to search for CamelCaseWords as separate words.
⚠️ Changing this setting will clear the cache.
- Needs a restart to fully take effect. + ${needsARestart} ` }) new Setting(containerEl) @@ -364,7 +371,9 @@ export class SettingsTab extends PluginSettingTab { new Setting(containerEl) .setName('Enable verbose logging') - .setDesc('Adds a LOT of logs for debugging purposes. Don\'t forget to disable it.') + .setDesc( + "Adds a LOT of logs for debugging purposes. Don't forget to disable it." + ) .addToggle(toggle => toggle.setValue(settings.verboseLogging).onChange(async v => { settings.verboseLogging = v @@ -375,14 +384,33 @@ export class SettingsTab extends PluginSettingTab { //#endregion Debugginh //#region Danger Zone - if (isCacheEnabled()) { - new Setting(containerEl).setName('Danger Zone').setHeading() + new Setting(containerEl).setName('Danger Zone').setHeading() + const disableDesc = new DocumentFragment() + disableDesc.createSpan({}, span => { + span.innerHTML = `Disable Omnisearch on this device only.
+ ${needsARestart}` + }) + new Setting(containerEl) + .setName('Disable on this device') + .setDesc(disableDesc) + .addToggle(toggle => + toggle.setValue(isPluginDisabled()).onChange(async v => { + if (v) { + app.saveLocalStorage(K_DISABLE_OMNISEARCH, '1') + } else { + app.saveLocalStorage(K_DISABLE_OMNISEARCH) // No value = unset + } + new Notice('Omnisearch - Disabled. Please restart Obsidian.') + }) + ) + + if (isCacheEnabled()) { const resetCacheDesc = new DocumentFragment() resetCacheDesc.createSpan({}, span => { span.innerHTML = `Erase all Omnisearch cache data. - Use this if Omnisearch results are inconsistent, missing, or appear outdated.
- Needs a restart to fully take effect.` + Use this if Omnisearch results are inconsistent, missing, or appear outdated.
+ ${needsARestart}` }) new Setting(containerEl) .setName('Clear cache data') @@ -446,3 +474,7 @@ export async function loadSettings(plugin: Plugin): Promise { export async function saveSettings(plugin: Plugin): Promise { await plugin.saveData(settings) } + +export function isPluginDisabled(): boolean { + return app.loadLocalStorage(K_DISABLE_OMNISEARCH) == '1' +} \ No newline at end of file diff --git a/src/typings/types-obsidian.d.ts b/src/typings/types-obsidian.d.ts index ce997a3..e12dc9d 100644 --- a/src/typings/types-obsidian.d.ts +++ b/src/typings/types-obsidian.d.ts @@ -17,7 +17,7 @@ declare module 'obsidian' { interface App { appId: string + loadLocalStorage(key: string): string | null + saveLocalStorage(key: string, value?: string): void } } - -