From 0d7898c00fbaad0653883e0143fa1e855c44aaba Mon Sep 17 00:00:00 2001 From: Nick Winters <65742767+DynamicPlayerSector@users.noreply.github.com> Date: Sun, 3 Sep 2023 19:57:30 +0300 Subject: [PATCH] Saner code --- main.ts | 64 ++++++++++++++++++++++++++++++--------------------- manifest.json | 2 +- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/main.ts b/main.ts index 19fd7e4..6e2de3d 100644 --- a/main.ts +++ b/main.ts @@ -81,7 +81,7 @@ export default class TimeThings extends Plugin { } if (this.settings.enableEditDurationKey) { this.allowEditDurationUpdate && this.objectUpdateEditDuration(file); - this.setEditDurationBar(editor); + this.setEditDurationBar(false, file); } if (this.settings.enableModifiedKeyUpdate) { @@ -104,46 +104,58 @@ export default class TimeThings extends Plugin { this.editorUpdateKey(editor, this.settings.modifiedKeyName, dateFormatted); if (this.settings.enableEditDurationKey) { this.allowEditDurationUpdate && this.updateEditDuration(editor); - this.setEditDurationBar(editor); + this.setEditDurationBar(true, editor); } - }); - this.app.workspace.on("active-leaf-change", () => { - this.settings.enableEditDurationKey && this.setEditDurationBar(); - }) + this.registerEvent(this.app.workspace.on("active-leaf-change", (file) => { + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); + if (activeView === null) { + return; + } + const editor = activeView.editor; + this.settings.enableEditDurationKey && this.settings.useCustomFrontmatterHandlingSolution && this.setEditDurationBar(true, editor); + })) // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new TimeThingsSettingsTab(this.app, this)); } - setEditDurationBar(editor: Editor) { - const fieldLine = this.getFieldLine(editor, this.settings.editDurationPath); - if (fieldLine === undefined) { - this.editDurationBar.setText("⌛ --"); - return; + setEditDurationBar(useCustomSolution: false, solution: TAbstractFile): void; + setEditDurationBar(useCustomSolution: true, solution: Editor): void; + async setEditDurationBar(useCustomSolution: boolean, solution: Editor | TAbstractFile) { + let value = 0; + if (solution instanceof Editor) { + let editor = solution; + const fieldLine = this.getFieldLine(editor, this.settings.editDurationPath); + if (fieldLine === undefined) { + this.editDurationBar.setText("⌛ --"); + return; + } + value = +editor.getLine(fieldLine).split(/:(.*)/s)[1].trim(); } - const value = editor.getLine(fieldLine).split(/:(.*)/s)[1].trim(); + if (solution instanceof TAbstractFile) { + let file = solution; + await this.app.fileManager.processFrontMatter(file as TFile, (frontmatter) => { + value = this.objectGetValue(frontmatter, this.settings.editDurationPath); + if (value === undefined) { + value = 0; + } + }) + } + let text = ""; if (+value < 60) { - this.editDurationBar.setText("⌛ <1 m"); - return; + text = `⌛ <1 m`; } - if (+value * 60 >= 1 && +value < 60 * 60 * 24) { + else if (+value < 60 * 60 * 24) { const minutes = Math.floor(+value / 60); - let text = ""; - text = "⌛ " + minutes.toString() + " m"; - - this.editDurationBar.setText(text); - return; + text = `⌛ ${minutes} m`; } - if (+value * 60 * 60 >= 24) { // I know that the code of this plugin is spaghetti, I'll refactor everything later + else { const days = Math.floor(+value / (24 * 60 * 60)); - let text = ""; - text = "⌛ " + days.toString() + " d"; - - this.editDurationBar.setText(text); - return; + text = `⌛ ${days} d`; } + this.editDurationBar.setText(text); } async updateEditDuration(editor: Editor) { diff --git a/manifest.json b/manifest.json index 1ac83e2..ec011e0 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "timethings", "name": "Time Things", - "version": "1.2.0", + "version": "1.2.1", "minAppVersion": "0.15.0", "description": "Show clock in the corner. Track total editing time of a note and the last time it was modified.", "author": "Nick Winters",