From e41f0f93de68661d92ff22a38fc9b09f9f6e558f Mon Sep 17 00:00:00 2001 From: saertna <83655354+saertna@users.noreply.github.com> Date: Sat, 10 Feb 2024 22:16:34 +0100 Subject: [PATCH 1/7] auto rate after 5sec of change --- src/main.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index c97e709..5795035 100644 --- a/src/main.ts +++ b/src/main.ts @@ -53,6 +53,9 @@ export default class gamification extends Plugin { private statusBarItem = this.addStatusBarItem(); private statusbarGamification = this.statusBarItem.createEl("span", { text: "" }); public settings: ISettings; + private lastEditTimes: Record = {}; + private editTimers: Record> = {}; + getSettingString(key: string) { @@ -126,7 +129,7 @@ export default class gamification extends Plugin { // take care to reset when opened on a new day, don't wait for trigger setTimeout(async () => { - // Code that you want to execute after the delay + // Code to execute after the delay await this.loadSettings(); await this.resetDailyGoals() await this.updateStatusBar(this.statusbarGamification) @@ -137,7 +140,9 @@ export default class gamification extends Plugin { this.timerInterval = 30 * 60 * 1000; // minutes x seconds x milliseconds this.timerId = window.setInterval(this.resetDailyGoals.bind(this), this.timerInterval); - + this.registerEvent( + this.app.workspace.on('editor-change', this.onEditorChanged.bind(this)) + ); if (this.getSettingBoolean('debug')){ @@ -290,6 +295,39 @@ export default class gamification extends Plugin { } + onEditorChanged() { + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); + if (!activeView ) return; + + const activeFile = activeView.file; + if (!activeFile) return; + + const filePath = activeFile.path; + const currentTime = Date.now(); + + // Update last edit time for the file + this.lastEditTimes[filePath] = currentTime; + + // Clear previous timer if exists + if (this.editTimers[filePath]) { + clearTimeout(this.editTimers[filePath]); + } + + // Set new timer to trigger action after 5-10 seconds + this.editTimers[filePath] = setTimeout(() => { + // Check if no further edits happened within the delay + if (this.lastEditTimes[filePath] === currentTime) { + // Trigger your action here + this.triggerAction(filePath); + } + }, 5000); // Adjust delay as needed (e.g., 10000 for 10 seconds) + } + + triggerAction(filePath: string) { + this.calculateNoteMajurity().then(r => console.log(r)); + if(debugLogs) console.log(`File ${filePath} was edited and no further changes occurred.`); + } + private async resetGame() { await this.removeKeysFromFrontmatter(); this.setSettingNumber('statusLevel', 1) @@ -459,6 +497,10 @@ export default class gamification extends Plugin { clearInterval(this.timerId); this.timerId = null; } + + for (const timerId in this.editTimers) { + clearTimeout(this.editTimers[timerId]); + } } From fdec027c450c396bc9afbce60bb8a212ce675b12 Mon Sep 17 00:00:00 2001 From: saertna <83655354+saertna@users.noreply.github.com> Date: Sat, 10 Feb 2024 22:25:44 +0100 Subject: [PATCH 2/7] auto rate after 5sec of file name change --- src/main.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main.ts b/src/main.ts index 5795035..347c6b0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -144,6 +144,9 @@ export default class gamification extends Plugin { this.app.workspace.on('editor-change', this.onEditorChanged.bind(this)) ); + this.registerEvent( + this.app.vault.on('rename', this.onFileRenamed.bind(this)) + ); if (this.getSettingBoolean('debug')){ this.addRibbonIcon("accessibility", "Crafting", async () => { @@ -323,6 +326,12 @@ export default class gamification extends Plugin { }, 5000); // Adjust delay as needed (e.g., 10000 for 10 seconds) } + + onFileRenamed(oldPath: string, newPath: string) { + // Trigger action when a file is renamed + this.triggerAction(newPath); + } + triggerAction(filePath: string) { this.calculateNoteMajurity().then(r => console.log(r)); if(debugLogs) console.log(`File ${filePath} was edited and no further changes occurred.`); From e0f459a77f4016840bf36c9c93f0a3b977c4a320 Mon Sep 17 00:00:00 2001 From: saertna <83655354+saertna@users.noreply.github.com> Date: Sat, 10 Feb 2024 22:51:06 +0100 Subject: [PATCH 3/7] Settings to enable/disable auto rating --- src/main.ts | 9 +++++---- src/settings.ts | 31 ++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/main.ts b/src/main.ts index 347c6b0..ad5185f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -316,14 +316,13 @@ export default class gamification extends Plugin { clearTimeout(this.editTimers[filePath]); } - // Set new timer to trigger action after 5-10 seconds this.editTimers[filePath] = setTimeout(() => { // Check if no further edits happened within the delay if (this.lastEditTimes[filePath] === currentTime) { // Trigger your action here this.triggerAction(filePath); } - }, 5000); // Adjust delay as needed (e.g., 10000 for 10 seconds) + }, this.getSettingNumber('autoRateOnChangeDelayTime') * 1000); } @@ -333,8 +332,10 @@ export default class gamification extends Plugin { } triggerAction(filePath: string) { - this.calculateNoteMajurity().then(r => console.log(r)); - if(debugLogs) console.log(`File ${filePath} was edited and no further changes occurred.`); + if(this.getSettingBoolean('autoRateOnChange')){ + this.calculateNoteMajurity().then(r => console.log(r)); + if(debugLogs) console.log(`File ${filePath} was edited and no further changes occurred.`); + } } private async resetGame() { diff --git a/src/settings.ts b/src/settings.ts index 46ff1fa..c377a96 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -92,7 +92,9 @@ export const defaultSettings: Partial = { delayLoadTime: "U2FsdGVkX19TLndonGY4Y8vHuZFfLJ5gZ2t/CLprh0o=", timeShowNotice: "U2FsdGVkX190u8cOsylOs1cQ8MeZFq+i+Wv4ox6qq0k=", receivedBadges: "U2FsdGVkX1/skTUHmzuMYD86hDA/uF1kElPVYm04ijQ=", - showNewVersionNotification: "U2FsdGVkX1+7lWe/h95uqzgl27JBGW2iki7sBwk44YQ=" + showNewVersionNotification: "U2FsdGVkX1+7lWe/h95uqzgl27JBGW2iki7sBwk44YQ=", + autoRateOnChange: "U2FsdGVkX1/KT5I5txOiZ+r6Aa1F5RuE5b4eqpaZAqQ=", + autoRateOnChangeDelayTime: "U2FsdGVkX1/RiGtHePLD9og+g+w+DL31vVK02vCSkQQ=" }; export interface DynamicSettings { @@ -184,6 +186,8 @@ export interface ISettings extends DynamicSettings{ timeShowNotice: string; receivedBadges: string; showNewVersionNotification: string + autoRateOnChange: string + autoRateOnChangeDelayTime: string //[key: string]: number | string | boolean | MomentInput; } @@ -275,6 +279,8 @@ export class GamificationPluginSettings extends PluginSettingTab { public timeShowNotice: string; public receivedBadges: string; public showNewVersionNotification: string; + public autoRateOnChange: string; + public autoRateOnChangeDelayTime: string; constructor(app: App, plugin: gamification) { super(app, plugin); @@ -374,6 +380,29 @@ export class GamificationPluginSettings extends PluginSettingTab { }), ); + new Setting(containerEl) + .setName('enable auto rate after change') + .setDesc('you can enable here an automatic trigger to rate a note when changed/created') + .addToggle((toggle) => + toggle + .setValue(decryptBoolean(this.plugin.settings.autoRateOnChange)) + .onChange((value) => { + this.plugin.settings.autoRateOnChange = encryptBoolean(value); + this.plugin.saveData(this.plugin.settings); + }), + ); + new Setting(containerEl) + .setName('Wait time for automatic note rating') + .setDesc('Enter in seconds how long to wait after a change before automatical note ratting will be done') + .addText(text => text + .setPlaceholder('5') + .setValue(decryptNumber(this.plugin.settings.delayLoadTime).toString()) + //.setValue("0") + .onChange(async (value) => { + this.plugin.settings.delayLoadTime = encryptNumber(parseInt(value)); + await this.plugin.saveSettings(); + })); + new Setting(containerEl) .setName('Delay load settings at startup') From 82866cfd9a87b01ffcb03e95ebdd86302f6df998 Mon Sep 17 00:00:00 2001 From: saertna <83655354+saertna@users.noreply.github.com> Date: Sat, 10 Feb 2024 23:14:24 +0100 Subject: [PATCH 4/7] Catch double rating (bounce) --- src/main.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index ad5185f..7ac4051 100644 --- a/src/main.ts +++ b/src/main.ts @@ -298,7 +298,7 @@ export default class gamification extends Plugin { } - onEditorChanged() { + async onEditorChanged() { const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); if (!activeView ) return; @@ -308,6 +308,13 @@ export default class gamification extends Plugin { const filePath = activeFile.path; const currentTime = Date.now(); + const fileLastModifiedTime = activeFile?.stat.mtime || 0; + + // Check if the file was recently modified by comparing the last modification time + if (currentTime - fileLastModifiedTime < 900) { + return; + } + // Update last edit time for the file this.lastEditTimes[filePath] = currentTime; From 35c125a051eb234a0a76e20210dca7bce54758cf Mon Sep 17 00:00:00 2001 From: saertna <83655354+saertna@users.noreply.github.com> Date: Sun, 11 Feb 2024 16:50:55 +0100 Subject: [PATCH 5/7] Exclude files in given folders --- src/constants.ts | 2 +- src/main.ts | 46 +++++++++++++++++++++++++++----------- src/maturitycalculation.ts | 3 ++- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 60dfd78..0af4153 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -8,7 +8,7 @@ export const streakboosterIncreaseDaily: number = 0.1; export const streakboosterIncreaseWeekly: number = 1; export const CryptoJS = require("crypto-js"); export const secretKey = "2ZU^12y#QmNB5$yEin5^"; -export const debugLogs = false; +export const debugLogs = true; export const avatarInitContent = `# Avatar | | | diff --git a/src/main.ts b/src/main.ts index 7ac4051..ac5a7d2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -58,7 +58,7 @@ export default class gamification extends Plugin { - getSettingString(key: string) { + getSettingString(key: string): string { const decryptedValue = this.settings[key] !== undefined ? this.settings[key].toString() : '' //if(debugLogs) console.debug(`String: decrypted ${key} is ${decryptString(decryptedValue)}`) return decryptString(decryptedValue); @@ -299,16 +299,23 @@ export default class gamification extends Plugin { async onEditorChanged() { - const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); - if (!activeView ) return; + const activeView = this.app.workspace.getActiveViewOfType(MarkdownView); + if (!activeView) return; const activeFile = activeView.file; if (!activeFile) return; - const filePath = activeFile.path; + //this.getSettingString('folderExclude') + const foldersToExclude = this.getSettingString('folderExclude'); + const folderNames = foldersToExclude.split(',').map(folder => folder.trim()); + + const isInExcludedFolder = folderNames.some(folderName => activeFile.path.includes(folderName)); + + if (isInExcludedFolder) return; + const currentTime = Date.now(); - const fileLastModifiedTime = activeFile?.stat.mtime || 0; + const fileLastModifiedTime = activeFile.stat.mtime || 0; // Check if the file was recently modified by comparing the last modification time if (currentTime - fileLastModifiedTime < 900) { @@ -316,32 +323,45 @@ export default class gamification extends Plugin { } // Update last edit time for the file - this.lastEditTimes[filePath] = currentTime; + this.lastEditTimes[activeFile.path] = currentTime; // Clear previous timer if exists - if (this.editTimers[filePath]) { - clearTimeout(this.editTimers[filePath]); + if (this.editTimers[activeFile.path]) { + clearTimeout(this.editTimers[activeFile.path]); } - this.editTimers[filePath] = setTimeout(() => { + this.editTimers[activeFile.path] = setTimeout(() => { // Check if no further edits happened within the delay - if (this.lastEditTimes[filePath] === currentTime) { + if (this.lastEditTimes[activeFile.path] === currentTime) { // Trigger your action here - this.triggerAction(filePath); + this.triggerAction(activeFile.path); } }, this.getSettingNumber('autoRateOnChangeDelayTime') * 1000); } + onFileRenamed(oldPath: string, newPath: string) { - // Trigger action when a file is renamed + console.log(`${newPath}`); + const foldersToExclude = this.getSettingString('folderExclude'); + console.log(`foldersToExclude: ${foldersToExclude}`); + const folderNames = foldersToExclude.split(',').map(folder => folder.trim() + '/'); + + const isInExcludedFolder = folderNames.some(folderName => newPath.includes(folderName)); + + if (isInExcludedFolder) { + console.log(isInExcludedFolder); + return; + } + this.triggerAction(newPath); } + triggerAction(filePath: string) { if(this.getSettingBoolean('autoRateOnChange')){ this.calculateNoteMajurity().then(r => console.log(r)); - if(debugLogs) console.log(`File ${filePath} was edited and no further changes occurred.`); + //if(debugLogs) console.log(`File ${filePath} was edited and no further changes occurred.`); } } diff --git a/src/maturitycalculation.ts b/src/maturitycalculation.ts index 05e8278..1e42ed1 100644 --- a/src/maturitycalculation.ts +++ b/src/maturitycalculation.ts @@ -1,5 +1,6 @@ import { TFile, App, } from 'obsidian'; import * as path from 'path'; +import {debugLogs} from "./constants"; export function rateProgressiveSummarization(charCountTotal: number, layer2count: number, layer3count: number): number { const percentLayer2 = layer2count * 100 / charCountTotal; @@ -407,7 +408,7 @@ export function count_inlinks(file: TFile): number { const { app: { metadataCache: { resolvedLinks } } } = this; const { path } = file; - console.log('Resolved Links Data:', resolvedLinks); // Add this line + if(debugLogs) console.log('Resolved Links Data:', resolvedLinks); // Add this line const sumInlinks = Object.values(resolvedLinks) .map((val: { [key: string]: number }) => val[path] ?? 0) From 34fe9e40c41da119cfe395e59ecbcb22642b5fc1 Mon Sep 17 00:00:00 2001 From: saertna <83655354+saertna@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:02:55 +0100 Subject: [PATCH 6/7] Connect correct auto rate delay time variable --- src/settings.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/settings.ts b/src/settings.ts index c377a96..754e194 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -396,10 +396,10 @@ export class GamificationPluginSettings extends PluginSettingTab { .setDesc('Enter in seconds how long to wait after a change before automatical note ratting will be done') .addText(text => text .setPlaceholder('5') - .setValue(decryptNumber(this.plugin.settings.delayLoadTime).toString()) + .setValue(decryptNumber(this.plugin.settings.autoRateOnChangeDelayTime).toString()) //.setValue("0") .onChange(async (value) => { - this.plugin.settings.delayLoadTime = encryptNumber(parseInt(value)); + this.plugin.settings.autoRateOnChangeDelayTime = encryptNumber(parseInt(value)); await this.plugin.saveSettings(); })); From e7122200af9346ec3f8e75092abbb97ac0bf76b3 Mon Sep 17 00:00:00 2001 From: saertna <83655354+saertna@users.noreply.github.com> Date: Sun, 11 Feb 2024 17:09:00 +0100 Subject: [PATCH 7/7] version 0.0.89 --- manifest.json | 2 +- package-lock.json | 4 ++-- package.json | 2 +- src/constants.ts | 17 +++++++++-------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/manifest.json b/manifest.json index d867baa..a46c983 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "gamified-pkm", "name": "Gamificate your PKM", - "version": "0.0.88", + "version": "0.0.89", "minAppVersion": "0.15.0", "description": "Enhance your Personal Knowledge Management with gamification elements. Boost motivation and achieve growth as you engage with your PKM.", "author": "Andreas Trebing", diff --git a/package-lock.json b/package-lock.json index b94be99..a8bf6c7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "obsidian-gamified-pkm", - "version": "0.0.88", + "version": "0.0.89", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "obsidian-gamified-pkm", - "version": "0.0.88", + "version": "0.0.89", "license": "MIT", "dependencies": { "crypto-js": "^4.1.1", diff --git a/package.json b/package.json index dcdf9e8..06d0091 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-gamified-pkm", - "version": "0.0.88", + "version": "0.0.89", "description": "Enhance your Personal Knowledge Management with gamification elements. Boost motivation and achieve growth as you engage with your PKM.", "main": "main.js", "scripts": { diff --git a/src/constants.ts b/src/constants.ts index 0af4153..46eedff 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,14 +1,15 @@ import { Badge } from './badges' -export const pointsNoteMajurity: number = 100; -export const pointsMajurity: number = 10; -export const pointsForDailyChallenge: number = 500; -export const pointsForWeeklyChallenge: number = 2000; -export const streakboosterDecrease: number = 0.2; -export const streakboosterIncreaseDaily: number = 0.1; -export const streakboosterIncreaseWeekly: number = 1; +export const pointsNoteMajurity = 100; +export const pointsMajurity = 10; +export const pointsForDailyChallenge = 500; +export const pointsForWeeklyChallenge = 2000; +export const streakboosterDecrease = 0.2; +export const streakboosterIncreaseDaily = 0.1; +export const streakboosterIncreaseWeekly = 1; +// eslint-disable-next-line @typescript-eslint/no-var-requires export const CryptoJS = require("crypto-js"); export const secretKey = "2ZU^12y#QmNB5$yEin5^"; -export const debugLogs = true; +export const debugLogs = false; export const avatarInitContent = `# Avatar | | |