chore: Move editor into codebase (#2930)
This commit is contained in:
43
shared/editor/commands/toggleList.ts
Normal file
43
shared/editor/commands/toggleList.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NodeType } from "prosemirror-model";
|
||||
import { wrapInList, liftListItem } from "prosemirror-schema-list";
|
||||
import { EditorState, Transaction } from "prosemirror-state";
|
||||
import { findParentNode } from "prosemirror-utils";
|
||||
import isList from "../queries/isList";
|
||||
|
||||
export default function toggleList(listType: NodeType, itemType: NodeType) {
|
||||
return (state: EditorState, dispatch: (tr: Transaction) => void) => {
|
||||
const { schema, selection } = state;
|
||||
const { $from, $to } = selection;
|
||||
const range = $from.blockRange($to);
|
||||
|
||||
if (!range) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const parentList = findParentNode((node) => isList(node, schema))(
|
||||
selection
|
||||
);
|
||||
|
||||
if (range.depth >= 1 && parentList && range.depth - parentList.depth <= 1) {
|
||||
if (parentList.node.type === listType) {
|
||||
return liftListItem(itemType)(state, dispatch);
|
||||
}
|
||||
|
||||
if (
|
||||
isList(parentList.node, schema) &&
|
||||
listType.validContent(parentList.node.content)
|
||||
) {
|
||||
const { tr } = state;
|
||||
tr.setNodeMarkup(parentList.pos, listType);
|
||||
|
||||
if (dispatch) {
|
||||
dispatch(tr);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return wrapInList(listType)(state, dispatch);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user