From 6bd004facf95cf83650e3b32722b306847dd8062 Mon Sep 17 00:00:00 2001 From: Andrea Alberti Date: Sun, 23 Jun 2024 11:31:10 +0200 Subject: [PATCH 1/4] Fixed two bugs --- src/main.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main.ts b/src/main.ts index 5e575a0..9b76401 100644 --- a/src/main.ts +++ b/src/main.ts @@ -165,6 +165,13 @@ export default class PluginsAnnotations extends Plugin { if (isPlaceholder) { comment.innerText = ''; comment.classList.remove('plugin-comment-placeholder'); + const range = document.createRange(); + range.selectNodeContents(comment); + const selection = window.getSelection(); + if (selection) { + selection.removeAllRanges(); + selection.addRange(range); + } isPlaceholder = false; } }); @@ -216,9 +223,11 @@ export default class PluginsAnnotations extends Plugin { if (this.saveTimeout) { clearTimeout(this.saveTimeout); } - + + const annotations = this.annotations; + this.saveTimeout = window.setTimeout(() => { - db.saveAnnotations(this.app.vault, this.annotations); + db.saveAnnotations(this.app.vault, annotations); this.saveTimeout = null; }, timeout_ms); } From 1afc83aea90cddf7fd96b12ed9c683e7c46f1416 Mon Sep 17 00:00:00 2001 From: Andrea Alberti Date: Sun, 23 Jun 2024 11:42:07 +0200 Subject: [PATCH 2/4] Fixed bug not showing caret. --- src/main.ts | 18 +++++++++++++----- src/types.ts | 4 ++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main.ts b/src/main.ts index 9b76401..31c636c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,7 +10,7 @@ import { } from 'obsidian'; import { around } from 'monkey-around'; import * as db from './db'; -import { PluginAnnotationDict } from './types'; +import { PluginAnnotationDict, HTMLDivElementWithInput } from './types'; export default class PluginsAnnotations extends Plugin { private annotations: PluginAnnotationDict = {}; @@ -147,7 +147,7 @@ export default class PluginsAnnotations extends Plugin { label.className = 'plugin-comment-label'; comment_container.appendChild(label); - const comment = document.createElement('div'); + const comment = document.createElement('div') as HTMLDivElementWithInput; comment.className = 'plugin-comment-annotation'; comment.contentEditable = 'true'; const placeholder = `Add your personal comment about '${pluginName}' here...`; @@ -160,10 +160,13 @@ export default class PluginsAnnotations extends Plugin { comment.innerText = initialText; + // Add a custom property to track input status + comment.inputTriggered = false; + // Remove placeholder class when user starts typing comment.addEventListener('focus', () => { if (isPlaceholder) { - comment.innerText = ''; + // comment.innerText = ''; comment.classList.remove('plugin-comment-placeholder'); const range = document.createRange(); range.selectNodeContents(comment); @@ -176,12 +179,15 @@ export default class PluginsAnnotations extends Plugin { } }); + // Add placeholder class back if no changes are made comment.addEventListener('blur', () => { - if (comment.innerText.trim() === '') { + if (!comment.inputTriggered || comment.innerText.trim() === '') { comment.innerText = placeholder; comment.classList.add('plugin-comment-placeholder'); isPlaceholder = true; } + // Reset the inputTriggered status on blur + comment.inputTriggered = false; }); label.addEventListener('click', (event) => { @@ -193,8 +199,9 @@ export default class PluginsAnnotations extends Plugin { event.stopPropagation(); }); - // Save the comment on input change + // Save the comment on input change and update inputTriggered status comment.addEventListener('input', () => { + comment.inputTriggered = true; // Update the custom property if (comment.innerText.trim() === '') { delete this.annotations[pluginId]; comment.classList.add('plugin-comment-placeholder'); @@ -216,6 +223,7 @@ export default class PluginsAnnotations extends Plugin { }); } + debouncedSaveAnnotations() { // timeout after 250 ms const timeout_ms = 250; diff --git a/src/types.ts b/src/types.ts index 7bc1bec..bcbf3fb 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,3 +3,7 @@ export interface PluginAnnotationDict { [pluginId: string]: string; } + +export interface HTMLDivElementWithInput extends HTMLDivElement { + inputTriggered: boolean; +} \ No newline at end of file From 82c3b7c6c2a13033bcb91e2ce72e93799bbaf294 Mon Sep 17 00:00:00 2001 From: Andrea Alberti Date: Sun, 23 Jun 2024 11:56:31 +0200 Subject: [PATCH 3/4] Fixed bug on handling the placeholder flag --- src/main.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/main.ts b/src/main.ts index 31c636c..8a99dd3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,7 +10,7 @@ import { } from 'obsidian'; import { around } from 'monkey-around'; import * as db from './db'; -import { PluginAnnotationDict, HTMLDivElementWithInput } from './types'; +import { PluginAnnotationDict } from './types'; export default class PluginsAnnotations extends Plugin { private annotations: PluginAnnotationDict = {}; @@ -147,7 +147,7 @@ export default class PluginsAnnotations extends Plugin { label.className = 'plugin-comment-label'; comment_container.appendChild(label); - const comment = document.createElement('div') as HTMLDivElementWithInput; + const comment = document.createElement('div'); comment.className = 'plugin-comment-annotation'; comment.contentEditable = 'true'; const placeholder = `Add your personal comment about '${pluginName}' here...`; @@ -160,9 +160,6 @@ export default class PluginsAnnotations extends Plugin { comment.innerText = initialText; - // Add a custom property to track input status - comment.inputTriggered = false; - // Remove placeholder class when user starts typing comment.addEventListener('focus', () => { if (isPlaceholder) { @@ -175,19 +172,16 @@ export default class PluginsAnnotations extends Plugin { selection.removeAllRanges(); selection.addRange(range); } - isPlaceholder = false; } }); // Add placeholder class back if no changes are made comment.addEventListener('blur', () => { - if (!comment.inputTriggered || comment.innerText.trim() === '') { + if (isPlaceholder || comment.innerText.trim() === '') { comment.innerText = placeholder; comment.classList.add('plugin-comment-placeholder'); isPlaceholder = true; } - // Reset the inputTriggered status on blur - comment.inputTriggered = false; }); label.addEventListener('click', (event) => { @@ -201,12 +195,12 @@ export default class PluginsAnnotations extends Plugin { // Save the comment on input change and update inputTriggered status comment.addEventListener('input', () => { - comment.inputTriggered = true; // Update the custom property if (comment.innerText.trim() === '') { + isPlaceholder = true; delete this.annotations[pluginId]; comment.classList.add('plugin-comment-placeholder'); - isPlaceholder = true; } else { + isPlaceholder = false; this.annotations[pluginId] = comment.innerText; comment.classList.remove('plugin-comment-placeholder'); isPlaceholder = false; From 15aa13a578a0f0b2bdd1d56c94bf6a28ed7f00ee Mon Sep 17 00:00:00 2001 From: Andrea Alberti Date: Sun, 23 Jun 2024 11:56:42 +0200 Subject: [PATCH 4/4] Fixed bug on handling the placeholder flag --- manifest.json | 2 +- src/types.ts | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/manifest.json b/manifest.json index bd1bb30..244fdb2 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "plugins-annotations", "name": "Plugins Annotations", - "version": "1.0.11", + "version": "1.0.12", "minAppVersion": "1.5.0", "description": "Allows adding personal comments to each installed plugin.", "author": "Andrea Alberti", diff --git a/src/types.ts b/src/types.ts index bcbf3fb..7bc1bec 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,3 @@ export interface PluginAnnotationDict { [pluginId: string]: string; } - -export interface HTMLDivElementWithInput extends HTMLDivElement { - inputTriggered: boolean; -} \ No newline at end of file