Files
outline/shared/editor/lib/listInputRule.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

79 lines
2.8 KiB
TypeScript

import { wrappingInputRule, InputRule } from "prosemirror-inputrules";
import type {
NodeType,
Node as ProsemirrorNode,
Attrs,
} from "prosemirror-model";
import { TextSelection } from "prosemirror-state";
import toggleList from "../commands/toggleList";
import { findParentNodeClosestToPos } from "../queries/findParentNode";
import { isInHeading } from "../queries/isInHeading";
import { isList } from "../queries/isList";
/**
* A wrapper for wrappingInputRule that prevents execution inside heading nodes.
* This fixes the bug where typing list triggers ("* ", "- ", "1. ", etc.) inside
* a heading would trigger list conversion.
*/
export function listWrappingInputRule(
regexp: RegExp,
nodeType: NodeType,
getAttrs?: (match: RegExpMatchArray) => Attrs | null,
joinPredicate?: (match: RegExpMatchArray, node: ProsemirrorNode) => boolean
): InputRule {
const rule = wrappingInputRule(regexp, nodeType, getAttrs, joinPredicate);
// Wrap the original rule to check if we're inside a heading
return new InputRule(regexp, (state, match, start, end) => {
// Don't apply the rule if we're inside a heading
if (isInHeading(state)) {
return null;
}
// Otherwise, execute the original wrappingInputRule handler
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return (rule as any).handler(state, match, start, end);
});
}
/**
* An input rule that converts an existing plain list (bullet or ordered) to a
* checklist when the checkbox marker is typed at the start of a list item,
* preserving any nested list structure.
*
* @param regexp the pattern matching the checkbox marker, e.g. "[ ] ".
* @param listType the checkbox list node type to convert to.
* @param itemType the checkbox item node type to convert items to.
* @returns the input rule.
*/
export function checkboxListInputRule(
regexp: RegExp,
listType: NodeType,
itemType: NodeType
): InputRule {
return new InputRule(regexp, (state, _match, start, end) => {
const { schema } = state;
// Only act when the selection sits inside a plain (non-checkbox) list —
// converting a paragraph is already handled by listWrappingInputRule.
const list = findParentNodeClosestToPos(state.selection.$from, (node) =>
isList(node, schema)
);
if (!list || list.node.type === listType) {
return null;
}
// Remove the typed marker, then convert the list in place.
const tr = state.tr.delete(start, end);
const after = state.apply(tr);
toggleList(listType, itemType)(after, (toggleTr) => {
toggleTr.steps.forEach((step) => tr.step(step));
});
// Place the cursor at the start of the converted item, where the marker
// was, so the user can continue typing.
tr.setSelection(TextSelection.near(tr.doc.resolve(start)));
return tr;
});
}