Skip to content

Commit

Permalink
Saner code
Browse files Browse the repository at this point in the history
  • Loading branch information
plasmabit committed Sep 3, 2023
1 parent a9a9235 commit 0d7898c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
64 changes: 38 additions & 26 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 0d7898c

Please sign in to comment.