@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.mjs CHANGED
@@ -1299,55 +1299,85 @@ function createWithInsertBreak(types) {
1299
1299
  }, editor;
1300
1300
  };
1301
1301
  }
1302
+ function withRemoteChanges(editor, fn) {
1303
+ const prev = isChangingRemotely(editor) || !1;
1304
+ IS_PROCESSING_REMOTE_CHANGES.set(editor, !0), fn(), IS_PROCESSING_REMOTE_CHANGES.set(editor, prev);
1305
+ }
1306
+ function isChangingRemotely(editor) {
1307
+ return IS_PROCESSING_REMOTE_CHANGES.get(editor);
1308
+ }
1309
+ function isChangingLocally(editor) {
1310
+ return IS_PROCESSING_LOCAL_CHANGES.get(editor);
1311
+ }
1312
+ const IS_UDOING = /* @__PURE__ */ new WeakMap(), IS_REDOING = /* @__PURE__ */ new WeakMap();
1313
+ function withUndoing(editor, fn) {
1314
+ const prev = isUndoing(editor);
1315
+ IS_UDOING.set(editor, !0), fn(), IS_UDOING.set(editor, prev);
1316
+ }
1317
+ function isUndoing(editor) {
1318
+ return IS_UDOING.get(editor) ?? !1;
1319
+ }
1320
+ function setIsUndoing(editor, isUndoing2) {
1321
+ IS_UDOING.set(editor, isUndoing2);
1322
+ }
1323
+ function withRedoing(editor, fn) {
1324
+ const prev = isRedoing(editor);
1325
+ IS_REDOING.set(editor, !0), fn(), IS_REDOING.set(editor, prev);
1326
+ }
1327
+ function isRedoing(editor) {
1328
+ return IS_REDOING.get(editor) ?? !1;
1329
+ }
1330
+ function setIsRedoing(editor, isRedoing2) {
1331
+ IS_REDOING.set(editor, isRedoing2);
1332
+ }
1302
1333
  function createWithMaxBlocks(maxBlocks) {
1303
1334
  return function(editor) {
1304
1335
  const { apply: apply2 } = editor;
1305
1336
  return editor.apply = (operation) => {
1337
+ if (isChangingRemotely(editor)) {
1338
+ apply2(operation);
1339
+ return;
1340
+ }
1341
+ if (isUndoing(editor) || isRedoing(editor)) {
1342
+ apply2(operation);
1343
+ return;
1344
+ }
1306
1345
  const rows = maxBlocks;
1307
1346
  rows > 0 && editor.children.length >= rows && (operation.type === "insert_node" || operation.type === "split_node") && operation.path.length === 1 || apply2(operation);
1308
1347
  }, editor;
1309
1348
  };
1310
1349
  }
1311
- const PRESERVE_KEYS = /* @__PURE__ */ new WeakMap();
1312
- function withPreserveKeys(editor, fn) {
1313
- const prev = isPreservingKeys(editor);
1314
- PRESERVE_KEYS.set(editor, !0), fn(), PRESERVE_KEYS.set(editor, prev);
1315
- }
1316
- function withoutPreserveKeys(editor, fn) {
1317
- const prev = isPreservingKeys(editor);
1318
- PRESERVE_KEYS.set(editor, !1), fn(), PRESERVE_KEYS.set(editor, prev);
1319
- }
1320
- function isPreservingKeys(editor) {
1321
- return PRESERVE_KEYS.get(editor);
1322
- }
1323
1350
  function createWithObjectKeys(schemaTypes, keyGenerator) {
1324
1351
  return function(editor) {
1325
- PRESERVE_KEYS.set(editor, !1);
1326
1352
  const { apply: apply2, normalizeNode } = editor;
1327
1353
  return editor.apply = (operation) => {
1354
+ if (isChangingRemotely(editor)) {
1355
+ apply2(operation);
1356
+ return;
1357
+ }
1358
+ if (isUndoing(editor) || isRedoing(editor)) {
1359
+ apply2(operation);
1360
+ return;
1361
+ }
1328
1362
  if (operation.type === "split_node") {
1329
- const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.properties);
1330
1363
  apply2({
1331
1364
  ...operation,
1332
1365
  properties: {
1333
1366
  ...operation.properties,
1334
- ...withNewKey ? { _key: keyGenerator() } : {}
1367
+ _key: keyGenerator()
1335
1368
  }
1336
1369
  });
1337
1370
  return;
1338
1371
  }
1339
- if (operation.type === "insert_node") {
1340
- const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.node);
1341
- if (!Editor.isEditor(operation.node)) {
1342
- apply2({
1343
- ...operation,
1344
- node: {
1345
- ...operation.node,
1346
- ...withNewKey ? { _key: keyGenerator() } : {}
1347
- }
1348
- });
1349
- return;
1350
- }
1372
+ if (operation.type === "insert_node" && !Editor.isEditor(operation.node)) {
1373
+ apply2({
1374
+ ...operation,
1375
+ node: {
1376
+ ...operation.node,
1377
+ _key: keyGenerator()
1378
+ }
1379
+ });
1380
+ return;
1351
1381
  }
1352
1382
  apply2(operation);
1353
1383
  }, editor.normalizeNode = (entry) => {
@@ -2237,16 +2267,6 @@ function findBlockAndChildFromPath(editor, path) {
2237
2267
  });
2238
2268
  return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
2239
2269
  }
2240
- function withRemoteChanges(editor, fn) {
2241
- const prev = isChangingRemotely(editor) || !1;
2242
- IS_PROCESSING_REMOTE_CHANGES.set(editor, !0), fn(), IS_PROCESSING_REMOTE_CHANGES.set(editor, prev);
2243
- }
2244
- function isChangingRemotely(editor) {
2245
- return IS_PROCESSING_REMOTE_CHANGES.get(editor);
2246
- }
2247
- function isChangingLocally(editor) {
2248
- return IS_PROCESSING_LOCAL_CHANGES.get(editor);
2249
- }
2250
2270
  const PATCHING = /* @__PURE__ */ new WeakMap();
2251
2271
  function withoutPatching(editor, fn) {
2252
2272
  const prev = isPatching(editor);
@@ -2327,7 +2347,7 @@ function createWithUndoRedo(options) {
2327
2347
  const reversedOperations = transformedOperations.map(Operation.inverse).reverse();
2328
2348
  try {
2329
2349
  Editor.withoutNormalizing(editor, () => {
2330
- withPreserveKeys(editor, () => {
2350
+ withUndoing(editor, () => {
2331
2351
  withoutSaving(editor, () => {
2332
2352
  reversedOperations.forEach((op) => {
2333
2353
  editor.apply(op);
@@ -2336,7 +2356,7 @@ function createWithUndoRedo(options) {
2336
2356
  });
2337
2357
  }), editor.normalize(), editor.onChange();
2338
2358
  } catch (err) {
2339
- debug$h("Could not perform undo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = { undos: [], redos: [] }, SAVING.set(editor, !0), editor.onChange();
2359
+ debug$h("Could not perform undo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = { undos: [], redos: [] }, SAVING.set(editor, !0), setIsUndoing(editor, !1), editor.onChange();
2340
2360
  return;
2341
2361
  }
2342
2362
  editor.history.redos.push(step), editor.history.undos.pop();
@@ -2360,7 +2380,7 @@ function createWithUndoRedo(options) {
2360
2380
  });
2361
2381
  try {
2362
2382
  Editor.withoutNormalizing(editor, () => {
2363
- withPreserveKeys(editor, () => {
2383
+ withRedoing(editor, () => {
2364
2384
  withoutSaving(editor, () => {
2365
2385
  transformedOperations.forEach((op) => {
2366
2386
  editor.apply(op);
@@ -2369,7 +2389,7 @@ function createWithUndoRedo(options) {
2369
2389
  });
2370
2390
  }), editor.normalize(), editor.onChange();
2371
2391
  } catch (err) {
2372
- debug$h("Could not perform redo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = { undos: [], redos: [] }, SAVING.set(editor, !0), editor.onChange();
2392
+ debug$h("Could not perform redo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = { undos: [], redos: [] }, SAVING.set(editor, !0), setIsRedoing(editor, !1), editor.onChange();
2373
2393
  return;
2374
2394
  }
2375
2395
  editor.history.undos.push(step), editor.history.redos.pop();
@@ -2483,10 +2503,8 @@ function createWithPatches({
2483
2503
  Editor.withoutNormalizing(editor, () => {
2484
2504
  withoutPatching(editor, () => {
2485
2505
  withoutSaving(editor, () => {
2486
- withPreserveKeys(editor, () => {
2487
- patches.forEach((patch) => {
2488
- debug$g.enabled && debug$g(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
2489
- });
2506
+ patches.forEach((patch) => {
2507
+ debug$g.enabled && debug$g(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
2490
2508
  });
2491
2509
  });
2492
2510
  });
@@ -2583,6 +2601,14 @@ function createWithPlaceholderBlock() {
2583
2601
  return function(editor) {
2584
2602
  const { apply: apply2 } = editor;
2585
2603
  return editor.apply = (op) => {
2604
+ if (isChangingRemotely(editor)) {
2605
+ apply2(op);
2606
+ return;
2607
+ }
2608
+ if (isUndoing(editor) || isRedoing(editor)) {
2609
+ apply2(op);
2610
+ return;
2611
+ }
2586
2612
  if (op.type === "remove_node") {
2587
2613
  const node = op.node;
2588
2614
  if (op.path[0] === 0 && Editor.isVoid(editor, node)) {
@@ -2822,6 +2848,14 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2822
2848
  ), editor.onChange());
2823
2849
  }
2824
2850
  }, editor.apply = (op) => {
2851
+ if (isChangingRemotely(editor)) {
2852
+ apply2(op);
2853
+ return;
2854
+ }
2855
+ if (isUndoing(editor) || isRedoing(editor)) {
2856
+ apply2(op);
2857
+ return;
2858
+ }
2825
2859
  if (op.type === "insert_text") {
2826
2860
  const { selection } = editor;
2827
2861
  if (selection && Range.isCollapsed(selection) && Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
@@ -2862,15 +2896,23 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2862
2896
  ), deletingPartOfTheNode = op.offset !== 0, deletingFromTheEnd = op.offset + op.text.length === node.text.length;
2863
2897
  if (nodeHasAnnotations && deletingPartOfTheNode && deletingFromTheEnd) {
2864
2898
  Editor.withoutNormalizing(editor, () => {
2865
- withoutPreserveKeys(editor, () => {
2866
- Transforms.splitNodes(editor, {
2867
- match: Text.isText,
2868
- at: { path: op.path, offset: op.offset }
2869
- });
2899
+ Transforms.splitNodes(editor, {
2900
+ match: Text.isText,
2901
+ at: { path: op.path, offset: op.offset }
2870
2902
  }), Transforms.removeNodes(editor, { at: Path.next(op.path) });
2871
2903
  }), editor.onChange();
2872
2904
  return;
2873
2905
  }
2906
+ const deletingAllText = op.offset === 0 && deletingFromTheEnd;
2907
+ if (nodeHasAnnotations && deletingAllText) {
2908
+ const marksWithoutAnnotationMarks = ({
2909
+ ...Editor.marks(editor) || {}
2910
+ }.marks || []).filter((mark) => decorators.includes(mark));
2911
+ Editor.withoutNormalizing(editor, () => {
2912
+ apply2(op), Transforms.setNodes(editor, { marks: marksWithoutAnnotationMarks }, { at: op.path });
2913
+ }), editor.onChange();
2914
+ return;
2915
+ }
2874
2916
  }
2875
2917
  }
2876
2918
  apply2(op);
@@ -3837,10 +3879,8 @@ function useSyncValue(props) {
3837
3879
  debug$5.enabled && debug$5(
3838
3880
  "Validating and inserting new block in the end of the value",
3839
3881
  currentBlock
3840
- ), validation.valid || validation.resolution?.autoResolve ? withPreserveKeys(slateEditor, () => {
3841
- Transforms.insertNodes(slateEditor, currentBlock, {
3842
- at: [currentBlockIndex]
3843
- });
3882
+ ), validation.valid || validation.resolution?.autoResolve ? Transforms.insertNodes(slateEditor, currentBlock, {
3883
+ at: [currentBlockIndex]
3844
3884
  }) : (debug$5("Invalid", validation), change$.next({
3845
3885
  type: "invalidValue",
3846
3886
  resolution: validation.resolution,
@@ -3890,9 +3930,7 @@ function useSyncValue(props) {
3890
3930
  }
3891
3931
  function _replaceBlock(slateEditor, currentBlock, currentBlockIndex) {
3892
3932
  const currentSelection = slateEditor.selection, selectionFocusOnBlock = currentSelection && currentSelection.focus.path[0] === currentBlockIndex;
3893
- selectionFocusOnBlock && Transforms.deselect(slateEditor), Transforms.removeNodes(slateEditor, { at: [currentBlockIndex] }), withPreserveKeys(slateEditor, () => {
3894
- Transforms.insertNodes(slateEditor, currentBlock, { at: [currentBlockIndex] });
3895
- }), slateEditor.onChange(), selectionFocusOnBlock && Transforms.select(slateEditor, currentSelection);
3933
+ selectionFocusOnBlock && Transforms.deselect(slateEditor), Transforms.removeNodes(slateEditor, { at: [currentBlockIndex] }), Transforms.insertNodes(slateEditor, currentBlock, { at: [currentBlockIndex] }), slateEditor.onChange(), selectionFocusOnBlock && Transforms.select(slateEditor, currentSelection);
3896
3934
  }
3897
3935
  function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3898
3936
  if (Transforms.setNodes(slateEditor, currentBlock, {
@@ -3928,15 +3966,11 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3928
3966
  ));
3929
3967
  } else oldBlockChild ? (debug$5("Replacing child", currentBlockChild), Transforms.removeNodes(slateEditor, {
3930
3968
  at: [currentBlockIndex, currentBlockChildIndex]
3931
- }), withPreserveKeys(slateEditor, () => {
3932
- Transforms.insertNodes(slateEditor, currentBlockChild, {
3933
- at: [currentBlockIndex, currentBlockChildIndex]
3934
- });
3935
- }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), withPreserveKeys(slateEditor, () => {
3936
- Transforms.insertNodes(slateEditor, currentBlockChild, {
3937
- at: [currentBlockIndex, currentBlockChildIndex]
3938
- }), slateEditor.onChange();
3939
- }));
3969
+ }), Transforms.insertNodes(slateEditor, currentBlockChild, {
3970
+ at: [currentBlockIndex, currentBlockChildIndex]
3971
+ }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), Transforms.insertNodes(slateEditor, currentBlockChild, {
3972
+ at: [currentBlockIndex, currentBlockChildIndex]
3973
+ }), slateEditor.onChange());
3940
3974
  });
3941
3975
  }
3942
3976
  }