Files
outline/shared/editor/commands/toggleList.ts
T
Tom Moor ce845b89e9 fix: Correct logic errors in shared utilities (#14904)
* fix: Correct logic errors in shared utilities

* fix: Restore per-column cells in getCellsInRow and convert every attachment url

The g flag made the attachment regex skip every second match, and removing
duplicate cells broke callers that index getCellsInRow by column.

* test: Cover uppercase attachment URLs

* revert: Remove uncertain fixes
2026-12-05 12:48:21 -05:00

158 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type {
Attrs,
Node as ProsemirrorNode,
NodeType,
Schema,
} from "prosemirror-model";
import { liftListItem, wrapInList } from "prosemirror-schema-list";
import type { Command } from "prosemirror-state";
import { chainTransactions } from "../lib/chainTransactions";
import { findParentNode } from "../queries/findParentNode";
import { isList } from "../queries/isList";
import clearNodes from "./clearNodes";
export default function toggleList(
listType: NodeType,
itemType: NodeType,
listStyle?: string
): Command {
return (state, dispatch) => {
const { schema, selection } = state;
const { $from, $to } = selection;
const range = $from.blockRange($to);
const { tr } = state;
if (!range) {
return false;
}
const parentList = findParentNode((node) => isList(node, schema))(
selection
);
if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) {
const currentStyle = parentList.node.attrs.listStyle;
const differentListStyle = currentStyle && currentStyle !== listStyle;
if (
parentList.node.type === listType &&
(!differentListStyle || !listStyle)
) {
return liftListItem(itemType)(state, dispatch);
}
const currentItemType = parentList.node.content.firstChild?.type;
const differentType = currentItemType && currentItemType !== itemType;
if (differentType) {
// Convert the list in place, preserving any nested list structure,
// rather than clearing the nodes and re-wrapping which would flatten
// nesting – this path is hit when toggling to or from a checklist.
try {
const converted = convertListType(
parentList.node,
listType,
itemType,
schema,
listStyle ? { listStyle } : undefined
);
dispatch?.(
tr.replaceWith(
parentList.pos,
parentList.pos + parentList.node.nodeSize,
converted
)
);
return true;
} catch (_err) {
return chainTransactions(
clearNodes(),
wrapInList(listType, { listStyle })
)(state, dispatch);
}
}
if (
isList(parentList.node, schema) &&
listType.validContent(parentList.node.content)
) {
tr.doc.nodesBetween(
parentList.pos,
parentList.pos + parentList.node.nodeSize,
(node, pos) => {
// nodesBetween also visits the ancestors of the given range, these
// must be skipped so that toggling a nested list does not convert
// the lists it is nested within.
if (
pos >= parentList.pos &&
isList(node, schema) &&
listType.validContent(node.content)
) {
tr.setNodeMarkup(pos, listType, listStyle ? { listStyle } : {});
}
}
);
dispatch?.(tr);
return true;
}
}
const attrs = listStyle ? { listStyle } : undefined;
const canWrapInList = wrapInList(listType, attrs)(state);
if (canWrapInList) {
return wrapInList(listType, attrs)(state, dispatch);
}
return chainTransactions(clearNodes(), wrapInList(listType, attrs))(
state,
dispatch
);
};
}
/**
* Recursively converts a list node, its items, and any lists nested within
* those items to the given list and item type, preserving nesting structure.
*
* @param node the list node to convert.
* @param listType the node type to convert lists to.
* @param itemType the node type to convert list items to.
* @param schema the document schema.
* @param attrs optional attributes for the converted lists.
* @returns the converted list node.
* @throws if the content of an item is not valid for the new item type.
*/
function convertListType(
node: ProsemirrorNode,
listType: NodeType,
itemType: NodeType,
schema: Schema,
attrs?: Attrs
): ProsemirrorNode {
const items: ProsemirrorNode[] = [];
node.forEach((item) => {
const content: ProsemirrorNode[] = [];
item.forEach((child) => {
content.push(
isList(child, schema)
? convertListType(child, listType, itemType, schema, attrs)
: child
);
});
items.push(
itemType.createChecked(
item.type === itemType ? item.attrs : null,
content,
item.marks
)
);
});
return listType.createChecked(
node.type === listType ? node.attrs : (attrs ?? null),
items,
node.marks
);
}