@portabletext/editor 1.52.4 → 1.52.6

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,9 +13,9 @@ import { getBlockStartPoint, getBlockKeyFromSelectionPoint, isTextBlock, parseBl
13
13
  import { getBlockEndPoint, isEmptyTextBlock, isEqualSelectionPoints } from "./_chunks-es/util.is-equal-selection-points.js";
14
14
  import { isSelectionCollapsed, selectionPointToBlockOffset } from "./_chunks-es/util.selection-point-to-block-offset.js";
15
15
  import isEqual from "lodash/isEqual.js";
16
- import { isKeySegment, defineType, defineField, isPortableTextSpan as isPortableTextSpan$1 } from "@sanity/types";
16
+ import { isKeySegment, defineType, defineField } from "@sanity/types";
17
17
  import { isSelectionCollapsed as isSelectionCollapsed$1, getFocusTextBlock, getFocusSpan as getFocusSpan$1, getSelectedBlocks, isSelectionExpanded, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, getFocusBlock as getFocusBlock$1, getFocusBlockObject, getPreviousBlock, getNextBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock } from "./_chunks-es/selector.is-selection-expanded.js";
18
- import { getFocusInlineObject, isOverlappingSelection, isSelectingEntireBlocks, getTrimmedSelection, getCaretWordSelection, isAtTheEndOfBlock, isAtTheStartOfBlock, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, getActiveAnnotations as getActiveAnnotations$1 } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
18
+ import { getFocusInlineObject, isOverlappingSelection, isSelectingEntireBlocks, getTrimmedSelection, getSelectedSpans, getCaretWordSelection, isAtTheEndOfBlock, isAtTheStartOfBlock, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, getActiveAnnotations as getActiveAnnotations$1 } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
19
19
  import getRandomValues from "get-random-values-esm";
20
20
  import { defineBehavior, forward, raise, effect } from "./behaviors/index.js";
21
21
  import uniq from "lodash/uniq.js";
@@ -195,20 +195,6 @@ function getFocusSpan({
195
195
  }
196
196
  return [void 0, void 0];
197
197
  }
198
- function getSelectedSpans({
199
- editor
200
- }) {
201
- if (!editor.selection)
202
- return [];
203
- try {
204
- return Array.from(Editor.nodes(editor, {
205
- at: editor.selection,
206
- match: (node) => editor.isTextSpan(node)
207
- }));
208
- } catch {
209
- return [];
210
- }
211
- }
212
198
  function getSelectionStartBlock({
213
199
  editor
214
200
  }) {
@@ -4571,40 +4557,6 @@ function createWithPlaceholderBlock(editorActor) {
4571
4557
  }, editor;
4572
4558
  };
4573
4559
  }
4574
- debugWithName("plugin:withPortableTextSelections");
4575
- function createWithPortableTextSelections(editorActor) {
4576
- let prevSelection = null;
4577
- return function(editor) {
4578
- const emitPortableTextSelection = () => {
4579
- if (prevSelection !== editor.selection) {
4580
- let ptRange = null;
4581
- if (editor.selection) {
4582
- const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
4583
- existing ? ptRange = existing : (ptRange = slateRangeToSelection({
4584
- schema: editorActor.getSnapshot().context.schema,
4585
- editor,
4586
- range: editor.selection
4587
- }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange));
4588
- }
4589
- ptRange ? editorActor.send({
4590
- type: "update selection",
4591
- selection: ptRange
4592
- }) : editorActor.send({
4593
- type: "update selection",
4594
- selection: null
4595
- });
4596
- }
4597
- prevSelection = editor.selection;
4598
- }, {
4599
- onChange
4600
- } = editor;
4601
- return editor.onChange = () => {
4602
- onChange(), editorActor.getSnapshot().matches({
4603
- setup: "setting up"
4604
- }) || emitPortableTextSelection();
4605
- }, editor;
4606
- };
4607
- }
4608
4560
  const debug$b = debugWithName("plugin:withSchemaTypes");
4609
4561
  function createWithSchemaTypes({
4610
4562
  editorActor
@@ -4692,12 +4644,23 @@ function getMarkState({
4692
4644
  if (!block || !editor.isTextBlock(block) || !span)
4693
4645
  return;
4694
4646
  if (Range.isExpanded(editor.selection)) {
4695
- const selectedSpans = getSelectedSpans({
4696
- editor
4647
+ const selection = editor.selection ? slateRangeToSelection({
4648
+ schema,
4649
+ editor,
4650
+ range: editor.selection
4651
+ }) : null, selectedSpans = getSelectedSpans({
4652
+ context: {
4653
+ value: editor.value,
4654
+ selection,
4655
+ schema,
4656
+ converters: [],
4657
+ keyGenerator: () => "",
4658
+ readOnly: !1
4659
+ }
4697
4660
  });
4698
4661
  let index = 0, marks2 = [];
4699
- for (const [span2] of selectedSpans)
4700
- index === 0 ? marks2 = span2.marks ?? [] : (span2.marks?.length === 0 || (span2.marks ?? [])?.some((mark) => !marks2.includes(mark))) && (marks2 = []), index++;
4662
+ for (const span2 of selectedSpans)
4663
+ index === 0 ? marks2 = span2.node.marks ?? [] : (span2.node.marks?.length === 0 || (span2.node.marks ?? [])?.some((mark) => !marks2.includes(mark))) && (marks2 = []), index++;
4701
4664
  return {
4702
4665
  state: "unchanged",
4703
4666
  marks: marks2
@@ -4770,6 +4733,43 @@ function pluginUpdateMarkState(context, editor) {
4770
4733
  });
4771
4734
  }, editor;
4772
4735
  }
4736
+ function pluginUpdateSelection({
4737
+ editor,
4738
+ editorActor
4739
+ }) {
4740
+ const updateSelection = () => {
4741
+ if (editor.selection) {
4742
+ const existingSelection = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
4743
+ if (existingSelection)
4744
+ editorActor.send({
4745
+ type: "update selection",
4746
+ selection: existingSelection
4747
+ });
4748
+ else {
4749
+ const selection = slateRangeToSelection({
4750
+ schema: editorActor.getSnapshot().context.schema,
4751
+ editor,
4752
+ range: editor.selection
4753
+ });
4754
+ SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, selection), editorActor.send({
4755
+ type: "update selection",
4756
+ selection
4757
+ });
4758
+ }
4759
+ } else
4760
+ editorActor.send({
4761
+ type: "update selection",
4762
+ selection: null
4763
+ });
4764
+ }, {
4765
+ onChange
4766
+ } = editor;
4767
+ return editor.onChange = () => {
4768
+ onChange(), editorActor.getSnapshot().matches({
4769
+ setup: "setting up"
4770
+ }) || updateSelection();
4771
+ }, editor;
4772
+ }
4773
4773
  function isEditorNode(node) {
4774
4774
  return typeof node == "object" && node !== null ? !("_type" in node) && "children" in node && Array.isArray(node.children) : !1;
4775
4775
  }
@@ -5065,8 +5065,11 @@ const withPlugins = (editor, options) => {
5065
5065
  subscriptions: options.subscriptions
5066
5066
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5067
5067
  editorActor
5068
- }), withPortableTextSelections = createWithPortableTextSelections(editorActor);
5069
- return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(pluginUpdateValue(editorActor.getSnapshot().context, pluginUpdateMarkState(editorActor.getSnapshot().context, e))))))))))));
5068
+ });
5069
+ return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(pluginUpdateValue(editorActor.getSnapshot().context, pluginUpdateMarkState(editorActor.getSnapshot().context, pluginUpdateSelection({
5070
+ editorActor,
5071
+ editor: e
5072
+ }))))))))))));
5070
5073
  }, debug$a = debugWithName("setup");
5071
5074
  function createSlateEditor(config) {
5072
5075
  debug$a("Creating new Slate editor instance");
@@ -8334,12 +8337,13 @@ function createEditableAPI(editor, editorActor) {
8334
8337
  return [];
8335
8338
  }
8336
8339
  },
8337
- isAnnotationActive: (annotationType) => isAnnotationActive({
8338
- editor,
8339
- annotation: {
8340
- name: annotationType
8341
- }
8342
- }),
8340
+ isAnnotationActive: (annotationType) => {
8341
+ const snapshot = getEditorSnapshot({
8342
+ editorActorSnapshot: editorActor.getSnapshot(),
8343
+ slateEditorInstance: editor
8344
+ });
8345
+ return isActiveAnnotation(annotationType)(snapshot);
8346
+ },
8343
8347
  addAnnotation: (type, value) => {
8344
8348
  let paths;
8345
8349
  return Editor.withoutNormalizing(editor, () => {
@@ -8426,41 +8430,60 @@ function createEditableAPI(editor, editorActor) {
8426
8430
  }
8427
8431
  };
8428
8432
  }
8429
- function isAnnotationActive({
8430
- editor,
8431
- annotation
8432
- }) {
8433
- if (!editor.selection || editor.selection.focus.path.length < 2)
8434
- return !1;
8435
- try {
8436
- const spans = [...Editor.nodes(editor, {
8437
- at: editor.selection,
8438
- match: (node) => Text.isText(node)
8439
- })];
8440
- if (spans.length === 0 || spans.some(([span]) => !isPortableTextSpan$1(span) || !span.marks || span.marks?.length === 0)) return !1;
8441
- const selectionMarkDefs = spans.reduce((accMarkDefs, [, path]) => {
8442
- const [block] = Editor.node(editor, path, {
8443
- depth: 1
8444
- });
8445
- return editor.isTextBlock(block) && block.markDefs ? [...accMarkDefs, ...block.markDefs] : accMarkDefs;
8446
- }, []);
8447
- return spans.every(([span]) => isPortableTextSpan$1(span) ? span.marks?.map((markKey) => selectionMarkDefs.find((def) => def?._key === markKey)?._type)?.includes(annotation.name) : !1);
8448
- } catch {
8449
- return !1;
8450
- }
8451
- }
8452
8433
  const relayMachine = setup({
8453
8434
  types: {
8435
+ context: {},
8454
8436
  events: {},
8455
8437
  emitted: {}
8456
8438
  }
8457
8439
  }).createMachine({
8458
8440
  id: "relay",
8441
+ context: {
8442
+ prevSelection: null,
8443
+ lastEventWasFocused: !1
8444
+ },
8459
8445
  on: {
8446
+ focused: {
8447
+ actions: [assign({
8448
+ lastEventWasFocused: !0
8449
+ }), emit(({
8450
+ event
8451
+ }) => event)]
8452
+ },
8453
+ selection: [{
8454
+ guard: ({
8455
+ context
8456
+ }) => context.lastEventWasFocused,
8457
+ actions: [assign({
8458
+ prevSelection: ({
8459
+ event
8460
+ }) => event.selection
8461
+ }), emit(({
8462
+ event
8463
+ }) => event), assign({
8464
+ lastEventWasFocused: !1
8465
+ })]
8466
+ }, {
8467
+ guard: ({
8468
+ context,
8469
+ event
8470
+ }) => context.prevSelection !== event.selection,
8471
+ actions: [assign({
8472
+ prevSelection: ({
8473
+ event
8474
+ }) => event.selection
8475
+ }), emit(({
8476
+ event
8477
+ }) => event), assign({
8478
+ lastEventWasFocused: !1
8479
+ })]
8480
+ }],
8460
8481
  "*": {
8461
- actions: emit(({
8482
+ actions: [emit(({
8462
8483
  event
8463
- }) => event)
8484
+ }) => event), assign({
8485
+ lastEventWasFocused: !1
8486
+ })]
8464
8487
  }
8465
8488
  }
8466
8489
  });
@@ -10748,22 +10771,11 @@ const debug = debugWithName("component:Editable"), PortableTextEditable = forwar
10748
10771
  }
10749
10772
  debug("No result from custom paste handler, pasting normally");
10750
10773
  }, [editorActor, onPaste, portableTextEditor, relayActor, slateEditor]), handleOnFocus = useCallback((event_2) => {
10751
- if (onFocus && onFocus(event_2), !event_2.isDefaultPrevented()) {
10752
- relayActor.send({
10753
- type: "focused",
10754
- event: event_2
10755
- });
10756
- const selection_3 = slateEditor.selection ? slateRangeToSelection({
10757
- schema: editorActor.getSnapshot().context.schema,
10758
- editor: slateEditor,
10759
- range: slateEditor.selection
10760
- }) : null;
10761
- selection_3 && editorActor.send({
10762
- type: "update selection",
10763
- selection: selection_3
10764
- });
10765
- }
10766
- }, [editorActor, onFocus, slateEditor, relayActor]), handleClick = useCallback((event_3) => {
10774
+ onFocus && onFocus(event_2), event_2.isDefaultPrevented() || relayActor.send({
10775
+ type: "focused",
10776
+ event: event_2
10777
+ });
10778
+ }, [onFocus, relayActor]), handleClick = useCallback((event_3) => {
10767
10779
  if (onClick && onClick(event_3), event_3.isDefaultPrevented() || event_3.isPropagationStopped())
10768
10780
  return;
10769
10781
  const position_3 = getEventPosition({