@portabletext/editor 1.35.3 → 1.35.4

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.
@@ -4,9 +4,9 @@ import React, { createContext, useContext, useEffect, useState, startTransition,
4
4
  import { ReactEditor, withReact, Slate } from "slate-react";
5
5
  import { useSelector, useActorRef } from "@xstate/react";
6
6
  import debug$e from "debug";
7
- import { Editor, Element, Range, Point, Text, Operation, Transforms, Path, Node, insertText, select, deleteFragment, deleteForward, createEditor as createEditor$1 } from "slate";
8
- import { setup, emit, assign, fromCallback, assertEvent, enqueueActions, createActor } from "xstate";
9
7
  import isEqual from "lodash/isEqual.js";
8
+ import { Editor, Element, Range, Point, Text, Operation, Transforms, Path, Node, insertText, select, deleteFragment, deleteForward, createEditor as createEditor$1 } from "slate";
9
+ import { setup, emit, enqueueActions, assign, assertEvent, fromCallback, stateIn, and, not, createActor } from "xstate";
10
10
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
11
11
  import { defineType, defineField, isKeySegment, isPortableTextTextBlock, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock } from "@sanity/types";
12
12
  import flatten from "lodash/flatten.js";
@@ -343,7 +343,7 @@ function compileType(rawType) {
343
343
  types: [rawType]
344
344
  }).get(rawType.name);
345
345
  }
346
- const FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3, mutationMachine = setup({
346
+ const mutationMachine = setup({
347
347
  types: {
348
348
  context: {},
349
349
  events: {},
@@ -354,63 +354,152 @@ const FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3,
354
354
  "emit has pending patches": emit({
355
355
  type: "has pending patches"
356
356
  }),
357
- "emit mutation": emit(({
358
- context
359
- }) => ({
360
- type: "mutation",
361
- patches: context.pendingPatches,
362
- snapshot: fromSlateValue(context.slateEditor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(context.slateEditor))
363
- })),
364
- "clear pending patches": assign({
365
- pendingPatches: []
357
+ "emit mutations": enqueueActions(({
358
+ context,
359
+ enqueue
360
+ }) => {
361
+ for (const bulk of context.pendingMutations)
362
+ enqueue.emit({
363
+ type: "mutation",
364
+ patches: bulk.patches,
365
+ snapshot: bulk.value
366
+ });
367
+ }),
368
+ "clear pending mutations": assign({
369
+ pendingMutations: []
366
370
  }),
367
371
  "defer patch": assign({
368
- pendingPatches: ({
372
+ pendingMutations: ({
369
373
  context,
370
374
  event
371
- }) => [...context.pendingPatches, event.patch]
375
+ }) => {
376
+ if (assertEvent(event, "patch"), context.pendingMutations.length === 0)
377
+ return [{
378
+ actionId: event.actionId,
379
+ value: event.value,
380
+ patches: [event.patch]
381
+ }];
382
+ const lastBulk = context.pendingMutations.at(-1);
383
+ return lastBulk && lastBulk.actionId === event.actionId ? context.pendingMutations.slice(0, -1).concat({
384
+ value: event.value,
385
+ actionId: lastBulk.actionId,
386
+ patches: [...lastBulk.patches, event.patch]
387
+ }) : context.pendingMutations.concat({
388
+ value: event.value,
389
+ actionId: event.actionId,
390
+ patches: [event.patch]
391
+ });
392
+ }
393
+ })
394
+ },
395
+ actors: {
396
+ "type listener": fromCallback(({
397
+ input,
398
+ sendBack
399
+ }) => {
400
+ const originalApply = input.slateEditor.apply;
401
+ return input.slateEditor.apply = (op) => {
402
+ op.type === "insert_text" || op.type === "remove_text" ? sendBack({
403
+ type: "typing"
404
+ }) : sendBack({
405
+ type: "not typing"
406
+ }), originalApply(op);
407
+ }, () => {
408
+ input.slateEditor.apply = originalApply;
409
+ };
372
410
  })
373
411
  },
374
412
  guards: {
413
+ "is typing": stateIn({
414
+ typing: "typing"
415
+ }),
416
+ "no pending mutations": ({
417
+ context
418
+ }) => context.pendingMutations.length === 0,
375
419
  "slate is normalizing": ({
376
420
  context
377
421
  }) => Editor.isNormalizing(context.slateEditor)
422
+ },
423
+ delays: {
424
+ "mutation debounce": process.env.NODE_ENV === "test" ? 250 : 0,
425
+ "type debounce": process.env.NODE_ENV === "test" ? 0 : 250
378
426
  }
379
427
  }).createMachine({
380
428
  id: "mutation",
381
429
  context: ({
382
430
  input
383
431
  }) => ({
384
- pendingPatches: [],
432
+ pendingMutations: [],
385
433
  schema: input.schema,
386
434
  slateEditor: input.slateEditor
387
435
  }),
388
- initial: "idle",
436
+ type: "parallel",
389
437
  states: {
390
- idle: {
391
- on: {
392
- patch: {
393
- actions: ["defer patch", "emit has pending patches"],
394
- target: "has pending patches"
438
+ typing: {
439
+ initial: "idle",
440
+ invoke: {
441
+ src: "type listener",
442
+ input: ({
443
+ context
444
+ }) => ({
445
+ slateEditor: context.slateEditor
446
+ })
447
+ },
448
+ states: {
449
+ idle: {
450
+ on: {
451
+ typing: {
452
+ target: "typing"
453
+ }
454
+ }
455
+ },
456
+ typing: {
457
+ after: {
458
+ "type debounce": {
459
+ target: "idle"
460
+ }
461
+ },
462
+ on: {
463
+ "not typing": {
464
+ target: "idle"
465
+ },
466
+ typing: {
467
+ target: "typing",
468
+ reenter: !0
469
+ }
470
+ }
395
471
  }
396
472
  }
397
473
  },
398
- "has pending patches": {
399
- after: {
400
- [FLUSH_PATCHES_THROTTLED_MS]: [{
401
- guard: "slate is normalizing",
402
- target: "idle",
403
- actions: ["emit mutation", "clear pending patches"]
404
- }, {
405
- target: "has pending patches",
406
- reenter: !0
407
- }]
408
- },
409
- on: {
410
- patch: {
411
- target: "has pending patches",
412
- actions: ["defer patch"],
413
- reenter: !0
474
+ mutations: {
475
+ initial: "idle",
476
+ states: {
477
+ idle: {
478
+ on: {
479
+ patch: {
480
+ actions: ["defer patch", "emit has pending patches"],
481
+ target: "emitting mutations"
482
+ }
483
+ }
484
+ },
485
+ "emitting mutations": {
486
+ after: {
487
+ "mutation debounce": [{
488
+ guard: and([not("is typing"), "slate is normalizing"]),
489
+ target: "idle",
490
+ actions: ["emit mutations", "clear pending mutations"]
491
+ }, {
492
+ target: "emitting mutations",
493
+ reenter: !0
494
+ }]
495
+ },
496
+ on: {
497
+ patch: {
498
+ target: "emitting mutations",
499
+ actions: ["defer patch"],
500
+ reenter: !0
501
+ }
502
+ }
414
503
  }
415
504
  }
416
505
  }
@@ -1436,13 +1525,15 @@ function randomKey(length) {
1436
1525
  const table = getByteHexTable();
1437
1526
  return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
1438
1527
  }
1439
- const IS_APPLYING_BEHAVIOR_ACTIONS = /* @__PURE__ */ new WeakMap();
1528
+ const CURRENT_ACTION_ID = /* @__PURE__ */ new WeakMap();
1440
1529
  function withApplyingBehaviorActions(editor, fn) {
1441
- const prev = IS_APPLYING_BEHAVIOR_ACTIONS.get(editor);
1442
- IS_APPLYING_BEHAVIOR_ACTIONS.set(editor, !0), Editor.withoutNormalizing(editor, fn), IS_APPLYING_BEHAVIOR_ACTIONS.set(editor, prev);
1530
+ CURRENT_ACTION_ID.set(editor, defaultKeyGenerator()), Editor.withoutNormalizing(editor, fn), CURRENT_ACTION_ID.set(editor, void 0);
1531
+ }
1532
+ function getCurrentActionId(editor) {
1533
+ return CURRENT_ACTION_ID.get(editor);
1443
1534
  }
1444
1535
  function isApplyingBehaviorActions(editor) {
1445
- return IS_APPLYING_BEHAVIOR_ACTIONS.get(editor) ?? !1;
1536
+ return getCurrentActionId(editor) !== void 0;
1446
1537
  }
1447
1538
  const CURRENT_BEHAVIOR_ACTION_INTEND_SET = /* @__PURE__ */ new WeakMap();
1448
1539
  function withApplyingBehaviorActionIntendSet(editor, fn) {
@@ -2165,7 +2256,7 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
2165
2256
  }
2166
2257
  const debug$b = debugWithName("component:PortableTextEditor:Synchronizer");
2167
2258
  function Synchronizer(props) {
2168
- const $ = c(40), {
2259
+ const $ = c(41), {
2169
2260
  editorActor,
2170
2261
  slateEditor
2171
2262
  } = props, value = useSelector(props.editorActor, _temp), readOnly = useSelector(props.editorActor, _temp2);
@@ -2217,7 +2308,7 @@ function Synchronizer(props) {
2217
2308
  };
2218
2309
  }, t8 = [mutationActorRef, syncActorRef, editorActor], $[16] = editorActor, $[17] = mutationActorRef, $[18] = syncActorRef, $[19] = t7, $[20] = t8) : (t7 = $[19], t8 = $[20]), useEffect(t7, t8);
2219
2310
  let t10, t9;
2220
- $[21] !== props.editorActor || $[22] !== syncActorRef ? (t9 = () => {
2311
+ $[21] !== props.editorActor || $[22] !== slateEditor || $[23] !== syncActorRef ? (t9 = () => {
2221
2312
  const subscription_0 = syncActorRef.on("*", (event_0) => {
2222
2313
  bb15: switch (event_0.type) {
2223
2314
  case "invalid value": {
@@ -2234,6 +2325,14 @@ function Synchronizer(props) {
2234
2325
  });
2235
2326
  break bb15;
2236
2327
  }
2328
+ case "patch": {
2329
+ props.editorActor.send({
2330
+ ...event_0,
2331
+ type: "internal.patch",
2332
+ value: fromSlateValue(slateEditor.children, props.editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(slateEditor))
2333
+ });
2334
+ break bb15;
2335
+ }
2237
2336
  default:
2238
2337
  props.editorActor.send(event_0);
2239
2338
  }
@@ -2241,33 +2340,36 @@ function Synchronizer(props) {
2241
2340
  return () => {
2242
2341
  subscription_0.unsubscribe();
2243
2342
  };
2244
- }, t10 = [props.editorActor, syncActorRef], $[21] = props.editorActor, $[22] = syncActorRef, $[23] = t10, $[24] = t9) : (t10 = $[23], t9 = $[24]), useEffect(t9, t10);
2343
+ }, t10 = [props.editorActor, slateEditor, syncActorRef], $[21] = props.editorActor, $[22] = slateEditor, $[23] = syncActorRef, $[24] = t10, $[25] = t9) : (t10 = $[24], t9 = $[25]), useEffect(t9, t10);
2245
2344
  let t11, t12;
2246
- $[25] !== readOnly || $[26] !== syncActorRef ? (t11 = () => {
2345
+ $[26] !== readOnly || $[27] !== syncActorRef ? (t11 = () => {
2247
2346
  syncActorRef.send({
2248
2347
  type: "update readOnly",
2249
2348
  readOnly
2250
2349
  });
2251
- }, t12 = [syncActorRef, readOnly], $[25] = readOnly, $[26] = syncActorRef, $[27] = t11, $[28] = t12) : (t11 = $[27], t12 = $[28]), useEffect(t11, t12);
2350
+ }, t12 = [syncActorRef, readOnly], $[26] = readOnly, $[27] = syncActorRef, $[28] = t11, $[29] = t12) : (t11 = $[28], t12 = $[29]), useEffect(t11, t12);
2252
2351
  let t13, t14;
2253
- $[29] !== syncActorRef || $[30] !== value ? (t13 = () => {
2352
+ $[30] !== syncActorRef || $[31] !== value ? (t13 = () => {
2254
2353
  debug$b("Value from props changed, syncing new value"), syncActorRef.send({
2255
2354
  type: "update value",
2256
2355
  value
2257
2356
  });
2258
- }, t14 = [syncActorRef, value], $[29] = syncActorRef, $[30] = value, $[31] = t13, $[32] = t14) : (t13 = $[31], t14 = $[32]), useEffect(t13, t14);
2357
+ }, t14 = [syncActorRef, value], $[30] = syncActorRef, $[31] = value, $[32] = t13, $[33] = t14) : (t13 = $[32], t14 = $[33]), useEffect(t13, t14);
2259
2358
  let t15;
2260
- $[33] !== editorActor || $[34] !== mutationActorRef ? (t15 = () => {
2359
+ $[34] !== editorActor || $[35] !== mutationActorRef ? (t15 = () => {
2261
2360
  debug$b("Subscribing to patch events");
2262
- const sub = editorActor.on("patch", (event_1) => {
2263
- mutationActorRef.send(event_1);
2361
+ const sub = editorActor.on("internal.patch", (event_1) => {
2362
+ mutationActorRef.send({
2363
+ ...event_1,
2364
+ type: "patch"
2365
+ });
2264
2366
  });
2265
2367
  return () => {
2266
2368
  debug$b("Unsubscribing to patch events"), sub.unsubscribe();
2267
2369
  };
2268
- }, $[33] = editorActor, $[34] = mutationActorRef, $[35] = t15) : t15 = $[35];
2370
+ }, $[34] = editorActor, $[35] = mutationActorRef, $[36] = t15) : t15 = $[36];
2269
2371
  let t16;
2270
- return $[36] !== editorActor || $[37] !== mutationActorRef || $[38] !== slateEditor ? (t16 = [editorActor, mutationActorRef, slateEditor], $[36] = editorActor, $[37] = mutationActorRef, $[38] = slateEditor, $[39] = t16) : t16 = $[39], useEffect(t15, t16), null;
2372
+ return $[37] !== editorActor || $[38] !== mutationActorRef || $[39] !== slateEditor ? (t16 = [editorActor, mutationActorRef, slateEditor], $[37] = editorActor, $[38] = mutationActorRef, $[39] = slateEditor, $[40] = t16) : t16 = $[40], useEffect(t15, t16), null;
2271
2373
  }
2272
2374
  function _temp2(s_0) {
2273
2375
  return s_0.matches({
@@ -4868,6 +4970,7 @@ function createWithEventListeners(editorActor, subscriptions) {
4868
4970
  case "loading":
4869
4971
  case "mutation":
4870
4972
  case "patch":
4973
+ case "internal.patch":
4871
4974
  case "patches":
4872
4975
  case "read only":
4873
4976
  case "ready":
@@ -5510,18 +5613,21 @@ function createWithPatches({
5510
5613
  patches = [...patches, ...patchFunctions.moveNodePatch(editor, operation, previousChildren)];
5511
5614
  break;
5512
5615
  }
5513
- return !editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches = [...patches, unset([])], editorActor.send({
5616
+ if (!editorWasEmpty && editorIsEmpty && ["merge_node", "set_node", "remove_text", "remove_node"].includes(operation.type) && (patches = [...patches, unset([])], editorActor.send({
5514
5617
  type: "notify.unset",
5515
5618
  previousValue: fromSlateValue(previousChildren, schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))
5516
- })), editorWasEmpty && patches.length > 0 && (patches = [setIfMissing([], []), ...patches]), patches.length > 0 && patches.forEach((patch) => {
5517
- editorActor.send({
5518
- type: "patch",
5519
- patch: {
5520
- ...patch,
5521
- origin: "local"
5522
- }
5523
- });
5524
- }), editor;
5619
+ })), editorWasEmpty && patches.length > 0 && (patches = [setIfMissing([], []), ...patches]), patches.length > 0)
5620
+ for (const patch of patches)
5621
+ editorActor.send({
5622
+ type: "internal.patch",
5623
+ patch: {
5624
+ ...patch,
5625
+ origin: "local"
5626
+ },
5627
+ actionId: getCurrentActionId(editor),
5628
+ value: fromSlateValue(editor.children, schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))
5629
+ });
5630
+ return editor;
5525
5631
  }, editor;
5526
5632
  };
5527
5633
  }
@@ -5978,9 +6084,15 @@ const editorMachine = setup({
5978
6084
  event
5979
6085
  }) => (assertEvent(event, "update schema"), event.schema)
5980
6086
  }),
5981
- "emit patch event": emit(({
5982
- event
5983
- }) => (assertEvent(event, "patch"), event)),
6087
+ "emit patch event": enqueueActions(({
6088
+ event,
6089
+ enqueue
6090
+ }) => {
6091
+ assertEvent(event, "internal.patch"), enqueue.emit(event), enqueue.emit({
6092
+ type: "patch",
6093
+ patch: event.patch
6094
+ });
6095
+ }),
5984
6096
  "emit mutation event": emit(({
5985
6097
  event
5986
6098
  }) => (assertEvent(event, "mutation"), event)),
@@ -5994,14 +6106,17 @@ const editorMachine = setup({
5994
6106
  pendingEvents: ({
5995
6107
  context,
5996
6108
  event
5997
- }) => (assertEvent(event, ["patch", "mutation"]), [...context.pendingEvents, event])
6109
+ }) => (assertEvent(event, ["internal.patch", "mutation"]), [...context.pendingEvents, event])
5998
6110
  }),
5999
6111
  "emit pending events": enqueueActions(({
6000
6112
  context,
6001
6113
  enqueue
6002
6114
  }) => {
6003
6115
  for (const event of context.pendingEvents)
6004
- enqueue(emit(event));
6116
+ event.type === "internal.patch" ? (enqueue.emit(event), enqueue.emit({
6117
+ type: "patch",
6118
+ patch: event.patch
6119
+ })) : enqueue.emit(event);
6005
6120
  }),
6006
6121
  "emit ready": emit({
6007
6122
  type: "ready"
@@ -6417,7 +6532,7 @@ const editorMachine = setup({
6417
6532
  "setting up": {
6418
6533
  exit: ["emit ready"],
6419
6534
  on: {
6420
- patch: {
6535
+ "internal.patch": {
6421
6536
  actions: "defer event"
6422
6537
  },
6423
6538
  mutation: {
@@ -6436,7 +6551,7 @@ const editorMachine = setup({
6436
6551
  normalizing: {
6437
6552
  target: "normalizing"
6438
6553
  },
6439
- patch: {
6554
+ "internal.patch": {
6440
6555
  actions: "defer event",
6441
6556
  target: "#editor.setup.dirty"
6442
6557
  },
@@ -6451,7 +6566,7 @@ const editorMachine = setup({
6451
6566
  "done normalizing": {
6452
6567
  target: "idle"
6453
6568
  },
6454
- patch: {
6569
+ "internal.patch": {
6455
6570
  actions: "defer event"
6456
6571
  },
6457
6572
  mutation: {
@@ -6464,7 +6579,7 @@ const editorMachine = setup({
6464
6579
  dirty: {
6465
6580
  entry: ["emit pending events", "clear pending events"],
6466
6581
  on: {
6467
- patch: {
6582
+ "internal.patch": {
6468
6583
  actions: "emit patch event"
6469
6584
  },
6470
6585
  mutation: {