diff --git a/plugs/tasks/task.ts b/plugs/tasks/task.ts index e1cccf4d..8f1100fd 100644 --- a/plugs/tasks/task.ts +++ b/plugs/tasks/task.ts @@ -178,9 +178,18 @@ export function previewTaskToggle(eventString: string) { } } -async function cycleTaskState( - node: ParseTree, -) { +async function convertListItemToTask(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!; let changeTo: string | undefined; if (completeStates.includes(stateText)) { @@ -325,17 +334,27 @@ export async function taskCycleCommand() { return; } console.log("Node", node); - const taskNode = node.type === "Task" - ? node - : findParentMatching(node!, (n) => n.type === "Task"); - if (!taskNode) { + const taskNode = + node.type === "Task" + ? node + : 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"); return; } - const taskState = findNodeOfType(taskNode!, "TaskState"); - if (taskState) { - await cycleTaskState(taskState); - } + + convertListItemToTask(listItem); } export async function postponeCommand() {