From 0eaafb439ac81469e2f8af286822bf3f0430fde9 Mon Sep 17 00:00:00 2001 From: mgmeyers Date: Sun, 26 Dec 2021 17:01:30 -0800 Subject: [PATCH] Minor bug fixes --- manifest.json | 2 +- src/main.ts | 28 ++++++++++++++++++---------- src/titleDecoration.ts | 11 +++++++++++ 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/manifest.json b/manifest.json index d1ef761..af2f107 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-embedded-note-titles", "name": "Embedded Note Titles", - "version": "1.2.2", + "version": "1.2.3", "minAppVersion": "0.13.8", "description": "Inserts the note file name as an H1 heading above each note.", "author": "mgmeyers", diff --git a/src/main.ts b/src/main.ts index 9db0c2e..ddefafb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -104,14 +104,20 @@ export default class EmbeddedNoteTitlesPlugin extends Plugin { this.previewHeadingsManager.createHeadings(this.app); }, 0); - setTimeout(() => { - this.observedTitles.forEach((_, el) => { - if (!document.getElementById(el.id)) { - this.unobserveTitle(el); - el.remove(); - } - }); - }, 100); + if (!this.isLegacyEditor) { + setTimeout(() => { + this.observedTitles.forEach((_, el) => { + if ( + this.app.workspace + .getLeavesOfType("markdown") + .every((leaf) => !leaf.view.containerEl.find(`#${el.id}`)) + ) { + this.unobserveTitle(el); + el.remove(); + } + }); + }, 100); + } }) ); @@ -156,8 +162,10 @@ export default class EmbeddedNoteTitlesPlugin extends Plugin { } unobserveTitle(el: HTMLElement) { - this.observedTitles.delete(el); - this.observer.unobserve(el); + if (this.observedTitles.has(el)) { + this.observedTitles.delete(el); + this.observer.unobserve(el); + } } async loadSettings() { diff --git a/src/titleDecoration.ts b/src/titleDecoration.ts index 402142b..f1efd7d 100644 --- a/src/titleDecoration.ts +++ b/src/titleDecoration.ts @@ -110,6 +110,17 @@ export function buildTitleDecoration( view.state.field(editorViewField) ); + // This shouldn't happen, but just to be safe, remove any straggling titles + view.contentDOM.parentElement.childNodes.forEach((node) => { + if ( + node instanceof HTMLElement && + node.hasClass("embedded-note-title") + ) { + plugin.unobserveTitle(node); + node.remove(); + } + }); + this.header = createEl("h1", { text: this.title, cls: `cm-line embedded-note-title embedded-note-title__edit${