|
| 1 | +import { NodeType, Node } from "prosemirror-model"; |
| 2 | +import { Command, EditorState, Transaction } from "prosemirror-state"; |
| 3 | +import { canJoin } from "prosemirror-transform"; |
| 4 | +import { findParentNode } from "prosemirror-utils"; |
| 5 | +import { wrapInList, liftListItem } from "prosemirror-schema-list"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Toggles a list. |
| 9 | + * When the provided list type wrapper (e.g. bullet_list) is inactive then wrap the list with |
| 10 | + * this type. When it is active then remove the selected line from the list. |
| 11 | + * |
| 12 | + * @param listType - the list node type |
| 13 | + * @param itemType - the list item node type |
| 14 | + */ |
| 15 | +export function toggleList(listType: NodeType, itemType: NodeType): Command { |
| 16 | + return (state: EditorState, dispatch?: (tr: Transaction) => void) => { |
| 17 | + const { $from, $to } = state.tr.selection; |
| 18 | + const range = $from.blockRange($to); |
| 19 | + |
| 20 | + if (!range) { |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + const parentList = findParentNode((node) => isListType(node.type))( |
| 25 | + state.tr.selection |
| 26 | + ); |
| 27 | + |
| 28 | + if (parentList) { |
| 29 | + return liftListItem(itemType)(state, dispatch); |
| 30 | + } |
| 31 | + |
| 32 | + return wrapAndMaybeJoinList(listType)(state, dispatch); |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Wraps the selected content in a list and attempts to join the newly wrapped list |
| 38 | + * with exisiting list(s) of the same type. |
| 39 | + * |
| 40 | + * @param nodeType - the list node type |
| 41 | + */ |
| 42 | +export function wrapAndMaybeJoinList(nodeType: NodeType) { |
| 43 | + return function (state: EditorState, dispatch: (tr: Transaction) => void) { |
| 44 | + return wrapInList(nodeType)(state, (tr) => { |
| 45 | + dispatch?.(tr); |
| 46 | + const { tr: newTr } = state.apply(tr); |
| 47 | + maybeJoinList(newTr); |
| 48 | + dispatch?.(newTr); |
| 49 | + }); |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Joins lists when they are of the same type. |
| 55 | + * Inspired by https://github.com/remirror/remirror/blob/main/packages/remirror__extension-list/src/list-commands.ts#L535 |
| 56 | + * |
| 57 | + * @param tr - the transaction |
| 58 | + */ |
| 59 | +export function maybeJoinList(tr: Transaction): boolean { |
| 60 | + const $from = tr.selection.$from; |
| 61 | + |
| 62 | + let joinable: number[] = []; |
| 63 | + let index: number; |
| 64 | + let parent: Node; |
| 65 | + let before: Node | null | undefined; |
| 66 | + let after: Node | null | undefined; |
| 67 | + |
| 68 | + for (let depth = $from.depth; depth >= 0; depth--) { |
| 69 | + parent = $from.node(depth); |
| 70 | + |
| 71 | + // join backward |
| 72 | + index = $from.index(depth); |
| 73 | + before = parent.maybeChild(index - 1); |
| 74 | + after = parent.maybeChild(index); |
| 75 | + |
| 76 | + if ( |
| 77 | + before && |
| 78 | + after && |
| 79 | + before.type.name === after.type.name && |
| 80 | + isListType(before.type) |
| 81 | + ) { |
| 82 | + const pos = $from.before(depth + 1); |
| 83 | + joinable.push(pos); |
| 84 | + } |
| 85 | + |
| 86 | + // join forward |
| 87 | + index = $from.indexAfter(depth); |
| 88 | + before = parent.maybeChild(index - 1); |
| 89 | + after = parent.maybeChild(index); |
| 90 | + |
| 91 | + if ( |
| 92 | + before && |
| 93 | + after && |
| 94 | + before.type.name === after.type.name && |
| 95 | + isListType(before.type) |
| 96 | + ) { |
| 97 | + const pos = $from.after(depth + 1); |
| 98 | + joinable.push(pos); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // sort `joinable` reversely |
| 103 | + joinable = [...new Set(joinable)].sort((a, b) => b - a); |
| 104 | + let updated = false; |
| 105 | + |
| 106 | + for (const pos of joinable) { |
| 107 | + if (canJoin(tr.doc, pos)) { |
| 108 | + tr.join(pos); |
| 109 | + updated = true; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return updated; |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Checks if the node type is a list type (e.g. "bullet_list", "ordered_list", etc...). |
| 118 | + * |
| 119 | + * @param type - the node type |
| 120 | + */ |
| 121 | +export function isListType(type: NodeType) { |
| 122 | + return !!type.name.includes("_list"); |
| 123 | +} |
0 commit comments