@portabletext/editor 4.3.4 → 4.3.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
@@ -6,7 +6,7 @@ import { Element as Element$1, Text, Range, Editor, Node, Point, Path, Transform
6
6
  import { useSlateStatic, useSlateSelector, ReactEditor, useSlate, Editable, withReact, Slate } from "slate-react";
7
7
  import rawDebug from "debug";
8
8
  import { DOMEditor, isDOMNode, EDITOR_TO_PENDING_SELECTION, IS_FOCUSED, IS_READ_ONLY } from "slate-dom";
9
- import { getBlockEndPoint, getBlockStartPoint, getBlockKeyFromSelectionPoint, isSelectionCollapsed, isKeyedSegment, isEqualSelectionPoints, getChildKeyFromSelectionPoint, blockOffsetToSpanSelectionPoint, defaultKeyGenerator, parseBlocks, parseBlock, isListBlock, isTypedObject, getSelectionStartPoint as getSelectionStartPoint$1, getSelectionEndPoint as getSelectionEndPoint$1, parseAnnotation, parseMarkDefs, parseSpan, parseInlineObject } from "./_chunks-es/util.slice-blocks.js";
9
+ import { getBlockEndPoint, getBlockStartPoint, getBlockKeyFromSelectionPoint, isSelectionCollapsed, isKeyedSegment, isEqualSelectionPoints, getChildKeyFromSelectionPoint, blockOffsetToSpanSelectionPoint, defaultKeyGenerator, parseBlocks, parseBlock, isListBlock, isTypedObject, getSelectionStartPoint as getSelectionStartPoint$1, getSelectionEndPoint as getSelectionEndPoint$1, parseAnnotation, parseMarkDefs, parseSpan, parseInlineObject, isEqualPathSegments } from "./_chunks-es/util.slice-blocks.js";
10
10
  import { isTextBlock, isSpan, compileSchema } from "@portabletext/schema";
11
11
  import { defineSchema } from "@portabletext/schema";
12
12
  import { isEmptyTextBlock, sliceTextBlock, getTextBlockText } from "./_chunks-es/util.slice-text-block.js";
@@ -283,7 +283,7 @@ function isEqualToEmptyEditor(initialValue, blocks, schemaTypes) {
283
283
  if (!Element$1.isElement(firstBlock) || firstBlock._type !== schemaTypes.block.name || "listItem" in firstBlock || !("style" in firstBlock) || firstBlock.style !== schemaTypes.styles.at(0)?.name || !Array.isArray(firstBlock.children) || firstBlock.children.length !== 1)
284
284
  return !1;
285
285
  const firstChild = firstBlock.children.at(0);
286
- return !(!firstChild || !Text.isText(firstChild) || !("_type" in firstChild) || firstChild._type !== schemaTypes.span.name || firstChild.text !== "" || firstChild.marks?.join("") || Object.keys(firstBlock).some((key) => key !== "_type" && key !== "_key" && key !== "children" && key !== "markDefs" && key !== "style") || isEqualValues({
286
+ return !(!firstChild || !Text.isText(firstChild) || !("_type" in firstChild) || firstChild._type !== schemaTypes.span.name || firstChild.text !== "" || firstChild.marks?.join("") || "markDefs" in firstBlock && Array.isArray(firstBlock.markDefs) && firstBlock.markDefs.length > 0 || Object.keys(firstBlock).some((key) => key !== "_type" && key !== "_key" && key !== "children" && key !== "markDefs" && key !== "style") || isEqualValues({
287
287
  schema: schemaTypes
288
288
  }, initialValue, [firstBlock]));
289
289
  }
@@ -5218,11 +5218,21 @@ function mergeNodePatch(schema, children, operation, beforeValue) {
5218
5218
  }, updatedBlock.children[operation.path[1] - 1]) ? updatedBlock.children[operation.path[1] - 1] : void 0, removedSpan = block.children[operation.path[1]] && isSpan({
5219
5219
  schema
5220
5220
  }, block.children[operation.path[1]]) ? block.children[operation.path[1]] : void 0;
5221
- updatedSpan && (block.children.filter((span) => span._key === updatedSpan._key).length === 1 ? patches.push(set(updatedSpan.text, [{
5222
- _key: block._key
5223
- }, "children", {
5224
- _key: updatedSpan._key
5225
- }, "text"])) : console.warn(`Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`, JSON.stringify(block, null, 2))), removedSpan && (block.children.filter((span) => span._key === removedSpan._key).length === 1 ? patches.push(unset([{
5221
+ if (updatedSpan) {
5222
+ const spansMatchingKey = block.children.filter((span) => span._key === updatedSpan._key);
5223
+ if (spansMatchingKey.length === 1) {
5224
+ const prevSpan = spansMatchingKey[0];
5225
+ isSpan({
5226
+ schema
5227
+ }, prevSpan) && prevSpan.text !== updatedSpan.text && patches.push(set(updatedSpan.text, [{
5228
+ _key: block._key
5229
+ }, "children", {
5230
+ _key: updatedSpan._key
5231
+ }, "text"]));
5232
+ } else
5233
+ console.warn(`Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`, JSON.stringify(block, null, 2));
5234
+ }
5235
+ removedSpan && (block.children.filter((span) => span._key === removedSpan._key).length === 1 ? patches.push(unset([{
5226
5236
  _key: block._key
5227
5237
  }, "children", {
5228
5238
  _key: removedSpan._key
@@ -11317,6 +11327,13 @@ function sortByPriority(items) {
11317
11327
  result.includes(item) || result.push(item);
11318
11328
  return [...result, ...itemsWithoutPriority];
11319
11329
  }
11330
+ function pathsOverlap(pathA, pathB) {
11331
+ const minLength = Math.min(pathA.length, pathB.length);
11332
+ for (let i = 0; i < minLength; i++)
11333
+ if (!isEqualPathSegments(pathA[i], pathB[i]))
11334
+ return !1;
11335
+ return !0;
11336
+ }
11320
11337
  function rerouteExternalBehaviorEvent({
11321
11338
  event,
11322
11339
  slateEditor
@@ -11412,6 +11429,20 @@ const editorMachine = setup({
11412
11429
  "clear pending events": assign({
11413
11430
  pendingEvents: []
11414
11431
  }),
11432
+ "discard conflicting pending patches": assign({
11433
+ pendingEvents: ({
11434
+ context,
11435
+ event
11436
+ }) => {
11437
+ if (event.type !== "patches")
11438
+ return context.pendingEvents;
11439
+ const incomingPaths = event.patches.map((patch) => patch.path);
11440
+ return context.pendingEvents.filter((pendingEvent) => pendingEvent.type !== "internal.patch" ? !0 : !incomingPaths.some((incomingPath) => pathsOverlap(pendingEvent.patch.path, incomingPath)));
11441
+ }
11442
+ }),
11443
+ "discard all pending events": assign({
11444
+ pendingEvents: []
11445
+ }),
11415
11446
  "defer incoming patches": assign({
11416
11447
  pendingIncomingPatchesEvents: ({
11417
11448
  context,
@@ -11811,7 +11842,13 @@ const editorMachine = setup({
11811
11842
  }, {
11812
11843
  actions: "defer event",
11813
11844
  target: "#editor.setup.set up.writing.dirty"
11814
- }]
11845
+ }],
11846
+ patches: {
11847
+ actions: "discard conflicting pending patches"
11848
+ },
11849
+ "syncing value": {
11850
+ actions: "discard all pending events"
11851
+ }
11815
11852
  }
11816
11853
  }
11817
11854
  }