@portabletext/editor 1.16.1 → 1.16.3

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
@@ -19,13 +19,13 @@ import noop from "lodash/noop.js";
19
19
  import React, { useRef, useState, useEffect, useMemo, createContext, useContext, startTransition, Component, useCallback, forwardRef, useImperativeHandle } from "react";
20
20
  import { Editor, Element as Element$1, Range, Point, Text, Path, Transforms, Operation, Node, createEditor as createEditor$1, deleteBackward, deleteForward, insertText } from "slate";
21
21
  import { useSlateStatic, ReactEditor, useSelected, withReact, Slate, useSlate, Editable } from "slate-react";
22
- import debug$m from "debug";
22
+ import debug$l from "debug";
23
23
  import { c } from "react-compiler-runtime";
24
24
  import { styled } from "styled-components";
25
25
  import uniq from "lodash/uniq.js";
26
26
  import { Subject } from "rxjs";
27
27
  import { useEffectEvent } from "use-effect-event";
28
- import { setup, emit, assign, fromCallback, assertEvent, and, not, or, enqueueActions, createActor } from "xstate";
28
+ import { setup, emit, assign, fromCallback, assertEvent, enqueueActions, createActor } from "xstate";
29
29
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
30
30
  import flatten from "lodash/flatten.js";
31
31
  import isPlainObject from "lodash/isPlainObject.js";
@@ -175,10 +175,10 @@ function compileSchemaDefinition(definition) {
175
175
  });
176
176
  }
177
177
  const rootName = "sanity-pte:";
178
- var debug$l = debug$m(rootName);
178
+ debug$l(rootName);
179
179
  function debugWithName(name) {
180
180
  const namespace = `${rootName}${name}`;
181
- return debug$m && debug$m.enabled(namespace) ? debug$m(namespace) : debug$m(rootName);
181
+ return debug$l && debug$l.enabled(namespace) ? debug$l(namespace) : debug$l(rootName);
182
182
  }
183
183
  function createKeyedPath(point, value, types) {
184
184
  const blockPath = [point.path[0]];
@@ -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
@@ -2318,24 +2335,20 @@ const syncValueCallback = ({
2318
2335
  event
2319
2336
  }) => (assertEvent(event, "done syncing"), event.value)
2320
2337
  }),
2321
- "emit done syncing": emit(({
2322
- event
2323
- }) => (assertEvent(event, "done syncing"), event))
2338
+ "emit done syncing initial value": emit({
2339
+ type: "done syncing initial value"
2340
+ })
2324
2341
  },
2325
2342
  guards: {
2326
- "is readOnly": ({
2343
+ "initial value synced": ({
2327
2344
  context
2328
- }) => context.readOnly,
2329
- "is processing local changes": ({
2330
- context
2331
- }) => context.isProcessingLocalChanges,
2332
- "is processing remote changes": ({
2345
+ }) => context.initialValueSynced,
2346
+ "is busy": ({
2333
2347
  context
2334
2348
  }) => {
2335
2349
  var _a;
2336
- return (_a = isChangingRemotely(context.slateEditor)) != null ? _a : !1;
2350
+ return !context.readOnly && (context.isProcessingLocalChanges || ((_a = isChangingRemotely(context.slateEditor)) != null ? _a : !1));
2337
2351
  },
2338
- "is busy": and([not("is readOnly"), or(["is processing local changes", "is processing remote changes"])]),
2339
2352
  "value changed while syncing": ({
2340
2353
  context,
2341
2354
  event
@@ -2352,6 +2365,7 @@ const syncValueCallback = ({
2352
2365
  context: ({
2353
2366
  input
2354
2367
  }) => ({
2368
+ initialValueSynced: !1,
2355
2369
  isProcessingLocalChanges: !1,
2356
2370
  keyGenerator: input.keyGenerator,
2357
2371
  schema: input.schema,
@@ -2360,7 +2374,6 @@ const syncValueCallback = ({
2360
2374
  pendingValue: void 0,
2361
2375
  previousValue: void 0
2362
2376
  }),
2363
- initial: "idle",
2364
2377
  on: {
2365
2378
  "has pending patches": {
2366
2379
  actions: assign({
@@ -2376,101 +2389,114 @@ const syncValueCallback = ({
2376
2389
  actions: ["assign readOnly"]
2377
2390
  }
2378
2391
  },
2392
+ type: "parallel",
2379
2393
  states: {
2380
- idle: {
2381
- on: {
2382
- "update value": [{
2383
- guard: "is busy",
2384
- target: "busy",
2385
- actions: ["assign pending value"]
2386
- }, {
2387
- target: "syncing",
2388
- actions: ["assign pending value"]
2389
- }]
2390
- }
2391
- },
2392
- busy: {
2393
- after: {
2394
- 1e3: {
2395
- target: "syncing"
2394
+ "setting up": {
2395
+ initial: "syncing initial value",
2396
+ states: {
2397
+ "syncing initial value": {
2398
+ always: {
2399
+ guard: "initial value synced",
2400
+ target: "done syncing initial value"
2401
+ }
2402
+ },
2403
+ "done syncing initial value": {
2404
+ entry: ["emit done syncing initial value"],
2405
+ type: "final"
2396
2406
  }
2397
- },
2398
- on: {
2399
- "update value": [{
2400
- guard: "is busy",
2401
- actions: ["assign pending value"],
2402
- reenter: !0
2403
- }, {
2404
- target: "syncing",
2405
- actions: ["assign pending value"]
2406
- }]
2407
2407
  }
2408
2408
  },
2409
2409
  syncing: {
2410
- invoke: {
2411
- src: "sync value",
2412
- id: "sync value",
2413
- input: ({
2414
- context
2415
- }) => {
2416
- var _a;
2417
- return {
2418
- context: {
2419
- keyGenerator: context.keyGenerator,
2420
- previousValue: context.previousValue,
2421
- readOnly: context.readOnly,
2422
- schema: context.schema
2423
- },
2424
- slateEditor: context.slateEditor,
2425
- value: (_a = context.pendingValue) != null ? _a : void 0
2426
- };
2427
- }
2428
- },
2429
- always: {
2430
- guard: "pending value equals previous value",
2431
- actions: [emit(({
2432
- context
2433
- }) => ({
2434
- type: "done syncing",
2435
- value: context.previousValue
2436
- }))],
2437
- target: "idle"
2438
- },
2439
- on: {
2440
- "update value": {
2441
- actions: ["assign pending value"]
2442
- },
2443
- patch: {
2444
- actions: [emit(({
2445
- event
2446
- }) => event)]
2447
- },
2448
- "invalid value": {
2449
- actions: [emit(({
2450
- event
2451
- }) => event)]
2410
+ initial: "idle",
2411
+ states: {
2412
+ idle: {
2413
+ on: {
2414
+ "update value": [{
2415
+ guard: "is busy",
2416
+ target: "busy",
2417
+ actions: ["assign pending value"]
2418
+ }, {
2419
+ target: "syncing",
2420
+ actions: ["assign pending value"]
2421
+ }]
2422
+ }
2452
2423
  },
2453
- "value changed": {
2454
- actions: [emit(({
2455
- event
2456
- }) => event)]
2424
+ busy: {
2425
+ after: {
2426
+ 1e3: [{
2427
+ guard: "is busy",
2428
+ reenter: !0
2429
+ }, {
2430
+ target: "syncing"
2431
+ }]
2432
+ },
2433
+ on: {
2434
+ "update value": [{
2435
+ actions: ["assign pending value"]
2436
+ }]
2437
+ }
2457
2438
  },
2458
- "done syncing": [{
2459
- guard: "value changed while syncing",
2460
- actions: ["assign previous value", "emit done syncing"],
2461
- reenter: !0
2462
- }, {
2463
- target: "idle",
2464
- actions: ["clear pending value", "assign previous value", "emit done syncing"]
2465
- }]
2439
+ syncing: {
2440
+ always: {
2441
+ guard: "pending value equals previous value",
2442
+ target: "idle",
2443
+ actions: ["clear pending value", "assign initial value synced"]
2444
+ },
2445
+ invoke: {
2446
+ src: "sync value",
2447
+ id: "sync value",
2448
+ input: ({
2449
+ context
2450
+ }) => ({
2451
+ context: {
2452
+ keyGenerator: context.keyGenerator,
2453
+ previousValue: context.previousValue,
2454
+ readOnly: context.readOnly,
2455
+ schema: context.schema
2456
+ },
2457
+ slateEditor: context.slateEditor,
2458
+ streamBlocks: !context.initialValueSynced,
2459
+ value: context.pendingValue
2460
+ })
2461
+ },
2462
+ on: {
2463
+ "update value": {
2464
+ actions: ["assign pending value"]
2465
+ },
2466
+ patch: {
2467
+ actions: [emit(({
2468
+ event
2469
+ }) => event)]
2470
+ },
2471
+ "invalid value": {
2472
+ actions: [emit(({
2473
+ event
2474
+ }) => event)]
2475
+ },
2476
+ "value changed": {
2477
+ actions: [emit(({
2478
+ event
2479
+ }) => event)]
2480
+ },
2481
+ "done syncing": [{
2482
+ guard: "value changed while syncing",
2483
+ actions: ["assign previous value", "assign initial value synced"],
2484
+ reenter: !0
2485
+ }, {
2486
+ target: "idle",
2487
+ actions: ["clear pending value", "assign previous value", "assign initial value synced"]
2488
+ }]
2489
+ }
2490
+ }
2466
2491
  }
2467
2492
  }
2468
2493
  }
2469
2494
  }), debug$i = debugWithName("hook:useSyncValue");
2470
- function updateValue({
2495
+ async function updateValue({
2471
2496
  context,
2472
2497
  sendBack,
2473
2498
  slateEditor,
2499
+ streamBlocks,
2474
2500
  value
2475
2501
  }) {
2476
2502
  let isChanged = !1, isValid = !0;
@@ -2495,31 +2521,48 @@ function updateValue({
2495
2521
  const slateValueFromProps = toSlateValue(value, {
2496
2522
  schemaTypes: context.schema
2497
2523
  });
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
- }
2524
+ await new Promise((resolve) => {
2525
+ Editor.withoutNormalizing(slateEditor, () => {
2526
+ withRemoteChanges(slateEditor, () => {
2527
+ withoutPatching(slateEditor, async () => {
2528
+ const childrenLength = slateEditor.children.length;
2529
+ if (slateValueFromProps.length < childrenLength) {
2530
+ for (let i = childrenLength - 1; i > slateValueFromProps.length - 1; i--)
2531
+ Transforms.removeNodes(slateEditor, {
2532
+ at: [i]
2533
+ });
2534
+ isChanged = !0;
2535
+ }
2536
+ try {
2537
+ for (var iter = __forAwait(getBlocks({
2538
+ slateValue: slateValueFromProps,
2539
+ streamBlocks
2540
+ })), more, temp, error; more = !(temp = await iter.next()).done; more = !1) {
2541
+ const [currentBlock, currentBlockIndex] = temp.value, {
2542
+ blockChanged,
2543
+ blockValid
2544
+ } = syncBlock({
2545
+ context,
2546
+ sendBack,
2547
+ block: currentBlock,
2548
+ index: currentBlockIndex,
2549
+ slateEditor,
2550
+ value
2551
+ });
2552
+ isChanged = blockChanged || isChanged, isValid = isValid && blockValid;
2553
+ }
2554
+ } catch (temp2) {
2555
+ error = [temp2];
2556
+ } finally {
2557
+ try {
2558
+ more && (temp = iter.return) && await temp.call(iter);
2559
+ } finally {
2560
+ if (error)
2561
+ throw error[0];
2562
+ }
2563
+ }
2564
+ resolve();
2565
+ });
2523
2566
  });
2524
2567
  });
2525
2568
  });
@@ -2566,6 +2609,29 @@ function updateValue({
2566
2609
  value
2567
2610
  });
2568
2611
  }
2612
+ function getBlocks(_0) {
2613
+ return __asyncGenerator(this, arguments, function* ({
2614
+ slateValue,
2615
+ streamBlocks
2616
+ }) {
2617
+ let index = 0;
2618
+ try {
2619
+ for (var iter = __forAwait(slateValue), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = !1) {
2620
+ const block = temp.value;
2621
+ streamBlocks && (yield new __await(new Promise((resolve) => setTimeout(resolve, 0)))), yield [block, index], index++;
2622
+ }
2623
+ } catch (temp2) {
2624
+ error = [temp2];
2625
+ } finally {
2626
+ try {
2627
+ more && (temp = iter.return) && (yield new __await(temp.call(iter)));
2628
+ } finally {
2629
+ if (error)
2630
+ throw error[0];
2631
+ }
2632
+ }
2633
+ });
2634
+ }
2569
2635
  function syncBlock({
2570
2636
  context,
2571
2637
  sendBack,
@@ -2574,33 +2640,38 @@ function syncBlock({
2574
2640
  slateEditor,
2575
2641
  value
2576
2642
  }) {
2577
- var _a, _b, _c, _d, _e;
2578
2643
  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
2644
+ const currentBlock = block, currentBlockIndex = index, oldBlock = slateEditor.children[currentBlockIndex], hasChanges = oldBlock && !isEqual(currentBlock, oldBlock);
2645
+ return Editor.withoutNormalizing(slateEditor, () => {
2646
+ withRemoteChanges(slateEditor, () => {
2647
+ withoutPatching(slateEditor, () => {
2648
+ var _a, _b, _c, _d, _e;
2649
+ if (hasChanges && blockValid) {
2650
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2651
+ !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) => {
2652
+ sendBack({
2653
+ type: "patch",
2654
+ patch
2655
+ });
2656
+ })), 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({
2657
+ type: "invalid value",
2658
+ resolution: validation.resolution,
2659
+ value
2660
+ }), blockValid = !1);
2661
+ }
2662
+ if (!oldBlock && blockValid) {
2663
+ const validationValue = [value[currentBlockIndex]], validation = validateValue(validationValue, context.schema, context.keyGenerator);
2664
+ 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, {
2665
+ at: [currentBlockIndex]
2666
+ }) : (debug$i("Invalid", validation), sendBack({
2667
+ type: "invalid value",
2668
+ resolution: validation.resolution,
2669
+ value
2670
+ }), blockValid = !1);
2671
+ }
2586
2672
  });
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 {
2673
+ });
2674
+ }), {
2604
2675
  blockChanged,
2605
2676
  blockValid
2606
2677
  };
@@ -5659,7 +5730,7 @@ function performAction({
5659
5730
  context,
5660
5731
  action
5661
5732
  }) {
5662
- switch (debug$l("Behavior action", action), action.type) {
5733
+ switch (action.type) {
5663
5734
  case "delete.block": {
5664
5735
  behaviorActionImplementations["delete.block"]({
5665
5736
  context,
@@ -5998,7 +6069,7 @@ const editorMachine = setup({
5998
6069
  enqueue
5999
6070
  }) => {
6000
6071
  var _a;
6001
- assertEvent(event, ["behavior event"]), debug$l("Behavior event", event);
6072
+ assertEvent(event, ["behavior event"]);
6002
6073
  const defaultAction = event.behaviorEvent.type === "copy" || event.behaviorEvent.type === "key.down" || event.behaviorEvent.type === "key.up" || event.behaviorEvent.type === "paste" ? void 0 : __spreadProps$4(__spreadValues$4({}, event.behaviorEvent), {
6003
6074
  editor: event.editor
6004
6075
  }), eventBehaviors = context.behaviors.filter((behavior) => behavior.on === event.behaviorEvent.type);
@@ -6186,7 +6257,7 @@ const editorMachine = setup({
6186
6257
  states: {
6187
6258
  "determine initial edit mode": {
6188
6259
  on: {
6189
- "done syncing": [{
6260
+ "done syncing initial value": [{
6190
6261
  target: "#editor.edit mode.read only.read only",
6191
6262
  guard: ({
6192
6263
  context
@@ -6282,7 +6353,7 @@ const editorMachine = setup({
6282
6353
  mutation: {
6283
6354
  actions: "defer event"
6284
6355
  },
6285
- "done syncing": {
6356
+ "done syncing initial value": {
6286
6357
  target: "pristine"
6287
6358
  }
6288
6359
  }