@portabletext/editor 1.52.1 → 1.52.2

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
@@ -7,7 +7,7 @@ import { useSelector, useActorRef } from "@xstate/react";
7
7
  import noop from "lodash/noop.js";
8
8
  import { Element as Element$1, Text, Range, Editor, Node, Point, Transforms, Path, Operation, createEditor } from "slate";
9
9
  import { useSlateStatic, useSelected, withReact, ReactEditor, Slate, useSlate, Editable } from "slate-react";
10
- import debug$h from "debug";
10
+ import debug$g from "debug";
11
11
  import { DOMEditor, isDOMNode } from "slate-dom";
12
12
  import { getBlockStartPoint, getBlockKeyFromSelectionPoint, isTextBlock, parseBlock, sliceBlocks, parseAnnotation, blockOffsetToSpanSelectionPoint, parseInlineObject, isKeyedSegment, isSpan$1 as isSpan, isListBlock, isTypedObject, getSelectionStartPoint, getSelectionEndPoint, getTextBlockText, parseBlocks } from "./_chunks-es/selection-point.js";
13
13
  import { getBlockEndPoint, isEmptyTextBlock, isEqualSelectionPoints } from "./_chunks-es/util.is-equal-selection-points.js";
@@ -47,10 +47,10 @@ function EditorEventListener(props) {
47
47
  return $[3] !== editor ? (t1 = [editor], $[3] = editor, $[4] = t1) : t1 = $[4], useEffect(t0, t1), null;
48
48
  }
49
49
  const rootName = "sanity-pte:";
50
- debug$h(rootName);
50
+ debug$g(rootName);
51
51
  function debugWithName(name) {
52
52
  const namespace = `${rootName}${name}`;
53
- return debug$h && debug$h.enabled(namespace) ? debug$h(namespace) : debug$h(rootName);
53
+ return debug$g && debug$g.enabled(namespace) ? debug$g(namespace) : debug$g(rootName);
54
54
  }
55
55
  const VOID_CHILD_KEY = "void-child";
56
56
  function keepObjectEquality(object, keyMap) {
@@ -1512,7 +1512,7 @@ function getEditorSnapshot({
1512
1512
  }
1513
1513
  };
1514
1514
  }
1515
- const debug$g = debugWithName("plugin:withPortableTextMarkModel");
1515
+ const debug$f = debugWithName("plugin:withPortableTextMarkModel");
1516
1516
  function createWithPortableTextMarkModel(editorActor) {
1517
1517
  return function(editor) {
1518
1518
  const {
@@ -1526,7 +1526,7 @@ function createWithPortableTextMarkModel(editorActor) {
1526
1526
  for (const [child, childPath] of children) {
1527
1527
  const nextNode = node.children[childPath[1] + 1];
1528
1528
  if (editor.isTextSpan(child) && editor.isTextSpan(nextNode) && child.marks?.every((mark) => nextNode.marks?.includes(mark)) && nextNode.marks?.every((mark) => child.marks?.includes(mark))) {
1529
- debug$g("Merging spans", JSON.stringify(child, null, 2), JSON.stringify(nextNode, null, 2)), editorActor.send({
1529
+ debug$f("Merging spans", JSON.stringify(child, null, 2), JSON.stringify(nextNode, null, 2)), editorActor.send({
1530
1530
  type: "normalizing"
1531
1531
  }), Transforms.mergeNodes(editor, {
1532
1532
  at: [childPath[0], childPath[1] + 1],
@@ -1539,7 +1539,7 @@ function createWithPortableTextMarkModel(editorActor) {
1539
1539
  }
1540
1540
  }
1541
1541
  if (editor.isTextBlock(node) && !Array.isArray(node.markDefs)) {
1542
- debug$g("Adding .markDefs to block node"), editorActor.send({
1542
+ debug$f("Adding .markDefs to block node"), editorActor.send({
1543
1543
  type: "normalizing"
1544
1544
  }), Transforms.setNodes(editor, {
1545
1545
  markDefs: []
@@ -1551,7 +1551,7 @@ function createWithPortableTextMarkModel(editorActor) {
1551
1551
  return;
1552
1552
  }
1553
1553
  if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
1554
- debug$g("Adding .marks to span node"), editorActor.send({
1554
+ debug$f("Adding .marks to span node"), editorActor.send({
1555
1555
  type: "normalizing"
1556
1556
  }), Transforms.setNodes(editor, {
1557
1557
  marks: []
@@ -1565,7 +1565,7 @@ function createWithPortableTextMarkModel(editorActor) {
1565
1565
  if (editor.isTextSpan(node)) {
1566
1566
  const blockPath = Path.parent(path), [block] = Editor.node(editor, blockPath), decorators2 = editorActor.getSnapshot().context.schema.decorators.map((decorator) => decorator.name), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
1567
1567
  if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
1568
- debug$g("Removing annotations from empty span node"), editorActor.send({
1568
+ debug$f("Removing annotations from empty span node"), editorActor.send({
1569
1569
  type: "normalizing"
1570
1570
  }), Transforms.setNodes(editor, {
1571
1571
  marks: node.marks?.filter((mark) => decorators2.includes(mark))
@@ -1583,7 +1583,7 @@ function createWithPortableTextMarkModel(editorActor) {
1583
1583
  if (editor.isTextSpan(child)) {
1584
1584
  const marks = child.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !node.markDefs?.find((def) => def._key === mark));
1585
1585
  if (orphanedAnnotations.length > 0) {
1586
- debug$g("Removing orphaned annotations from span node"), editorActor.send({
1586
+ debug$f("Removing orphaned annotations from span node"), editorActor.send({
1587
1587
  type: "normalizing"
1588
1588
  }), Transforms.setNodes(editor, {
1589
1589
  marks: marks.filter((mark) => !orphanedAnnotations.includes(mark))
@@ -1601,7 +1601,7 @@ function createWithPortableTextMarkModel(editorActor) {
1601
1601
  if (editor.isTextBlock(block)) {
1602
1602
  const decorators2 = editorActor.getSnapshot().context.schema.decorators.map((decorator) => decorator.name), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
1603
1603
  if (orphanedAnnotations.length > 0) {
1604
- debug$g("Removing orphaned annotations from span node"), editorActor.send({
1604
+ debug$f("Removing orphaned annotations from span node"), editorActor.send({
1605
1605
  type: "normalizing"
1606
1606
  }), Transforms.setNodes(editor, {
1607
1607
  marks: marks.filter((mark) => !orphanedAnnotations.includes(mark))
@@ -1619,7 +1619,7 @@ function createWithPortableTextMarkModel(editorActor) {
1619
1619
  for (const markDef of markDefs)
1620
1620
  markDefKeys.has(markDef._key) || (markDefKeys.add(markDef._key), newMarkDefs.push(markDef));
1621
1621
  if (markDefs.length !== newMarkDefs.length) {
1622
- debug$g("Removing duplicate markDefs"), editorActor.send({
1622
+ debug$f("Removing duplicate markDefs"), editorActor.send({
1623
1623
  type: "normalizing"
1624
1624
  }), Transforms.setNodes(editor, {
1625
1625
  markDefs: newMarkDefs
@@ -1634,7 +1634,7 @@ function createWithPortableTextMarkModel(editorActor) {
1634
1634
  if (editor.isTextBlock(node) && !editor.operations.some((op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1)) {
1635
1635
  const newMarkDefs = (node.markDefs || []).filter((def) => node.children.find((child) => Text.isText(child) && Array.isArray(child.marks) && child.marks.includes(def._key)));
1636
1636
  if (node.markDefs && !isEqual(newMarkDefs, node.markDefs)) {
1637
- debug$g("Removing markDef not in use"), editorActor.send({
1637
+ debug$f("Removing markDef not in use"), editorActor.send({
1638
1638
  type: "normalizing"
1639
1639
  }), Transforms.setNodes(editor, {
1640
1640
  markDefs: newMarkDefs
@@ -1787,7 +1787,7 @@ function createWithPortableTextMarkModel(editorActor) {
1787
1787
  const [targetBlock, targetPath] = Editor.node(editor, [op.path[0] - 1]);
1788
1788
  if (editor.isTextBlock(targetBlock)) {
1789
1789
  const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq([...oldDefs, ...op.properties.markDefs]);
1790
- debug$g("Copying markDefs over to merged block", op), Transforms.setNodes(editor, {
1790
+ debug$f("Copying markDefs over to merged block", op), Transforms.setNodes(editor, {
1791
1791
  markDefs: newMarkDefs
1792
1792
  }, {
1793
1793
  at: targetPath,
@@ -2497,7 +2497,7 @@ function withUndoStep(editor, fn) {
2497
2497
  function getCurrentUndoStepId(editor) {
2498
2498
  return CURRENT_UNDO_STEP.get(editor)?.undoStepId;
2499
2499
  }
2500
- const debug$f = debugWithName("plugin:withUndoRedo"), SAVING = /* @__PURE__ */ new WeakMap(), REMOTE_PATCHES = /* @__PURE__ */ new WeakMap(), UNDO_STEP_LIMIT = 1e3, isSaving = (editor) => {
2500
+ const debug$e = debugWithName("plugin:withUndoRedo"), SAVING = /* @__PURE__ */ new WeakMap(), REMOTE_PATCHES = /* @__PURE__ */ new WeakMap(), UNDO_STEP_LIMIT = 1e3, isSaving = (editor) => {
2501
2501
  const state = SAVING.get(editor);
2502
2502
  return state === void 0 ? !0 : state;
2503
2503
  }, getRemotePatches = (editor) => (REMOTE_PATCHES.get(editor) || REMOTE_PATCHES.set(editor, []), REMOTE_PATCHES.get(editor) || []);
@@ -2510,7 +2510,7 @@ function createWithUndoRedo(options) {
2510
2510
  const remotePatches = getRemotePatches(editor);
2511
2511
  let previousUndoStepId = getCurrentUndoStepId(editor);
2512
2512
  options.subscriptions.push(() => {
2513
- debug$f("Subscribing to patches");
2513
+ debug$e("Subscribing to patches");
2514
2514
  const sub = editorActor.on("patches", ({
2515
2515
  patches,
2516
2516
  snapshot
@@ -2519,7 +2519,7 @@ function createWithUndoRedo(options) {
2519
2519
  patches.forEach((patch) => {
2520
2520
  if (!reset && patch.origin !== "local" && remotePatches) {
2521
2521
  if (patch.type === "unset" && patch.path.length === 0) {
2522
- debug$f("Someone else cleared the content, resetting undo/redo history"), editor.history = {
2522
+ debug$e("Someone else cleared the content, resetting undo/redo history"), editor.history = {
2523
2523
  undos: [],
2524
2524
  redos: []
2525
2525
  }, remotePatches.splice(0, remotePatches.length), SAVING.set(editor, !0), reset = !0;
@@ -2535,7 +2535,7 @@ function createWithUndoRedo(options) {
2535
2535
  }), previousSnapshot = snapshot;
2536
2536
  });
2537
2537
  return () => {
2538
- debug$f("Unsubscribing to patches"), sub.unsubscribe();
2538
+ debug$e("Unsubscribing to patches"), sub.unsubscribe();
2539
2539
  };
2540
2540
  }), editor.history = {
2541
2541
  undos: [],
@@ -2574,7 +2574,7 @@ function createWithUndoRedo(options) {
2574
2574
  operations: [...editor.selection === null ? [] : [createSelectOperation(editor)], op],
2575
2575
  timestamp: /* @__PURE__ */ new Date()
2576
2576
  };
2577
- undos.push(newStep), debug$f("Created new undo step", step);
2577
+ undos.push(newStep), debug$e("Created new undo step", step);
2578
2578
  }
2579
2579
  for (; undos.length > UNDO_STEP_LIMIT; )
2580
2580
  undos.shift();
@@ -2592,7 +2592,7 @@ const historyUndoOperationImplementation = ({
2592
2592
  } = editor.history, remotePatches = getRemotePatches(editor);
2593
2593
  if (undos.length > 0) {
2594
2594
  const step = undos[undos.length - 1];
2595
- if (debug$f("Undoing", step), step.operations.length > 0) {
2595
+ if (debug$e("Undoing", step), step.operations.length > 0) {
2596
2596
  const otherPatches = remotePatches.filter((item) => item.time >= step.timestamp);
2597
2597
  let transformedOperations = step.operations;
2598
2598
  otherPatches.forEach((item) => {
@@ -2610,7 +2610,7 @@ const historyUndoOperationImplementation = ({
2610
2610
  });
2611
2611
  });
2612
2612
  } catch (err) {
2613
- debug$f("Could not perform undo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = {
2613
+ debug$e("Could not perform undo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = {
2614
2614
  undos: [],
2615
2615
  redos: []
2616
2616
  }, SAVING.set(editor, !0), setIsUndoing(editor, !1), editor.onChange();
@@ -2627,7 +2627,7 @@ const historyUndoOperationImplementation = ({
2627
2627
  } = editor.history, remotePatches = getRemotePatches(editor);
2628
2628
  if (redos.length > 0) {
2629
2629
  const step = redos[redos.length - 1];
2630
- if (debug$f("Redoing", step), step.operations.length > 0) {
2630
+ if (debug$e("Redoing", step), step.operations.length > 0) {
2631
2631
  const otherPatches = remotePatches.filter((item) => item.time >= step.timestamp);
2632
2632
  let transformedOperations = step.operations;
2633
2633
  otherPatches.forEach((item) => {
@@ -2644,7 +2644,7 @@ const historyUndoOperationImplementation = ({
2644
2644
  });
2645
2645
  });
2646
2646
  } catch (err) {
2647
- debug$f("Could not perform redo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = {
2647
+ debug$e("Could not perform redo step", err), remotePatches.splice(0, remotePatches.length), Transforms.deselect(editor), editor.history = {
2648
2648
  undos: [],
2649
2649
  redos: []
2650
2650
  }, SAVING.set(editor, !0), setIsRedoing(editor, !1), editor.onChange();
@@ -2662,16 +2662,16 @@ function transformOperation(editor, patch, operation, snapshot, previousSnapshot
2662
2662
  const insertBlockIndex = (snapshot || []).findIndex((blk) => isEqual({
2663
2663
  _key: blk._key
2664
2664
  }, patch.path[0]));
2665
- return debug$f(`Adjusting block path (+${patch.items.length}) for '${transformedOperation.type}' operation and patch '${patch.type}'`), [adjustBlockPath(transformedOperation, patch.items.length, insertBlockIndex)];
2665
+ return debug$e(`Adjusting block path (+${patch.items.length}) for '${transformedOperation.type}' operation and patch '${patch.type}'`), [adjustBlockPath(transformedOperation, patch.items.length, insertBlockIndex)];
2666
2666
  }
2667
2667
  if (patch.type === "unset" && patch.path.length === 1) {
2668
2668
  const unsetBlockIndex = (previousSnapshot || []).findIndex((blk) => isEqual({
2669
2669
  _key: blk._key
2670
2670
  }, patch.path[0]));
2671
- return "path" in transformedOperation && Array.isArray(transformedOperation.path) && transformedOperation.path[0] === unsetBlockIndex ? (debug$f("Skipping transformation that targeted removed block"), []) : [adjustBlockPath(transformedOperation, -1, unsetBlockIndex)];
2671
+ return "path" in transformedOperation && Array.isArray(transformedOperation.path) && transformedOperation.path[0] === unsetBlockIndex ? (debug$e("Skipping transformation that targeted removed block"), []) : [adjustBlockPath(transformedOperation, -1, unsetBlockIndex)];
2672
2672
  }
2673
2673
  if (patch.type === "unset" && patch.path.length === 0)
2674
- return debug$f(`Adjusting selection for unset everything patch and ${operation.type} operation`), [];
2674
+ return debug$e(`Adjusting selection for unset everything patch and ${operation.type} operation`), [];
2675
2675
  if (patch.type === "diffMatchPatch") {
2676
2676
  const operationTargetBlock = findOperationTargetBlock(editor, transformedOperation);
2677
2677
  return !operationTargetBlock || !isEqual({
@@ -3121,6 +3121,7 @@ const addAnnotationOperationImplementation = ({
3121
3121
  };
3122
3122
  }
3123
3123
  }, deleteOperationImplementation = ({
3124
+ context,
3124
3125
  operation
3125
3126
  }) => {
3126
3127
  const anchorBlockKey = getBlockKeyFromSelectionPoint(operation.at.anchor), focusBlockKey = getBlockKeyFromSelectionPoint(operation.at.focus), anchorBlockPath = anchorBlockKey !== void 0 ? getBlockPath({
@@ -3133,7 +3134,7 @@ const addAnnotationOperationImplementation = ({
3133
3134
  if (operation.at.anchor.path.length === 1 && operation.at.focus.path.length === 1 && anchorBlockPath && focusBlockPath && anchorBlockPath[0] === focusBlockPath[0]) {
3134
3135
  Transforms.removeNodes(operation.editor, {
3135
3136
  at: [anchorBlockPath[0]]
3136
- });
3137
+ }), operation.editor.children.length === 0 && Transforms.insertNodes(operation.editor, createPlaceholderBlock(context));
3137
3138
  return;
3138
3139
  }
3139
3140
  const range = toSlateRange(operation.at, operation.editor);
@@ -4436,7 +4437,7 @@ function withoutPatching(editor, fn) {
4436
4437
  function isPatching(editor) {
4437
4438
  return PATCHING.get(editor);
4438
4439
  }
4439
- const debug$e = debugWithName("plugin:withPatches");
4440
+ const debug$d = debugWithName("plugin:withPatches");
4440
4441
  function createWithPatches({
4441
4442
  editorActor,
4442
4443
  relayActor,
@@ -4461,7 +4462,7 @@ function createWithPatches({
4461
4462
  withoutPatching(editor, () => {
4462
4463
  withoutSaving(editor, () => {
4463
4464
  for (const patch of patches) {
4464
- debug$e.enabled && debug$e(`Handling remote patch ${JSON.stringify(patch)}`);
4465
+ debug$d.enabled && debug$d(`Handling remote patch ${JSON.stringify(patch)}`);
4465
4466
  try {
4466
4467
  changed = applyPatch(editor, patch);
4467
4468
  } catch (error) {
@@ -4479,10 +4480,10 @@ function createWithPatches({
4479
4480
  remotePatches.length !== 0 && (bufferedPatches = bufferedPatches.concat(remotePatches), handleBufferedRemotePatches());
4480
4481
  };
4481
4482
  return subscriptions.push(() => {
4482
- debug$e("Subscribing to remote patches");
4483
+ debug$d("Subscribing to remote patches");
4483
4484
  const sub = editorActor.on("patches", handlePatches);
4484
4485
  return () => {
4485
- debug$e("Unsubscribing to remote patches"), sub.unsubscribe();
4486
+ debug$d("Unsubscribing to remote patches"), sub.unsubscribe();
4486
4487
  };
4487
4488
  }), editor.apply = (operation) => {
4488
4489
  let patches = [];
@@ -4536,7 +4537,7 @@ function createWithPatches({
4536
4537
  }, editor;
4537
4538
  };
4538
4539
  }
4539
- const debug$d = debugWithName("plugin:withPlaceholderBlock");
4540
+ const debug$c = debugWithName("plugin:withPlaceholderBlock");
4540
4541
  function createWithPlaceholderBlock(editorActor) {
4541
4542
  return function(editor) {
4542
4543
  const {
@@ -4561,7 +4562,7 @@ function createWithPlaceholderBlock(editorActor) {
4561
4562
  const node = op.node;
4562
4563
  if (op.path[0] === 0 && Editor.isVoid(editor, node)) {
4563
4564
  const nextPath = Path.next(op.path);
4564
- editor.children[nextPath[0]] || (debug$d("Adding placeholder block"), Editor.insertNode(editor, editor.pteCreateTextBlock({
4565
+ editor.children[nextPath[0]] || (debug$c("Adding placeholder block"), Editor.insertNode(editor, editor.pteCreateTextBlock({
4565
4566
  decorators: []
4566
4567
  })));
4567
4568
  }
@@ -4570,36 +4571,6 @@ function createWithPlaceholderBlock(editorActor) {
4570
4571
  }, editor;
4571
4572
  };
4572
4573
  }
4573
- const debug$c = debugWithName("plugin:withPortableTextBlockStyle");
4574
- function createWithPortableTextBlockStyle(editorActor) {
4575
- const defaultStyle = editorActor.getSnapshot().context.schema.styles[0].name;
4576
- return function(editor) {
4577
- const {
4578
- normalizeNode
4579
- } = editor;
4580
- return editor.normalizeNode = (nodeEntry) => {
4581
- const [, path] = nodeEntry;
4582
- for (const op of editor.operations)
4583
- if (op.type === "split_node" && op.path.length === 1 && editor.isTextBlock(op.properties) && op.properties.style !== defaultStyle && op.path[0] === path[0] && !Path.equals(path, op.path)) {
4584
- const [child] = Editor.node(editor, [op.path[0] + 1, 0]);
4585
- if (Text.isText(child) && child.text === "") {
4586
- debug$c(`Normalizing split node to ${defaultStyle} style`, op), editorActor.send({
4587
- type: "normalizing"
4588
- }), Transforms.setNodes(editor, {
4589
- style: defaultStyle
4590
- }, {
4591
- at: [op.path[0] + 1],
4592
- voids: !1
4593
- }), editorActor.send({
4594
- type: "done normalizing"
4595
- });
4596
- return;
4597
- }
4598
- }
4599
- normalizeNode(nodeEntry);
4600
- }, editor;
4601
- };
4602
- }
4603
4574
  debugWithName("plugin:withPortableTextSelections");
4604
4575
  function createWithPortableTextSelections(editorActor) {
4605
4576
  let prevSelection = null;
@@ -5092,10 +5063,10 @@ const withPlugins = (editor, options) => {
5092
5063
  }), withMaxBlocks = createWithMaxBlocks(editorActor), withUndoRedo = createWithUndoRedo({
5093
5064
  editorActor,
5094
5065
  subscriptions: options.subscriptions
5095
- }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5066
+ }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5096
5067
  editorActor
5097
5068
  }), withPortableTextSelections = createWithPortableTextSelections(editorActor);
5098
- return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(pluginUpdateValue(editorActor.getSnapshot().context, pluginUpdateMarkState(editorActor.getSnapshot().context, e)))))))))))));
5069
+ return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(pluginUpdateValue(editorActor.getSnapshot().context, pluginUpdateMarkState(editorActor.getSnapshot().context, e))))))))))));
5099
5070
  }, debug$a = debugWithName("setup");
5100
5071
  function createSlateEditor(config) {
5101
5072
  debug$a("Creating new Slate editor instance");