From 9b779f551b286c8acdfa29a2c5c3f6d63418e892 Mon Sep 17 00:00:00 2001 From: Andrea Alberti Date: Tue, 13 Aug 2024 18:13:26 +0200 Subject: [PATCH] Small improvements --- package.json | 2 +- src/main.ts | 52 +++++++++++++++++++++------------------------------- 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 9f30427..2468de6 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "main.js", "scripts": { "dev": "node esbuild.config.mjs", - "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", + "build": "npm run grammar && tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", "version": "node version-bump.mjs && git add manifest.json versions.json", "grammar": "peggy --format es src/grammar.pegjs -o src/peggy.mjs" }, diff --git a/src/main.ts b/src/main.ts index bef22ba..79ef470 100644 --- a/src/main.ts +++ b/src/main.ts @@ -266,7 +266,7 @@ export default class PluginsAnnotations extends Plugin { if (annotation) { if(containerEl && containerEl.lastElementChild) { - self.addComment(containerEl.lastElementChild) + self.addAnnotation(containerEl.lastElementChild) } } }; @@ -281,7 +281,7 @@ export default class PluginsAnnotations extends Plugin { const observer = new MutationObserver(() => { this.addIcon(tab); - this.addComments(tab); + this.addAnnotations(tab); }); observer.observe(tab.containerEl, { childList: true, subtree: false }); @@ -290,7 +290,7 @@ export default class PluginsAnnotations extends Plugin { // Initial call to add comments to already present plugins this.addIcon(tab); - this.addComments(tab); + this.addAnnotations(tab); } disconnectObservers() { @@ -302,7 +302,7 @@ export default class PluginsAnnotations extends Plugin { } // Helper function to parse links and add click listeners - parse_links(element: HTMLElement) { + handleAnnotationLinks(element: HTMLElement) { const links = element.querySelectorAll('a'); links.forEach(link => { link.addEventListener('click', (event) => { @@ -328,50 +328,40 @@ export default class PluginsAnnotations extends Plugin { } } - async render_annotation(annotation_div: HTMLDivElement, t:AnnotationType, c:string) { - switch(t) { + async renderAnnotation(annotation_div: HTMLDivElement, annoType:AnnotationType, desc:string) { + switch(annoType) { case AnnotationType.text: { const p = document.createElement('p'); p.dir = 'auto'; const label = this.create_label(); if(label) { p.appendChild(label); - p.appendText(c); + p.appendText(desc); } else { - p.innerText = c; + p.innerText = desc; } annotation_div.appendChild(p); break; } case AnnotationType.html: { const label = Platform.isMobile ? this.settings.label_mobile : this.settings.label_desktop; - let c_with_label; - if(label.trim()==="") { - c_with_label = c; - } else { - c_with_label = c.replace(/\$\{label\}/g, label); - } - annotation_div.innerHTML = c_with_label; - this.parse_links(annotation_div); + const desc_with_label = desc.replace(/\$\{label\}/g, label); + annotation_div.innerHTML = desc_with_label; + this.handleAnnotationLinks(annotation_div); break; } case AnnotationType.markdown: { const label = Platform.isMobile ? this.settings.label_mobile : this.settings.label_desktop; - let c_with_label; - if(label.trim()==="") { - c_with_label = c; - } else { - c_with_label = c.replace(/\$\{label\}/g, label); - } - await MarkdownRenderer.renderMarkdown(c_with_label, annotation_div, '', this); - this.parse_links(annotation_div); + const desc_with_label = desc.replace(/\$\{label\}/g, label); + await MarkdownRenderer.renderMarkdown(desc_with_label, annotation_div, '', this); + this.handleAnnotationLinks(annotation_div); break; } } } - set_annotation(annotation_container:HTMLDivElement,annotation_div:HTMLDivElement,pluginId:string,pluginName:string) { + configureAnnotation(annotation_container:HTMLDivElement,annotation_div:HTMLDivElement,pluginId:string,pluginName:string) { annotation_div.contentEditable = this.settings.editable ? 'true' : 'false'; @@ -396,7 +386,7 @@ export default class PluginsAnnotations extends Plugin { } // Initial render - this.render_annotation(annotation_div,annoType,annotation_text); + this.renderAnnotation(annotation_div,annoType,annotation_text); let clickedLink = false; const handleMouseDown = (event:MouseEvent) => { @@ -456,7 +446,7 @@ export default class PluginsAnnotations extends Plugin { isPlaceholder = true; } else { const {type,content} = parse_annotation_1_4_0(annotation_text); - this.render_annotation(annotation_div,type,content); + this.renderAnnotation(annotation_div,type,content); } } @@ -567,7 +557,7 @@ export default class PluginsAnnotations extends Plugin { } } - addComment(plugin: Element) { + addAnnotation(plugin: Element) { const settingItemInfo = plugin.querySelector('.setting-item-info'); if (settingItemInfo) { const pluginNameDiv = plugin.querySelector('.setting-item-name'); @@ -594,7 +584,7 @@ export default class PluginsAnnotations extends Plugin { const annotation_div = document.createElement('div'); annotation_div.className = 'plugin-comment-annotation'; - this.set_annotation(annotation_container,annotation_div,pluginId,pluginName); + this.configureAnnotation(annotation_container,annotation_div,pluginId,pluginName); annotation_container.appendChild(annotation_div); descriptionDiv.appendChild(annotation_container); @@ -603,7 +593,7 @@ export default class PluginsAnnotations extends Plugin { } } - async addComments(tab: SettingTab) { + async addAnnotations(tab: SettingTab) { // force reload - this is convenient because since the loading of the plugin // there could be changes in the settings due to synchronization among devices // which only happens after the plugin is loaded @@ -614,7 +604,7 @@ export default class PluginsAnnotations extends Plugin { const plugins = pluginsContainer.querySelectorAll('.setting-item'); plugins.forEach(plugin => { - this.addComment(plugin); + this.addAnnotation(plugin); }); }