@magic-marker/prosemirror-suggest-changes 0.3.3-wrap-unwrap.29 → 0.3.3-wrap-unwrap.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/commands.js
CHANGED
|
@@ -5,6 +5,7 @@ import { suggestChangesKey } from "./plugin.js";
|
|
|
5
5
|
import { getSuggestionMarks } from "./utils.js";
|
|
6
6
|
import { ZWSP } from "./constants.js";
|
|
7
7
|
import { maybeRevertJoinMark } from "./features/joinOnDelete/index.js";
|
|
8
|
+
import { isJoinMark } from "./features/joinOnDelete/types.js";
|
|
8
9
|
import { revertAllStructureSuggestions, revertStructureSuggestion } from "./features/wrapUnwrap/revert/index.js";
|
|
9
10
|
import { applyAllStructureSuggestions, applyStructureSuggestion } from "./features/wrapUnwrap/apply/index.js";
|
|
10
11
|
/**
|
|
@@ -65,7 +66,9 @@ import { applyAllStructureSuggestions, applyStructureSuggestion } from "./featur
|
|
|
65
66
|
tr.removeMark(insertionFrom, insertionTo, markTypeToApply);
|
|
66
67
|
const joinRevertResult = maybeRevertJoinMark(tr, insertionFrom, insertionTo, child, markTypeToApply);
|
|
67
68
|
// reverting a join mark may produce new structure marks that were serialized in the join metadata
|
|
68
|
-
if (joinRevertResult)
|
|
69
|
+
if (joinRevertResult) {
|
|
70
|
+
joinRevertResult.restoredStructureSuggestionIds.forEach((id)=>restoredStructureSuggestionIds.add(id));
|
|
71
|
+
}
|
|
69
72
|
if (!joinRevertResult && child.text === ZWSP) {
|
|
70
73
|
tr.delete(insertionFrom, insertionTo);
|
|
71
74
|
}
|
|
@@ -78,6 +81,47 @@ import { applyAllStructureSuggestions, applyStructureSuggestion } from "./featur
|
|
|
78
81
|
restoredStructureSuggestionIds
|
|
79
82
|
};
|
|
80
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Collect suggestion IDs of join marks in the node
|
|
86
|
+
*
|
|
87
|
+
* @param node
|
|
88
|
+
* @param deletion
|
|
89
|
+
* @returns an array of suggestion IDs
|
|
90
|
+
*/ function findJoinSuggestionIds(node, deletion) {
|
|
91
|
+
const joinSuggestionIds = [];
|
|
92
|
+
node.descendants((child)=>{
|
|
93
|
+
const mark = deletion.isInSet(child.marks);
|
|
94
|
+
if (!mark || !isJoinMark(mark)) return true;
|
|
95
|
+
if (!child.isText || child.text !== ZWSP) return true;
|
|
96
|
+
joinSuggestionIds.push(mark.attrs.id);
|
|
97
|
+
return true;
|
|
98
|
+
});
|
|
99
|
+
return joinSuggestionIds.reverse();
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Revert suggestions revealed by reverting a join mark
|
|
103
|
+
* Prioritize reverting revealed suggestions with the same id as the join mark
|
|
104
|
+
* (that means they are linked to the join mark and has to be reverted together as one)
|
|
105
|
+
* Revert other revealed suggestions as well so the user doesn't have to revert multiple times at the same place
|
|
106
|
+
*
|
|
107
|
+
* @param tr
|
|
108
|
+
* @param suggestionId
|
|
109
|
+
* @param restoredStructureSuggestionIds
|
|
110
|
+
*/ function revertRestoredStructureSuggestions(tr, suggestionId, restoredStructureSuggestionIds) {
|
|
111
|
+
if (restoredStructureSuggestionIds.has(suggestionId)) {
|
|
112
|
+
const restoredStructureTransform = revertStructureSuggestion(tr.doc, suggestionId);
|
|
113
|
+
restoredStructureTransform.steps.forEach((step)=>{
|
|
114
|
+
tr.step(step);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
restoredStructureSuggestionIds.forEach((restoredSuggestionId)=>{
|
|
118
|
+
if (restoredSuggestionId === suggestionId) return;
|
|
119
|
+
const restoredStructureTransform = revertStructureSuggestion(tr.doc, restoredSuggestionId);
|
|
120
|
+
restoredStructureTransform.steps.forEach((step)=>{
|
|
121
|
+
tr.step(step);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
81
125
|
function revertModifications(node, pos, tr) {
|
|
82
126
|
const { modification } = getSuggestionMarks(node.type.schema);
|
|
83
127
|
const existingMods = node.marks.filter((mark)=>mark.type === modification);
|
|
@@ -291,7 +335,17 @@ export function applySuggestionsToRange(doc, from, to) {
|
|
|
291
335
|
const { deletion, insertion } = getSuggestionMarks(state.schema);
|
|
292
336
|
// create a structure transform that reverts all structure changes on the given document
|
|
293
337
|
const structureTransform = revertAllStructureSuggestions(state.doc);
|
|
294
|
-
//
|
|
338
|
+
// revert all join marks first as well as any structure marks they contain serialized
|
|
339
|
+
const joinSuggestionIds = findJoinSuggestionIds(structureTransform.doc, deletion);
|
|
340
|
+
joinSuggestionIds.forEach((suggestionId)=>{
|
|
341
|
+
const suggestionsTransform = new Transform(structureTransform.doc);
|
|
342
|
+
const { restoredStructureSuggestionIds } = applySuggestionsToTransform(suggestionsTransform.doc, suggestionsTransform, deletion, insertion, suggestionId);
|
|
343
|
+
suggestionsTransform.steps.forEach((step)=>{
|
|
344
|
+
structureTransform.step(step);
|
|
345
|
+
});
|
|
346
|
+
revertRestoredStructureSuggestions(structureTransform, suggestionId, restoredStructureSuggestionIds);
|
|
347
|
+
});
|
|
348
|
+
// then start a clear transform from the document where the structure changes and join marks are reverted
|
|
295
349
|
const suggestionsTransform = new Transform(structureTransform.doc);
|
|
296
350
|
applySuggestionsToTransform(suggestionsTransform.doc, suggestionsTransform, deletion, insertion);
|
|
297
351
|
applyModificationsToTransform(suggestionsTransform.doc, suggestionsTransform, -1);
|
|
@@ -370,19 +424,9 @@ export function applySuggestionsToRange(doc, from, to) {
|
|
|
370
424
|
suggestionsTransform.steps.forEach((step)=>{
|
|
371
425
|
structureTransform.step(step);
|
|
372
426
|
});
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
structureTransform.step(step);
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
restoredStructureSuggestionIds.forEach((restoredSuggestionId)=>{
|
|
380
|
-
if (restoredSuggestionId === suggestionId) return;
|
|
381
|
-
const restoredStructureTransform = revertStructureSuggestion(structureTransform.doc, restoredSuggestionId);
|
|
382
|
-
restoredStructureTransform.steps.forEach((step)=>{
|
|
383
|
-
structureTransform.step(step);
|
|
384
|
-
});
|
|
385
|
-
});
|
|
427
|
+
// in case there are structure marks revealed after join mark revert,
|
|
428
|
+
// revert them as well
|
|
429
|
+
revertRestoredStructureSuggestions(structureTransform, suggestionId, restoredStructureSuggestionIds);
|
|
386
430
|
// apply the structure transform to the transaction
|
|
387
431
|
const transaction = state.tr;
|
|
388
432
|
structureTransform.steps.forEach((step)=>{
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Mark, type Attrs, type Node } from "prosemirror-model";
|
|
2
|
+
import { type SuggestionId } from "../../generateId.js";
|
|
2
3
|
export interface SerializedJoinNode {
|
|
3
4
|
type: string;
|
|
4
5
|
attrs: object;
|
|
@@ -7,6 +8,7 @@ export interface SerializedJoinNode {
|
|
|
7
8
|
}[];
|
|
8
9
|
}
|
|
9
10
|
export interface JoinMarkAttrs {
|
|
11
|
+
id: SuggestionId;
|
|
10
12
|
type: "join";
|
|
11
13
|
data: {
|
|
12
14
|
leftNode?: SerializedJoinNode;
|
|
@@ -5,6 +5,9 @@ export function isSerializedJoinNode(node) {
|
|
|
5
5
|
return typeof data.type === "string" && typeof data.attrs === "object" && data.attrs !== null && Array.isArray(data.marks);
|
|
6
6
|
}
|
|
7
7
|
export function isJoinMarkAttrs(attrs) {
|
|
8
|
+
if (typeof attrs["id"] !== "string" && typeof attrs["id"] !== "number") {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
8
11
|
if (attrs["type"] !== "join") return false;
|
|
9
12
|
if (attrs["data"] == null) return false;
|
|
10
13
|
return true;
|