|
| 1 | +import { Node as ProseMirrorNode, NodeType } from 'prosemirror-model'; |
| 2 | +import { EditorState } from 'prosemirror-state'; |
| 3 | + |
| 4 | +import { createRtePlugin, RteCommandHandler, RtePlugin } from './rte-plugin'; |
| 5 | + |
| 6 | +export const ClearFormattingPlugin = createRtePlugin({ |
| 7 | + key: 'clearFormatting', |
| 8 | + commands: (schema) => ({ |
| 9 | + clearFormatting: createClearFormattingCommand(schema.nodes['paragraph']), |
| 10 | + }), |
| 11 | +}); |
| 12 | + |
| 13 | +export const ClearFormattingKit: readonly RtePlugin[] = [ |
| 14 | + ClearFormattingPlugin, |
| 15 | +]; |
| 16 | + |
| 17 | +function createClearFormattingCommand( |
| 18 | + paragraph: NodeType, |
| 19 | +): RteCommandHandler { |
| 20 | + return (state, dispatch) => { |
| 21 | + const markRanges = getMarkRangesToClear(state); |
| 22 | + const hasMarksToClear = |
| 23 | + Boolean(state.storedMarks?.length) || |
| 24 | + markRanges.some((range) => |
| 25 | + rangeHasInlineMarks(state.doc, range.from, range.to), |
| 26 | + ); |
| 27 | + const hasTextblocksToClear = selectionHasTextblockFormattingToClear( |
| 28 | + state, |
| 29 | + paragraph, |
| 30 | + ); |
| 31 | + |
| 32 | + if (!hasMarksToClear && !hasTextblocksToClear) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + |
| 36 | + if (dispatch) { |
| 37 | + const transaction = state.tr; |
| 38 | + |
| 39 | + if (state.storedMarks?.length) { |
| 40 | + transaction.setStoredMarks([]); |
| 41 | + } |
| 42 | + |
| 43 | + if (hasMarksToClear) { |
| 44 | + for (const range of markRanges) { |
| 45 | + if (range.from < range.to) { |
| 46 | + transaction.removeMark(range.from, range.to, null); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (hasTextblocksToClear) { |
| 52 | + for (const range of state.selection.ranges) { |
| 53 | + transaction.setBlockType(range.$from.pos, range.$to.pos, paragraph); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + dispatch(transaction.scrollIntoView()); |
| 58 | + } |
| 59 | + |
| 60 | + return true; |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +interface DocumentRange { |
| 65 | + from: number; |
| 66 | + to: number; |
| 67 | +} |
| 68 | + |
| 69 | +function getMarkRangesToClear(state: EditorState): readonly DocumentRange[] { |
| 70 | + if (!state.selection.empty) { |
| 71 | + return state.selection.ranges.map((range) => ({ |
| 72 | + from: range.$from.pos, |
| 73 | + to: range.$to.pos, |
| 74 | + })); |
| 75 | + } |
| 76 | + |
| 77 | + const { $from } = state.selection; |
| 78 | + |
| 79 | + if ($from.depth === 0 || !$from.parent.inlineContent) { |
| 80 | + return []; |
| 81 | + } |
| 82 | + |
| 83 | + return [ |
| 84 | + { |
| 85 | + from: $from.start(), |
| 86 | + to: $from.end(), |
| 87 | + }, |
| 88 | + ]; |
| 89 | +} |
| 90 | + |
| 91 | +function rangeHasInlineMarks( |
| 92 | + doc: ProseMirrorNode, |
| 93 | + from: number, |
| 94 | + to: number, |
| 95 | +): boolean { |
| 96 | + if (from >= to) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + let hasMarks = false; |
| 101 | + |
| 102 | + doc.nodesBetween(from, to, (node) => { |
| 103 | + if (hasMarks) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + if (node.isInline && node.marks.length > 0) { |
| 108 | + hasMarks = true; |
| 109 | + |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + return undefined; |
| 114 | + }); |
| 115 | + |
| 116 | + return hasMarks; |
| 117 | +} |
| 118 | + |
| 119 | +function selectionHasTextblockFormattingToClear( |
| 120 | + state: EditorState, |
| 121 | + paragraph: NodeType, |
| 122 | +): boolean { |
| 123 | + let applicable = false; |
| 124 | + |
| 125 | + for (const range of state.selection.ranges) { |
| 126 | + if (applicable) { |
| 127 | + break; |
| 128 | + } |
| 129 | + |
| 130 | + state.doc.nodesBetween(range.$from.pos, range.$to.pos, (node, pos) => { |
| 131 | + if (applicable) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + if (!node.isTextblock || node.hasMarkup(paragraph)) { |
| 136 | + return undefined; |
| 137 | + } |
| 138 | + |
| 139 | + if (node.type === paragraph) { |
| 140 | + applicable = true; |
| 141 | + |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + const $pos = state.doc.resolve(pos); |
| 146 | + const index = $pos.index(); |
| 147 | + |
| 148 | + if ($pos.parent.canReplaceWith(index, index + 1, paragraph)) { |
| 149 | + applicable = true; |
| 150 | + |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + return undefined; |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + return applicable; |
| 159 | +} |
0 commit comments