@portabletext/editor 1.55.1 → 1.55.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
@@ -9,11 +9,10 @@ import { Element as Element$1, Text, Range, Editor, Node, Point, Transforms, Pat
9
9
  import { useSelected, useSlateSelector, useSlateStatic, withReact, ReactEditor, Slate, useSlate, Editable } from "slate-react";
10
10
  import debug$f from "debug";
11
11
  import { DOMEditor, isDOMNode } from "slate-dom";
12
- import { getBlockStartPoint, getBlockKeyFromSelectionPoint, isTextBlock, parseBlock, parseAnnotation, blockOffsetToSpanSelectionPoint, parseInlineObject, isKeyedSegment, isSpan$1 as isSpan, isListBlock, isTypedObject, getSelectionStartPoint, getSelectionEndPoint, getTextBlockText, parseBlocks } from "./_chunks-es/selection-point.js";
12
+ import { getBlockStartPoint, getBlockKeyFromSelectionPoint, getChildKeyFromSelectionPoint, isTextBlock, parseBlock, 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";
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 } from "@sanity/types";
17
16
  import { isSelectionCollapsed as isSelectionCollapsed$1, getFocusTextBlock, getFocusSpan as getFocusSpan$1, isSelectionExpanded, getFocusBlock as getFocusBlock$1, getSelectedValue, getFocusChild as getFocusChild$1 } from "./_chunks-es/selector.is-selection-expanded.js";
18
17
  import { getFocusInlineObject, getSelectedBlocks, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, isOverlappingSelection, isSelectingEntireBlocks, getTrimmedSelection, getSelectedSpans, getCaretWordSelection, getFocusBlockObject, getPreviousBlock, getNextBlock, isAtTheEndOfBlock, isAtTheStartOfBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, getActiveAnnotations as getActiveAnnotations$1 } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
19
18
  import getRandomValues from "get-random-values-esm";
@@ -32,6 +31,7 @@ import get from "lodash/get.js";
32
31
  import isUndefined from "lodash/isUndefined.js";
33
32
  import omitBy from "lodash/omitBy.js";
34
33
  import { createDraft, finishDraft } from "immer";
34
+ import { defineType, defineField } from "@sanity/types";
35
35
  import startCase from "lodash.startcase";
36
36
  import isPlainObject from "lodash/isPlainObject.js";
37
37
  function EditorEventListener(props) {
@@ -549,24 +549,31 @@ function getSlateRangeFromEvent(editor, event) {
549
549
  return range;
550
550
  }
551
551
  function toSlatePath(path, editor) {
552
- if (!editor)
552
+ const blockKey = getBlockKeyFromSelectionPoint({
553
+ path
554
+ });
555
+ if (!blockKey)
553
556
  return [];
554
- const [block, blockPath] = Array.from(Editor.nodes(editor, {
555
- at: [],
556
- match: (n) => isKeySegment(path[0]) && n._key === path[0]._key
557
- }))[0] || [void 0, void 0];
557
+ const blockIndex = editor.blockIndexMap.get(blockKey);
558
+ if (blockIndex === void 0)
559
+ return [];
560
+ const block = editor.children.at(blockIndex);
558
561
  if (!block || !Element$1.isElement(block))
559
562
  return [];
560
563
  if (editor.isVoid(block))
561
- return [blockPath[0], 0];
562
- const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual([{
563
- _key: child._key
564
- }], childPath));
565
- if (childIndex >= 0 && block.children[childIndex]) {
566
- const child = block.children[childIndex];
567
- return Element$1.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
568
- }
569
- return [blockPath[0], 0];
564
+ return [blockIndex, 0];
565
+ const childKey = getChildKeyFromSelectionPoint({
566
+ path
567
+ });
568
+ if (!childKey)
569
+ return [blockIndex, 0];
570
+ let childPath = [], childIndex = -1;
571
+ for (const child of block.children)
572
+ if (childIndex++, child._key === childKey) {
573
+ Element$1.isElement(child) && editor.isVoid(child) ? childPath = [childIndex, 0] : childPath = [childIndex];
574
+ break;
575
+ }
576
+ return [blockIndex].concat(childPath);
570
577
  }
571
578
  function toSlateRange(selection, editor) {
572
579
  if (!selection || !editor)
@@ -1305,11 +1312,15 @@ function compileType(rawType) {
1305
1312
  types: [rawType]
1306
1313
  }).get(rawType.name);
1307
1314
  }
1308
- function buildIndexMaps(context, value) {
1309
- const blockIndexMap = /* @__PURE__ */ new Map(), listIndexMap = /* @__PURE__ */ new Map(), levelIndexMap = /* @__PURE__ */ new Map();
1315
+ const levelIndexMap = /* @__PURE__ */ new Map();
1316
+ function buildIndexMaps(context, {
1317
+ blockIndexMap,
1318
+ listIndexMap
1319
+ }) {
1320
+ blockIndexMap.clear(), listIndexMap.clear(), levelIndexMap.clear();
1310
1321
  let previousListItem;
1311
- for (let blockIndex = 0; blockIndex < value.length; blockIndex++) {
1312
- const block = value.at(blockIndex);
1322
+ for (let blockIndex = 0; blockIndex < context.value.length; blockIndex++) {
1323
+ const block = context.value.at(blockIndex);
1313
1324
  if (block !== void 0) {
1314
1325
  if (blockIndexMap.set(block._key, blockIndex), !isTextBlock(context, block)) {
1315
1326
  levelIndexMap.clear(), previousListItem = void 0;
@@ -1357,10 +1368,6 @@ function buildIndexMaps(context, value) {
1357
1368
  }
1358
1369
  }
1359
1370
  }
1360
- return {
1361
- blockIndexMap,
1362
- listIndexMap
1363
- };
1364
1371
  }
1365
1372
  function createPlaceholderBlock(context) {
1366
1373
  return {
@@ -5233,12 +5240,13 @@ function pluginUpdateValue(context, editor) {
5233
5240
  apply2(operation);
5234
5241
  return;
5235
5242
  }
5236
- editor.value = applyOperationToPortableText(context, editor.value, operation);
5237
- const {
5238
- blockIndexMap,
5239
- listIndexMap
5240
- } = buildIndexMaps(context, editor.value);
5241
- editor.blockIndexMap = blockIndexMap, editor.listIndexMap = listIndexMap, apply2(operation);
5243
+ editor.value = applyOperationToPortableText(context, editor.value, operation), buildIndexMaps({
5244
+ schema: context.schema,
5245
+ value: editor.value
5246
+ }, {
5247
+ blockIndexMap: editor.blockIndexMap,
5248
+ listIndexMap: editor.listIndexMap
5249
+ }), apply2(operation);
5242
5250
  }, editor;
5243
5251
  }
5244
5252
  const withPlugins = (editor, options) => {
@@ -5271,12 +5279,13 @@ function createSlateEditor(config) {
5271
5279
  });
5272
5280
  KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {}), instance.decoratedRanges = [], instance.decoratorState = {}, instance.markState = void 0;
5273
5281
  const placeholderBlock = createPlaceholderBlock(config.editorActor.getSnapshot().context);
5274
- instance.value = [placeholderBlock];
5275
- const {
5276
- blockIndexMap,
5277
- listIndexMap
5278
- } = buildIndexMaps(config.editorActor.getSnapshot().context, instance.value);
5279
- instance.blockIndexMap = blockIndexMap, instance.listIndexMap = listIndexMap;
5282
+ instance.value = [placeholderBlock], instance.blockIndexMap = /* @__PURE__ */ new Map(), instance.listIndexMap = /* @__PURE__ */ new Map(), buildIndexMaps({
5283
+ schema: config.editorActor.getSnapshot().context.schema,
5284
+ value: instance.value
5285
+ }, {
5286
+ blockIndexMap: instance.blockIndexMap,
5287
+ listIndexMap: instance.listIndexMap
5288
+ });
5280
5289
  const initialValue = toSlateValue(instance.value, {
5281
5290
  schemaTypes: config.editorActor.getSnapshot().context.schema
5282
5291
  });