@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.esm.js CHANGED
@@ -714,7 +714,6 @@ function compileType(rawType) {
714
714
  }).get(rawType.name);
715
715
  }
716
716
  const debug$k = debugWithName("operationToPatches");
717
- debug$k.enabled = !1;
718
717
  function createOperationToPatches(types) {
719
718
  const textBlockName = types.block.name;
720
719
  function insertTextPatch(editor, operation, beforeValue) {
@@ -1300,55 +1299,85 @@ function createWithInsertBreak(types) {
1300
1299
  }, editor;
1301
1300
  };
1302
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
+ }
1303
1333
  function createWithMaxBlocks(maxBlocks) {
1304
1334
  return function(editor) {
1305
1335
  const { apply: apply2 } = editor;
1306
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
+ }
1307
1345
  const rows = maxBlocks;
1308
1346
  rows > 0 && editor.children.length >= rows && (operation.type === "insert_node" || operation.type === "split_node") && operation.path.length === 1 || apply2(operation);
1309
1347
  }, editor;
1310
1348
  };
1311
1349
  }
1312
- const PRESERVE_KEYS = /* @__PURE__ */ new WeakMap();
1313
- function withPreserveKeys(editor, fn) {
1314
- const prev = isPreservingKeys(editor);
1315
- PRESERVE_KEYS.set(editor, !0), fn(), PRESERVE_KEYS.set(editor, prev);
1316
- }
1317
- function withoutPreserveKeys(editor, fn) {
1318
- const prev = isPreservingKeys(editor);
1319
- PRESERVE_KEYS.set(editor, !1), fn(), PRESERVE_KEYS.set(editor, prev);
1320
- }
1321
- function isPreservingKeys(editor) {
1322
- return PRESERVE_KEYS.get(editor);
1323
- }
1324
1350
  function createWithObjectKeys(schemaTypes, keyGenerator) {
1325
1351
  return function(editor) {
1326
- PRESERVE_KEYS.set(editor, !1);
1327
1352
  const { apply: apply2, normalizeNode } = editor;
1328
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
+ }
1329
1362
  if (operation.type === "split_node") {
1330
- const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.properties);
1331
1363
  apply2({
1332
1364
  ...operation,
1333
1365
  properties: {
1334
1366
  ...operation.properties,
1335
- ...withNewKey ? { _key: keyGenerator() } : {}
1367
+ _key: keyGenerator()
1336
1368
  }
1337
1369
  });
1338
1370
  return;
1339
1371
  }
1340
- if (operation.type === "insert_node") {
1341
- const withNewKey = !isPreservingKeys(editor) || !("_key" in operation.node);
1342
- if (!Editor.isEditor(operation.node)) {
1343
- apply2({
1344
- ...operation,
1345
- node: {
1346
- ...operation.node,
1347
- ...withNewKey ? { _key: keyGenerator() } : {}
1348
- }
1349
- });
1350
- return;
1351
- }
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;
1352
1381
  }
1353
1382
  apply2(operation);
1354
1383
  }, editor.normalizeNode = (entry) => {
@@ -2238,16 +2267,6 @@ function findBlockAndChildFromPath(editor, path) {
2238
2267
  });
2239
2268
  return child ? { block, child, blockPath, childPath: blockPath?.concat(childIndex) } : { block, blockPath, child: void 0, childPath: void 0 };
2240
2269
  }
2241
- function withRemoteChanges(editor, fn) {
2242
- const prev = isChangingRemotely(editor) || !1;
2243
- IS_PROCESSING_REMOTE_CHANGES.set(editor, !0), fn(), IS_PROCESSING_REMOTE_CHANGES.set(editor, prev);
2244
- }
2245
- function isChangingRemotely(editor) {
2246
- return IS_PROCESSING_REMOTE_CHANGES.get(editor);
2247
- }
2248
- function isChangingLocally(editor) {
2249
- return IS_PROCESSING_LOCAL_CHANGES.get(editor);
2250
- }
2251
2270
  const PATCHING = /* @__PURE__ */ new WeakMap();
2252
2271
  function withoutPatching(editor, fn) {
2253
2272
  const prev = isPatching(editor);
@@ -2328,7 +2347,7 @@ function createWithUndoRedo(options) {
2328
2347
  const reversedOperations = transformedOperations.map(Operation.inverse).reverse();
2329
2348
  try {
2330
2349
  Editor.withoutNormalizing(editor, () => {
2331
- withPreserveKeys(editor, () => {
2350
+ withUndoing(editor, () => {
2332
2351
  withoutSaving(editor, () => {
2333
2352
  reversedOperations.forEach((op) => {
2334
2353
  editor.apply(op);
@@ -2337,7 +2356,7 @@ function createWithUndoRedo(options) {
2337
2356
  });
2338
2357
  }), editor.normalize(), editor.onChange();
2339
2358
  } catch (err) {
2340
- 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();
2341
2360
  return;
2342
2361
  }
2343
2362
  editor.history.redos.push(step), editor.history.undos.pop();
@@ -2361,7 +2380,7 @@ function createWithUndoRedo(options) {
2361
2380
  });
2362
2381
  try {
2363
2382
  Editor.withoutNormalizing(editor, () => {
2364
- withPreserveKeys(editor, () => {
2383
+ withRedoing(editor, () => {
2365
2384
  withoutSaving(editor, () => {
2366
2385
  transformedOperations.forEach((op) => {
2367
2386
  editor.apply(op);
@@ -2370,7 +2389,7 @@ function createWithUndoRedo(options) {
2370
2389
  });
2371
2390
  }), editor.normalize(), editor.onChange();
2372
2391
  } catch (err) {
2373
- 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();
2374
2393
  return;
2375
2394
  }
2376
2395
  editor.history.undos.push(step), editor.history.redos.pop();
@@ -2484,10 +2503,8 @@ function createWithPatches({
2484
2503
  Editor.withoutNormalizing(editor, () => {
2485
2504
  withoutPatching(editor, () => {
2486
2505
  withoutSaving(editor, () => {
2487
- withPreserveKeys(editor, () => {
2488
- patches.forEach((patch) => {
2489
- debug$g.enabled && debug$g(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
2490
- });
2506
+ patches.forEach((patch) => {
2507
+ debug$g.enabled && debug$g(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
2491
2508
  });
2492
2509
  });
2493
2510
  });
@@ -2584,6 +2601,14 @@ function createWithPlaceholderBlock() {
2584
2601
  return function(editor) {
2585
2602
  const { apply: apply2 } = editor;
2586
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
+ }
2587
2612
  if (op.type === "remove_node") {
2588
2613
  const node = op.node;
2589
2614
  if (op.path[0] === 0 && Editor.isVoid(editor, node)) {
@@ -2745,7 +2770,7 @@ function isPortableTextBlock(node) {
2745
2770
  );
2746
2771
  }
2747
2772
  const debug$c = debugWithName("plugin:withPortableTextMarkModel");
2748
- function createWithPortableTextMarkModel(types, change$) {
2773
+ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2749
2774
  return function(editor) {
2750
2775
  const { apply: apply2, normalizeNode } = editor, decorators = types.decorators.map((t) => t.value), forceNewSelection = () => {
2751
2776
  editor.selection && (Transforms.select(editor, { ...editor.selection }), editor.selection = { ...editor.selection });
@@ -2823,6 +2848,14 @@ function createWithPortableTextMarkModel(types, change$) {
2823
2848
  ), editor.onChange());
2824
2849
  }
2825
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
+ }
2826
2859
  if (op.type === "insert_text") {
2827
2860
  const { selection } = editor;
2828
2861
  if (selection && Range.isCollapsed(selection) && Editor.marks(editor)?.marks?.some((mark) => !decorators.includes(mark))) {
@@ -2835,18 +2868,15 @@ function createWithPortableTextMarkModel(types, change$) {
2835
2868
  })
2836
2869
  )[0] || [void 0];
2837
2870
  if (Text.isText(node) && node.text.length === selection.focus.offset && Array.isArray(node.marks) && node.marks.length > 0) {
2838
- apply2(op), Transforms.splitNodes(editor, {
2839
- match: Text.isText,
2840
- at: { ...selection.focus, offset: selection.focus.offset }
2841
- });
2842
2871
  const marksWithoutAnnotationMarks = ({
2843
2872
  ...Editor.marks(editor) || {}
2844
2873
  }.marks || []).filter((mark) => decorators.includes(mark));
2845
- Transforms.setNodes(
2846
- editor,
2847
- { marks: marksWithoutAnnotationMarks },
2848
- { at: Path.next(selection.focus.path) }
2849
- ), debug$c("Inserting text at end of annotation");
2874
+ Transforms.insertNodes(editor, {
2875
+ _type: "span",
2876
+ _key: keyGenerator(),
2877
+ text: op.text,
2878
+ marks: marksWithoutAnnotationMarks
2879
+ }), debug$c("Inserting text at end of annotation");
2850
2880
  return;
2851
2881
  }
2852
2882
  }
@@ -2866,15 +2896,23 @@ function createWithPortableTextMarkModel(types, change$) {
2866
2896
  ), deletingPartOfTheNode = op.offset !== 0, deletingFromTheEnd = op.offset + op.text.length === node.text.length;
2867
2897
  if (nodeHasAnnotations && deletingPartOfTheNode && deletingFromTheEnd) {
2868
2898
  Editor.withoutNormalizing(editor, () => {
2869
- withoutPreserveKeys(editor, () => {
2870
- Transforms.splitNodes(editor, {
2871
- match: Text.isText,
2872
- at: { path: op.path, offset: op.offset }
2873
- });
2899
+ Transforms.splitNodes(editor, {
2900
+ match: Text.isText,
2901
+ at: { path: op.path, offset: op.offset }
2874
2902
  }), Transforms.removeNodes(editor, { at: Path.next(op.path) });
2875
2903
  }), editor.onChange();
2876
2904
  return;
2877
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
+ }
2878
2916
  }
2879
2917
  }
2880
2918
  apply2(op);
@@ -2965,18 +3003,22 @@ function createWithPortableTextMarkModel(types, change$) {
2965
3003
  };
2966
3004
  function mergeSpans(editor) {
2967
3005
  const { selection } = editor;
2968
- if (selection)
2969
- for (const [node, path] of Array.from(
3006
+ if (selection) {
3007
+ const textNodesInSelection = Array.from(
2970
3008
  Editor.nodes(editor, {
2971
- at: Editor.range(editor, [selection.anchor.path[0]], [selection.focus.path[0]])
3009
+ at: Editor.range(editor, [selection.anchor.path[0]], [selection.focus.path[0]]),
3010
+ match: Text.isText,
3011
+ reverse: !0
2972
3012
  })
2973
- ).reverse()) {
3013
+ );
3014
+ for (const [node, path] of textNodesInSelection) {
2974
3015
  const [parent] = path.length > 1 ? Editor.node(editor, Path.parent(path)) : [void 0], nextPath = [path[0], path[1] + 1];
2975
3016
  if (editor.isTextBlock(parent)) {
2976
3017
  const nextNode = parent.children[nextPath[1]];
2977
- Text.isText(node) && Text.isText(nextNode) && isEqual(nextNode.marks, node.marks) && (debug$c("Merging spans"), Transforms.mergeNodes(editor, { at: nextPath, voids: !0 }), editor.onChange());
3018
+ Text.isText(nextNode) && isEqual(nextNode.marks, node.marks) && (debug$c("Merging spans"), Transforms.mergeNodes(editor, { at: nextPath, voids: !0 }), editor.onChange());
2978
3019
  }
2979
3020
  }
3021
+ }
2980
3022
  }
2981
3023
  }
2982
3024
  const debug$b = debugWithName("plugin:withPortableTextSelections"), debugVerbose$2 = debug$b.enabled && !1;
@@ -3657,7 +3699,11 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
3657
3699
  readOnly,
3658
3700
  patches$,
3659
3701
  blockSchemaType: schemaTypes.block
3660
- }), withPortableTextMarkModel = createWithPortableTextMarkModel(schemaTypes, change$), withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes), withUtils = createWithUtils({ keyGenerator, schemaTypes, portableTextEditor }), withPortableTextSelections = createWithPortableTextSelections(change$, schemaTypes);
3702
+ }), withPortableTextMarkModel = createWithPortableTextMarkModel(
3703
+ schemaTypes,
3704
+ change$,
3705
+ keyGenerator
3706
+ ), withPortableTextBlockStyle = createWithPortableTextBlockStyle(schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(), withInsertBreak = createWithInsertBreak(schemaTypes), withUtils = createWithUtils({ keyGenerator, schemaTypes, portableTextEditor }), withPortableTextSelections = createWithPortableTextSelections(change$, schemaTypes);
3661
3707
  return e.destroy = () => {
3662
3708
  const originalFunctions = originalFnMap.get(e);
3663
3709
  if (!originalFunctions)
@@ -3833,10 +3879,8 @@ function useSyncValue(props) {
3833
3879
  debug$5.enabled && debug$5(
3834
3880
  "Validating and inserting new block in the end of the value",
3835
3881
  currentBlock
3836
- ), validation.valid || validation.resolution?.autoResolve ? withPreserveKeys(slateEditor, () => {
3837
- Transforms.insertNodes(slateEditor, currentBlock, {
3838
- at: [currentBlockIndex]
3839
- });
3882
+ ), validation.valid || validation.resolution?.autoResolve ? Transforms.insertNodes(slateEditor, currentBlock, {
3883
+ at: [currentBlockIndex]
3840
3884
  }) : (debug$5("Invalid", validation), change$.next({
3841
3885
  type: "invalidValue",
3842
3886
  resolution: validation.resolution,
@@ -3886,9 +3930,7 @@ function useSyncValue(props) {
3886
3930
  }
3887
3931
  function _replaceBlock(slateEditor, currentBlock, currentBlockIndex) {
3888
3932
  const currentSelection = slateEditor.selection, selectionFocusOnBlock = currentSelection && currentSelection.focus.path[0] === currentBlockIndex;
3889
- selectionFocusOnBlock && Transforms.deselect(slateEditor), Transforms.removeNodes(slateEditor, { at: [currentBlockIndex] }), withPreserveKeys(slateEditor, () => {
3890
- Transforms.insertNodes(slateEditor, currentBlock, { at: [currentBlockIndex] });
3891
- }), 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);
3892
3934
  }
3893
3935
  function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3894
3936
  if (Transforms.setNodes(slateEditor, currentBlock, {
@@ -3924,15 +3966,11 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
3924
3966
  ));
3925
3967
  } else oldBlockChild ? (debug$5("Replacing child", currentBlockChild), Transforms.removeNodes(slateEditor, {
3926
3968
  at: [currentBlockIndex, currentBlockChildIndex]
3927
- }), withPreserveKeys(slateEditor, () => {
3928
- Transforms.insertNodes(slateEditor, currentBlockChild, {
3929
- at: [currentBlockIndex, currentBlockChildIndex]
3930
- });
3931
- }), slateEditor.onChange()) : oldBlockChild || (debug$5("Inserting new child", currentBlockChild), withPreserveKeys(slateEditor, () => {
3932
- Transforms.insertNodes(slateEditor, currentBlockChild, {
3933
- at: [currentBlockIndex, currentBlockChildIndex]
3934
- }), slateEditor.onChange();
3935
- }));
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());
3936
3974
  });
3937
3975
  }
3938
3976
  }