Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/renderer/commands/commandSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createWrapSelectionOrWordCommand,
insertHorizontalRuleCommand,
toggleBlockquoteCommand,
toggleTaskCheckCommand,
toggleFencedCodeBlockCommand,
toggleOrderedListCommand,
toggleTaskListCommand,
Expand Down Expand Up @@ -34,6 +35,7 @@ export type CommandId =
| "insert.unorderedList"
| "insert.orderedList"
| "insert.taskList"
| "insert.taskCheck"
| "insert.blockquote"
| "insert.codeBlock"
| "theme.set"
Expand All @@ -58,6 +60,7 @@ export type CommandArgs = {
"insert.unorderedList": void;
"insert.orderedList": void;
"insert.taskList": void;
"insert.taskCheck": void;
"insert.blockquote": void;
"insert.codeBlock": void;
"theme.set": { theme: ThemeName };
Expand Down Expand Up @@ -179,6 +182,7 @@ const editorCommands = {
unorderedList: toggleUnorderedListCommand,
orderedList: toggleOrderedListCommand,
taskList: toggleTaskListCommand,
taskCheck: toggleTaskCheckCommand,
blockquote: toggleBlockquoteCommand,
codeBlock: toggleFencedCodeBlockCommand,
} as const;
Expand Down Expand Up @@ -365,6 +369,17 @@ export const createCommandRegistry = (): CommandRegistry => {
return view ? editorCommands.taskList(view) : false;
},
},
{
id: "insert.taskCheck",
title: "Toggle task check",
category: "Insert",
description: "Toggle checked state for task checklist items.",
requiresEditor: true,
Comment on lines +372 to +377
run: (ctx) => {
const view = ctx.getEditorView();
return view ? editorCommands.taskCheck(view) : false;
},
},
{
id: "insert.blockquote",
title: "Quote",
Expand Down
6 changes: 6 additions & 0 deletions src/renderer/components/EditorContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ export function EditorContextMenu({
Task list
<ContextMenuShortcut>{shortcuts.taskList}</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem
inset
onSelect={() => runCommand("insert.taskCheck")}
>
Toggle task check
</ContextMenuItem>
<ContextMenuItem
inset
onSelect={() => runCommand("insert.blockquote")}
Expand Down
37 changes: 37 additions & 0 deletions src/renderer/editor/codemirror/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,43 @@ export const toggleTaskListCommand = (
prefixForLine: () => "- [ ] ",
});

export const toggleTaskCheckCommand = (
view: import("@codemirror/view").EditorView
): boolean => {
const { start, end } = getSelectedLineNumbers(view);
const taskLines = [];

for (let lineNumber = start; lineNumber <= end; lineNumber += 1) {
const line = view.state.doc.line(lineNumber);
const match = line.text.match(/^(\s*[-*+]\s+\[)([ xX])(\]\s+)/);
if (match) {
taskLines.push({ line, match });
}
}

if (taskLines.length === 0) {
return false;
}

const shouldUncheck = taskLines.every(({ match }) =>
/[xX]/.test(match[2])
);
const changes = taskLines.map(({ line, match }) => {
const markerOffset = match[1].length;
return {
from: line.from + markerOffset,
to: line.from + markerOffset + 1,
insert: shouldUncheck ? " " : "x",
};
});

view.dispatch({
changes,
scrollIntoView: true,
});
return true;
};

export const toggleBlockquoteCommand = (
view: import("@codemirror/view").EditorView
): boolean =>
Expand Down
28 changes: 28 additions & 0 deletions tests/controllerShortcuts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
toggleBlockquoteCommand,
toggleFencedCodeBlockCommand,
toggleOrderedListCommand,
toggleTaskCheckCommand,
toggleTaskListCommand,
toggleUnorderedListCommand,
} from "../src/renderer/editor/codemirror/commands";
Expand Down Expand Up @@ -174,6 +175,33 @@ runTest("task list command toggles checklist markers", () => {
assert.equal(view.text, "one\ntwo");
});

runTest("task check command toggles unchecked tasks on", () => {
const view = new FakeView("- [ ] one\n- [x] two", { from: 0, to: 19 });

const handled = toggleTaskCheckCommand(view as unknown as EditorView);

assert.equal(handled, true);
assert.equal(view.text, "- [x] one\n- [x] two");
});

runTest("task check command toggles checked tasks off", () => {
const view = new FakeView("- [x] one\n- [X] two", { from: 0, to: 19 });

const handled = toggleTaskCheckCommand(view as unknown as EditorView);

assert.equal(handled, true);
assert.equal(view.text, "- [ ] one\n- [ ] two");
});

runTest("task check command falls through outside task lines", () => {
const view = new FakeView("- one", { from: 0, to: 5 });

const handled = toggleTaskCheckCommand(view as unknown as EditorView);

assert.equal(handled, false);
assert.equal(view.text, "- one");
});

runTest("blockquote command toggles selected lines", () => {
const view = new FakeView("one\ntwo", { from: 0, to: 7 });

Expand Down
Loading