From ce099d133b02b870f3ad207d07c11ed57b53dd97 Mon Sep 17 00:00:00 2001 From: valentine195 <38669521+valentine195@users.noreply.github.com> Date: Wed, 21 Jul 2021 13:33:10 -0400 Subject: [PATCH 1/2] Checkbox preview to markdown problem Fixes #65 --- src/main.ts | 88 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 57 insertions(+), 31 deletions(-) diff --git a/src/main.ts b/src/main.ts index b7cd691..c307c36 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import { addIcon, MarkdownPostProcessorContext, + MarkdownPreviewView, MarkdownRenderChild, MarkdownRenderer, MarkdownView, @@ -42,6 +43,12 @@ declare module "obsidian" { findCommand(id: string): Command; }; } + interface MarkdownPreviewView { + renderer: MarkdownPreviewRenderer; + } + interface MarkdownPreviewRenderer { + onCheckboxClick: (evt: MouseEvent, el: HTMLInputElement) => void; + } } Object.fromEntries = Object.fromEntries || @@ -772,38 +779,57 @@ title: const taskLists = admonitionContent.querySelectorAll( ".contains-task-list" ); - const splitContent = src.split("\n"); - - for (let i = 0; i < taskLists.length; i++) { - let tasks: NodeListOf = - taskLists[i].querySelectorAll(".task-list-item"); - if (!tasks.length) continue; - for (let j = 0; j < tasks.length; j++) { - let task = tasks[j]; - if (!task.children.length) continue; - const inputs = task.querySelectorAll( - "input[type='checkbox']" - ) as NodeListOf; - if (!inputs.length) continue; - const input = inputs[0]; - - if ( - !input.nextSibling || - input.nextSibling.nodeName != "#text" - ) - continue; - const innerText = input.nextSibling.textContent; - - const search = new RegExp( - `\\[\\s?[xX]?\\s?\\]\\s*${innerText}` - ); - - const line = splitContent.find((l) => search.test(l)); + if (taskLists.length) { + const view = + this.app.workspace.getActiveViewOfType(MarkdownView); - input.dataset["line"] = `${splitContent.indexOf(line) + 1}`; - input.onclick = (evt) => { - evt.stopPropagation(); - }; + if (view && view instanceof MarkdownView) { + const file = view.file; + const fileContent = view.currentMode.get(); + const splitContent = src.split("\n"); + let slicer = 0; + const start = fileContent.indexOf(src); + for (let i = 0; i < taskLists.length; i++) { + let tasks: NodeListOf = + taskLists[i].querySelectorAll(".task-list-item"); + if (!tasks.length) continue; + for (let j = 0; j < tasks.length; j++) { + let task = tasks[j]; + if (!task.children.length) continue; + const inputs = task.querySelectorAll( + "input[type='checkbox']" + ) as NodeListOf; + if (!inputs.length) continue; + const input = inputs[0]; + + if ( + !input.nextSibling || + input.nextSibling.nodeName != "#text" + ) + continue; + const line = splitContent + .slice(slicer) + .find((str) => + new RegExp( + `\\[.*\\]\\s*${task.innerText}` + ).test(str) + ); + slicer = + slicer + + splitContent.slice(slicer).indexOf(line) + + 1; + + const lineNumber = slicer; + + input.dataset["line"] = `${lineNumber}`; + input.onclick = async (evt) => { + view.previewMode.renderer.onCheckboxClick( + evt, + input + ); + }; + } + } } } From af0cbc1ca8474523c494a743acb27ee5f10b1980 Mon Sep 17 00:00:00 2001 From: valentine195 <38669521+valentine195@users.noreply.github.com> Date: Wed, 21 Jul 2021 13:33:41 -0400 Subject: [PATCH 2/2] version bump 6.2.2 --- manifest.json | 2 +- package.json | 2 +- versions.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/manifest.json b/manifest.json index ae37f2a..653472a 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-admonition", "name": "Admonition", - "version": "6.2.1", + "version": "6.2.2", "minAppVersion": "0.11.0", "description": "Admonition block-styled content for Obsidian.md", "author": "Jeremy Valentine", diff --git a/package.json b/package.json index 12535ee..beda182 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-admonition", - "version": "6.2.1", + "version": "6.2.2", "description": "Admonition block-styled content for Obsidian.md", "main": "main.js", "scripts": { diff --git a/versions.json b/versions.json index 0425bd6..89655d8 100644 --- a/versions.json +++ b/versions.json @@ -11,5 +11,5 @@ "4.3.1": "0.12.0", "4.4.2": "0.12.2", "5.0.3": "0.12.2", - "6.2.1": "0.12.4" + "6.2.2": "0.12.4" } \ No newline at end of file