@portabletext/editor 1.0.12 → 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
@@ -698,7 +698,6 @@ function compileType(rawType) {
698
698
  }).get(rawType.name);
699
699
  }
700
700
  const debug$k = debugWithName("operationToPatches");
701
- debug$k.enabled = !1;
702
701
  function createOperationToPatches(types2) {
703
702
  const textBlockName = types2.block.name;
704
703
  function insertTextPatch(editor, operation, beforeValue) {
@@ -1284,55 +1283,85 @@ function createWithInsertBreak(types2) {
1284
1283
  }, editor;
1285
1284
  };
1286
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
+ }
1287
1317
  function createWithMaxBlocks(maxBlocks) {
1288
1318
  return function(editor) {
1289
1319
  const { apply: apply2 } = editor;
1290
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
+ }
1291
1329
  const rows = maxBlocks;
1292
1330
  rows > 0 && editor.children.length >= rows && (operation.type === "insert_node" || operation.type === "split_node") && operation.path.length === 1 || apply2(operation);
1293
1331
  }, editor;
1294
1332
  };
1295
1333
  }
1296
- const PRESERVE_KEYS = /* @__PURE__ */ new WeakMap();
1297
- function withPreserveKeys(editor, fn) {
1298
- const prev = isPreservingKeys(editor);
1299
- PRESERVE_KEYS.set(editor, !0), fn(), PRESERVE_KEYS.set(editor, prev);
1300
- }
1301
- function withoutPreserveKeys(editor, fn) {
1302
- const prev = isPreservingKeys(editor);
1303
- PRESERVE_KEYS.set(editor, !1), fn(), PRESERVE_KEYS.set(editor, prev);
1304
- }
1305
- function isPreservingKeys(editor) {
1306
- return PRESERVE_KEYS.get(editor);
1307
- }
1308
1334
  function createWithObjectKeys(schemaTypes, keyGenerator) {
1309
1335
  return function(editor) {
1310
- PRESERVE_KEYS.set(editor, !1);
1311
1336
  const { apply: apply2, normalizeNode } = editor;
1312
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
+ }
1313
1346
  if (operation.type === "split_node") {
1314
- const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.properties);
1315
1347
  apply2({
1316
1348
  ...operation,
1317
1349
  properties: {
1318
1350
  ...operation.properties,
1319
- ...withNewKey ? { _key: keyGenerator() } : {}
1351
+ _key: keyGenerator()
1320
1352
  }
1321
1353
  });
1322
1354
  return;
1323
1355
  }
1324
- if (operation.type === "insert_node") {
1325
- const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.node);
1326
- if (!slate.Editor.isEditor(operation.node)) {
1327
- apply2({
1328
- ...operation,
1329
- node: {
1330
- ...operation.node,
1331
- ...withNewKey ? { _key: keyGenerator() } : {}
1332
- }
1333
- });
1334
- return;
1335
- }
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;
1336
1365
  }
1337
1366
  apply2(operation);
1338
1367
  }, editor.normalizeNode = (entry) => {
@@ -2222,16 +2251,6 @@ function findBlockAndChildFromPath(editor, path) {
2222
2251
  });
2223
2252
  return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
2224
2253
  }
2225
- function withRemoteChanges(editor, fn) {
2226
- const prev = isChangingRemotely(editor) || !1;
2227
- IS_PROCESSING_REMOTE_CHANGES.set(editor, !0), fn(), IS_PROCESSING_REMOTE_CHANGES.set(editor, prev);
2228
- }
2229
- function isChangingRemotely(editor) {
2230
- return IS_PROCESSING_REMOTE_CHANGES.get(editor);
2231
- }
2232
- function isChangingLocally(editor) {
2233
- return IS_PROCESSING_LOCAL_CHANGES.get(editor);
2234
- }
2235
2254
  const PATCHING = /* @__PURE__ */ new WeakMap();
2236
2255
  function withoutPatching(editor, fn) {
2237
2256
  const prev = isPatching(editor);
@@ -2312,7 +2331,7 @@ function createWithUndoRedo(options) {
2312
2331
  const reversedOperations = transformedOperations.map(slate.Operation.inverse).reverse();
2313
2332
  try {
2314
2333
  slate.Editor.withoutNormalizing(editor, () => {
2315
- withPreserveKeys(editor, () => {
2334
+ withUndoing(editor, () => {
2316
2335
  withoutSaving(editor, () => {
2317
2336
  reversedOperations.forEach((op) => {
2318
2337
  editor.apply(op);
@@ -2321,7 +2340,7 @@ function createWithUndoRedo(options) {
2321
2340
  });
2322
2341
  }), editor.normalize(), editor.onChange();
2323
2342
  } catch (err) {
2324
- 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();
2325
2344
  return;
2326
2345
  }
2327
2346
  editor.history.redos.push(step), editor.history.undos.pop();
@@ -2345,7 +2364,7 @@ function createWithUndoRedo(options) {
2345
2364
  });
2346
2365
  try {
2347
2366
  slate.Editor.withoutNormalizing(editor, () => {
2348
- withPreserveKeys(editor, () => {
2367
+ withRedoing(editor, () => {
2349
2368
  withoutSaving(editor, () => {
2350
2369
  transformedOperations.forEach((op) => {
2351
2370
  editor.apply(op);
@@ -2354,7 +2373,7 @@ function createWithUndoRedo(options) {
2354
2373
  });
2355
2374
  }), editor.normalize(), editor.onChange();
2356
2375
  } catch (err) {
2357
- 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();
2358
2377
  return;
2359
2378
  }
2360
2379
  editor.history.undos.push(step), editor.history.redos.pop();
@@ -2468,10 +2487,8 @@ function createWithPatches({
2468
2487
  slate.Editor.withoutNormalizing(editor, () => {
2469
2488
  withoutPatching(editor, () => {
2470
2489
  withoutSaving(editor, () => {
2471
- withPreserveKeys(editor, () => {
2472
- patches2.forEach((patch) => {
2473
- debug$g.enabled && debug$g(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
2474
- });
2490
+ patches2.forEach((patch) => {
2491
+ debug$g.enabled && debug$g(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
2475
2492
  });
2476
2493
  });
2477
2494
  });
@@ -2568,6 +2585,14 @@ function createWithPlaceholderBlock() {
2568
2585
  return function(editor) {
2569
2586
  const { apply: apply2 } = editor;
2570
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
+ }
2571
2596
  if (op.type === "remove_node") {
2572
2597
  const node = op.node;
2573
2598
  if (op.path[0] === 0 && slate.Editor.isVoid(editor, node)) {
@@ -2729,7 +2754,7 @@ function isPortableTextBlock(node) {
2729
2754
  );
2730
2755
  }
2731
2756
  const debug$c = debugWithName("plugin:withPortableTextMarkModel");
2732
- function createWithPortableTextMarkModel(types2, change$) {
2757
+ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2733
2758
  return function(editor) {
2734
2759
  const { apply: apply2, normalizeNode } = editor, decorators = types2.decorators.map((t) => t.value), forceNewSelection = () => {
2735
2760
  editor.selection && (slate.Transforms.select(editor, { ...editor.selection }), editor.selection = { ...editor.selection });
@@ -2807,6 +2832,14 @@ function createWithPortableTextMarkModel(types2, change$) {
2807
2832
  ), editor.onChange());
2808
2833
  }
2809
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
+ }
2810
2843
  if (op.type === "insert_text") {
2811
2844
  const { selection } = editor;
2812
2845
  if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
@@ -2819,18 +2852,15 @@ function createWithPortableTextMarkModel(types2, change$) {
2819
2852
  })
2820
2853
  )[0] || [void 0];
2821
2854
  if (slate.Text.isText(node) && node.text.length === selection.focus.offset && Array.isArray(node.marks) && node.marks.length > 0) {
2822
- apply2(op), slate.Transforms.splitNodes(editor, {
2823
- match: slate.Text.isText,
2824
- at: { ...selection.focus, offset: selection.focus.offset }
2825
- });
2826
2855
  const marksWithoutAnnotationMarks = ({
2827
2856
  ...slate.Editor.marks(editor) || {}
2828
2857
  }.marks || []).filter((mark) => decorators.includes(mark));
2829
- slate.Transforms.setNodes(
2830
- editor,
2831
- { marks: marksWithoutAnnotationMarks },
2832
- { at: slate.Path.next(selection.focus.path) }
2833
- ), debug$c("Inserting text at end of annotation");
2858
+ slate.Transforms.insertNodes(editor, {
2859
+ _type: "span",
2860
+ _key: keyGenerator(),
2861
+ text: op.text,
2862
+ marks: marksWithoutAnnotationMarks
2863
+ }), debug$c("Inserting text at end of annotation");
2834
2864
  return;
2835
2865
  }
2836
2866
  }
@@ -2850,15 +2880,23 @@ function createWithPortableTextMarkModel(types2, change$) {
2850
2880
  ), deletingPartOfTheNode = op.offset !== 0, deletingFromTheEnd = op.offset + op.text.length === node.text.length;
2851
2881
  if (nodeHasAnnotations && deletingPartOfTheNode && deletingFromTheEnd) {
2852
2882
  slate.Editor.withoutNormalizing(editor, () => {
2853
- withoutPreserveKeys(editor, () => {
2854
- slate.Transforms.splitNodes(editor, {
2855
- match: slate.Text.isText,
2856
- at: { path: op.path, offset: op.offset }
2857
- });
2883
+ slate.Transforms.splitNodes(editor, {
2884
+ match: slate.Text.isText,
2885
+ at: { path: op.path, offset: op.offset }
2858
2886
  }), slate.Transforms.removeNodes(editor, { at: slate.Path.next(op.path) });
2859
2887
  }), editor.onChange();
2860
2888
  return;
2861
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
+ }
2862
2900
  }
2863
2901
  }
2864
2902
  apply2(op);
@@ -2949,18 +2987,22 @@ function createWithPortableTextMarkModel(types2, change$) {
2949
2987
  };
2950
2988
  function mergeSpans(editor) {
2951
2989
  const { selection } = editor;
2952
- if (selection)
2953
- for (const [node, path] of Array.from(
2990
+ if (selection) {
2991
+ const textNodesInSelection = Array.from(
2954
2992
  slate.Editor.nodes(editor, {
2955
- at: slate.Editor.range(editor, [selection.anchor.path[0]], [selection.focus.path[0]])
2993
+ at: slate.Editor.range(editor, [selection.anchor.path[0]], [selection.focus.path[0]]),
2994
+ match: slate.Text.isText,
2995
+ reverse: !0
2956
2996
  })
2957
- ).reverse()) {
2997
+ );
2998
+ for (const [node, path] of textNodesInSelection) {
2958
2999
  const [parent] = path.length > 1 ? slate.Editor.node(editor, slate.Path.parent(path)) : [void 0], nextPath = [path[0], path[1] + 1];
2959
3000
  if (editor.isTextBlock(parent)) {
2960
3001
  const nextNode = parent.children[nextPath[1]];
2961
- slate.Text.isText(node) && slate.Text.isText(nextNode) && isEqual__default.default(nextNode.marks, node.marks) && (debug$c("Merging spans"), slate.Transforms.mergeNodes(editor, { at: nextPath, voids: !0 }), editor.onChange());
3002
+ slate.Text.isText(nextNode) && isEqual__default.default(nextNode.marks, node.marks) && (debug$c("Merging spans"), slate.Transforms.mergeNodes(editor, { at: nextPath, voids: !0 }), editor.onChange());
2962
3003
  }
2963
3004
  }
3005
+ }
2964
3006
  }
2965
3007
  }
2966
3008
  const debug$b = debugWithName("plugin:withPortableTextSelections"), debugVerbose$2 = debug$b.enabled && !1;
@@ -3641,7 +3683,11 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3641
3683
  readOnly,
3642
3684
  patches$,
3643
3685
  blockSchemaType: schemaTypes.block
3644
- }), withPortableTextMarkModel = createWithPortableTextMarkModel(schemaTypes, change$), withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes), withUtils = createWithUtils({ keyGenerator, schemaTypes, portableTextEditor }), withPortableTextSelections = createWithPortableTextSelections(change$, schemaTypes);
3686
+ }), withPortableTextMarkModel = createWithPortableTextMarkModel(
3687
+ schemaTypes,
3688
+ change$,
3689
+ keyGenerator
3690
+ ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes), withUtils = createWithUtils({ keyGenerator, schemaTypes, portableTextEditor }), withPortableTextSelections = createWithPortableTextSelections(change$, schemaTypes);
3645
3691
  return e.destroy = () => {
3646
3692
  const originalFunctions = originalFnMap.get(e);
3647
3693
  if (!originalFunctions)
@@ -3817,10 +3863,8 @@ function useSyncValue(props) {
3817
3863
  debug$5.enabled && debug$5(
3818
3864
  "Validating and inserting new block in the end of the value",
3819
3865
  currentBlock
3820
- ), validation.valid || validation.resolution?.autoResolve ? withPreserveKeys(slateEditor, () => {
3821
- slate.Transforms.insertNodes(slateEditor, currentBlock, {
3822
- at: [currentBlockIndex]
3823
- });
3866
+ ), validation.valid || validation.resolution?.autoResolve ? slate.Transforms.insertNodes(slateEditor, currentBlock, {
3867
+ at: [currentBlockIndex]
3824
3868
  }) : (debug$5("Invalid", validation), change$.next({
3825
3869
  type: "invalidValue",
3826
3870
  resolution: validation.resolution,
@@ -3870,9 +3914,7 @@ function useSyncValue(props) {
3870
3914
  }
3871
3915
  function _replaceBlock(slateEditor, currentBlock, currentBlockIndex) {
3872
3916
  const currentSelection = slateEditor.selection, selectionFocusOnBlock = currentSelection && currentSelection.focus.path[0] === currentBlockIndex;
3873
- selectionFocusOnBlock && slate.Transforms.deselect(slateEditor), slate.Transforms.removeNodes(slateEditor, { at: [currentBlockIndex] }), withPreserveKeys(slateEditor, () => {
3874
- slate.Transforms.insertNodes(slateEditor, currentBlock, { at: [currentBlockIndex] });
3875
- }), 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);
3876
3918
  }
3877
3919
  function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3878
3920
  if (slate.Transforms.setNodes(slateEditor, currentBlock, {
@@ -3908,15 +3950,11 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3908
3950
  ));
3909
3951
  } else oldBlockChild ? (debug$5("Replacing child", currentBlockChild), slate.Transforms.removeNodes(slateEditor, {
3910
3952
  at: [currentBlockIndex, currentBlockChildIndex]
3911
- }), withPreserveKeys(slateEditor, () => {
3912
- slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
3913
- at: [currentBlockIndex, currentBlockChildIndex]
3914
- });
3915
- }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), withPreserveKeys(slateEditor, () => {
3916
- slate.Transforms.insertNodes(slateEditor, currentBlockChild, {
3917
- at: [currentBlockIndex, currentBlockChildIndex]
3918
- }), slateEditor.onChange();
3919
- }));
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());
3920
3958
  });
3921
3959
  }
3922
3960
  }