@portabletext/editor 1.0.13 → 1.0.14

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/lib/index.js CHANGED
@@ -1283,55 +1283,85 @@ function createWithInsertBreak(types2) {
1283
1283
  }, editor;
1284
1284
  };
1285
1285
  }
1286
+ function withRemoteChanges(editor, fn) {
1287
+ const prev = isChangingRemotely(editor) || !1;
1288
+ IS_PROCESSING_REMOTE_CHANGES.set(editor, !0), fn(), IS_PROCESSING_REMOTE_CHANGES.set(editor, prev);
1289
+ }
1290
+ function isChangingRemotely(editor) {
1291
+ return IS_PROCESSING_REMOTE_CHANGES.get(editor);
1292
+ }
1293
+ function isChangingLocally(editor) {
1294
+ return IS_PROCESSING_LOCAL_CHANGES.get(editor);
1295
+ }
1296
+ const IS_UDOING = /* @__PURE__ */ new WeakMap(), IS_REDOING = /* @__PURE__ */ new WeakMap();
1297
+ function withUndoing(editor, fn) {
1298
+ const prev = isUndoing(editor);
1299
+ IS_UDOING.set(editor, !0), fn(), IS_UDOING.set(editor, prev);
1300
+ }
1301
+ function isUndoing(editor) {
1302
+ return IS_UDOING.get(editor) ?? !1;
1303
+ }
1304
+ function setIsUndoing(editor, isUndoing2) {
1305
+ IS_UDOING.set(editor, isUndoing2);
1306
+ }
1307
+ function withRedoing(editor, fn) {
1308
+ const prev = isRedoing(editor);
1309
+ IS_REDOING.set(editor, !0), fn(), IS_REDOING.set(editor, prev);
1310
+ }
1311
+ function isRedoing(editor) {
1312
+ return IS_REDOING.get(editor) ?? !1;
1313
+ }
1314
+ function setIsRedoing(editor, isRedoing2) {
1315
+ IS_REDOING.set(editor, isRedoing2);
1316
+ }
1286
1317
  function createWithMaxBlocks(maxBlocks) {
1287
1318
  return function(editor) {
1288
1319
  const { apply: apply2 } = editor;
1289
1320
  return editor.apply = (operation) => {
1321
+ if (isChangingRemotely(editor)) {
1322
+ apply2(operation);
1323
+ return;
1324
+ }
1325
+ if (isUndoing(editor) || isRedoing(editor)) {
1326
+ apply2(operation);
1327
+ return;
1328
+ }
1290
1329
  const rows = maxBlocks;
1291
1330
  rows > 0 && editor.children.length >= rows && (operation.type === "insert_node" || operation.type === "split_node") && operation.path.length === 1 || apply2(operation);
1292
1331
  }, editor;
1293
1332
  };
1294
1333
  }
1295
- const PRESERVE_KEYS = /* @__PURE__ */ new WeakMap();
1296
- function withPreserveKeys(editor, fn) {
1297
- const prev = isPreservingKeys(editor);
1298
- PRESERVE_KEYS.set(editor, !0), fn(), PRESERVE_KEYS.set(editor, prev);
1299
- }
1300
- function withoutPreserveKeys(editor, fn) {
1301
- const prev = isPreservingKeys(editor);
1302
- PRESERVE_KEYS.set(editor, !1), fn(), PRESERVE_KEYS.set(editor, prev);
1303
- }
1304
- function isPreservingKeys(editor) {
1305
- return PRESERVE_KEYS.get(editor);
1306
- }
1307
1334
  function createWithObjectKeys(schemaTypes, keyGenerator) {
1308
1335
  return function(editor) {
1309
- PRESERVE_KEYS.set(editor, !1);
1310
1336
  const { apply: apply2, normalizeNode } = editor;
1311
1337
  return editor.apply = (operation) => {
1338
+ if (isChangingRemotely(editor)) {
1339
+ apply2(operation);
1340
+ return;
1341
+ }
1342
+ if (isUndoing(editor) || isRedoing(editor)) {
1343
+ apply2(operation);
1344
+ return;
1345
+ }
1312
1346
  if (operation.type === "split_node") {
1313
- const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.properties);
1314
1347
  apply2({
1315
1348
  ...operation,
1316
1349
  properties: {
1317
1350
  ...operation.properties,
1318
- ...withNewKey ? { _key: keyGenerator() } : {}
1351
+ _key: keyGenerator()
1319
1352
  }
1320
1353
  });
1321
1354
  return;
1322
1355
  }
1323
- if (operation.type === "insert_node") {
1324
- const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.node);
1325
- if (!slate.Editor.isEditor(operation.node)) {
1326
- apply2({
1327
- ...operation,
1328
- node: {
1329
- ...operation.node,
1330
- ...withNewKey ? { _key: keyGenerator() } : {}
1331
- }
1332
- });
1333
- return;
1334
- }
1356
+ if (operation.type === "insert_node" && !slate.Editor.isEditor(operation.node)) {
1357
+ apply2({
1358
+ ...operation,
1359
+ node: {
1360
+ ...operation.node,
1361
+ _key: keyGenerator()
1362
+ }
1363
+ });
1364
+ return;
1335
1365
  }
1336
1366
  apply2(operation);
1337
1367
  }, editor.normalizeNode = (entry) => {
@@ -2221,16 +2251,6 @@ function findBlockAndChildFromPath(editor, path) {
2221
2251
  });
2222
2252
  return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
2223
2253
  }
2224
- function withRemoteChanges(editor, fn) {
2225
- const prev = isChangingRemotely(editor) || !1;
2226
- IS_PROCESSING_REMOTE_CHANGES.set(editor, !0), fn(), IS_PROCESSING_REMOTE_CHANGES.set(editor, prev);
2227
- }
2228
- function isChangingRemotely(editor) {
2229
- return IS_PROCESSING_REMOTE_CHANGES.get(editor);
2230
- }
2231
- function isChangingLocally(editor) {
2232
- return IS_PROCESSING_LOCAL_CHANGES.get(editor);
2233
- }
2234
2254
  const PATCHING = /* @__PURE__ */ new WeakMap();
2235
2255
  function withoutPatching(editor, fn) {
2236
2256
  const prev = isPatching(editor);
@@ -2311,7 +2331,7 @@ function createWithUndoRedo(options) {
2311
2331
  const reversedOperations = transformedOperations.map(slate.Operation.inverse).reverse();
2312
2332
  try {
2313
2333
  slate.Editor.withoutNormalizing(editor, () => {
2314
- withPreserveKeys(editor, () => {
2334
+ withUndoing(editor, () => {
2315
2335
  withoutSaving(editor, () => {
2316
2336
  reversedOperations.forEach((op) => {
2317
2337
  editor.apply(op);
@@ -2320,7 +2340,7 @@ function createWithUndoRedo(options) {
2320
2340
  });
2321
2341
  }), editor.normalize(), editor.onChange();
2322
2342
  } catch (err) {
2323
- debug$h("Could not perform undo step", err), remotePatches.splice(0, remotePatches.length), slate.Transforms.deselect(editor), editor.history = { undos: [], redos: [] }, SAVING.set(editor, !0), editor.onChange();
2343
+ debug$h("Could not perform undo step", err), remotePatches.splice(0, remotePatches.length), slate.Transforms.deselect(editor), editor.history = { undos: [], redos: [] }, SAVING.set(editor, !0), setIsUndoing(editor, !1), editor.onChange();
2324
2344
  return;
2325
2345
  }
2326
2346
  editor.history.redos.push(step), editor.history.undos.pop();
@@ -2344,7 +2364,7 @@ function createWithUndoRedo(options) {
2344
2364
  });
2345
2365
  try {
2346
2366
  slate.Editor.withoutNormalizing(editor, () => {
2347
- withPreserveKeys(editor, () => {
2367
+ withRedoing(editor, () => {
2348
2368
  withoutSaving(editor, () => {
2349
2369
  transformedOperations.forEach((op) => {
2350
2370
  editor.apply(op);
@@ -2353,7 +2373,7 @@ function createWithUndoRedo(options) {
2353
2373
  });
2354
2374
  }), editor.normalize(), editor.onChange();
2355
2375
  } catch (err) {
2356
- debug$h("Could not perform redo step", err), remotePatches.splice(0, remotePatches.length), slate.Transforms.deselect(editor), editor.history = { undos: [], redos: [] }, SAVING.set(editor, !0), editor.onChange();
2376
+ debug$h("Could not perform redo step", err), remotePatches.splice(0, remotePatches.length), slate.Transforms.deselect(editor), editor.history = { undos: [], redos: [] }, SAVING.set(editor, !0), setIsRedoing(editor, !1), editor.onChange();
2357
2377
  return;
2358
2378
  }
2359
2379
  editor.history.undos.push(step), editor.history.redos.pop();
@@ -2467,10 +2487,8 @@ function createWithPatches({
2467
2487
  slate.Editor.withoutNormalizing(editor, () => {
2468
2488
  withoutPatching(editor, () => {
2469
2489
  withoutSaving(editor, () => {
2470
- withPreserveKeys(editor, () => {
2471
- patches2.forEach((patch) => {
2472
- debug$g.enabled && debug$g(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
2473
- });
2490
+ patches2.forEach((patch) => {
2491
+ debug$g.enabled && debug$g(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
2474
2492
  });
2475
2493
  });
2476
2494
  });
@@ -2567,6 +2585,14 @@ function createWithPlaceholderBlock() {
2567
2585
  return function(editor) {
2568
2586
  const { apply: apply2 } = editor;
2569
2587
  return editor.apply = (op) => {
2588
+ if (isChangingRemotely(editor)) {
2589
+ apply2(op);
2590
+ return;
2591
+ }
2592
+ if (isUndoing(editor) || isRedoing(editor)) {
2593
+ apply2(op);
2594
+ return;
2595
+ }
2570
2596
  if (op.type === "remove_node") {
2571
2597
  const node = op.node;
2572
2598
  if (op.path[0] === 0 && slate.Editor.isVoid(editor, node)) {
@@ -2806,6 +2832,14 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2806
2832
  ), editor.onChange());
2807
2833
  }
2808
2834
  }, editor.apply = (op) => {
2835
+ if (isChangingRemotely(editor)) {
2836
+ apply2(op);
2837
+ return;
2838
+ }
2839
+ if (isUndoing(editor) || isRedoing(editor)) {
2840
+ apply2(op);
2841
+ return;
2842
+ }
2809
2843
  if (op.type === "insert_text") {
2810
2844
  const { selection } = editor;
2811
2845
  if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
@@ -2846,15 +2880,23 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2846
2880
  ), deletingPartOfTheNode = op.offset !== 0, deletingFromTheEnd = op.offset + op.text.length === node.text.length;
2847
2881
  if (nodeHasAnnotations && deletingPartOfTheNode && deletingFromTheEnd) {
2848
2882
  slate.Editor.withoutNormalizing(editor, () => {
2849
- withoutPreserveKeys(editor, () => {
2850
- slate.Transforms.splitNodes(editor, {
2851
- match: slate.Text.isText,
2852
- at: { path: op.path, offset: op.offset }
2853
- });
2883
+ slate.Transforms.splitNodes(editor, {
2884
+ match: slate.Text.isText,
2885
+ at: { path: op.path, offset: op.offset }
2854
2886
  }), slate.Transforms.removeNodes(editor, { at: slate.Path.next(op.path) });
2855
2887
  }), editor.onChange();
2856
2888
  return;
2857
2889
  }
2890
+ const deletingAllText = op.offset === 0 && deletingFromTheEnd;
2891
+ if (nodeHasAnnotations && deletingAllText) {
2892
+ const marksWithoutAnnotationMarks = ({
2893
+ ...slate.Editor.marks(editor) || {}
2894
+ }.marks || []).filter((mark) => decorators.includes(mark));
2895
+ slate.Editor.withoutNormalizing(editor, () => {
2896
+ apply2(op), slate.Transforms.setNodes(editor, { marks: marksWithoutAnnotationMarks }, { at: op.path });
2897
+ }), editor.onChange();
2898
+ return;
2899
+ }
2858
2900
  }
2859
2901
  }
2860
2902
  apply2(op);
@@ -3821,10 +3863,8 @@ function useSyncValue(props) {
3821
3863
  debug$5.enabled && debug$5(
3822
3864
  "Validating and inserting new block in the end of the value",
3823
3865
  currentBlock
3824
- ), validation.valid || validation.resolution?.autoResolve ? withPreserveKeys(slateEditor, () => {
3825
- slate.Transforms.insertNodes(slateEditor, currentBlock, {
3826
- at: [currentBlockIndex]
3827
- });
3866
+ ), validation.valid || validation.resolution?.autoResolve ? slate.Transforms.insertNodes(slateEditor, currentBlock, {
3867
+ at: [currentBlockIndex]
3828
3868
  }) : (debug$5("Invalid", validation), change$.next({
3829
3869
  type: "invalidValue",
3830
3870
  resolution: validation.resolution,
@@ -3874,9 +3914,7 @@ function useSyncValue(props) {
3874
3914
  }
3875
3915
  function _replaceBlock(slateEditor, currentBlock, currentBlockIndex) {
3876
3916
  const currentSelection = slateEditor.selection, selectionFocusOnBlock = currentSelection && currentSelection.focus.path[0] === currentBlockIndex;
3877
- selectionFocusOnBlock && slate.Transforms.deselect(slateEditor), slate.Transforms.removeNodes(slateEditor, { at: [currentBlockIndex] }), withPreserveKeys(slateEditor, () => {
3878
- slate.Transforms.insertNodes(slateEditor, currentBlock, { at: [currentBlockIndex] });
3879
- }), slateEditor.onChange(), selectionFocusOnBlock && slate.Transforms.select(slateEditor, currentSelection);
3917
+ selectionFocusOnBlock && slate.Transforms.deselect(slateEditor), slate.Transforms.removeNodes(slateEditor, { at: [currentBlockIndex] }), slate.Transforms.insertNodes(slateEditor, currentBlock, { at: [currentBlockIndex] }), slateEditor.onChange(), selectionFocusOnBlock && slate.Transforms.select(slateEditor, currentSelection);
3880
3918
  }
3881
3919
  function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3882
3920
  if (slate.Transforms.setNodes(slateEditor, currentBlock, {
@@ -3912,15 +3950,11 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3912
3950
  ));
3913
3951
  } else oldBlockChild ? (debug$5("Replacing child", currentBlockChild), slate.Transforms.removeNodes(slateEditor, {
3914
3952
  at: [currentBlockIndex, currentBlockChildIndex]
3915
- }), withPreserveKeys(slateEditor, () => {
3916
- slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
3917
- at: [currentBlockIndex, currentBlockChildIndex]
3918
- });
3919
- }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), withPreserveKeys(slateEditor, () => {
3920
- slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
3921
- at: [currentBlockIndex, currentBlockChildIndex]
3922
- }), slateEditor.onChange();
3923
- }));
3953
+ }), slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
3954
+ at: [currentBlockIndex, currentBlockChildIndex]
3955
+ }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
3956
+ at: [currentBlockIndex, currentBlockChildIndex]
3957
+ }), slateEditor.onChange());
3924
3958
  });
3925
3959
  }
3926
3960
  }