From 2c1fbaf8409c8213fba76dbfac27ffbffef4afcd Mon Sep 17 00:00:00 2001 From: Bambus Control Date: Wed, 20 Aug 2025 21:24:47 +0200 Subject: [PATCH] Property updates can be independantly disabled --- src/chronotyper/core/settingsTab.ts | 30 ++++++++++---- src/chronotyper/data/criterionFragment.ts | 13 +++++- .../eventHandlers/onWorkspaceFileOpen.ts | 40 ++++++++----------- src/chronotyper/storage/criterionStorage.ts | 32 ++++++++++++++- 4 files changed, 82 insertions(+), 33 deletions(-) diff --git a/src/chronotyper/core/settingsTab.ts b/src/chronotyper/core/settingsTab.ts index ff4cc30..aaf6575 100644 --- a/src/chronotyper/core/settingsTab.ts +++ b/src/chronotyper/core/settingsTab.ts @@ -1,5 +1,5 @@ -import {App, debounce, Plugin, PluginSettingTab, Setting} from "obsidian"; -import {CriterionStorage} from "../storage/criterionStorage"; +import { App, debounce, Plugin, PluginSettingTab, Setting } from "obsidian"; +import { CriterionStorage } from "../storage/criterionStorage"; export class SettingsTab extends PluginSettingTab { private rendered = false; @@ -18,7 +18,7 @@ export class SettingsTab extends PluginSettingTab { return; } - const {containerEl} = this; + const { containerEl } = this; const loadedExclusions = await this.criterionStore.getExclusion(); const exclusions = loadedExclusions || []; @@ -26,12 +26,14 @@ export class SettingsTab extends PluginSettingTab { const updatedPropertyName = await this.criterionStore.getUpdatedPropertyName(); const editTimePropertyName = await this.criterionStore.getEditTimePropertyName(); - await this.displayPropertySettings(containerEl, updatedPropertyName, editTimePropertyName); + const updatedEnabled = await this.criterionStore.getUpdatedPropertyEnabled(); + const editTimeEnabled = await this.criterionStore.getEditTimePropertyEnabled(); + await this.displayPropertySettings(containerEl, updatedPropertyName, editTimePropertyName, updatedEnabled, editTimeEnabled); this.rendered = true; } - private async displayPropertySettings(containerEl: HTMLElement, updatedPropertyName: string, editTimePropertyName: string): Promise { + private async displayPropertySettings(containerEl: HTMLElement, updatedPropertyName: string, editTimePropertyName: string, updatedEnabled: boolean, editTimeEnabled: boolean): Promise { // Add section for property name settings new Setting(containerEl).setHeading().setName("Property Names"); @@ -40,9 +42,16 @@ export class SettingsTab extends PluginSettingTab { .setDesc("Property name used to store the last update timestamp in frontmatter") .addText(text => text .setValue(updatedPropertyName) + .setDisabled(!updatedEnabled) .onChange(async (value) => { await this.criterionStore.overwriteUpdatedPropertyName(value); }) + ) + .addToggle(toggle => toggle + .setValue(updatedEnabled) + .onChange(async (value) => { + await this.criterionStore.overwriteUpdatedPropertyEnabled(value); + }) ); new Setting(containerEl) @@ -50,9 +59,16 @@ export class SettingsTab extends PluginSettingTab { .setDesc("Property name used to store the total edit time (in seconds) in frontmatter") .addText(text => text .setValue(editTimePropertyName) + .setDisabled(!editTimeEnabled) .onChange(async (value) => { await this.criterionStore.overwriteEditTimePropertyName(value); }) + ) + .addToggle(toggle => toggle + .setValue(editTimeEnabled) + .onChange(async (value) => { + await this.criterionStore.overwriteEditTimePropertyEnabled(value); + }) ); } @@ -94,10 +110,10 @@ export class SettingsTab extends PluginSettingTab { }); }); - const excludedList = containerEl.createEl("div", {cls: "excluded-paths-list"}); + const excludedList = containerEl.createEl("div", { cls: "excluded-paths-list" }); if (exclusions.length === 0) { - excludedList.createEl("p", {text: "No directories are currently excluded."}); + excludedList.createEl("p", { text: "No directories are currently excluded." }); } else { for (const path of exclusions) { new Setting(excludedList) diff --git a/src/chronotyper/data/criterionFragment.ts b/src/chronotyper/data/criterionFragment.ts index d317e67..462c8c1 100644 --- a/src/chronotyper/data/criterionFragment.ts +++ b/src/chronotyper/data/criterionFragment.ts @@ -1,4 +1,4 @@ -import {DataFragment} from "./dataFragment"; +import { DataFragment } from "./dataFragment"; export type Exclusion = string[]; @@ -14,8 +14,19 @@ export interface CriterionFragment extends DataFragment { */ updatedPropertyName?: string; + /** + * Whether the updated property is enabled + */ + updatedPropertyEnabled?: boolean; + /** * Property name for storing edit time in seconds in frontmatter */ editTimePropertyName?: string; + + /** + * Whether the edit time property is enabled + */ + editTimePropertyEnabled?: boolean; + } diff --git a/src/chronotyper/eventHandlers/onWorkspaceFileOpen.ts b/src/chronotyper/eventHandlers/onWorkspaceFileOpen.ts index a58852b..1cf4380 100644 --- a/src/chronotyper/eventHandlers/onWorkspaceFileOpen.ts +++ b/src/chronotyper/eventHandlers/onWorkspaceFileOpen.ts @@ -17,36 +17,37 @@ export function onWorkspaceFileOpen( throw new ChronotyperError(`File ${session.filepath} not found`) } - const closedSession = { ...session } // Because of the callback + const closedSession = { ...session } console.log("Closing file", session.filepath, "with", closedSession); - // Update edit time if there was any editing if (closedSession.totalEditTime > 0) { - // Get property names from settings const updatedPropertyName = await criterionStore.getUpdatedPropertyName(); const editTimePropertyName = await criterionStore.getEditTimePropertyName(); - - await app.fileManager.processFrontMatter(file, (frontmatter) => { - frontmatter[updatedPropertyName] = moment().toISOString(true); - - const addedEditTime = closedSession.totalEditTime / 1000; - frontmatter[editTimePropertyName] = Math.floor( - (frontmatter[editTimePropertyName] ?? 0) + - addedEditTime - ); - }); + const updatedEnabled = await criterionStore.getUpdatedPropertyEnabled(); + const editTimeEnabled = await criterionStore.getEditTimePropertyEnabled(); + if (updatedEnabled || editTimeEnabled) { + await app.fileManager.processFrontMatter(file, (frontmatter) => { + if (updatedEnabled) { + frontmatter[updatedPropertyName] = moment().toISOString(true); + } + if (editTimeEnabled) { + const addedEditTime = closedSession.totalEditTime / 1000; + frontmatter[editTimePropertyName] = Math.floor( + (frontmatter[editTimePropertyName] ?? 0) + + addedEditTime + ); + } + }); + } } } if (newFile == null) { - /* File closed */ session.filepath = null; session.viewStartTime = 0; } else { const exclusions = await criterionStore.getExclusion(); - - /* New file opened */ if (!isPathExcluded(newFile.path, exclusions)) { session.filepath = newFile.path; session.viewStartTime = Date.now(); @@ -55,19 +56,12 @@ export function onWorkspaceFileOpen( } } - /* Reset tracked parameters */ session.lastEditTime = null; session.totalEditTime = 0; }; } -/** - * Checks if a filepath matches any exclusion pattern - * @param filepath The filepath to check - * @param exclusions Array of exclusion patterns - * @returns True if the filepath should be excluded, false otherwise - */ function isPathExcluded(filepath: string | null, exclusions: string[]): boolean { return exclusions.some((excluded) => (filepath ?? "").startsWith(excluded)); } diff --git a/src/chronotyper/storage/criterionStorage.ts b/src/chronotyper/storage/criterionStorage.ts index 6da4e75..2390a09 100644 --- a/src/chronotyper/storage/criterionStorage.ts +++ b/src/chronotyper/storage/criterionStorage.ts @@ -1,5 +1,5 @@ -import {RootDataStore} from "./rootDataStore"; -import {Exclusion} from "../data/criterionFragment"; +import { RootDataStore } from "./rootDataStore"; +import { Exclusion } from "../data/criterionFragment"; // Default property names to maintain backward compatibility const DEFAULT_UPDATED_PROPERTY = 'updated'; @@ -62,4 +62,32 @@ export class CriterionStorage { editTimePropertyName: propertyName, }); } + + async getUpdatedPropertyEnabled(): Promise { + const criterion = await this.store.getCriterion(); + return criterion.updatedPropertyEnabled ?? true; // Default to enabled for backward compatibility + } + + async getEditTimePropertyEnabled(): Promise { + const criterion = await this.store.getCriterion(); + return criterion.editTimePropertyEnabled ?? true; // Default to enabled for backward compatibility + } + + async overwriteUpdatedPropertyEnabled(enabled: boolean): Promise { + const originalData = await this.store.getCriterion(); + + await this.store.overwriteCriterion({ + ...originalData, + updatedPropertyEnabled: enabled, + }); + } + + async overwriteEditTimePropertyEnabled(enabled: boolean): Promise { + const originalData = await this.store.getCriterion(); + + await this.store.overwriteCriterion({ + ...originalData, + editTimePropertyEnabled: enabled, + }); + } }