diff --git a/DEV.md b/DEV.md index c49077d..e0467ce 100644 --- a/DEV.md +++ b/DEV.md @@ -5,6 +5,7 @@ * [x] General UX Improvement * [ ] Add "Goto Source" action on suggestions +* [ ] Fix occurrences hint not being updated when new occurrences are found ## Refactors diff --git a/main.ts b/main.ts index 2f90e97..751d808 100644 --- a/main.ts +++ b/main.ts @@ -2,7 +2,6 @@ import { CrossbowView } from 'view/view'; import { registerCrossbowIcons } from 'icons'; import { CacheItem, Editor, MarkdownView, Plugin, TFile, EditorPosition, CachedMetadata } from 'obsidian'; import { CrossbowSettingTab } from './settings'; -import { crossbowLogger } from './util'; import './editorExtension'; export interface CrossbowPluginSettings { @@ -10,6 +9,8 @@ export interface CrossbowPluginSettings { suggestReferencesInSameFile: boolean; ignoreSuggestionsWhichStartWithLowercaseLetter: boolean; suggestedReferencesMinimumWordLength: number; + + useLogging: boolean; } const DEFAULT_SETTINGS: CrossbowPluginSettings = { @@ -17,6 +18,7 @@ const DEFAULT_SETTINGS: CrossbowPluginSettings = { suggestReferencesInSameFile: false, ignoreSuggestionsWhichStartWithLowercaseLetter: true, suggestedReferencesMinimumWordLength: 3, + useLogging: false, } export interface CrossbowCacheEntity { @@ -90,14 +92,14 @@ export default class CrossbowPlugin extends Plugin { this.registerEvent(this.app.workspace.on('file-open', this.onFileOpen)); this.registerEvent(this.app.metadataCache.on('changed', this.onMetadataChange)); - crossbowLogger.debugLog('Crossbow is ready.'); + this.debugLog('Crossbow is ready.'); } public onunload = () => { Object.assign(this.crossbowCache, {}); this.getCrossbowView().unload() - crossbowLogger.debugLog('Unloaded Crossbow.'); + this.debugLog('Unloaded Crossbow.'); } public loadSettings = async () => this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); @@ -106,9 +108,11 @@ export default class CrossbowPlugin extends Plugin { public saveSettings = async () => await this.saveData(this.settings); + public debugLog = (message: string) => this.settings.useLogging && console.log(`🏹: ${message}`); + private onMetadataChange = (file: TFile, data: string, cache: CachedMetadata) => { this.updateCrossbowCacheEntitiesOfFile(file, cache); - crossbowLogger.debugLog(`Metadata cache updated for ${file.basename}.`); + this.debugLog(`Metadata cache updated for ${file.basename}.`); if (this.updateTimeout) clearTimeout(this.updateTimeout) @@ -122,7 +126,7 @@ export default class CrossbowPlugin extends Plugin { const prevCurrentFile = this._currentFile; this.setActiveEditorAndFile() - crossbowLogger.debugLog('File opened.'); + this.debugLog('File opened.'); if (this.updateTimeout) clearTimeout(this.updateTimeout) @@ -227,6 +231,6 @@ export default class CrossbowPlugin extends Plugin { this._currentFile = leaf.view.file; } else - crossbowLogger.warn('Unable to determine current editor.'); + console.warn('🏹: Unable to determine current editor.'); } } diff --git a/settings.ts b/settings.ts index e2c5b14..847b7a7 100644 --- a/settings.ts +++ b/settings.ts @@ -47,6 +47,13 @@ export class CrossbowSettingTab extends PluginSettingTab { else await this.updateSettingValue('suggestedReferencesMinimumWordLength', parseInt(value, 10)); })); + + new Setting(containerEl) + .setName('Enable logging') + .setDesc('If checked, debug logs will be printed to the console') + .addToggle(toggle => toggle + .setValue(this.plugin.settings.useLogging) + .onChange(async value => await this.updateSettingValue('useLogging', value))); } private updateSettingValue = async (key: K, value: CrossbowPluginSettings[K]) => { diff --git a/util.ts b/util.ts index 0bdc604..cd7bbd3 100644 --- a/util.ts +++ b/util.ts @@ -114,10 +114,3 @@ export function stripMarkdown(md: string, options?: StripMarkdownOptions) { } return output; }; - -const crossbowLogPrefix = '🏹: '; - -export const crossbowLogger = { - debugLog: (message: string) => process.env.NODE_ENV === 'development' && console.log(`${crossbowLogPrefix}${message}`), - warn: (message: string) => console.warn(`${crossbowLogPrefix}${message}`), -} diff --git a/view/view.ts b/view/view.ts index 8e9daaf..e89e6e5 100644 --- a/view/view.ts +++ b/view/view.ts @@ -4,7 +4,6 @@ import { ItemView, WorkspaceLeaf, } from 'obsidian'; -import { crossbowLogger } from '../util'; import { TreeItem, TreeItemLeaf } from './treeItem'; type LeafTreeItem = TreeItemLeaf @@ -78,7 +77,7 @@ export class CrossbowView extends ItemView { } public updateSuggestions = (suggestions: CrossbowSuggestion[], fileHasChanged: boolean) => { - crossbowLogger.debugLog(`${fileHasChanged ? "Clearing & adding" : "Updating"} suggestions`); + this.crossbow.debugLog(`${fileHasChanged ? "Clearing & adding" : "Updating"} suggestions`); const occurrenceHash = (o: EditorPosition) => o.line + ":" + o.ch; const matchHash = (o: CrossbowCacheMatch) => o.file.path; @@ -211,8 +210,8 @@ export class CrossbowView extends ItemView { }); // Go to source action - matchItem.addButton("Source", 'lucide-search', () => { - crossbowLogger.warn("Go To Source is not yet implemented"); + matchItem.addButton("Go To Source", 'lucide-search', () => { + console.warn("🏹: 'Go To Source' is not yet implemented"); }); occurrenceTreeItem.addChild(matchItem);