@magic-marker/prosemirror-suggest-changes 0.3.3-wrap-unwrap.20 → 0.3.3-wrap-unwrap.22

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.
@@ -22,6 +22,7 @@ export declare class EditorPage {
22
22
  currentDoc: Node;
23
23
  expectedDoc: Node;
24
24
  }>;
25
+ revertSuggestion(suggestionId: number): Promise<void>;
25
26
  revertAll(): Promise<void>;
26
27
  applyAll(): Promise<void>;
27
28
  /**
package/dist/commands.js CHANGED
@@ -31,6 +31,7 @@ import { applyAllStructureSuggestions, applyStructureSuggestion } from "./featur
31
31
  tr.removeNodeMark(0, markTypeToApply);
32
32
  }
33
33
  }
34
+ const restoredStructureSuggestionIds = new Set();
34
35
  node.descendants((child, pos)=>{
35
36
  if (from !== undefined && pos < from) {
36
37
  return true;
@@ -62,8 +63,10 @@ import { applyAllStructureSuggestions, applyStructureSuggestion } from "./featur
62
63
  const insertionTo = insertionFrom + child.nodeSize;
63
64
  if (child.isInline) {
64
65
  tr.removeMark(insertionFrom, insertionTo, markTypeToApply);
65
- const reverted = maybeRevertJoinMark(tr, insertionFrom, insertionTo, child, markTypeToApply);
66
- if (!reverted && child.text === ZWSP) {
66
+ const joinRevertResult = maybeRevertJoinMark(tr, insertionFrom, insertionTo, child, markTypeToApply);
67
+ // reverting a join mark may produce new structure marks that were serialized in the join metadata
68
+ if (joinRevertResult) joinRevertResult.restoredStructureSuggestionIds.forEach((id)=>restoredStructureSuggestionIds.add(id));
69
+ if (!joinRevertResult && child.text === ZWSP) {
67
70
  tr.delete(insertionFrom, insertionTo);
68
71
  }
69
72
  } else {
@@ -71,6 +74,9 @@ import { applyAllStructureSuggestions, applyStructureSuggestion } from "./featur
71
74
  }
72
75
  return true;
73
76
  });
77
+ return {
78
+ restoredStructureSuggestionIds
79
+ };
74
80
  }
75
81
  function revertModifications(node, pos, tr) {
76
82
  const { modification } = getSuggestionMarks(node.type.schema);
@@ -358,12 +364,18 @@ export function applySuggestionsToRange(doc, from, to) {
358
364
  const structureTransform = revertStructureSuggestion(state.doc, suggestionId);
359
365
  // then start a clear transform from the document where the structure changes are reverted
360
366
  const suggestionsTransform = new Transform(structureTransform.doc);
361
- applySuggestionsToTransform(suggestionsTransform.doc, suggestionsTransform, deletion, insertion, suggestionId, from, to);
367
+ const { restoredStructureSuggestionIds } = applySuggestionsToTransform(suggestionsTransform.doc, suggestionsTransform, deletion, insertion, suggestionId, from, to);
362
368
  applyModificationsToTransform(suggestionsTransform.doc, suggestionsTransform, -1, undefined, from, to);
363
369
  // replay suggestion transform on top of the structure transform
364
370
  suggestionsTransform.steps.forEach((step)=>{
365
371
  structureTransform.step(step);
366
372
  });
373
+ restoredStructureSuggestionIds.forEach((suggestionId)=>{
374
+ const restoredStructureTransform = revertStructureSuggestion(structureTransform.doc, suggestionId);
375
+ restoredStructureTransform.steps.forEach((step)=>{
376
+ structureTransform.step(step);
377
+ });
378
+ });
367
379
  // apply the structure transform to the transaction
368
380
  const transaction = state.tr;
369
381
  structureTransform.steps.forEach((step)=>{
@@ -1,7 +1,7 @@
1
1
  import { Plugin, PluginKey, TextSelection } from "prosemirror-state";
2
2
  import { getSuggestionMarks } from "./utils.js";
3
3
  import { ZWSP } from "./constants.js";
4
- const TRACE_ENABLED = false;
4
+ const TRACE_ENABLED = true;
5
5
  function trace(...args) {
6
6
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
7
7
  if (!TRACE_ENABLED) return;
@@ -2,7 +2,9 @@ import { Mark, type Node, type MarkType, type ResolvedPos } from "prosemirror-mo
2
2
  import { Transform } from "prosemirror-transform";
3
3
  import { type Transaction } from "prosemirror-state";
4
4
  import { type SuggestionId } from "../../generateId.js";
5
- export declare function maybeRevertJoinMark(tr: Transform, from: number, to: number, node: Node, markType: MarkType): boolean;
5
+ export declare function maybeRevertJoinMark(tr: Transform, from: number, to: number, node: Node, markType: MarkType): false | {
6
+ restoredStructureSuggestionIds: Set<SuggestionId>;
7
+ };
6
8
  /**
7
9
  * Remove ZWSP text nodes marked as deletions (except for type=join) from the given range
8
10
  */
@@ -41,6 +41,23 @@ function restoreNodeMarkup(tr, pos, node) {
41
41
  tr.setNodeMarkup(pos, nodeType, node.attrs, marks);
42
42
  return true;
43
43
  }
44
+ // collect structure suggestion IDs that will be re-introduced into the document after the join is reverted
45
+ function getRestoredStructureSuggestionIds(joinNodes, schema) {
46
+ const { structure } = getSuggestionMarks(schema);
47
+ const restoredStructureSuggestionIds = new Set();
48
+ for (const node of [
49
+ ...joinNodes.leftNodes,
50
+ ...joinNodes.rightNodes
51
+ ]){
52
+ const marks = marksFromJSON(schema, node.marks);
53
+ for (const mark of marks){
54
+ if (mark.type !== structure) continue;
55
+ if (!guardStructureMarkAttrs(mark.attrs)) continue;
56
+ restoredStructureSuggestionIds.add(mark.attrs.id);
57
+ }
58
+ }
59
+ return restoredStructureSuggestionIds;
60
+ }
44
61
  export function maybeRevertJoinMark(tr, from, to, node, markType) {
45
62
  const mark = node.marks.find((mark)=>mark.type === markType);
46
63
  if (!mark || !isJoinMark(mark) || node.text !== ZWSP) return false;
@@ -58,6 +75,7 @@ export function maybeRevertJoinMark(tr, from, to, node, markType) {
58
75
  return false;
59
76
  }
60
77
  }
78
+ const restoredStructureSuggestionIds = getRestoredStructureSuggestionIds(joinNodes, tr.doc.type.schema);
61
79
  // Reverting a join marker removes its ZWSP anchor, splits at that position,
62
80
  // and restores markup because ProseMirror split creates nodes with defaults.
63
81
  const joinDepth = joinNodes.leftNodes.length;
@@ -79,7 +97,9 @@ export function maybeRevertJoinMark(tr, from, to, node, markType) {
79
97
  // Each deeper right node starts one position inside the right node restored before it.
80
98
  rightPos += 1;
81
99
  }
82
- return true;
100
+ return {
101
+ restoredStructureSuggestionIds
102
+ };
83
103
  }
84
104
  /**
85
105
  * Remove ZWSP text nodes marked as deletions (except for type=join) from the given range
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magic-marker/prosemirror-suggest-changes",
3
- "version": "0.3.3-wrap-unwrap.20",
3
+ "version": "0.3.3-wrap-unwrap.22",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "dist/index.js",