@manuscripts/track-changes-plugin 0.0.3 → 0.2.0

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/index.es.js CHANGED
@@ -327,8 +327,9 @@ function deleteNode(node, pos, tr) {
327
327
  var _a;
328
328
  const startPos = tr.doc.resolve(pos + 1);
329
329
  const range = startPos.blockRange(tr.doc.resolve(startPos.pos - 2 + node.nodeSize));
330
- const targetDepth = range ? Number(liftTarget(range)) : NaN;
331
- if (range && !Number.isNaN(targetDepth)) {
330
+ const targetDepth = range && liftTarget(range);
331
+ // Check with typeof since with old prosemirror-transform targetDepth is undefined
332
+ if (range && typeof targetDepth === 'number') {
332
333
  return tr.lift(range, targetDepth);
333
334
  }
334
335
  const resPos = tr.doc.resolve(pos);
@@ -505,7 +506,7 @@ function applyAcceptedRejectedChanges(tr, schema, changes, deleteMap = new Mappi
505
506
  // or were already deleted by an applied block delete
506
507
  const { pos: from, deleted } = deleteMap.mapResult(change.from), node = tr.doc.nodeAt(from), noChangeNeeded = deleted || ChangeSet.shouldNotDelete(change);
507
508
  if (!node) {
508
- log.warn('no node found to update for change', change);
509
+ !deleted && log.warn('no node found to update for change', change);
509
510
  return;
510
511
  }
511
512
  if (ChangeSet.isTextChange(change) && noChangeNeeded) {
@@ -929,6 +930,7 @@ function deleteAndMergeSplitNodes(from, to, gap, startDoc, newTr, schema, trackA
929
930
  // But from what I remember what it safeguards against is, when you've already deleted a node
930
931
  // say an inserted blockquote that had all its children deleted, nodesBetween still iterates over those
931
932
  // nodes and therefore we have to make this check to ensure they still exist in the doc.
933
+ //
932
934
  if (nodeEnd > offsetFrom && !nodeWasDeleted && !wasWithinGap) {
933
935
  // |<p>asdf</p>| -> node deleted completely
934
936
  const nodeCompletelyDeleted = offsetPos >= offsetFrom && nodeEnd <= offsetTo;
@@ -1209,11 +1211,9 @@ function trackReplaceStep(step, oldState, newTr, attrs) {
1209
1211
  * This skips the direct dependency to prosemirror-state where multiple versions might cause conflicts
1210
1212
  * as the created instances might belong to different prosemirror-state import than one used in the editor.
1211
1213
  * @param sel
1212
- * @param doc
1213
- * @param from
1214
1214
  * @returns
1215
1215
  */
1216
- const getSelectionStaticCreate = (sel, doc, from) => Object.getPrototypeOf(sel).constructor.create(doc, from);
1216
+ const getSelectionStaticConstructor = (sel) => Object.getPrototypeOf(sel).constructor;
1217
1217
  /**
1218
1218
  * Inverts transactions to wrap their contents/operations with track data instead
1219
1219
  *
@@ -1257,7 +1257,12 @@ function trackTransaction(tr, oldState, newTr, userID) {
1257
1257
  else if (step instanceof ReplaceStep) {
1258
1258
  const selectionPos = trackReplaceStep(step, oldState, newTr, emptyAttrs);
1259
1259
  if (!wasNodeSelection) {
1260
- newTr.setSelection(getSelectionStaticCreate(tr.selection, newTr.doc, selectionPos));
1260
+ const sel = getSelectionStaticConstructor(tr.selection);
1261
+ // Use Selection.near to fix selections that point to a block node instead of inline content
1262
+ // eg when inserting a complete new paragraph. -1 finds the first valid position moving backwards
1263
+ // inside the content
1264
+ const near = sel.near(newTr.doc.resolve(selectionPos), -1);
1265
+ newTr.setSelection(near);
1261
1266
  }
1262
1267
  }
1263
1268
  else if (step instanceof ReplaceAroundStep) {
@@ -1268,13 +1273,12 @@ function trackTransaction(tr, oldState, newTr, userID) {
1268
1273
  // TODO: here we could check whether adjacent inserts & deletes cancel each other out.
1269
1274
  // However, this should not be done by diffing and only matching node or char by char instead since
1270
1275
  // it's A easier and B more intuitive to user.
1271
- const { meta } = tr;
1272
- // This is quite non-optimal in some sense but to ensure no information is lost
1273
- // we have to re-add all the old meta keys, such as inputType or uiEvent.
1274
- // This should prevent bugs incase other plugins/widgets rely upon them existing (and they
1275
- // are not able to process the transactions before track-changes).
1276
- // TODO: will this cause race-condition if a meta causes another appendTransaction to fire
1277
- Object.keys(meta).forEach((key) => newTr.setMeta(key, tr.getMeta(key)));
1276
+ // The old meta keys are not copied to the new transaction since this will cause race-conditions
1277
+ // when a single meta-field is expected to having been processed / removed. Generic input meta keys,
1278
+ // inputType and uiEvent, are re-added since some plugins might depend on them and process the transaction
1279
+ // after track-changes plugin.
1280
+ tr.getMeta('inputType') && newTr.setMeta('inputType', tr.getMeta('inputType'));
1281
+ tr.getMeta('uiEvent') && newTr.setMeta('uiEvent', tr.getMeta('uiEvent'));
1278
1282
  });
1279
1283
  // This is kinda hacky solution at the moment to maintain NodeSelections over transactions
1280
1284
  // These are required by at least cross-references that need it to activate the selector pop-up
@@ -1282,7 +1286,8 @@ function trackTransaction(tr, oldState, newTr, userID) {
1282
1286
  const mappedPos = newTr.mapping.map(tr.selection.from);
1283
1287
  const resPos = newTr.doc.resolve(mappedPos);
1284
1288
  const nodePos = mappedPos - (((_a = resPos.nodeBefore) === null || _a === void 0 ? void 0 : _a.nodeSize) || 0);
1285
- newTr.setSelection(getSelectionStaticCreate(tr.selection, newTr.doc, nodePos));
1289
+ const sel = getSelectionStaticConstructor(tr.selection);
1290
+ newTr.setSelection(sel.create(newTr.doc, nodePos));
1286
1291
  }
1287
1292
  log.info('NEW transaction', newTr);
1288
1293
  return newTr;
@@ -1332,7 +1337,8 @@ const trackChangesPlugin = (opts = { userID: 'anonymous:Anonymous' }) => {
1332
1337
  key: trackChangesPluginKey,
1333
1338
  props: {
1334
1339
  editable(state) {
1335
- return this.getState(state).status !== TrackChangesStatus.viewSnapshots;
1340
+ var _a;
1341
+ return ((_a = trackChangesPluginKey.getState(state)) === null || _a === void 0 ? void 0 : _a.status) !== TrackChangesStatus.viewSnapshots;
1336
1342
  },
1337
1343
  },
1338
1344
  state: {
@@ -1402,7 +1408,6 @@ const trackChangesPlugin = (opts = { userID: 'anonymous:Anonymous' }) => {
1402
1408
  (wasAppended && getAction(wasAppended, TrackChangesAction.skipTrack));
1403
1409
  if (tr.docChanged && !skipMetaUsed && !skipTrackUsed && !tr.getMeta('history$')) {
1404
1410
  createdTr = trackTransaction(tr, oldState, createdTr, userID);
1405
- createdTr.setMeta('origin', trackChangesPluginKey);
1406
1411
  infiniteLoopCounter.iters += 1;
1407
1412
  }
1408
1413
  docChanged = docChanged || tr.docChanged;
@@ -1429,6 +1434,7 @@ const trackChangesPlugin = (opts = { userID: 'anonymous:Anonymous' }) => {
1429
1434
  log.warn('had to fix inconsistent changes in', createdTr);
1430
1435
  }
1431
1436
  if (docChanged || createdTr.docChanged || changed) {
1437
+ createdTr.setMeta('origin', trackChangesPluginKey);
1432
1438
  return setAction(createdTr, TrackChangesAction.refreshChanges, true);
1433
1439
  }
1434
1440
  return null;