Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(editor): handle shortcuts at the selected list level (#8109)
Jiayuan Zhang71ee15422e93
diff --git a/packages/views/editor/extensions/list-item.ts b/packages/views/editor/extensions/list-item.tsindex 45ac3231f4..33623fa69f 100644--- a/packages/views/editor/extensions/list-item.ts+++ b/packages/views/editor/extensions/list-item.ts@@ -86,26 +86,37 @@ function sinkListItemRange(itemType: NodeType): Command {* and must be swallowed even when the structural indent is a no-op (first child* with nothing to nest under, or already at max depth). Otherwise the unhandled* Tab falls through to the browser and moves focus out of the editor to the- * next control. So the return value tracks "is the caret in this list?"- * (`editor.isActive(name)`), NOT "did the indent move anything?": indent- * best-effort, then swallow while in a list, fall through (focus nav) when not.+ * next control. Handle only the nearest list range containing the selection:+ * `isActive` also matches ancestor items, so a checkbox keymap would otherwise+ * swallow Tab or lift the parent checkbox while editing a nested bullet.*/function listItemKeymap(editor: Editor, name: string) {+ const ownsSelection = () => {+ const { $from, $to } = editor.state.selection;+ const range = $from.blockRange($to, (node) => {+ const itemName = node.firstChild?.type.name;+ return itemName === "listItem" || itemName === "taskItem";+ });+ return range?.parent.firstChild?.type.name === name;+ };+return {Enter: () =>+ ownsSelection() &&editor.commands.first(({ commands }) => [() => commands.splitListItem(name),() => commands.liftListItem(name),]),Tab: () => {+ if (!ownsSelection()) return false;const itemType = editor.schema.nodes[name];if (!itemType) return false;sinkListItemRange(itemType)(editor.state, (tr) =>editor.view.dispatch(tr),);- return editor.isActive(name);+ return true;},- "Shift-Tab": () => editor.commands.liftListItem(name),+ "Shift-Tab": () => ownsSelection() && editor.commands.liftListItem(name),};}diff --git a/packages/views/editor/extensions/list-keyboard.test.ts b/packages/views/editor/extensions/list-keyboard.test.tsnew file mode 100644index 0000000000..796e338ba1--- /dev/null+++ b/packages/views/editor/extensions/list-keyboard.test.ts@@ -0,0 +1,141 @@+import { afterEach, describe, expect, it } from "vitest";+import { Editor } from "@tiptap/core";+import { createEditorExtensions } from ".";++let editor: Editor;++afterEach(() => {+ editor?.destroy();+ document.body.innerHTML = "";+});++function load(markdown: string) {+ const element = document.createElement("div");+ document.body.appendChild(element);+ editor = new Editor({+ element,+ extensions: createEditorExtensions({+ disableMentions: true,+ onUploadFileRef: { current: undefined },+ }),+ content: markdown,+ contentType: "markdown",+ });+}++function selectText(text: string) {+ let position: number | undefined;+ editor.state.doc.descendants((node, pos) => {+ if (node.isText && node.text === text) position = pos;+ });+ if (position === undefined) throw new Error(`Missing text: ${text}`);+ editor.commands.setTextSelection(position);+}++function press(key: string, shiftKey = false) {+ const event = new KeyboardEvent("keydown", {+ key,+ shiftKey,+ bubbles: true,+ cancelable: true,+ });+ editor.view.dom.dispatchEvent(event);+ return event.defaultPrevented;+}++function itemAncestors() {+ const { $from } = editor.state.selection;+ const names: string[] = [];+ for (let depth = 1; depth <= $from.depth; depth++) {+ const name = $from.node(depth).type.name;+ if (name === "listItem" || name === "taskItem") names.push(name);+ }+ return names;+}++describe("list shortcuts through the production keymaps", () => {+ it.each(["-", "1.", "- [ ]"])("indents and outdents a %s item", (marker) => {+ load(`${marker} first\n${marker} second`);+ selectText("second");+ expect(press("Tab")).toBe(true);+ expect(itemAncestors()).toHaveLength(2);+ expect(press("Tab", true)).toBe(true);+ expect(itemAncestors()).toHaveLength(1);+ });++ it.each(["-", "1."])("indents a %s item nested under a checkbox", (marker) => {+ load(`- [ ] outer\n ${marker} first\n ${marker} second`);+ selectText("second");+ expect(press("Tab")).toBe(true);+ expect(itemAncestors()).toEqual(["taskItem", "listItem", "listItem"]);+ expect(press("Tab", true)).toBe(true);+ expect(itemAncestors()).toEqual(["taskItem", "listItem"]);+ });++ it("outdents an inner bullet without lifting its enclosing checkbox", () => {+ load("- [ ] outer\n - first\n - second");+ selectText("second");+ expect(press("Tab", true)).toBe(true);+ expect(itemAncestors()).toEqual(["taskItem", "listItem"]);+ expect(editor.getJSON().content?.[0]?.type).toBe("taskList");+ });++ it("splits an inner bullet without splitting its enclosing checkbox", () => {+ load("- [ ] outer\n - first\n - second");+ selectText("second");+ expect(press("Enter")).toBe(true);+ expect(itemAncestors()).toEqual(["taskItem", "listItem"]);+ expect(editor.getJSON().content?.[0]?.content).toHaveLength(1);+ });++ it("keeps the first nested bullet in place instead of indenting its enclosing checkbox", () => {+ load("- [ ] previous\n- [ ] outer\n - first\n - second");+ selectText("first");+ const before = editor.getJSON();+ expect(press("Tab")).toBe(true);+ expect(editor.getJSON()).toEqual(before);+ });++ it("indents and outdents a checkbox nested under a bullet", () => {+ load("- outer\n - [ ] first\n - [ ] second");+ selectText("second");+ expect(press("Tab")).toBe(true);+ expect(itemAncestors()).toEqual(["listItem", "taskItem", "taskItem"]);+ expect(press("Tab", true)).toBe(true);+ expect(itemAncestors()).toEqual(["listItem", "taskItem"]);+ });++ it("indents a nested whole-list selection, preserves Markdown, and undoes in one step", () => {+ load("- [ ] outer\n - first\n - second\n - third");+ selectText("first");+ const from = editor.state.selection.from;+ selectText("third");+ editor.commands.setTextSelection({ from, to: editor.state.selection.from + 5 });+ const before = editor.getJSON();++ expect(press("Tab")).toBe(true);+ const indented = editor.getJSON();+ const markdown = editor.getMarkdown();+ expect(markdown.trim()).toBe("- [ ] outer\n - first\n - second\n - third");+ expect(editor.commands.undo()).toBe(true);+ expect(editor.getJSON()).toEqual(before);+ editor.commands.setContent(markdown, { contentType: "markdown" });+ expect(editor.getJSON()).toEqual(indented);+ });++ it("leaves Tab available for focus navigation in plain text", () => {+ load("plain");+ selectText("plain");+ expect(press("Tab")).toBe(false);+ expect(press("Tab", true)).toBe(false);+ });++ it("preserves table cell navigation", () => {+ load("| first | second |\n| --- | --- |\n| third | fourth |");+ selectText("first");+ expect(press("Tab")).toBe(true);+ expect(editor.state.selection.$from.parent.textContent).toBe("second");+ expect(press("Tab", true)).toBe(true);+ expect(editor.state.selection.$from.parent.textContent).toBe("first");+ });+});