@portabletext/editor 3.2.2 → 3.2.3

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
@@ -3232,29 +3232,31 @@ function createWithPortableTextMarkModel(editorActor) {
3232
3232
  apply2(op);
3233
3233
  return;
3234
3234
  }
3235
- if (op.type === "set_selection" && op.properties && op.newProperties && op.properties.anchor && op.properties.focus && op.newProperties.anchor && op.newProperties.focus) {
3236
- const previousSelectionIsCollapsed = Range.isCollapsed({
3237
- anchor: op.properties.anchor,
3238
- focus: op.properties.focus
3239
- }), newSelectionIsCollapsed = Range.isCollapsed({
3240
- anchor: op.newProperties.anchor,
3241
- focus: op.newProperties.focus
3242
- });
3243
- if (previousSelectionIsCollapsed && newSelectionIsCollapsed) {
3244
- const focusSpan = Array.from(Editor.nodes(editor, {
3245
- mode: "lowest",
3246
- at: op.properties.focus,
3247
- match: (n) => editor.isTextSpan(n),
3248
- voids: !1
3249
- }))[0]?.[0], newFocusSpan = Array.from(Editor.nodes(editor, {
3250
- mode: "lowest",
3251
- at: op.newProperties.focus,
3252
- match: (n) => editor.isTextSpan(n),
3253
- voids: !1
3254
- }))[0]?.[0], movedToNextSpan = focusSpan && newFocusSpan && op.newProperties.focus.path[0] === op.properties.focus.path[0] && op.newProperties.focus.path[1] === op.properties.focus.path[1] + 1 && focusSpan.text.length === op.properties.focus.offset && op.newProperties.focus.offset === 0, movedToPreviousSpan = focusSpan && newFocusSpan && op.newProperties.focus.path[0] === op.properties.focus.path[0] && op.newProperties.focus.path[1] === op.properties.focus.path[1] - 1 && op.properties.focus.offset === 0 && newFocusSpan.text.length === op.newProperties.focus.offset;
3255
- !movedToNextSpan && !movedToPreviousSpan && (editor.decoratorState = {});
3256
- }
3257
- }
3235
+ if (op.type === "set_selection")
3236
+ if (op.properties && op.newProperties && op.properties.anchor && op.properties.focus && op.newProperties.anchor && op.newProperties.focus) {
3237
+ const previousSelectionIsCollapsed = Range.isCollapsed({
3238
+ anchor: op.properties.anchor,
3239
+ focus: op.properties.focus
3240
+ }), newSelectionIsCollapsed = Range.isCollapsed({
3241
+ anchor: op.newProperties.anchor,
3242
+ focus: op.newProperties.focus
3243
+ });
3244
+ if (previousSelectionIsCollapsed && newSelectionIsCollapsed) {
3245
+ const focusSpan = Array.from(Editor.nodes(editor, {
3246
+ mode: "lowest",
3247
+ at: op.properties.focus,
3248
+ match: (n) => editor.isTextSpan(n),
3249
+ voids: !1
3250
+ }))[0]?.[0], newFocusSpan = Array.from(Editor.nodes(editor, {
3251
+ mode: "lowest",
3252
+ at: op.newProperties.focus,
3253
+ match: (n) => editor.isTextSpan(n),
3254
+ voids: !1
3255
+ }))[0]?.[0], movedToNextSpan = focusSpan && newFocusSpan && op.newProperties.focus.path[0] === op.properties.focus.path[0] && op.newProperties.focus.path[1] === op.properties.focus.path[1] + 1 && focusSpan.text.length === op.properties.focus.offset && op.newProperties.focus.offset === 0, movedToPreviousSpan = focusSpan && newFocusSpan && op.newProperties.focus.path[0] === op.properties.focus.path[0] && op.newProperties.focus.path[1] === op.properties.focus.path[1] - 1 && op.properties.focus.offset === 0 && newFocusSpan.text.length === op.newProperties.focus.offset;
3256
+ !movedToNextSpan && !movedToPreviousSpan && (editor.decoratorState = {});
3257
+ }
3258
+ } else
3259
+ editor.decoratorState = {};
3258
3260
  if (op.type === "remove_text") {
3259
3261
  const {
3260
3262
  selection
@@ -4581,12 +4583,12 @@ const debug$a = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4581
4583
  selection: operation.at
4582
4584
  },
4583
4585
  blockIndexMap: operation.editor.blockIndexMap
4584
- }) : void 0, selection = operation.editor.selection ? slateRangeToSelection({
4585
- schema: context.schema,
4586
- editor: operation.editor,
4587
- range: operation.editor.selection
4588
- }) : void 0, reverse = operation.direction === "backward", anchorPoint = operation.at?.anchor ?? selection?.anchor, focusPoint = operation.at?.focus ?? selection?.focus, startPoint = reverse ? focusPoint : anchorPoint, endPoint = reverse ? anchorPoint : focusPoint, startBlockKey = startPoint ? getBlockKeyFromSelectionPoint(startPoint) : void 0, endBlockKey = endPoint ? getBlockKeyFromSelectionPoint(endPoint) : void 0, startBlockIndex = startBlockKey ? operation.editor.blockIndexMap.get(startBlockKey) : void 0, endBlockIndex = endBlockKey ? operation.editor.blockIndexMap.get(endBlockKey) : void 0, startBlock = startBlockIndex ? operation.editor.value.at(startBlockIndex) : void 0, endBlock = endBlockIndex ? operation.editor.value.at(endBlockIndex) : void 0;
4586
+ }) : operation.editor.selection;
4587
+ if (!at)
4588
+ throw new Error("Unable to delete without a selection");
4589
+ const [start, end] = Range.edges(at);
4589
4590
  if (operation.unit === "block") {
4591
+ const startBlockIndex = start.path.at(0), endBlockIndex = end.path.at(0);
4590
4592
  if (startBlockIndex === void 0 || endBlockIndex === void 0)
4591
4593
  throw new Error("Failed to get start or end block index");
4592
4594
  Transforms.removeNodes(operation.editor, {
@@ -4605,25 +4607,19 @@ const debug$a = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4605
4607
  return;
4606
4608
  }
4607
4609
  if (operation.unit === "child") {
4608
- const range = at ?? operation.editor.selection ?? void 0;
4609
- if (!range)
4610
- throw new Error("Unable to delete children without a selection");
4611
4610
  Transforms.removeNodes(operation.editor, {
4612
- at: range,
4611
+ at,
4613
4612
  match: (node) => isSpan(context, node) && node._key !== VOID_CHILD_KEY || "__inline" in node && node.__inline === !0
4614
4613
  });
4615
4614
  return;
4616
4615
  }
4617
4616
  if (operation.direction === "backward" && operation.unit === "line") {
4618
- const range = at ?? operation.editor.selection ?? void 0;
4619
- if (!range)
4620
- throw new Error("Unable to delete line without a selection");
4621
4617
  const parentBlockEntry = Editor.above(operation.editor, {
4622
4618
  match: (n) => Element$1.isElement(n) && Editor.isBlock(operation.editor, n),
4623
- at: range
4619
+ at
4624
4620
  });
4625
4621
  if (parentBlockEntry) {
4626
- const [, parentBlockPath] = parentBlockEntry, parentElementRange = Editor.range(operation.editor, parentBlockPath, range.anchor), currentLineRange = findCurrentLineRange(operation.editor, parentElementRange);
4622
+ const [, parentBlockPath] = parentBlockEntry, parentElementRange = Editor.range(operation.editor, parentBlockPath, at.anchor), currentLineRange = findCurrentLineRange(operation.editor, parentElementRange);
4627
4623
  if (!Range.isCollapsed(currentLineRange)) {
4628
4624
  Transforms.delete(operation.editor, {
4629
4625
  at: currentLineRange
@@ -4632,21 +4628,80 @@ const debug$a = debugWithName("behavior.operation.history.redo"), historyRedoOpe
4632
4628
  }
4633
4629
  }
4634
4630
  }
4635
- if (operation.unit === "word") {
4636
- const range = at ?? operation.editor.selection ?? void 0;
4637
- if (!range)
4638
- throw new Error("Unable to delete word without a selection");
4639
- if (Range.isCollapsed(range)) {
4640
- deleteText(operation.editor, {
4641
- at: range,
4642
- unit: "word",
4643
- reverse: operation.direction === "backward"
4631
+ if (operation.unit === "word" && Range.isCollapsed(at)) {
4632
+ deleteText(operation.editor, {
4633
+ at,
4634
+ unit: "word",
4635
+ reverse: operation.direction === "backward"
4636
+ });
4637
+ return;
4638
+ }
4639
+ const startBlock = Editor.above(operation.editor, {
4640
+ match: (n) => Element$1.isElement(n) && Editor.isBlock(operation.editor, n),
4641
+ at: start,
4642
+ voids: !1
4643
+ }), endBlock = Editor.above(operation.editor, {
4644
+ match: (n) => Element$1.isElement(n) && Editor.isBlock(operation.editor, n),
4645
+ at: end,
4646
+ voids: !1
4647
+ }), isAcrossBlocks = startBlock && endBlock && !Path.equals(startBlock[1], endBlock[1]), startNonEditable = Editor.void(operation.editor, {
4648
+ at: start,
4649
+ mode: "highest"
4650
+ }) ?? Editor.elementReadOnly(operation.editor, {
4651
+ at: start,
4652
+ mode: "highest"
4653
+ }), endNonEditable = Editor.void(operation.editor, {
4654
+ at: end,
4655
+ mode: "highest"
4656
+ }) ?? Editor.elementReadOnly(operation.editor, {
4657
+ at: end,
4658
+ mode: "highest"
4659
+ }), matches = [];
4660
+ let lastPath;
4661
+ for (const entry of Editor.nodes(operation.editor, {
4662
+ at,
4663
+ voids: !1
4664
+ })) {
4665
+ const [node, path] = entry;
4666
+ lastPath && Path.compare(path, lastPath) === 0 || (Element$1.isElement(node) && (Editor.isVoid(operation.editor, node) || Editor.isElementReadOnly(operation.editor, node)) || !Path.isCommon(path, start.path) && !Path.isCommon(path, end.path)) && (matches.push(entry), lastPath = path);
4667
+ }
4668
+ const pathRefs = Array.from(matches, ([, path]) => Editor.pathRef(operation.editor, path)), startRef = Editor.pointRef(operation.editor, start), endRef = Editor.pointRef(operation.editor, end);
4669
+ if (startBlock && endBlock && Point.equals(start, Editor.start(operation.editor, startBlock[1])) && Point.equals(end, Editor.end(operation.editor, endBlock[1])) && isAcrossBlocks && !startNonEditable && !endNonEditable) {
4670
+ if (!startNonEditable) {
4671
+ const point = startRef.current, [node] = Editor.leaf(operation.editor, point);
4672
+ node.text.length > 0 && operation.editor.apply({
4673
+ type: "remove_text",
4674
+ path: point.path,
4675
+ offset: 0,
4676
+ text: node.text
4644
4677
  });
4645
- return;
4646
4678
  }
4679
+ for (const pathRef of pathRefs.reverse()) {
4680
+ const path = pathRef.unref();
4681
+ path && Transforms.removeNodes(operation.editor, {
4682
+ at: path,
4683
+ voids: !1
4684
+ });
4685
+ }
4686
+ if (!endNonEditable) {
4687
+ const point = endRef.current, [node] = Editor.leaf(operation.editor, point), {
4688
+ path
4689
+ } = point, offset = 0, text = node.text.slice(offset, end.offset);
4690
+ text.length > 0 && operation.editor.apply({
4691
+ type: "remove_text",
4692
+ path,
4693
+ offset,
4694
+ text
4695
+ });
4696
+ }
4697
+ endRef.current && startRef.current && Transforms.removeNodes(operation.editor, {
4698
+ at: endRef.current,
4699
+ voids: !1
4700
+ });
4701
+ return;
4647
4702
  }
4648
- const hanging = reverse ? endPoint ? isTextBlock(context, endBlock) ? endPoint.offset === 0 : !0 : !1 : startPoint ? isTextBlock(context, startBlock) ? startPoint.offset === 0 : !0 : !1;
4649
- at ? deleteText(operation.editor, {
4703
+ const reverse = operation.direction === "backward", hanging = reverse ? end ? isTextBlock(context, endBlock) ? end.offset === 0 : !0 : !1 : start ? isTextBlock(context, startBlock) ? start.offset === 0 : !0 : !1;
4704
+ operation.at ? deleteText(operation.editor, {
4650
4705
  at,
4651
4706
  hanging,
4652
4707
  reverse