@portabletext/editor 7.10.6 → 7.10.8-crx.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
@@ -18,7 +18,7 @@ import { setup, fromCallback, assign, and, assertEvent, enqueueActions, emit, no
18
18
  import { htmlToPortableText } from "@portabletext/html";
19
19
  import { toHTML } from "@portabletext/to-html";
20
20
  import { markdownToPortableText, portableTextToMarkdown } from "@portabletext/markdown";
21
- import { applyAll, unset, set, insert, setIfMissing, diffMatchPatch as diffMatchPatch$1 } from "@portabletext/patches";
21
+ import { unset, applyAll, set, insert, setIfMissing, diffMatchPatch as diffMatchPatch$1 } from "@portabletext/patches";
22
22
  import "xstate/guards";
23
23
  import { EditorContext as EditorContext$1 } from "./_chunks-es/use-editor.js";
24
24
  import { useEditor } from "./_chunks-es/use-editor.js";
@@ -7950,6 +7950,17 @@ function applyOperation(editor, op) {
7950
7950
  let {
7951
7951
  node
7952
7952
  } = op;
7953
+ if (!targetsStructuralChildren(editor, path2)) {
7954
+ applyOnRootBlock(editor, path2, {
7955
+ type: "insert",
7956
+ path: path2.slice(1),
7957
+ position: op.position,
7958
+ // Sidecar array members aren't necessarily objects (e.g. `marks`
7959
+ // holds strings), so the node is plain JSON data here.
7960
+ items: [node]
7961
+ }), transformSelection = !0;
7962
+ break;
7963
+ }
7953
7964
  modifyChildren(editor, parentPath(path2), (children) => {
7954
7965
  !editor.isProcessingRemoteChanges && !editor.isUndoing && !editor.isRedoing && node._key !== void 0 && children.some((sibling) => sibling._key === node._key) && (node = {
7955
7966
  ...node,
@@ -8038,6 +8049,10 @@ function applyOperation(editor, op) {
8038
8049
  if (setNodePath.length === 0)
8039
8050
  break;
8040
8051
  if (setPropertyPath.length === 0) {
8052
+ if (!targetsStructuralChildren(editor, setNodePath)) {
8053
+ applyOnRootBlock(editor, path2, set(value, path2.slice(1))), transformSelection = !0;
8054
+ break;
8055
+ }
8041
8056
  value !== null && typeof value == "object" && !Array.isArray(value) && modifyDescendant(editor, setNodePath, () => value), transformSelection = !0;
8042
8057
  break;
8043
8058
  }
@@ -8079,6 +8094,18 @@ function applyOperation(editor, op) {
8079
8094
  break;
8080
8095
  }
8081
8096
  const lastSegment = path2[path2.length - 1];
8097
+ if ((isKeyedSegment(lastSegment) || typeof lastSegment == "number") && !targetsStructuralChildren(editor, path2)) {
8098
+ if (!op.inverse && !editor.isProcessingRemoteChanges) {
8099
+ const arrayValue = getValue(editor.snapshot.context.value, path2.slice(0, -1));
8100
+ Array.isArray(arrayValue) && (op.inverse = {
8101
+ type: "set",
8102
+ path: path2.slice(0, -1),
8103
+ value: arrayValue
8104
+ });
8105
+ }
8106
+ applyOnRootBlock(editor, path2, unset(path2.slice(1))), transformSelection = !0;
8107
+ break;
8108
+ }
8082
8109
  if (isKeyedSegment(lastSegment) || typeof lastSegment == "number") {
8083
8110
  if (editor.snapshot.context.selection) {
8084
8111
  let selection = {
@@ -8223,6 +8250,29 @@ function applyOperation(editor, op) {
8223
8250
  });
8224
8251
  }
8225
8252
  }
8253
+ function targetsStructuralChildren(editor, path2) {
8254
+ if (path2.length < 2)
8255
+ return !0;
8256
+ const fieldSegment = path2[path2.length - 2];
8257
+ return typeof fieldSegment != "string" ? !0 : getChildFieldName({
8258
+ schema: editor.snapshot.context.schema,
8259
+ containers: editor.snapshot.context.containers,
8260
+ value: editor.snapshot.context.value
8261
+ }, path2.slice(0, -2)) === fieldSegment;
8262
+ }
8263
+ function applyOnRootBlock(editor, path2, patch) {
8264
+ const blockSegment = findBlockSegment(path2);
8265
+ if (!blockSegment)
8266
+ return;
8267
+ const blockIndex = resolveBlockIndex(editor, blockSegment);
8268
+ if (blockIndex === -1)
8269
+ return;
8270
+ const block = editor.snapshot.context.value[blockIndex];
8271
+ if (!block)
8272
+ return;
8273
+ const updatedBlock = applyAll(block, [patch]), newValue = editor.snapshot.context.value.slice();
8274
+ newValue[blockIndex] = updatedBlock, editor.snapshot.context.value = newValue;
8275
+ }
8226
8276
  function replaceLastSegment(path2, segment) {
8227
8277
  if (path2.length === 0)
8228
8278
  return [segment];
@@ -10124,8 +10174,19 @@ function createApplyPatch(context) {
10124
10174
  };
10125
10175
  }
10126
10176
  function diffMatchPatch(editor, patch) {
10127
- if (patch.path.at(-1) !== "text")
10128
- return !1;
10177
+ if (patch.path.at(-1) !== "text") {
10178
+ const currentValue = getValue(editor.snapshot.context.value, patch.path);
10179
+ if (typeof currentValue != "string")
10180
+ return !1;
10181
+ const [newValue2] = apply(parse(patch.value), currentValue, {
10182
+ allowExceedingIndices: !0
10183
+ });
10184
+ return editor.apply({
10185
+ type: "set",
10186
+ path: patch.path,
10187
+ value: newValue2
10188
+ }), !0;
10189
+ }
10129
10190
  const spanPath = patch.path.slice(0, -1), spanEntry = getNode(editor.snapshot, spanPath);
10130
10191
  if (!spanEntry || !isSpan({
10131
10192
  schema: editor.snapshot.context.schema
@@ -18410,65 +18471,81 @@ async function updateValue({
18410
18471
  }) {
18411
18472
  let doneSyncing = !1, isChanged = !1, isValid = !0;
18412
18473
  const hadSelection = !!editorEngine.snapshot.context.selection;
18413
- if ((!value || value.length === 0) && (clearEditor({
18414
- editorEngine,
18415
- doneSyncing
18416
- }), isChanged = !0), value && value.length > 0)
18417
- if (streamBlocks)
18418
- await new Promise((resolve) => {
18419
- if (doneSyncing) {
18420
- resolve();
18474
+ try {
18475
+ if ((!value || value.length === 0) && (clearEditor({
18476
+ editorEngine,
18477
+ doneSyncing
18478
+ }), isChanged = !0), value && value.length > 0)
18479
+ if (streamBlocks)
18480
+ await new Promise((resolve, reject) => {
18481
+ if (doneSyncing) {
18482
+ resolve();
18483
+ return;
18484
+ }
18485
+ isChanged = removeExtraBlocks({
18486
+ editorEngine,
18487
+ value
18488
+ }), (async () => {
18489
+ try {
18490
+ for await (const [currentBlock, currentBlockIndex] of getStreamedBlocks({
18491
+ value
18492
+ })) {
18493
+ const {
18494
+ blockChanged,
18495
+ blockValid
18496
+ } = syncBlock({
18497
+ context,
18498
+ sendBack,
18499
+ block: currentBlock,
18500
+ index: currentBlockIndex,
18501
+ editorEngine,
18502
+ value
18503
+ });
18504
+ if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !isValid)
18505
+ break;
18506
+ }
18507
+ resolve();
18508
+ } catch (error) {
18509
+ reject(error);
18510
+ }
18511
+ })();
18512
+ });
18513
+ else {
18514
+ if (doneSyncing)
18421
18515
  return;
18422
- }
18423
18516
  isChanged = removeExtraBlocks({
18424
18517
  editorEngine,
18425
18518
  value
18426
- }), (async () => {
18427
- for await (const [currentBlock, currentBlockIndex] of getStreamedBlocks({
18428
- value
18429
- })) {
18430
- const {
18431
- blockChanged,
18432
- blockValid
18433
- } = syncBlock({
18434
- context,
18435
- sendBack,
18436
- block: currentBlock,
18437
- index: currentBlockIndex,
18438
- editorEngine,
18439
- value
18440
- });
18441
- if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !isValid)
18442
- break;
18443
- }
18444
- resolve();
18445
- })();
18446
- });
18447
- else {
18448
- if (doneSyncing)
18449
- return;
18450
- isChanged = removeExtraBlocks({
18451
- editorEngine,
18452
- value
18453
- });
18454
- let index = 0;
18455
- for (const block of value) {
18456
- const {
18457
- blockChanged,
18458
- blockValid
18459
- } = syncBlock({
18460
- context,
18461
- sendBack,
18462
- block,
18463
- index,
18464
- editorEngine,
18465
- value
18466
18519
  });
18467
- if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !blockValid)
18468
- break;
18469
- index++;
18520
+ let index = 0;
18521
+ for (const block of value) {
18522
+ const {
18523
+ blockChanged,
18524
+ blockValid
18525
+ } = syncBlock({
18526
+ context,
18527
+ sendBack,
18528
+ block,
18529
+ index,
18530
+ editorEngine,
18531
+ value
18532
+ });
18533
+ if (isChanged = blockChanged || isChanged, isValid = isValid && blockValid, !blockValid)
18534
+ break;
18535
+ index++;
18536
+ }
18470
18537
  }
18471
- }
18538
+ } catch (err) {
18539
+ console.error(err), sendBack({
18540
+ type: "invalid value",
18541
+ resolution: null,
18542
+ value
18543
+ }), doneSyncing = !0, sendBack({
18544
+ type: "done syncing",
18545
+ value
18546
+ });
18547
+ return;
18548
+ }
18472
18549
  if (!isValid) {
18473
18550
  debug.syncValue("Invalid value, returning"), doneSyncing = !0, sendBack({
18474
18551
  type: "done syncing",
@@ -18717,6 +18794,10 @@ function updateBlock({
18717
18794
  const childNode = oldEngineBlock.children[childIndex];
18718
18795
  if (!childNode)
18719
18796
  return;
18797
+ if (!liveBlockHasChildKey(editorEngine, oldEngineBlock._key, childNode._key)) {
18798
+ debug.syncValue("Skipping superfluous-child unset; _key not present in live children", childNode._key);
18799
+ return;
18800
+ }
18720
18801
  editorEngine.apply({
18721
18802
  type: "unset",
18722
18803
  path: [{
@@ -18767,14 +18848,14 @@ function updateBlock({
18767
18848
  const oldChild = oldEngineBlock.children[currentBlockChildIndex];
18768
18849
  if (!oldChild)
18769
18850
  return;
18770
- editorEngine.apply({
18851
+ liveBlockHasChildKey(editorEngine, oldEngineBlock._key, oldChild._key) ? editorEngine.apply({
18771
18852
  type: "unset",
18772
18853
  path: [{
18773
18854
  _key: oldEngineBlock._key
18774
18855
  }, "children", {
18775
18856
  _key: oldChild._key
18776
18857
  }]
18777
- });
18858
+ }) : debug.syncValue("Skipping replace-child unset; _key not present in live children", oldChild._key);
18778
18859
  const prevSibling = currentBlockChildIndex > 0 ? engineBlock.children[currentBlockChildIndex - 1] : void 0;
18779
18860
  prevSibling ? editorEngine.apply({
18780
18861
  type: "insert",
@@ -18818,6 +18899,12 @@ function updateBlock({
18818
18899
  });
18819
18900
  }
18820
18901
  }
18902
+ function liveBlockHasChildKey(editorEngine, blockKey, childKey) {
18903
+ const liveBlock = editorEngine.snapshot.context.value.find((node) => node._key === blockKey);
18904
+ return !liveBlock || !isTextBlock({
18905
+ schema: editorEngine.snapshot.context.schema
18906
+ }, liveBlock) ? !1 : liveBlock.children.some((child) => child._key === childKey);
18907
+ }
18821
18908
  function createInternalEditor(config) {
18822
18909
  debug.setup("creating new editor instance");
18823
18910
  const subscriptions = [], editorActor = createActor(editorMachine, {