@magic-marker/prosemirror-suggest-changes 0.3.3-wrap-unwrap.3 → 0.3.3-wrap-unwrap.4

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.
@@ -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 = true;
4
+ const TRACE_ENABLED = false;
5
5
  function trace(...args) {
6
6
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
7
7
  if (!TRACE_ENABLED) return;
@@ -1,5 +1,12 @@
1
1
  import { getNodeId } from "./getNodeId.js";
2
+ const TRACE_ENABLED = true;
3
+ function trace(...args) {
4
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
5
+ if (!TRACE_ENABLED) return;
6
+ console.log("[structureChanges]", ...args);
7
+ }
2
8
  export function buildMaterializedPaths(doc) {
9
+ const perfPaths = performance.now();
3
10
  const paths = new Map();
4
11
  // add direct doc children first since they have a specific parent signature due to doc not having an ID
5
12
  doc.children.forEach((node, index, children)=>{
@@ -67,5 +74,6 @@ export function buildMaterializedPaths(doc) {
67
74
  });
68
75
  return true;
69
76
  });
77
+ trace("perf", "buildMaterializedPaths", "took", Number((performance.now() - perfPaths).toFixed(2)), "ms");
70
78
  return paths;
71
79
  }
@@ -232,10 +232,10 @@ function findOrderedSuggestionIdsToRevert(node, suggestionId, materializedPaths)
232
232
  if (attrs.data.op.op !== "move") return false;
233
233
  return !sameParentChain(attrs.data.op.to, parentChain.chain);
234
234
  });
235
- console.log("findOrderedSuggestionIdsToRevert", "mismatch?", mismatch);
236
235
  if (mismatch == null) {
237
236
  return Array.from(suggestionIds).reverse();
238
237
  }
238
+ console.log("findOrderedSuggestionIdsToRevert", "suggestion", suggestionId, "contains mark with a mismatched 'to':", mismatch, "searching a different suggestion with the matching 'to'...");
239
239
  // find first mark on the node that does have a matching op.to
240
240
  const match = mismatch.node.marks.find((mark)=>{
241
241
  if (mark.type !== structure) return false;
@@ -249,6 +249,7 @@ function findOrderedSuggestionIdsToRevert(node, suggestionId, materializedPaths)
249
249
  return sameParentChain(attrs.data.op.to, parentChain.chain);
250
250
  });
251
251
  if (match) {
252
+ console.log("findOrderedSuggestionIdsToRevert", "found suggestin with matching 'to'", match);
252
253
  suggestionIds.add(match.attrs["id"]);
253
254
  }
254
255
  return Array.from(suggestionIds).reverse();
@@ -7,6 +7,12 @@ import { Transform } from "prosemirror-transform";
7
7
  import { isSuggestChangesEnabled, suggestChangesKey } from "../../plugin.js";
8
8
  import { buildMaterializedPaths } from "./buildMaterializedPaths.js";
9
9
  import { sameParentChain } from "./sameParentChain.js";
10
+ const TRACE_ENABLED = true;
11
+ function trace(...args) {
12
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
13
+ if (!TRACE_ENABLED) return;
14
+ console.log("[structureChanges]", ...args);
15
+ }
10
16
  export const structureChangesKey = new PluginKey("@handlewithcare/prosemirror-suggest-changes-structure-changes");
11
17
  // const listStructure = {
12
18
  // type: [LIST_NODE],
@@ -82,7 +88,7 @@ export function structureChangesPlugin(generateId) {
82
88
  });
83
89
  return;
84
90
  }
85
- console.log("structureChangesPlugin", "appendTransaction", [
91
+ trace("structureChangesPlugin", "appendTransaction", [
86
92
  ...transactions
87
93
  ]);
88
94
  const transform = suggestStructureChanges(oldDoc, newDoc, generateId);
@@ -105,12 +111,12 @@ export function suggestStructureChanges(docBefore, docAfter, generateId) {
105
111
  const suggestionId = generateId ? generateId(docBefore.type.schema, docBefore) : generateNextNumberId(docBefore.type.schema, docBefore);
106
112
  const pathsBefore = buildMaterializedPaths(docBefore);
107
113
  const pathsAfter = buildMaterializedPaths(docAfter);
108
- console.log("suggestStructureChanges", "materialized paths", {
114
+ trace("suggestStructureChanges", "materialized paths", {
109
115
  pathsBefore: Object.fromEntries(pathsBefore.entries()),
110
116
  pathsAfter: Object.fromEntries(pathsAfter.entries())
111
117
  });
112
118
  const ops = getOps(pathsBefore, pathsAfter);
113
- console.log("suggestStructureChanges", "ops", {
119
+ trace("suggestStructureChanges", "ops", {
114
120
  ops: Object.fromEntries(ops.entries())
115
121
  });
116
122
  const transform = new Transform(docAfter);
@@ -118,6 +124,7 @@ export function suggestStructureChanges(docBefore, docAfter, generateId) {
118
124
  return transform;
119
125
  }
120
126
  function addMarks(ops, tr, suggestionId) {
127
+ const perfAddMarks = performance.now();
121
128
  const { structure } = getSuggestionMarks(tr.doc.type.schema);
122
129
  tr.doc.descendants((node, pos)=>{
123
130
  if (node.isText) return true;
@@ -133,6 +140,7 @@ function addMarks(ops, tr, suggestionId) {
133
140
  }));
134
141
  return true;
135
142
  });
143
+ trace("perf", "addMarks", "took", Number((performance.now() - perfAddMarks).toFixed(2)), "ms");
136
144
  }
137
145
  function getOps(beforePaths, afterPaths) {
138
146
  const ops = new Map();
@@ -126,15 +126,19 @@ function getStepHandler(step) {
126
126
  // after a transaction, some nodes may not yet have unique ids (they were just added, and the unique id plugin has not yet run)
127
127
  // this hook allows to "post-process" the transaction and add the missing ids
128
128
  // basically it allows to run the core logic of the unique ids plugin earlier
129
+ const perfUid = performance.now();
129
130
  const uniqueNodeIdsTransform = opts.experimental_ensureUniqueNodeIds([
130
131
  transaction
131
132
  ], docBefore, transaction.doc);
133
+ trace("perf", "structure", "ensureUniqueNodsIds took", Number((performance.now() - perfUid).toFixed(2)), "ms");
132
134
  const docAfter = uniqueNodeIdsTransform.doc;
133
135
  trace("unique node ids set", docAfter);
134
136
  // try running structure changes first
135
137
  // if handled, then ignore the main plugin
136
138
  // otherwise use the main plugin
139
+ const perfStructure = performance.now();
137
140
  structureChangesTransform = suggestStructureChanges(docBefore, docAfter, generateId);
141
+ trace("perf", "structure", "suggestStructureChanges took", Number((performance.now() - perfStructure).toFixed(2)), "ms");
138
142
  trace("structure changes transform completed", structureChangesTransform);
139
143
  if (structureChangesTransform.steps.length > 0) {
140
144
  uniqueNodeIdsTransform.steps.forEach((step)=>{
@@ -148,7 +152,9 @@ function getStepHandler(step) {
148
152
  }
149
153
  if (transaction.docChanged && (structureChangesTransform == null || structureChangesTransform.steps.length === 0)) {
150
154
  trace("running the main suggestions plugin...");
155
+ const perfSuggestions = performance.now();
151
156
  transaction = transformToSuggestionTransaction(tr, this.state, generateId);
157
+ trace("perf", "suggestions", "transformToSuggestionTransaction took", Number((performance.now() - perfSuggestions).toFixed(2)), "ms");
152
158
  trace("main suggestions plugin completed", transaction);
153
159
  }
154
160
  }
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.3",
3
+ "version": "0.3.3-wrap-unwrap.4",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "dist/index.js",