Convert bullet to task when Task: Cycle State is called

Fixes #1074
pull/1099/head
Abin Simon 2024-09-28 11:43:41 +05:30
parent 4bc6ed719c
commit 9387bd988c
1 changed files with 30 additions and 11 deletions

View File

@ -178,9 +178,18 @@ export function previewTaskToggle(eventString: string) {
} }
} }
async function cycleTaskState( async function convertListItemToTask(node: ParseTree){
node: ParseTree, const listMark = node.children![0];
) { await editor.dispatch({
changes: {
from: listMark.from,
to: listMark.to,
insert: "- [ ]",
},
});
}
async function cycleTaskState(node: ParseTree) {
const stateText = node.children![1].text!; const stateText = node.children![1].text!;
let changeTo: string | undefined; let changeTo: string | undefined;
if (completeStates.includes(stateText)) { if (completeStates.includes(stateText)) {
@ -325,17 +334,27 @@ export async function taskCycleCommand() {
return; return;
} }
console.log("Node", node); console.log("Node", node);
const taskNode = node.type === "Task" const taskNode =
? node node.type === "Task"
: findParentMatching(node!, (n) => n.type === "Task"); ? node
if (!taskNode) { : findParentMatching(node!, (n) => n.type === "Task");
if (taskNode) {
const taskState = findNodeOfType(taskNode!, "TaskState");
if (taskState) {
await cycleTaskState(taskState);
}
return
}
// Convert a bullet point to a task
const listItem = findParentMatching(node!, (n) => n.type === "ListItem");
if (!listItem) {
await editor.flashNotification("No task at cursor"); await editor.flashNotification("No task at cursor");
return; return;
} }
const taskState = findNodeOfType(taskNode!, "TaskState");
if (taskState) { convertListItemToTask(listItem);
await cycleTaskState(taskState);
}
} }
export async function postponeCommand() { export async function postponeCommand() {