-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoveNodeMarkStep.ts
More file actions
55 lines (52 loc) · 1.76 KB
/
Copy pathremoveNodeMarkStep.ts
File metadata and controls
55 lines (52 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { type Node } from "prosemirror-model";
import { type EditorState, type Transaction } from "prosemirror-state";
import { type RemoveNodeMarkStep, type Step } from "prosemirror-transform";
import { rebasePos } from "./rebasePos.js";
import { getSuggestionMarks } from "./utils.js";
/**
* Transform a remove node mark step into its equivalent tracked steps.
*
* Remove node mark steps are processed normally, and then a modification
* mark is added to the node as well, to track the change.
*/
export function suggestRemoveNodeMarkStep(
trackedTransaction: Transaction,
state: EditorState,
_doc: Node,
step: RemoveNodeMarkStep,
prevSteps: Step[],
suggestionId: number,
) {
const { modification } = getSuggestionMarks(state.schema);
const rebasedPos = rebasePos(step.pos, prevSteps, trackedTransaction.steps);
const $pos = trackedTransaction.doc.resolve(rebasedPos);
const node = $pos.nodeAfter;
let marks = node?.marks ?? [];
const existingMod = marks.find(
(mark) =>
mark.type === modification &&
mark.attrs["type"] === "mark" &&
mark.attrs["newValue"] &&
step.mark.eq(state.schema.markFromJSON(mark.attrs["newValue"])),
);
if (existingMod) {
trackedTransaction.removeNodeMark(rebasedPos, existingMod);
trackedTransaction.removeNodeMark(
rebasedPos,
state.schema.markFromJSON(existingMod.attrs["newValue"]),
);
return false;
}
marks = step.mark.removeFromSet(marks);
marks = modification
.create({
id: suggestionId,
type: "mark",
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
previousValue: step.mark.toJSON(),
newValue: null,
})
.addToSet(marks);
trackedTransaction.setNodeMarkup(rebasedPos, null, null, marks);
return true;
}