@portabletext/editor 1.16.1 → 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.js CHANGED
@@ -2282,6 +2282,19 @@ function findOperationTargetBlock(editor, operation) {
2282
2282
  let block;
2283
2283
  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;
2284
2284
  }
2285
+ var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name), __await = function(promise, isYieldStar) {
2286
+ this[0] = promise, this[1] = isYieldStar;
2287
+ }, __asyncGenerator = (__this, __arguments, generator) => {
2288
+ var resume = (k, v, yes, no) => {
2289
+ try {
2290
+ var x = generator[k](v), isAwait = (v = x.value) instanceof __await, done = x.done;
2291
+ 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));
2292
+ } catch (e) {
2293
+ no(e);
2294
+ }
2295
+ }, method = (k) => it[k] = (x) => new Promise((yes, no) => resume(k, x, yes, no)), it = {};
2296
+ return generator = generator.apply(__this, __arguments), it[__knownSymbol("asyncIterator")] = () => it, method("next"), method("throw"), method("return"), it;
2297
+ }, __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);
2285
2298
  const syncValueCallback = ({
2286
2299
  sendBack,
2287
2300
  input
@@ -2290,7 +2303,8 @@ const syncValueCallback = ({
2290
2303
  context: input.context,
2291
2304
  sendBack,
2292
2305
  slateEditor: input.slateEditor,
2293
- value: input.value
2306
+ value: input.value,
2307
+ streamBlocks: input.streamBlocks
2294
2308
  });
2295
2309
  }, syncValueLogic = fromCallback(syncValueCallback), syncMachine = setup({
2296
2310
  types: {
@@ -2300,6 +2314,9 @@ const syncValueCallback = ({
2300
2314
  emitted: {}
2301
2315
  },
2302
2316
  actions: {
2317
+ "assign initial value synced": assign({
2318
+ initialValueSynced: !0
2319
+ }),
2303
2320
  "assign readOnly": assign({
2304
2321
  readOnly: ({
2305
2322
  event
@@ -2352,6 +2369,7 @@ const syncValueCallback = ({
2352
2369
  context: ({
2353
2370
  input
2354
2371
  }) => ({
2372
+ initialValueSynced: !1,
2355
2373
  isProcessingLocalChanges: !1,
2356
2374
  keyGenerator: input.keyGenerator,
2357
2375
  schema: input.schema,
@@ -2422,6 +2440,7 @@ const syncValueCallback = ({
2422
2440
  schema: context.schema
2423
2441
  },
2424
2442
  slateEditor: context.slateEditor,
2443
+ streamBlocks: !context.initialValueSynced,
2425
2444
  value: (_a = context.pendingValue) != null ? _a : void 0
2426
2445
  };
2427
2446
  }
@@ -2457,20 +2476,21 @@ const syncValueCallback = ({
2457
2476
  },
2458
2477
  "done syncing": [{
2459
2478
  guard: "value changed while syncing",
2460
- actions: ["assign previous value", "emit done syncing"],
2479
+ actions: ["assign previous value", "emit done syncing", "assign initial value synced"],
2461
2480
  reenter: !0
2462
2481
  }, {
2463
2482
  target: "idle",
2464
- actions: ["clear pending value", "assign previous value", "emit done syncing"]
2483
+ actions: ["clear pending value", "assign previous value", "emit done syncing", "assign initial value synced"]
2465
2484
  }]
2466
2485
  }
2467
2486
  }
2468
2487
  }
2469
2488
  }), debug$i = debugWithName("hook:useSyncValue");
2470
- function updateValue({
2489
+ async function updateValue({
2471
2490
  context,
2472
2491
  sendBack,
2473
2492
  slateEditor,
2493
+ streamBlocks,
2474
2494
  value
2475
2495
  }) {
2476
2496
  let isChanged = !1, isValid = !0;
@@ -2495,31 +2515,48 @@ function updateValue({
2495
2515
  const slateValueFromProps = toSlateValue(value, {
2496
2516
  schemaTypes: context.schema
2497
2517
  });
2498
- Editor.withoutNormalizing(slateEditor, () => {
2499
- withRemoteChanges(slateEditor, () => {
2500
- withoutPatching(slateEditor, () => {
2501
- const childrenLength = slateEditor.children.length;
2502
- if (slateValueFromProps.length < childrenLength) {
2503
- for (let i = childrenLength - 1; i > slateValueFromProps.length - 1; i--)
2504
- Transforms.removeNodes(slateEditor, {
2505
- at: [i]
2506
- });
2507
- isChanged = !0;
2508
- }
2509
- for (const [currentBlockIndex, currentBlock] of slateValueFromProps.entries()) {
2510
- const {
2511
- blockChanged,
2512
- blockValid
2513
- } = syncBlock({
2514
- context,
2515
- sendBack,
2516
- block: currentBlock,
2517
- index: currentBlockIndex,
2518
- slateEditor,
2519
- value
2520
- });
2521
- isChanged = blockChanged || isChanged, isValid = isValid && blockValid;
2522
- }
2518
+ await new Promise((resolve) => {
2519
+ Editor.withoutNormalizing(slateEditor, () => {
2520
+ withRemoteChanges(slateEditor, () => {
2521
+ withoutPatching(slateEditor, async () => {
2522
+ const childrenLength = slateEditor.children.length;
2523
+ if (slateValueFromProps.length < childrenLength) {
2524
+ for (let i = childrenLength - 1; i > slateValueFromProps.length - 1; i--)
2525
+ Transforms.removeNodes(slateEditor, {
2526
+ at: [i]
2527
+ });
2528
+ isChanged = !0;
2529
+ }
2530
+ try {
2531
+ for (var iter = __forAwait(getBlocks({
2532
+ slateValue: slateValueFromProps,
2533
+ streamBlocks
2534
+ })), more, temp, error; more = !(temp = await iter.next()).done; more = !1) {
2535
+ const [currentBlock, currentBlockIndex] = temp.value, {
2536
+ blockChanged,
2537
+ blockValid
2538
+ } = syncBlock({
2539
+ context,
2540
+ sendBack,
2541
+ block: currentBlock,
2542
+ index: currentBlockIndex,
2543
+ slateEditor,
2544
+ value
2545
+ });
2546
+ isChanged = blockChanged || isChanged, isValid = isValid && blockValid;
2547
+ }
2548
+ } catch (temp2) {
2549
+ error = [temp2];
2550
+ } finally {
2551
+ try {
2552
+ more && (temp = iter.return) && await temp.call(iter);
2553
+ } finally {
2554
+ if (error)
2555
+ throw error[0];
2556
+ }
2557
+ }
2558
+ resolve();
2559
+ });
2523
2560
  });
2524
2561
  });
2525
2562
  });
@@ -2566,6 +2603,29 @@ function updateValue({
2566
2603
  value
2567
2604
  });
2568
2605
  }
2606
+ function getBlocks(_0) {
2607
+ return __asyncGenerator(this, arguments, function* ({
2608
+ slateValue,
2609
+ streamBlocks
2610
+ }) {
2611
+ let index = 0;
2612
+ try {
2613
+ for (var iter = __forAwait(slateValue), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = !1) {
2614
+ const block = temp.value;
2615
+ streamBlocks && (yield new __await(new Promise((resolve) => setTimeout(resolve, 0)))), yield [block, index], index++;
2616
+ }
2617
+ } catch (temp2) {
2618
+ error = [temp2];
2619
+ } finally {
2620
+ try {
2621
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
2622
+ } finally {
2623
+ if (error)
2624
+ throw error[0];
2625
+ }
2626
+ }
2627
+ });
2628
+ }
2569
2629
  function syncBlock({
2570
2630
  context,
2571
2631
  sendBack,
@@ -2574,33 +2634,38 @@ function syncBlock({
2574
2634
  slateEditor,
2575
2635
  value
2576
2636
  }) {
2577
- var _a, _b, _c, _d, _e;
2578
2637
  let blockChanged = !1, blockValid = !0;
2579
- const currentBlock = block, currentBlockIndex = index, oldBlock = slateEditor.children[currentBlockIndex];
2580
- if (oldBlock && !isEqual(currentBlock, oldBlock) && blockValid) {
2581
- const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2582
- !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) => {
2583
- sendBack({
2584
- type: "patch",
2585
- patch
2638
+ const currentBlock = block, currentBlockIndex = index, oldBlock = slateEditor.children[currentBlockIndex], hasChanges = oldBlock && !isEqual(currentBlock, oldBlock);
2639
+ return Editor.withoutNormalizing(slateEditor, () => {
2640
+ withRemoteChanges(slateEditor, () => {
2641
+ withoutPatching(slateEditor, () => {
2642
+ var _a, _b, _c, _d, _e;
2643
+ if (hasChanges && blockValid) {
2644
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2645
+ !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) => {
2646
+ sendBack({
2647
+ type: "patch",
2648
+ patch
2649
+ });
2650
+ })), 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({
2651
+ type: "invalid value",
2652
+ resolution: validation.resolution,
2653
+ value
2654
+ }), blockValid = !1);
2655
+ }
2656
+ if (!oldBlock && blockValid) {
2657
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2658
+ 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 ? Transforms.insertNodes(slateEditor, currentBlock, {
2659
+ at: [currentBlockIndex]
2660
+ }) : (debug$i("Invalid", validation), sendBack({
2661
+ type: "invalid value",
2662
+ resolution: validation.resolution,
2663
+ value
2664
+ }), blockValid = !1);
2665
+ }
2586
2666
  });
2587
- })), 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({
2588
- type: "invalid value",
2589
- resolution: validation.resolution,
2590
- value
2591
- }), blockValid = !1);
2592
- }
2593
- if (!oldBlock && blockValid) {
2594
- const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2595
- 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 ? Transforms.insertNodes(slateEditor, currentBlock, {
2596
- at: [currentBlockIndex]
2597
- }) : (debug$i("Invalid", validation), sendBack({
2598
- type: "invalid value",
2599
- resolution: validation.resolution,
2600
- value
2601
- }), blockValid = !1);
2602
- }
2603
- return {
2667
+ });
2668
+ }), {
2604
2669
  blockChanged,
2605
2670
  blockValid
2606
2671
  };