@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.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,6 +2294,9 @@ 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
2302
  event
@@ -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,
@@ -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
  };