Files
outline/shared/editor/nodes/CheckboxList.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

108 lines
2.8 KiB
TypeScript

import type {
NodeSpec,
NodeType,
Schema,
Node as ProsemirrorNode,
} from "prosemirror-model";
import { Plugin } from "prosemirror-state";
import { v4 as generateUuid } from "uuid";
import toggleList from "../commands/toggleList";
import type { MarkdownSerializerState } from "../lib/markdown/serializer";
import {
checkboxListInputRule,
listWrappingInputRule,
} from "../lib/listInputRule";
import { findBlockNodes } from "../queries/findChildren";
import { CheckboxListView } from "./CheckboxListView";
import Node from "./Node";
export default class CheckboxList extends Node {
get name() {
return "checkbox_list";
}
get schema(): NodeSpec {
return {
group: "block list",
content: "checkbox_item+",
attrs: {
id: { default: null },
},
toDOM: () => ["ul", { class: this.name }, 0],
parseDOM: [
{
tag: `[class="${this.name}"]`,
},
],
};
}
get plugins() {
const userIdentifier = this.editor.props.userId;
// Plugin to auto-assign IDs to checkbox lists
const assignIdsPlugin = new Plugin({
appendTransaction: (txs, _oldSt, newSt) => {
const hasDocChanges = txs.some((t) => t.docChanged);
if (!hasDocChanges) {
return null;
}
const checkboxLists = findBlockNodes(newSt.doc, true).filter(
(b) => b.node.type.name === this.name && !b.node.attrs.id
);
if (checkboxLists.length === 0) {
return null;
}
let modifyTx = newSt.tr;
checkboxLists.forEach((listBlock) => {
modifyTx.setNodeAttribute(listBlock.pos, "id", generateUuid());
});
return modifyTx;
},
});
// Plugin to provide NodeViews
const nodeViewPlugin = new Plugin({
props: {
nodeViews: {
[this.name]: (node, view, getPos) =>
new CheckboxListView(node, view, getPos, userIdentifier || ""),
},
},
});
return [assignIdsPlugin, nodeViewPlugin];
}
keys({ type, schema }: { type: NodeType; schema: Schema }) {
return {
"Shift-Ctrl-7": toggleList(type, schema.nodes.checkbox_item),
};
}
commands({ type, schema }: { type: NodeType; schema: Schema }) {
return () => toggleList(type, schema.nodes.checkbox_item);
}
inputRules({ type, schema }: { type: NodeType; schema: Schema }) {
const pattern = /^-?\s*(\[\s?\])\s$/i;
return [
// Convert an existing plain list to a checklist, keeping nesting intact.
checkboxListInputRule(pattern, type, schema.nodes.checkbox_item),
// Wrap a plain paragraph into a new checklist.
listWrappingInputRule(pattern, type),
];
}
toMarkdown(state: MarkdownSerializerState, node: ProsemirrorNode) {
state.renderList(node, " ", () => "- ");
}
parseMarkdown() {
return { block: "checkbox_list" };
}
}