Files
outline/shared/editor/lib/listInputRule.test.ts
T
Tom MoorandClaude 93c928439d fix: Nested list behavior (#12678)
* fix: preserve nesting when toggling to/from checklist in nested lists

Converting a list to or from a checklist previously cleared all nodes and
re-wrapped them, which flattened any nested list structure. Convert the
list nodes in place instead, recursively mapping list and item types so
nesting is preserved.

https://claude.ai/code/session_01BGH191WyuL9SgQyjfJ3YsD

* feat: convert plain list to checklist when typing checkbox marker

Typing "[ ] " at the start of an existing bullet or ordered list item now
converts the whole list to a checklist, preserving nested list structure,
reusing the in-place list conversion utility.

https://claude.ai/code/session_01BGH191WyuL9SgQyjfJ3YsD

* fix: place cursor at start of item after checklist conversion

After converting a list to a checklist via the checkbox marker input rule,
position the selection at the start of the converted item so the user can
continue typing immediately.

https://claude.ai/code/session_01BGH191WyuL9SgQyjfJ3YsD

* fix: remove vertical margin from checklist nested in a list

A checklist nested inside any list (bullet, ordered, or another checklist)
no longer carries the 1em vertical margin of a top-level checklist. Since
both list_item and checkbox_item render as li elements, target any
checklist-wrapper inside an li rather than only those inside another wrapper.

https://claude.ai/code/session_01BGH191WyuL9SgQyjfJ3YsD

* fix: only highlight the closest list in the formatting toolbar

When the selection sits inside a nested list, the toolbar previously marked
both the inner list and any ancestor list of a different type as active,
because each list button walked the full ancestor chain. Highlight only the
list closest to the selection.

https://claude.ai/code/session_01BGH191WyuL9SgQyjfJ3YsD

* fix: consider node selections on lists in isListActive

When a list node is the target of a NodeSelection, findParentNode resolves
to its parent list rather than the selected node. Check the selected node
directly so the toolbar reflects the selected list.

https://claude.ai/code/session_01BGH191WyuL9SgQyjfJ3YsD

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-06-13 12:33:17 -04:00

168 lines
5.0 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { InputRule } from "prosemirror-inputrules";
import type { Node } from "prosemirror-model";
import {
createEditorStateWithSelection,
doc,
p,
schema,
} from "@shared/test/editor";
import { checkboxListInputRule } from "./listInputRule";
const { bullet_list, ordered_list, list_item, checkbox_list, checkbox_item } =
schema.nodes;
/**
* Creates a list item node with the given block content.
*/
function li(content: Node[]) {
return list_item.create(null, content);
}
/**
* Returns the position directly after the first occurrence of the given text.
*
* @throws if no matching text node exists in the document.
*/
function posAfterText(node: Node, text: string) {
let found = -1;
node.descendants((child, pos) => {
if (found === -1 && child.isText && child.text?.startsWith(text)) {
found = pos;
}
return found === -1;
});
if (found === -1) {
throw new Error(`Text "${text}" not found in document`);
}
return found + text.length;
}
/**
* Simulates typing the trigger character of an input rule, mirroring the way
* prosemirror-inputrules invokes a rule's handler, and returns the resulting
* editor state (unchanged when the rule does not fire).
*/
function typeTrigger(
rule: InputRule,
testDoc: Node,
markerInDoc: string,
triggerChar: string
) {
let state = createEditorStateWithSelection(
testDoc,
posAfterText(testDoc, markerInDoc)
);
const { from, to } = state.selection;
const $from = state.doc.resolve(from);
const textBefore =
$from.parent.textBetween(
Math.max(0, $from.parentOffset - 500),
$from.parentOffset,
null,
""
) + triggerChar;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const match = (rule as any).match.exec(textBefore) as RegExpMatchArray | null;
if (!match) {
return state;
}
const startPos = from - (match[0].length - triggerChar.length);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tr = (rule as any).handler(state, match, startPos, to);
if (tr) {
state = state.apply(tr);
}
return state;
}
const rule = checkboxListInputRule(
/^-?\s*(\[\s?\])\s$/i,
checkbox_list,
checkbox_item
);
describe("checkboxListInputRule", () => {
it("converts a plain bullet list to a checklist", () => {
const testDoc = doc([
bullet_list.create(null, [li([p("[ ]")]), li([p("two")])]),
]);
const result = typeTrigger(rule, testDoc, "[ ]", " ");
const list = result.doc.firstChild;
expect(list?.type.name).toBe("checkbox_list");
expect(list?.childCount).toBe(2);
expect(list?.child(0).type.name).toBe("checkbox_item");
expect(list?.child(0).textContent).toBe("");
expect(list?.child(1).textContent).toBe("two");
});
it("places the cursor at the start of the converted item", () => {
const testDoc = doc([
bullet_list.create(null, [li([p("[ ]")]), li([p("two")])]),
]);
const result = typeTrigger(rule, testDoc, "[ ]", " ");
// The selection should sit empty at the start of the first item's content.
const { selection } = result;
expect(selection.empty).toBe(true);
const $from = result.doc.resolve(selection.from);
expect($from.parent.type.name).toBe("paragraph");
expect($from.parentOffset).toBe(0);
expect($from.node(-1).type.name).toBe("checkbox_item");
});
it("converts a plain ordered list to a checklist", () => {
const testDoc = doc([
ordered_list.create(null, [li([p("[ ]")]), li([p("two")])]),
]);
const result = typeTrigger(rule, testDoc, "[ ]", " ");
expect(result.doc.firstChild?.type.name).toBe("checkbox_list");
});
it("preserves nesting when converting a list with a nested list", () => {
const testDoc = doc([
bullet_list.create(null, [
li([p("[ ]")]),
li([p("two"), bullet_list.create(null, [li([p("nested")])])]),
]),
]);
const result = typeTrigger(rule, testDoc, "[ ]", " ");
const list = result.doc.firstChild;
expect(list?.type.name).toBe("checkbox_list");
const nested = list?.child(1).child(1);
expect(nested?.type.name).toBe("checkbox_list");
expect(nested?.child(0).type.name).toBe("checkbox_item");
expect(nested?.child(0).textContent).toBe("nested");
});
it("does nothing when already in a checklist", () => {
const testDoc = doc([
checkbox_list.create(null, [
checkbox_item.create(null, p("[ ]")),
checkbox_item.create(null, p("two")),
]),
]);
const result = typeTrigger(rule, testDoc, "[ ]", " ");
// The rule should not fire; the marker text is left untouched.
expect(result.doc.firstChild?.type.name).toBe("checkbox_list");
expect(result.doc.firstChild?.child(0).textContent).toBe("[ ]");
});
it("does nothing when not inside a list", () => {
const testDoc = doc([p("[ ]")]);
const result = typeTrigger(rule, testDoc, "[ ]", " ");
expect(result.doc.firstChild?.type.name).toBe("paragraph");
expect(result.doc.firstChild?.textContent).toBe("[ ]");
});
});