|
| 1 | +import { RemoveMarkStep } from "@mxm-editor/pm"; |
| 2 | +import { Extension } from "../Extension"; |
| 3 | +import { |
| 4 | + combineTransactionSteps, |
| 5 | + getChangedRanges, |
| 6 | +} from "../helpers"; |
| 7 | + |
| 8 | +export const Delete = Extension.create({ |
| 9 | + name: "delete", |
| 10 | + |
| 11 | + onUpdate({ transaction, appendedTransactions }) { |
| 12 | + const callback = () => { |
| 13 | + if ( |
| 14 | + this.editor.options.coreExtensionOptions?.delete?.filterTransaction?.(transaction) |
| 15 | + ?? transaction.getMeta("y-sync$") |
| 16 | + ) { |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + const combinedTransform = combineTransactionSteps( |
| 21 | + transaction.before, |
| 22 | + [transaction, ...appendedTransactions], |
| 23 | + ); |
| 24 | + const changes = getChangedRanges(combinedTransform); |
| 25 | + |
| 26 | + changes.forEach((change) => { |
| 27 | + if ( |
| 28 | + combinedTransform.mapping.mapResult(change.oldRange.from).deletedAfter |
| 29 | + && combinedTransform.mapping.mapResult(change.oldRange.to).deletedBefore |
| 30 | + ) { |
| 31 | + combinedTransform.before.nodesBetween(change.oldRange.from, change.oldRange.to, (node, from) => { |
| 32 | + const to = from + node.nodeSize - 2; |
| 33 | + const isFullyWithinRange = |
| 34 | + change.oldRange.from <= from |
| 35 | + && to <= change.oldRange.to; |
| 36 | + const payload = { |
| 37 | + type: "node" as const, |
| 38 | + node, |
| 39 | + from, |
| 40 | + to, |
| 41 | + newFrom: combinedTransform.mapping.map(from), |
| 42 | + newTo: combinedTransform.mapping.map(to), |
| 43 | + deletedRange: change.oldRange, |
| 44 | + newRange: change.newRange, |
| 45 | + partial: !isFullyWithinRange, |
| 46 | + editor: this.editor, |
| 47 | + transaction, |
| 48 | + combinedTransform, |
| 49 | + }; |
| 50 | + |
| 51 | + this.editor.emit("delete", payload); |
| 52 | + this.editor.options.onDelete(payload); |
| 53 | + }); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + const { mapping } = combinedTransform; |
| 58 | + |
| 59 | + combinedTransform.steps.forEach((step, index) => { |
| 60 | + if (!(step instanceof RemoveMarkStep)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const newStart = mapping.slice(index).map(step.from, -1); |
| 65 | + const newEnd = mapping.slice(index).map(step.to); |
| 66 | + const oldStart = mapping.invert().map(newStart, -1); |
| 67 | + const oldEnd = mapping.invert().map(newEnd); |
| 68 | + const foundBeforeMark = combinedTransform.doc |
| 69 | + .nodeAt(newStart - 1) |
| 70 | + ?.marks.some((mark) => mark.eq(step.mark)); |
| 71 | + const foundAfterMark = combinedTransform.doc |
| 72 | + .nodeAt(newEnd) |
| 73 | + ?.marks.some((mark) => mark.eq(step.mark)); |
| 74 | + const payload = { |
| 75 | + type: "mark" as const, |
| 76 | + mark: step.mark, |
| 77 | + from: step.from, |
| 78 | + to: step.to, |
| 79 | + deletedRange: { |
| 80 | + from: oldStart, |
| 81 | + to: oldEnd, |
| 82 | + }, |
| 83 | + newRange: { |
| 84 | + from: newStart, |
| 85 | + to: newEnd, |
| 86 | + }, |
| 87 | + partial: Boolean(foundAfterMark || foundBeforeMark), |
| 88 | + editor: this.editor, |
| 89 | + transaction, |
| 90 | + combinedTransform, |
| 91 | + }; |
| 92 | + |
| 93 | + this.editor.emit("delete", payload); |
| 94 | + this.editor.options.onDelete(payload); |
| 95 | + }); |
| 96 | + }; |
| 97 | + |
| 98 | + if (this.editor.options.coreExtensionOptions?.delete?.async ?? true) { |
| 99 | + setTimeout(callback, 0); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + callback(); |
| 104 | + }, |
| 105 | +}); |
0 commit comments