Property updates can be independantly disabled

This commit is contained in:
Bambus Control 2025-08-20 21:24:47 +02:00
parent c1049f26fe
commit 2c1fbaf840
4 changed files with 82 additions and 33 deletions

View file

@ -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<void> {
private async displayPropertySettings(containerEl: HTMLElement, updatedPropertyName: string, editTimePropertyName: string, updatedEnabled: boolean, editTimeEnabled: boolean): Promise<void> {
// 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)

View file

@ -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;
}

View file

@ -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));
}

View file

@ -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<boolean> {
const criterion = await this.store.getCriterion();
return criterion.updatedPropertyEnabled ?? true; // Default to enabled for backward compatibility
}
async getEditTimePropertyEnabled(): Promise<boolean> {
const criterion = await this.store.getCriterion();
return criterion.editTimePropertyEnabled ?? true; // Default to enabled for backward compatibility
}
async overwriteUpdatedPropertyEnabled(enabled: boolean): Promise<void> {
const originalData = await this.store.getCriterion();
await this.store.overwriteCriterion({
...originalData,
updatedPropertyEnabled: enabled,
});
}
async overwriteEditTimePropertyEnabled(enabled: boolean): Promise<void> {
const originalData = await this.store.getCriterion();
await this.store.overwriteCriterion({
...originalData,
editTimePropertyEnabled: enabled,
});
}
}