Skip to content

Commit

Permalink
6.2.2
Browse files Browse the repository at this point in the history
Fixes #65 - Checkboxes now update markdown again
  • Loading branch information
valentine195 authored Jul 21, 2021
2 parents 33c4ed7 + af0cbc1 commit fbe9cf4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
88 changes: 57 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addIcon,
MarkdownPostProcessorContext,
MarkdownPreviewView,
MarkdownRenderChild,
MarkdownRenderer,
MarkdownView,
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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<HTMLLIElement> =
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<HTMLInputElement>;
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<HTMLLIElement> =
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<HTMLInputElement>;
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
);
};
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit fbe9cf4

Please sign in to comment.