Files
outline/shared/editor/commands/toggleList.ts
SalihuandTom Moor f57a189077 feat: Ordered alphabetical lists (#10079)
* feat: letter-list

* simplify list toggle

* use more common shortcuts for list toggle

* fix toggle list conflict

* wrap letter index to avoid overflow

* ensure the markdown letter representation matches the css representation on overflow

* improve list style validation

* fix list indexing

* fix: Toggling ordered lists from formatting menu

* fix: Ordered list in block menu

---------

Co-authored-by: Tom Moor <tom@getoutline.com>
2025-09-13 13:03:19 +00:00

77 lines
2.1 KiB
TypeScript

import { NodeType } from "prosemirror-model";
import { liftListItem, wrapInList } from "prosemirror-schema-list";
import { 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 || differentListStyle) {
return chainTransactions(
clearNodes(),
wrapInList(listType, { listStyle })
)(state, dispatch);
}
if (
isList(parentList.node, schema) &&
listType.validContent(parentList.node.content)
) {
tr.setNodeMarkup(
parentList.pos,
listType,
listStyle ? { listStyle } : {}
);
dispatch?.(tr);
return false;
}
}
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
);
};
}