From f24fbbb472a3b80fb8ffa4d5304777ac76a14960 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Fri, 28 Jul 2023 13:54:31 +0200 Subject: [PATCH] Fixes #433 --- plugs/directive/command.ts | 2 ++ plugs/tasks/task.ts | 49 ++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/plugs/directive/command.ts b/plugs/directive/command.ts index 47a742e2..45e3de10 100644 --- a/plugs/directive/command.ts +++ b/plugs/directive/command.ts @@ -19,6 +19,8 @@ export async function updateDirectivesOnPageCommand() { return; } + await editor.save(); + // Collect all directives and their body replacements const replacements: { fullMatch: string; textPromise: Promise }[] = []; diff --git a/plugs/tasks/task.ts b/plugs/tasks/task.ts index ccec3f9f..c37851d3 100644 --- a/plugs/tasks/task.ts +++ b/plugs/tasks/task.ts @@ -14,8 +14,6 @@ import { import { addParentPointers, collectNodesMatching, - collectNodesMatchingAsync, - collectNodesOfType, findNodeOfType, nodeAtPos, ParseTree, @@ -109,9 +107,8 @@ export async function previewTaskToggle(eventString: string) { } async function toggleTaskMarker( - _pageName: string, + pageName: string, node: ParseTree, - moveToPos: number, ) { let changeTo = "[x]"; if (node.children![0].text === "[x]" || node.children![0].text === "[X]") { @@ -132,23 +129,35 @@ async function toggleTaskMarker( for (const wikiLink of parentWikiLinks) { const ref = wikiLink.children![0].text!; if (ref.includes("@")) { - const [page, pos] = ref.split("@"); - let text = await space.readPage(page); + const [page, posS] = ref.split("@"); + const pos = +posS; + if (page === pageName) { + // In current page, just update the task marker with dispatch + await editor.dispatch({ + changes: { + from: pos, + to: pos + changeTo.length, + insert: changeTo, + }, + }); + } else { + let text = await space.readPage(page); - const referenceMdTree = await markdown.parseMarkdown(text); - // Adding +1 to immediately hit the task marker - const taskMarkerNode = nodeAtPos(referenceMdTree, +pos + 1); + const referenceMdTree = await markdown.parseMarkdown(text); + // Adding +1 to immediately hit the task marker + const taskMarkerNode = nodeAtPos(referenceMdTree, pos + 1); - if (!taskMarkerNode || taskMarkerNode.type !== "TaskMarker") { - console.error( - "Reference not a task marker, out of date?", - taskMarkerNode, - ); - return; + if (!taskMarkerNode || taskMarkerNode.type !== "TaskMarker") { + console.error( + "Reference not a task marker, out of date?", + taskMarkerNode, + ); + return; + } + taskMarkerNode.children![0].text = changeTo; + text = renderToText(referenceMdTree); + await space.writePage(page, text); } - taskMarkerNode.children![0].text = changeTo; - text = renderToText(referenceMdTree); - await space.writePage(page, text); } } } @@ -160,7 +169,7 @@ export async function taskToggleAtPos(pageName: string, pos: number) { const node = nodeAtPos(mdTree, pos); if (node && node.type === "TaskMarker") { - await toggleTaskMarker(pageName, node, pos); + await toggleTaskMarker(pageName, node); } } @@ -173,7 +182,7 @@ export async function taskToggleCommand() { const node = nodeAtPos(tree, pos); // We kwow node.type === Task (due to the task context) const taskMarker = findNodeOfType(node!, "TaskMarker"); - await toggleTaskMarker(await editor.getCurrentPage(), taskMarker!, pos); + await toggleTaskMarker(await editor.getCurrentPage(), taskMarker!); } export async function postponeCommand() {