@portabletext/editor 2.19.3 → 2.20.0

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
@@ -13,7 +13,7 @@ import { getBlockStartPoint, getBlockKeyFromSelectionPoint, getChildKeyFromSelec
13
13
  import isEqual from "lodash/isEqual.js";
14
14
  import { isTextBlock, isSpan, compileSchema } from "@portabletext/schema";
15
15
  import { defineSchema } from "@portabletext/schema";
16
- import { getFocusInlineObject, getSelectedBlocks, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, isOverlappingSelection, isSelectingEntireBlocks, getActiveDecorators, getTrimmedSelection, isActiveAnnotation, getCaretWordSelection, getFocusBlockObject, getPreviousBlock, getNextBlock, getMarkState, getActiveAnnotationsMarks, isAtTheEndOfBlock, isAtTheStartOfBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock, getSelectionEndPoint as getSelectionEndPoint$1, isActiveDecorator, getActiveAnnotations, getSelectedTextBlocks, isActiveListItem, isActiveStyle } from "./_chunks-es/selector.is-at-the-start-of-block.js";
16
+ import { getFocusInlineObject, getSelectedBlocks, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, isOverlappingSelection, isSelectingEntireBlocks, getActiveDecorators, isActiveAnnotation, getCaretWordSelection, getFocusBlockObject, getPreviousBlock, getNextBlock, getMarkState, getActiveAnnotationsMarks, isAtTheEndOfBlock, isAtTheStartOfBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock, getSelectionEndPoint as getSelectionEndPoint$1, isActiveDecorator, getActiveAnnotations, getSelectedTextBlocks, isActiveListItem, isActiveStyle } from "./_chunks-es/selector.is-at-the-start-of-block.js";
17
17
  import { isSelectionCollapsed as isSelectionCollapsed$1, getFocusTextBlock, getFocusSpan as getFocusSpan$1, isSelectionExpanded, getFocusBlock as getFocusBlock$1, getSelectedValue, getSelectionStartPoint as getSelectionStartPoint$1, getFocusChild as getFocusChild$1 } from "./_chunks-es/selector.get-selection-text.js";
18
18
  import { defineBehavior, forward, raise, effect } from "./behaviors/index.js";
19
19
  import uniq from "lodash/uniq.js";
@@ -25,10 +25,9 @@ import { Schema } from "@sanity/schema";
25
25
  import flatten from "lodash/flatten.js";
26
26
  import omit from "lodash/omit.js";
27
27
  import { applyAll, unset, insert, set, setIfMissing, diffMatchPatch as diffMatchPatch$1 } from "@portabletext/patches";
28
- import { blockOffsetsToSelection } from "./_chunks-es/util.child-selection-point-to-block-offset.js";
29
- import { selectionPointToBlockOffset, sliceTextBlock } from "./_chunks-es/util.slice-text-block.js";
30
28
  import { createDraft, finishDraft } from "immer";
31
29
  import { createKeyboardShortcut, code, underline, italic, bold, undo, redo } from "@portabletext/keyboard-shortcuts";
30
+ import { sliceTextBlock } from "./_chunks-es/util.slice-text-block.js";
32
31
  import isPlainObject from "lodash/isPlainObject.js";
33
32
  import { Subject } from "rxjs";
34
33
  function EditorEventListener(props) {
@@ -4494,97 +4493,42 @@ const addAnnotationOperationImplementation = ({
4494
4493
  context,
4495
4494
  operation
4496
4495
  }) => {
4497
- const editor = operation.editor, mark = operation.decorator, value = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), manualAnchor = operation.at?.anchor ? blockOffsetToSpanSelectionPoint({
4498
- context: {
4499
- ...context,
4500
- value
4501
- },
4502
- blockOffset: operation.at.anchor,
4503
- direction: "backward"
4504
- }) : void 0, manualFocus = operation.at?.focus ? blockOffsetToSpanSelectionPoint({
4505
- context: {
4506
- ...context,
4507
- value
4508
- },
4509
- blockOffset: operation.at.focus,
4510
- direction: "forward"
4511
- }) : void 0, manualSelection = manualAnchor && manualFocus ? {
4512
- anchor: manualAnchor,
4513
- focus: manualFocus
4514
- } : void 0, selection = manualSelection ? toSlateRange({
4496
+ const editor = operation.editor, mark = operation.decorator;
4497
+ let at = operation.at ? toSlateRange({
4515
4498
  context: {
4516
4499
  schema: context.schema,
4517
4500
  value: operation.editor.value,
4518
- selection: manualSelection
4501
+ selection: operation.at
4519
4502
  },
4520
4503
  blockIndexMap: operation.editor.blockIndexMap
4521
- }) ?? editor.selection : editor.selection;
4522
- if (!selection)
4523
- return;
4524
- const editorSelection = slateRangeToSelection({
4525
- schema: context.schema,
4526
- editor,
4527
- range: selection
4528
- }), anchorOffset = editorSelection ? selectionPointToBlockOffset({
4529
- context: {
4530
- ...context,
4531
- value
4532
- },
4533
- selectionPoint: editorSelection.anchor
4534
- }) : void 0, focusOffset = editorSelection ? selectionPointToBlockOffset({
4535
- context: {
4536
- ...context,
4537
- value
4538
- },
4539
- selectionPoint: editorSelection.focus
4540
- }) : void 0;
4541
- if (!anchorOffset || !focusOffset)
4542
- throw new Error("Unable to find anchor or focus offset");
4543
- if (Range.isExpanded(selection)) {
4544
- Transforms.setNodes(editor, {}, {
4545
- at: selection,
4504
+ }) : operation.editor.selection;
4505
+ if (!at)
4506
+ throw new Error("Unable to add decorator without a selection");
4507
+ if (Range.isExpanded(at)) {
4508
+ const rangeRef = Editor.rangeRef(editor, at, {
4509
+ affinity: "inward"
4510
+ }), [start, end] = Range.edges(at), endAtEndOfNode = Editor.isEnd(editor, end, end.path);
4511
+ Transforms.splitNodes(editor, {
4512
+ at: end,
4546
4513
  match: Text.isText,
4547
- split: !0,
4548
- hanging: !0
4549
- });
4550
- const newValue = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), newSelection = blockOffsetsToSelection({
4551
- context: {
4552
- ...context,
4553
- value: newValue
4554
- },
4555
- offsets: {
4556
- anchor: anchorOffset,
4557
- focus: focusOffset
4558
- },
4559
- backward: editorSelection?.backward
4560
- }), trimmedSelection = getTrimmedSelection({
4561
- blockIndexMap: editor.blockIndexMap,
4562
- context: {
4563
- converters: [],
4564
- keyGenerator: context.keyGenerator,
4565
- readOnly: !1,
4566
- schema: context.schema,
4567
- selection: newSelection,
4568
- value: newValue
4569
- },
4570
- decoratorState: editor.decoratorState
4514
+ mode: "lowest",
4515
+ voids: !1,
4516
+ always: !endAtEndOfNode
4571
4517
  });
4572
- if (!trimmedSelection)
4573
- throw new Error("Unable to find trimmed selection");
4574
- const newRange = toSlateRange({
4575
- context: {
4576
- schema: context.schema,
4577
- value: operation.editor.value,
4578
- selection: trimmedSelection
4579
- },
4580
- blockIndexMap: operation.editor.blockIndexMap
4518
+ const startAtStartOfNode = Editor.isStart(editor, start, start.path);
4519
+ if (Transforms.splitNodes(editor, {
4520
+ at: start,
4521
+ match: Text.isText,
4522
+ mode: "lowest",
4523
+ voids: !1,
4524
+ always: !startAtStartOfNode
4525
+ }), at = rangeRef.unref(), !at)
4526
+ throw new Error("Unable to add decorator without a selection");
4527
+ operation.at || Transforms.select(editor, at);
4528
+ const splitTextNodes = Editor.nodes(editor, {
4529
+ at,
4530
+ match: Text.isText
4581
4531
  });
4582
- if (!newRange)
4583
- throw new Error("Unable to find new selection");
4584
- const splitTextNodes = Range.isRange(newRange) ? [...Editor.nodes(editor, {
4585
- at: newRange,
4586
- match: (node) => Text.isText(node)
4587
- })] : [];
4588
4532
  for (const [node, path] of splitTextNodes) {
4589
4533
  const marks = [...(Array.isArray(node.marks) ? node.marks : []).filter((eMark) => eMark !== mark), mark];
4590
4534
  Transforms.setNodes(editor, {
@@ -4598,11 +4542,11 @@ const addAnnotationOperationImplementation = ({
4598
4542
  }
4599
4543
  } else {
4600
4544
  if (!Array.from(Editor.nodes(editor, {
4601
- at: selection,
4545
+ at,
4602
4546
  match: (node) => editor.isTextSpan(node)
4603
4547
  }))?.at(0))
4604
4548
  return;
4605
- const [block, blockPath] = Editor.node(editor, selection, {
4549
+ const [block, blockPath] = Editor.node(editor, at, {
4606
4550
  depth: 1
4607
4551
  }), lonelyEmptySpan = editor.isTextBlock(block) && block.children.length === 1 && editor.isTextSpan(block.children[0]) && block.children[0].text === "" ? block.children[0] : void 0;
4608
4552
  if (lonelyEmptySpan) {
@@ -4617,9 +4561,9 @@ const addAnnotationOperationImplementation = ({
4617
4561
  editor.decoratorState[mark] = !0;
4618
4562
  }
4619
4563
  if (editor.selection) {
4620
- const selection2 = editor.selection;
4564
+ const selection = editor.selection;
4621
4565
  editor.selection = {
4622
- ...selection2
4566
+ ...selection
4623
4567
  };
4624
4568
  }
4625
4569
  }, deleteOperationImplementation = ({
@@ -8317,19 +8261,13 @@ const abstractAnnotationBehaviors = [defineBehavior({
8317
8261
  guard: ({
8318
8262
  snapshot,
8319
8263
  event
8320
- }) => {
8321
- const manualSelection = event.at ? blockOffsetsToSelection({
8322
- context: snapshot.context,
8323
- offsets: event.at
8324
- }) : null;
8325
- return manualSelection ? !isActiveDecorator(event.decorator)({
8326
- ...snapshot,
8327
- context: {
8328
- ...snapshot.context,
8329
- selection: manualSelection
8330
- }
8331
- }) : !isActiveDecorator(event.decorator)(snapshot);
8332
- },
8264
+ }) => event.at ? !isActiveDecorator(event.decorator)({
8265
+ ...snapshot,
8266
+ context: {
8267
+ ...snapshot.context,
8268
+ selection: event.at
8269
+ }
8270
+ }) : !isActiveDecorator(event.decorator)(snapshot),
8333
8271
  actions: [({
8334
8272
  event
8335
8273
  }) => [raise({
@@ -8542,33 +8480,11 @@ const abstractAnnotationBehaviors = [defineBehavior({
8542
8480
  })]]
8543
8481
  }), defineBehavior({
8544
8482
  on: "delete.text",
8545
- guard: ({
8546
- snapshot,
8483
+ actions: [({
8547
8484
  event
8548
- }) => {
8549
- const selection = blockOffsetsToSelection({
8550
- context: snapshot.context,
8551
- offsets: event.at
8552
- });
8553
- if (!selection)
8554
- return !1;
8555
- const trimmedSelection = getTrimmedSelection({
8556
- ...snapshot,
8557
- context: {
8558
- ...snapshot.context,
8559
- value: snapshot.context.value,
8560
- selection
8561
- }
8562
- });
8563
- return trimmedSelection ? {
8564
- selection: trimmedSelection
8565
- } : !1;
8566
- },
8567
- actions: [(_, {
8568
- selection
8569
8485
  }) => [raise({
8570
- type: "delete",
8571
- at: selection
8486
+ ...event,
8487
+ type: "delete"
8572
8488
  })]]
8573
8489
  })], abstractDeserializeBehaviors = [
8574
8490
  defineBehavior({