@portabletext/editor 1.16.0 → 1.16.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.cjs CHANGED
@@ -2262,6 +2262,19 @@ function findOperationTargetBlock(editor, operation) {
2262
2262
  let block;
2263
2263
  return operation.type === "set_selection" && editor.selection ? block = editor.children[editor.selection.focus.path[0]] : "path" in operation && (block = editor.children[operation.path[0]]), block;
2264
2264
  }
2265
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name), __await = function(promise, isYieldStar) {
2266
+ this[0] = promise, this[1] = isYieldStar;
2267
+ }, __asyncGenerator = (__this, __arguments, generator) => {
2268
+ var resume = (k, v, yes, no) => {
2269
+ try {
2270
+ var x = generator[k](v), isAwait = (v = x.value) instanceof __await, done = x.done;
2271
+ Promise.resolve(isAwait ? v[0] : v).then((y) => isAwait ? resume(k === "return" ? k : "next", v[1] ? { done: y.done, value: y.value } : y, yes, no) : yes({ value: y, done })).catch((e) => resume("throw", e, yes, no));
2272
+ } catch (e) {
2273
+ no(e);
2274
+ }
2275
+ }, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
2276
+ return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
2277
+ }, __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
2265
2278
  const syncValueCallback = ({
2266
2279
  sendBack,
2267
2280
  input
@@ -2270,7 +2283,8 @@ const syncValueCallback = ({
2270
2283
  context: input.context,
2271
2284
  sendBack,
2272
2285
  slateEditor: input.slateEditor,
2273
- value: input.value
2286
+ value: input.value,
2287
+ streamBlocks: input.streamBlocks
2274
2288
  });
2275
2289
  }, syncValueLogic = xstate.fromCallback(syncValueCallback), syncMachine = xstate.setup({
2276
2290
  types: {
@@ -2280,10 +2294,13 @@ const syncValueCallback = ({
2280
2294
  emitted: {}
2281
2295
  },
2282
2296
  actions: {
2297
+ "assign initial value synced": xstate.assign({
2298
+ initialValueSynced: !0
2299
+ }),
2283
2300
  "assign readOnly": xstate.assign({
2284
2301
  readOnly: ({
2285
- context
2286
- }) => !context.readOnly
2302
+ event
2303
+ }) => (xstate.assertEvent(event, "update readOnly"), event.readOnly)
2287
2304
  }),
2288
2305
  "assign pending value": xstate.assign({
2289
2306
  pendingValue: ({
@@ -2332,6 +2349,7 @@ const syncValueCallback = ({
2332
2349
  context: ({
2333
2350
  input
2334
2351
  }) => ({
2352
+ initialValueSynced: !1,
2335
2353
  isProcessingLocalChanges: !1,
2336
2354
  keyGenerator: input.keyGenerator,
2337
2355
  schema: input.schema,
@@ -2352,7 +2370,7 @@ const syncValueCallback = ({
2352
2370
  isProcessingLocalChanges: !1
2353
2371
  })
2354
2372
  },
2355
- "toggle readOnly": {
2373
+ "update readOnly": {
2356
2374
  actions: ["assign readOnly"]
2357
2375
  }
2358
2376
  },
@@ -2402,6 +2420,7 @@ const syncValueCallback = ({
2402
2420
  schema: context.schema
2403
2421
  },
2404
2422
  slateEditor: context.slateEditor,
2423
+ streamBlocks: !context.initialValueSynced,
2405
2424
  value: (_a = context.pendingValue) != null ? _a : void 0
2406
2425
  };
2407
2426
  }
@@ -2437,20 +2456,21 @@ const syncValueCallback = ({
2437
2456
  },
2438
2457
  "done syncing": [{
2439
2458
  guard: "value changed while syncing",
2440
- actions: ["assign previous value", "emit done syncing"],
2459
+ actions: ["assign previous value", "emit done syncing", "assign initial value synced"],
2441
2460
  reenter: !0
2442
2461
  }, {
2443
2462
  target: "idle",
2444
- actions: ["clear pending value", "assign previous value", "emit done syncing"]
2463
+ actions: ["clear pending value", "assign previous value", "emit done syncing", "assign initial value synced"]
2445
2464
  }]
2446
2465
  }
2447
2466
  }
2448
2467
  }
2449
2468
  }), debug$i = debugWithName("hook:useSyncValue");
2450
- function updateValue({
2469
+ async function updateValue({
2451
2470
  context,
2452
2471
  sendBack,
2453
2472
  slateEditor,
2473
+ streamBlocks,
2454
2474
  value
2455
2475
  }) {
2456
2476
  let isChanged = !1, isValid = !0;
@@ -2475,31 +2495,48 @@ function updateValue({
2475
2495
  const slateValueFromProps = toSlateValue(value, {
2476
2496
  schemaTypes: context.schema
2477
2497
  });
2478
- slate.Editor.withoutNormalizing(slateEditor, () => {
2479
- withRemoteChanges(slateEditor, () => {
2480
- withoutPatching(slateEditor, () => {
2481
- const childrenLength = slateEditor.children.length;
2482
- if (slateValueFromProps.length < childrenLength) {
2483
- for (let i = childrenLength - 1; i > slateValueFromProps.length - 1; i--)
2484
- slate.Transforms.removeNodes(slateEditor, {
2485
- at: [i]
2486
- });
2487
- isChanged = !0;
2488
- }
2489
- for (const [currentBlockIndex, currentBlock] of slateValueFromProps.entries()) {
2490
- const {
2491
- blockChanged,
2492
- blockValid
2493
- } = syncBlock({
2494
- context,
2495
- sendBack,
2496
- block: currentBlock,
2497
- index: currentBlockIndex,
2498
- slateEditor,
2499
- value
2500
- });
2501
- isChanged = blockChanged || isChanged, isValid = isValid && blockValid;
2502
- }
2498
+ await new Promise((resolve) => {
2499
+ slate.Editor.withoutNormalizing(slateEditor, () => {
2500
+ withRemoteChanges(slateEditor, () => {
2501
+ withoutPatching(slateEditor, async () => {
2502
+ const childrenLength = slateEditor.children.length;
2503
+ if (slateValueFromProps.length < childrenLength) {
2504
+ for (let i = childrenLength - 1; i > slateValueFromProps.length - 1; i--)
2505
+ slate.Transforms.removeNodes(slateEditor, {
2506
+ at: [i]
2507
+ });
2508
+ isChanged = !0;
2509
+ }
2510
+ try {
2511
+ for (var iter = __forAwait(getBlocks({
2512
+ slateValue: slateValueFromProps,
2513
+ streamBlocks
2514
+ })), more, temp, error; more = !(temp = await iter.next()).done; more = !1) {
2515
+ const [currentBlock, currentBlockIndex] = temp.value, {
2516
+ blockChanged,
2517
+ blockValid
2518
+ } = syncBlock({
2519
+ context,
2520
+ sendBack,
2521
+ block: currentBlock,
2522
+ index: currentBlockIndex,
2523
+ slateEditor,
2524
+ value
2525
+ });
2526
+ isChanged = blockChanged || isChanged, isValid = isValid && blockValid;
2527
+ }
2528
+ } catch (temp2) {
2529
+ error = [temp2];
2530
+ } finally {
2531
+ try {
2532
+ more && (temp = iter.return) && await temp.call(iter);
2533
+ } finally {
2534
+ if (error)
2535
+ throw error[0];
2536
+ }
2537
+ }
2538
+ resolve();
2539
+ });
2503
2540
  });
2504
2541
  });
2505
2542
  });
@@ -2546,6 +2583,29 @@ function updateValue({
2546
2583
  value
2547
2584
  });
2548
2585
  }
2586
+ function getBlocks(_0) {
2587
+ return __asyncGenerator(this, arguments, function* ({
2588
+ slateValue,
2589
+ streamBlocks
2590
+ }) {
2591
+ let index = 0;
2592
+ try {
2593
+ for (var iter = __forAwait(slateValue), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = !1) {
2594
+ const block = temp.value;
2595
+ streamBlocks && (yield new __await(new Promise((resolve) => setTimeout(resolve, 0)))), yield [block, index], index++;
2596
+ }
2597
+ } catch (temp2) {
2598
+ error = [temp2];
2599
+ } finally {
2600
+ try {
2601
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
2602
+ } finally {
2603
+ if (error)
2604
+ throw error[0];
2605
+ }
2606
+ }
2607
+ });
2608
+ }
2549
2609
  function syncBlock({
2550
2610
  context,
2551
2611
  sendBack,
@@ -2554,33 +2614,38 @@ function syncBlock({
2554
2614
  slateEditor,
2555
2615
  value
2556
2616
  }) {
2557
- var _a, _b, _c, _d, _e;
2558
2617
  let blockChanged = !1, blockValid = !0;
2559
- const currentBlock = block, currentBlockIndex = index, oldBlock = slateEditor.children[currentBlockIndex];
2560
- if (oldBlock && !isEqual__default.default(currentBlock, oldBlock) && blockValid) {
2561
- const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2562
- !validation.valid && (_a = validation.resolution) != null && _a.autoResolve && ((_b = validation.resolution) == null ? void 0 : _b.patches.length) > 0 && !context.readOnly && context.previousValue && context.previousValue !== value && (console.warn(`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${(_c = validation.resolution) == null ? void 0 : _c.description}`), validation.resolution.patches.forEach((patch) => {
2563
- sendBack({
2564
- type: "patch",
2565
- patch
2618
+ const currentBlock = block, currentBlockIndex = index, oldBlock = slateEditor.children[currentBlockIndex], hasChanges = oldBlock && !isEqual__default.default(currentBlock, oldBlock);
2619
+ return slate.Editor.withoutNormalizing(slateEditor, () => {
2620
+ withRemoteChanges(slateEditor, () => {
2621
+ withoutPatching(slateEditor, () => {
2622
+ var _a, _b, _c, _d, _e;
2623
+ if (hasChanges && blockValid) {
2624
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2625
+ !validation.valid && (_a = validation.resolution) != null && _a.autoResolve && ((_b = validation.resolution) == null ? void 0 : _b.patches.length) > 0 && !context.readOnly && context.previousValue && context.previousValue !== value && (console.warn(`${validation.resolution.action} for block with _key '${validationValue[0]._key}'. ${(_c = validation.resolution) == null ? void 0 : _c.description}`), validation.resolution.patches.forEach((patch) => {
2626
+ sendBack({
2627
+ type: "patch",
2628
+ patch
2629
+ });
2630
+ })), validation.valid || (_d = validation.resolution) != null && _d.autoResolve ? (oldBlock._key === currentBlock._key ? (debug$i.enabled && debug$i("Updating block", oldBlock, currentBlock), _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex)) : (debug$i.enabled && debug$i("Replacing block", oldBlock, currentBlock), _replaceBlock(slateEditor, currentBlock, currentBlockIndex)), blockChanged = !0) : (sendBack({
2631
+ type: "invalid value",
2632
+ resolution: validation.resolution,
2633
+ value
2634
+ }), blockValid = !1);
2635
+ }
2636
+ if (!oldBlock && blockValid) {
2637
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2638
+ debug$i.enabled && debug$i("Validating and inserting new block in the end of the value", currentBlock), validation.valid || (_e = validation.resolution) != null && _e.autoResolve ? slate.Transforms.insertNodes(slateEditor, currentBlock, {
2639
+ at: [currentBlockIndex]
2640
+ }) : (debug$i("Invalid", validation), sendBack({
2641
+ type: "invalid value",
2642
+ resolution: validation.resolution,
2643
+ value
2644
+ }), blockValid = !1);
2645
+ }
2566
2646
  });
2567
- })), validation.valid || (_d = validation.resolution) != null && _d.autoResolve ? (oldBlock._key === currentBlock._key ? (debug$i.enabled && debug$i("Updating block", oldBlock, currentBlock), _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex)) : (debug$i.enabled && debug$i("Replacing block", oldBlock, currentBlock), _replaceBlock(slateEditor, currentBlock, currentBlockIndex)), blockChanged = !0) : (sendBack({
2568
- type: "invalid value",
2569
- resolution: validation.resolution,
2570
- value
2571
- }), blockValid = !1);
2572
- }
2573
- if (!oldBlock && blockValid) {
2574
- const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2575
- debug$i.enabled && debug$i("Validating and inserting new block in the end of the value", currentBlock), validation.valid || (_e = validation.resolution) != null && _e.autoResolve ? slate.Transforms.insertNodes(slateEditor, currentBlock, {
2576
- at: [currentBlockIndex]
2577
- }) : (debug$i("Invalid", validation), sendBack({
2578
- type: "invalid value",
2579
- resolution: validation.resolution,
2580
- value
2581
- }), blockValid = !1);
2582
- }
2583
- return {
2647
+ });
2648
+ }), {
2584
2649
  blockChanged,
2585
2650
  blockValid
2586
2651
  };
@@ -2642,7 +2707,7 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
2642
2707
  }
2643
2708
  const debug$h = debugWithName("component:PortableTextEditor:Synchronizer");
2644
2709
  function Synchronizer(props) {
2645
- const $ = reactCompilerRuntime.c(41), {
2710
+ const $ = reactCompilerRuntime.c(40), {
2646
2711
  editorActor,
2647
2712
  slateEditor
2648
2713
  } = props, value = react.useSelector(props.editorActor, _temp), readOnly = react.useSelector(props.editorActor, _temp2);
@@ -2697,23 +2762,22 @@ function Synchronizer(props) {
2697
2762
  subscription_0.unsubscribe();
2698
2763
  };
2699
2764
  }, t10 = [props.editorActor, syncActorRef], $[21] = props.editorActor, $[22] = syncActorRef, $[23] = t10, $[24] = t9) : (t10 = $[23], t9 = $[24]), React.useEffect(t9, t10);
2700
- let t11;
2701
- $[25] !== syncActorRef ? (t11 = () => {
2765
+ let t11, t12;
2766
+ $[25] !== readOnly || $[26] !== syncActorRef ? (t11 = () => {
2702
2767
  syncActorRef.send({
2703
- type: "toggle readOnly"
2768
+ type: "update readOnly",
2769
+ readOnly
2704
2770
  });
2705
- }, $[25] = syncActorRef, $[26] = t11) : t11 = $[26];
2706
- let t12;
2707
- $[27] !== readOnly || $[28] !== syncActorRef ? (t12 = [syncActorRef, readOnly], $[27] = readOnly, $[28] = syncActorRef, $[29] = t12) : t12 = $[29], React.useEffect(t11, t12);
2771
+ }, t12 = [syncActorRef, readOnly], $[25] = readOnly, $[26] = syncActorRef, $[27] = t11, $[28] = t12) : (t11 = $[27], t12 = $[28]), React.useEffect(t11, t12);
2708
2772
  let t13, t14;
2709
- $[30] !== syncActorRef || $[31] !== value ? (t13 = () => {
2773
+ $[29] !== syncActorRef || $[30] !== value ? (t13 = () => {
2710
2774
  debug$h("Value from props changed, syncing new value"), syncActorRef.send({
2711
2775
  type: "update value",
2712
2776
  value
2713
2777
  });
2714
- }, t14 = [syncActorRef, value], $[30] = syncActorRef, $[31] = value, $[32] = t13, $[33] = t14) : (t13 = $[32], t14 = $[33]), React.useEffect(t13, t14);
2778
+ }, t14 = [syncActorRef, value], $[29] = syncActorRef, $[30] = value, $[31] = t13, $[32] = t14) : (t13 = $[31], t14 = $[32]), React.useEffect(t13, t14);
2715
2779
  let t15;
2716
- $[34] !== editorActor || $[35] !== mutationActorRef ? (t15 = () => {
2780
+ $[33] !== editorActor || $[34] !== mutationActorRef ? (t15 = () => {
2717
2781
  debug$h("Subscribing to patch events");
2718
2782
  const sub = editorActor.on("patch", (event_1) => {
2719
2783
  mutationActorRef.send(event_1);
@@ -2721,9 +2785,9 @@ function Synchronizer(props) {
2721
2785
  return () => {
2722
2786
  debug$h("Unsubscribing to patch events"), sub.unsubscribe();
2723
2787
  };
2724
- }, $[34] = editorActor, $[35] = mutationActorRef, $[36] = t15) : t15 = $[36];
2788
+ }, $[33] = editorActor, $[34] = mutationActorRef, $[35] = t15) : t15 = $[35];
2725
2789
  let t16;
2726
- return $[37] !== editorActor || $[38] !== mutationActorRef || $[39] !== slateEditor ? (t16 = [editorActor, mutationActorRef, slateEditor], $[37] = editorActor, $[38] = mutationActorRef, $[39] = slateEditor, $[40] = t16) : t16 = $[40], React.useEffect(t15, t16), null;
2790
+ return $[36] !== editorActor || $[37] !== mutationActorRef || $[38] !== slateEditor ? (t16 = [editorActor, mutationActorRef, slateEditor], $[36] = editorActor, $[37] = mutationActorRef, $[38] = slateEditor, $[39] = t16) : t16 = $[39], React.useEffect(t15, t16), null;
2727
2791
  }
2728
2792
  function _temp2(s_0) {
2729
2793
  return s_0.matches({
@@ -6179,7 +6243,10 @@ const editorMachine = xstate.setup({
6179
6243
  },
6180
6244
  "read only": {
6181
6245
  on: {
6182
- "toggle readOnly": {
6246
+ "update readOnly": {
6247
+ guard: ({
6248
+ event
6249
+ }) => !event.readOnly,
6183
6250
  target: "#editor.edit mode.editable",
6184
6251
  actions: ["emit editable"]
6185
6252
  }
@@ -6189,7 +6256,10 @@ const editorMachine = xstate.setup({
6189
6256
  },
6190
6257
  editable: {
6191
6258
  on: {
6192
- "toggle readOnly": {
6259
+ "update readOnly": {
6260
+ guard: ({
6261
+ event
6262
+ }) => event.readOnly,
6193
6263
  target: "#editor.edit mode.read only.read only",
6194
6264
  actions: ["emit read only"]
6195
6265
  },
@@ -6432,11 +6502,13 @@ class PortableTextEditor extends React.Component {
6432
6502
  }), this.schemaTypes = this.editor._internal.editorActor.getSnapshot().context.schema, this.editable = this.editor._internal.editable;
6433
6503
  }
6434
6504
  componentDidUpdate(prevProps) {
6505
+ var _a;
6435
6506
  !this.props.editor && !prevProps.editor && this.props.schemaType !== prevProps.schemaType && (this.schemaTypes = createEditorSchema(this.props.schemaType.hasOwnProperty("jsonType") ? this.props.schemaType : compileType(this.props.schemaType)), this.editor._internal.editorActor.send({
6436
6507
  type: "update schema",
6437
6508
  schema: this.schemaTypes
6438
6509
  })), !this.props.editor && !prevProps.editor && (this.props.readOnly !== prevProps.readOnly && this.editor._internal.editorActor.send({
6439
- type: "toggle readOnly"
6510
+ type: "update readOnly",
6511
+ readOnly: (_a = this.props.readOnly) != null ? _a : !1
6440
6512
  }), this.props.maxBlocks !== prevProps.maxBlocks && this.editor._internal.editorActor.send({
6441
6513
  type: "update maxBlocks",
6442
6514
  maxBlocks: this.props.maxBlocks === void 0 ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10)