@portabletext/editor 1.10.1 → 1.11.0

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.mjs CHANGED
@@ -5,25 +5,25 @@ import { jsx, Fragment, jsxs } from "react/jsx-runtime";
5
5
  import { useSelector, useActorRef } from "@xstate/react";
6
6
  import isEqual from "lodash/isEqual.js";
7
7
  import noop from "lodash/noop.js";
8
- import { useRef, useState, useEffect, useMemo, createContext, useContext, useCallback, startTransition, Component, forwardRef, useImperativeHandle } from "react";
9
- import { Editor, Element as Element$1, Range, Point, Text, Path, Transforms, Operation, Node, createEditor, deleteBackward, deleteForward, insertText } from "slate";
10
- import { useSlateStatic, ReactEditor, useSelected, useSlate, withReact, Slate, Editable } from "slate-react";
8
+ import React, { useRef, useState, useEffect, useMemo, createContext, useContext, useCallback, startTransition, Component, forwardRef, useImperativeHandle } from "react";
9
+ import { Editor, Element as Element$1, Range, Point, Text, Path, Transforms, Operation, Node, createEditor as createEditor$1, deleteBackward, deleteForward, insertText } from "slate";
10
+ import { useSlateStatic, ReactEditor, useSelected, withReact, Slate, useSlate, Editable } from "slate-react";
11
11
  import debug$m from "debug";
12
12
  import { c } from "react-compiler-runtime";
13
13
  import { styled } from "styled-components";
14
14
  import uniq from "lodash/uniq.js";
15
15
  import { Subject } from "rxjs";
16
- import { fromCallback, setup, assign, assertEvent, emit, enqueueActions, createActor } from "xstate";
17
- import throttle from "lodash/throttle.js";
18
16
  import { useEffectEvent } from "use-effect-event";
17
+ import throttle from "lodash/throttle.js";
19
18
  import debounce from "lodash/debounce.js";
20
19
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
21
20
  import flatten from "lodash/flatten.js";
22
21
  import isPlainObject from "lodash/isPlainObject.js";
22
+ import getRandomValues from "get-random-values-esm";
23
+ import { setup, assign, assertEvent, emit, enqueueActions, createActor } from "xstate";
23
24
  import get from "lodash/get.js";
24
25
  import isUndefined from "lodash/isUndefined.js";
25
26
  import omitBy from "lodash/omitBy.js";
26
- import getRandomValues from "get-random-values-esm";
27
27
  import { isHotkey } from "is-hotkey-esm";
28
28
  import { htmlToBlocks, normalizeBlock } from "@sanity/block-tools";
29
29
  function defineBehavior(behavior) {
@@ -317,6 +317,65 @@ const breakingBlockObject = {
317
317
  blockObjects: coreBlockObjectBehaviors,
318
318
  lists: coreListBehaviors
319
319
  };
320
+ function createLinkBehaviors(config) {
321
+ const pasteLinkOnSelection = {
322
+ on: "paste",
323
+ guard: ({
324
+ context,
325
+ event
326
+ }) => {
327
+ const selectionCollapsed = selectionIsCollapsed(context), text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
328
+ url,
329
+ schema: context.schema
330
+ }) : void 0;
331
+ return annotation && !selectionCollapsed ? {
332
+ annotation
333
+ } : !1;
334
+ },
335
+ actions: [(_, {
336
+ annotation
337
+ }) => [{
338
+ type: "annotation.add",
339
+ annotation
340
+ }]]
341
+ }, pasteLinkAtCaret = {
342
+ on: "paste",
343
+ guard: ({
344
+ context,
345
+ event
346
+ }) => {
347
+ const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
348
+ if (!focusSpan || !selectionCollapsed)
349
+ return !1;
350
+ const text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
351
+ url,
352
+ schema: context.schema
353
+ }) : void 0;
354
+ return url && annotation && selectionCollapsed ? {
355
+ focusSpan,
356
+ annotation,
357
+ url
358
+ } : !1;
359
+ },
360
+ actions: [(_, {
361
+ annotation,
362
+ url
363
+ }) => [{
364
+ type: "insert span",
365
+ text: url,
366
+ annotations: [annotation]
367
+ }]]
368
+ };
369
+ return [pasteLinkOnSelection, pasteLinkAtCaret];
370
+ }
371
+ function looksLikeUrl(text) {
372
+ let looksLikeUrl2 = !1;
373
+ try {
374
+ new URL(text), looksLikeUrl2 = !0;
375
+ } catch {
376
+ }
377
+ return looksLikeUrl2;
378
+ }
320
379
  function isPortableTextSpan(node) {
321
380
  return node._type === "span" && "text" in node && typeof node.text == "string" && (typeof node.marks > "u" || Array.isArray(node.marks) && node.marks.every((mark) => typeof mark == "string"));
322
381
  }
@@ -573,65 +632,6 @@ function createMarkdownBehaviors(config) {
573
632
  };
574
633
  return [automaticBlockquoteOnSpace, automaticBreak, automaticHeadingOnSpace, clearStyleOnBackspace, automaticListOnSpace];
575
634
  }
576
- function createLinkBehaviors(config) {
577
- const pasteLinkOnSelection = {
578
- on: "paste",
579
- guard: ({
580
- context,
581
- event
582
- }) => {
583
- const selectionCollapsed = selectionIsCollapsed(context), text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
584
- url,
585
- schema: context.schema
586
- }) : void 0;
587
- return annotation && !selectionCollapsed ? {
588
- annotation
589
- } : !1;
590
- },
591
- actions: [(_, {
592
- annotation
593
- }) => [{
594
- type: "annotation.add",
595
- annotation
596
- }]]
597
- }, pasteLinkAtCaret = {
598
- on: "paste",
599
- guard: ({
600
- context,
601
- event
602
- }) => {
603
- const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
604
- if (!focusSpan || !selectionCollapsed)
605
- return !1;
606
- const text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
607
- url,
608
- schema: context.schema
609
- }) : void 0;
610
- return url && annotation && selectionCollapsed ? {
611
- focusSpan,
612
- annotation,
613
- url
614
- } : !1;
615
- },
616
- actions: [(_, {
617
- annotation,
618
- url
619
- }) => [{
620
- type: "insert span",
621
- text: url,
622
- annotations: [annotation]
623
- }]]
624
- };
625
- return [pasteLinkOnSelection, pasteLinkAtCaret];
626
- }
627
- function looksLikeUrl(text) {
628
- let looksLikeUrl2 = !1;
629
- try {
630
- new URL(text), looksLikeUrl2 = !0;
631
- } catch {
632
- }
633
- return looksLikeUrl2;
634
- }
635
635
  function getPortableTextMemberSchemaTypes(portableTextType) {
636
636
  if (!portableTextType)
637
637
  throw new Error("Parameter 'portabletextType' missing (required)");
@@ -2773,8 +2773,9 @@ function useSyncValue(props) {
2773
2773
  const {
2774
2774
  editorActor,
2775
2775
  portableTextEditor,
2776
- readOnly
2777
- } = props, schemaTypes = editorActor.getSnapshot().context.schema, previousValue = useRef(), slateEditor = useSlate(), updateValueFunctionRef = useRef(), updateFromCurrentValue = useCallback(() => {
2776
+ readOnly,
2777
+ slateEditor
2778
+ } = props, schemaTypes = editorActor.getSnapshot().context.schema, previousValue = useRef(), updateValueFunctionRef = useRef(), updateFromCurrentValue = useCallback(() => {
2778
2779
  const currentValue = CURRENT_VALUE.get(portableTextEditor);
2779
2780
  if (previousValue.current === currentValue) {
2780
2781
  debug$j("Value is the same object as previous, not need to sync");
@@ -2957,27 +2958,29 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
2957
2958
  }
2958
2959
  const debug$i = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$3 = debug$i.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
2959
2960
  function Synchronizer(props) {
2960
- const $ = c(35), portableTextEditor = usePortableTextEditor(), readOnly = useSelector(props.editorActor, _temp$1), value = useSelector(props.editorActor, _temp2), {
2961
+ const $ = c(26), readOnly = useSelector(props.editorActor, _temp), value = useSelector(props.editorActor, _temp2), {
2961
2962
  editorActor,
2962
2963
  getValue,
2963
- onChange
2964
+ portableTextEditor,
2965
+ slateEditor
2964
2966
  } = props;
2965
2967
  let t0;
2966
2968
  $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = [], $[0] = t0) : t0 = $[0];
2967
2969
  const pendingPatches = useRef(t0);
2968
2970
  let t1;
2969
- $[1] !== editorActor || $[2] !== portableTextEditor || $[3] !== readOnly ? (t1 = {
2971
+ $[1] !== editorActor || $[2] !== portableTextEditor || $[3] !== readOnly || $[4] !== slateEditor ? (t1 = {
2970
2972
  editorActor,
2971
2973
  portableTextEditor,
2972
- readOnly
2973
- }, $[1] = editorActor, $[2] = portableTextEditor, $[3] = readOnly, $[4] = t1) : t1 = $[4];
2974
- const syncValue = useSyncValue(t1), slateEditor = useSlate();
2974
+ readOnly,
2975
+ slateEditor
2976
+ }, $[1] = editorActor, $[2] = portableTextEditor, $[3] = readOnly, $[4] = slateEditor, $[5] = t1) : t1 = $[5];
2977
+ const syncValue = useSyncValue(t1);
2975
2978
  let t2, t3;
2976
- $[5] !== slateEditor ? (t2 = () => {
2979
+ $[6] !== slateEditor ? (t2 = () => {
2977
2980
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !1);
2978
- }, t3 = [slateEditor], $[5] = slateEditor, $[6] = t2, $[7] = t3) : (t2 = $[6], t3 = $[7]), useEffect(t2, t3);
2981
+ }, t3 = [slateEditor], $[6] = slateEditor, $[7] = t2, $[8] = t3) : (t2 = $[7], t3 = $[8]), useEffect(t2, t3);
2979
2982
  let t4;
2980
- $[8] !== editorActor || $[9] !== getValue || $[10] !== slateEditor ? (t4 = () => {
2983
+ $[9] !== editorActor || $[10] !== getValue || $[11] !== slateEditor ? (t4 = () => {
2981
2984
  if (pendingPatches.current.length > 0) {
2982
2985
  debug$i("Flushing pending patches"), debugVerbose$3 && debug$i(`Patches:
2983
2986
  ${JSON.stringify(pendingPatches.current, null, 2)}`);
@@ -2989,17 +2992,14 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
2989
2992
  }), pendingPatches.current = [];
2990
2993
  }
2991
2994
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !1);
2992
- }, $[8] = editorActor, $[9] = getValue, $[10] = slateEditor, $[11] = t4) : t4 = $[11];
2995
+ }, $[9] = editorActor, $[10] = getValue, $[11] = slateEditor, $[12] = t4) : t4 = $[12];
2993
2996
  const onFlushPendingPatches = t4;
2994
2997
  let t5, t6;
2995
- $[12] !== onFlushPendingPatches ? (t5 = () => () => {
2998
+ $[13] !== onFlushPendingPatches ? (t5 = () => () => {
2996
2999
  onFlushPendingPatches();
2997
- }, t6 = [onFlushPendingPatches], $[12] = onFlushPendingPatches, $[13] = t5, $[14] = t6) : (t5 = $[13], t6 = $[14]), useEffect(t5, t6);
2998
- let t7;
2999
- $[15] !== onChange ? (t7 = (change) => onChange(change), $[15] = onChange, $[16] = t7) : t7 = $[16];
3000
- const handleChange = useEffectEvent(t7);
3001
- let t8, t9;
3002
- $[17] !== editorActor || $[18] !== handleChange || $[19] !== onFlushPendingPatches || $[20] !== slateEditor ? (t8 = () => {
3000
+ }, t6 = [onFlushPendingPatches], $[13] = onFlushPendingPatches, $[14] = t5, $[15] = t6) : (t5 = $[14], t6 = $[15]), useEffect(t5, t6);
3001
+ let t7, t8;
3002
+ $[16] !== editorActor || $[17] !== onFlushPendingPatches || $[18] !== slateEditor ? (t7 = () => {
3003
3003
  const onFlushPendingPatchesThrottled = throttle(() => {
3004
3004
  if (Editor.isNormalizing(slateEditor)) {
3005
3005
  onFlushPendingPatches();
@@ -3010,116 +3010,76 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
3010
3010
  leading: !1,
3011
3011
  trailing: !0
3012
3012
  });
3013
- debug$i("Subscribing to editor changes");
3014
- const sub = editorActor.on("*", (event) => {
3015
- bb22: switch (event.type) {
3016
- case "patch": {
3017
- IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !0), pendingPatches.current.push(event.patch), onFlushPendingPatchesThrottled(), handleChange(event);
3018
- break bb22;
3019
- }
3020
- case "loading": {
3021
- handleChange({
3022
- type: "loading",
3023
- isLoading: !0
3024
- });
3025
- break bb22;
3026
- }
3027
- case "done loading": {
3028
- handleChange({
3029
- type: "loading",
3030
- isLoading: !1
3031
- });
3032
- break bb22;
3033
- }
3034
- case "focused": {
3035
- handleChange({
3036
- type: "focus",
3037
- event: event.event
3038
- });
3039
- break bb22;
3040
- }
3041
- case "offline": {
3042
- handleChange({
3043
- type: "connection",
3044
- value: "offline"
3045
- });
3046
- break bb22;
3047
- }
3048
- case "online": {
3049
- handleChange({
3050
- type: "connection",
3051
- value: "online"
3052
- });
3053
- break bb22;
3054
- }
3055
- case "value changed": {
3056
- handleChange({
3057
- type: "value",
3058
- value: event.value
3059
- });
3060
- break bb22;
3061
- }
3062
- case "invalid value": {
3063
- handleChange({
3064
- type: "invalidValue",
3065
- resolution: event.resolution,
3066
- value: event.value
3067
- });
3068
- break bb22;
3069
- }
3070
- case "error": {
3071
- handleChange({
3072
- ...event,
3073
- level: "warning"
3074
- });
3075
- break bb22;
3076
- }
3077
- case "annotation.add":
3078
- case "annotation.remove":
3079
- case "annotation.toggle":
3080
- case "focus":
3081
- case "patches":
3082
- break bb22;
3083
- default:
3084
- handleChange(event);
3085
- }
3013
+ debug$i("Subscribing to patch events");
3014
+ const sub = editorActor.on("patch", (event) => {
3015
+ IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !0), pendingPatches.current.push(event.patch), onFlushPendingPatchesThrottled();
3086
3016
  });
3087
3017
  return () => {
3088
- debug$i("Unsubscribing to changes"), sub.unsubscribe();
3018
+ debug$i("Unsubscribing to patch events"), sub.unsubscribe();
3089
3019
  };
3090
- }, t9 = [editorActor, handleChange, onFlushPendingPatches, slateEditor], $[17] = editorActor, $[18] = handleChange, $[19] = onFlushPendingPatches, $[20] = slateEditor, $[21] = t8, $[22] = t9) : (t8 = $[21], t9 = $[22]), useEffect(t8, t9);
3091
- let t10;
3092
- $[23] !== syncValue || $[24] !== value ? (t10 = () => {
3093
- debug$i("Editor is online, syncing from props.value"), syncValue(value);
3094
- }, $[23] = syncValue, $[24] = value, $[25] = t10) : t10 = $[25];
3095
- const handleOnline = t10;
3096
- let t11, t12;
3097
- $[26] !== editorActor || $[27] !== handleOnline ? (t11 = () => {
3098
- const subscription = editorActor.on("online", handleOnline);
3099
- return () => {
3100
- subscription.unsubscribe();
3101
- };
3102
- }, t12 = [handleOnline, editorActor], $[26] = editorActor, $[27] = handleOnline, $[28] = t11, $[29] = t12) : (t11 = $[28], t12 = $[29]), useEffect(t11, t12);
3020
+ }, t8 = [editorActor, onFlushPendingPatches, slateEditor], $[16] = editorActor, $[17] = onFlushPendingPatches, $[18] = slateEditor, $[19] = t7, $[20] = t8) : (t7 = $[19], t8 = $[20]), useEffect(t7, t8);
3103
3021
  const isInitialValueFromProps = useRef(!0);
3104
- let t13, t14;
3105
- return $[30] !== editorActor || $[31] !== syncValue || $[32] !== value ? (t13 = () => {
3022
+ let t10, t9;
3023
+ return $[21] !== editorActor || $[22] !== syncValue || $[23] !== value ? (t9 = () => {
3106
3024
  debug$i("Value from props changed, syncing new value"), syncValue(value), isInitialValueFromProps.current && (editorActor.send({
3107
3025
  type: "ready"
3108
3026
  }), isInitialValueFromProps.current = !1);
3109
- }, t14 = [editorActor, syncValue, value], $[30] = editorActor, $[31] = syncValue, $[32] = value, $[33] = t13, $[34] = t14) : (t13 = $[33], t14 = $[34]), useEffect(t13, t14), null;
3027
+ }, t10 = [editorActor, syncValue, value], $[21] = editorActor, $[22] = syncValue, $[23] = value, $[24] = t10, $[25] = t9) : (t10 = $[24], t9 = $[25]), useEffect(t9, t10), null;
3110
3028
  }
3111
3029
  function _temp2(s_0) {
3112
3030
  return s_0.context.value;
3113
3031
  }
3114
- function _temp$1(s) {
3032
+ function _temp(s) {
3115
3033
  return s.context.readOnly;
3116
3034
  }
3117
3035
  Synchronizer.displayName = "Synchronizer";
3118
- const debug$h = debugWithName("operationToPatches");
3036
+ const EditorActorContext = createContext({}), PortableTextEditorSelectionContext = createContext(null), usePortableTextEditorSelection = () => {
3037
+ const selection = useContext(PortableTextEditorSelectionContext);
3038
+ if (selection === void 0)
3039
+ throw new Error("The `usePortableTextEditorSelection` hook must be used inside the <PortableTextEditor> component's context.");
3040
+ return selection;
3041
+ }, debug$h = debugWithName("component:PortableTextEditor:SelectionProvider"), debugVerbose$2 = debug$h.enabled && !1;
3042
+ function PortableTextEditorSelectionProvider(props) {
3043
+ const $ = c(6), [selection, setSelection] = useState(null);
3044
+ let t0, t1;
3045
+ $[0] !== props.editorActor ? (t0 = () => {
3046
+ debug$h("Subscribing to selection changes");
3047
+ const subscription = props.editorActor.on("selection", (event) => {
3048
+ startTransition(() => {
3049
+ debugVerbose$2 && debug$h("Setting selection"), setSelection(event.selection);
3050
+ });
3051
+ });
3052
+ return () => {
3053
+ debug$h("Unsubscribing to selection changes"), subscription.unsubscribe();
3054
+ };
3055
+ }, t1 = [props.editorActor], $[0] = props.editorActor, $[1] = t0, $[2] = t1) : (t0 = $[1], t1 = $[2]), useEffect(t0, t1);
3056
+ let t2;
3057
+ return $[3] !== props.children || $[4] !== selection ? (t2 = /* @__PURE__ */ jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children }), $[3] = props.children, $[4] = selection, $[5] = t2) : t2 = $[5], t2;
3058
+ }
3059
+ const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
3060
+ let table;
3061
+ return () => {
3062
+ if (table)
3063
+ return table;
3064
+ table = [];
3065
+ for (let i = 0; i < 256; ++i)
3066
+ table[i] = (i + 256).toString(16).slice(1);
3067
+ return table;
3068
+ };
3069
+ })();
3070
+ function whatwgRNG(length = 16) {
3071
+ const rnds8 = new Uint8Array(length);
3072
+ return getRandomValues(rnds8), rnds8;
3073
+ }
3074
+ function randomKey(length) {
3075
+ const table = getByteHexTable();
3076
+ return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
3077
+ }
3078
+ const debug$g = debugWithName("operationToPatches");
3119
3079
  function createOperationToPatches(types) {
3120
3080
  const textBlockName = types.block.name;
3121
3081
  function insertTextPatch(editor, operation, beforeValue) {
3122
- debug$h.enabled && debug$h("Operation", JSON.stringify(operation, null, 2));
3082
+ debug$g.enabled && debug$g("Operation", JSON.stringify(operation, null, 2));
3123
3083
  const block = editor.isTextBlock(editor.children[operation.path[0]]) && editor.children[operation.path[0]];
3124
3084
  if (!block)
3125
3085
  throw new Error("Could not find block");
@@ -3212,7 +3172,7 @@ function createOperationToPatches(types) {
3212
3172
  _key: block.children[operation.path[1] - 1]._key
3213
3173
  }])];
3214
3174
  }
3215
- return debug$h("Something was inserted into a void block. Not producing editor patches."), [];
3175
+ return debug$g("Something was inserted into a void block. Not producing editor patches."), [];
3216
3176
  }
3217
3177
  function splitNodePatch(editor, operation, beforeValue) {
3218
3178
  const patches = [], splitBlock = editor.children[operation.path[0]];
@@ -3270,9 +3230,9 @@ function createOperationToPatches(types) {
3270
3230
  _key: block._key
3271
3231
  }, "children", {
3272
3232
  _key: spanToRemove._key
3273
- }])] : (debug$h("Span not found in editor trying to remove node"), []);
3233
+ }])] : (debug$g("Span not found in editor trying to remove node"), []);
3274
3234
  } else
3275
- return debug$h("Not creating patch inside object block"), [];
3235
+ return debug$g("Not creating patch inside object block"), [];
3276
3236
  }
3277
3237
  function mergeNodePatch(editor, operation, beforeValue) {
3278
3238
  const patches = [], block = beforeValue[operation.path[0]], updatedBlock = editor.children[operation.path[0]];
@@ -3298,7 +3258,7 @@ function createOperationToPatches(types) {
3298
3258
  _key: removedSpan._key
3299
3259
  }])) : console.warn(`Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`, JSON.stringify(block, null, 2)));
3300
3260
  } else
3301
- debug$h("Void nodes can't be merged, not creating any patches");
3261
+ debug$g("Void nodes can't be merged, not creating any patches");
3302
3262
  return patches;
3303
3263
  }
3304
3264
  function moveNodePatch(editor, operation, beforeValue) {
@@ -3543,13 +3503,13 @@ function createWithObjectKeys(editorActor, schemaTypes) {
3543
3503
  }, editor;
3544
3504
  };
3545
3505
  }
3546
- const debug$g = debugWithName("applyPatches"), debugVerbose$2 = debug$g.enabled && !0;
3506
+ const debug$f = debugWithName("applyPatches"), debugVerbose$1 = debug$f.enabled && !0;
3547
3507
  function createApplyPatch(schemaTypes) {
3548
3508
  return (editor, patch) => {
3549
3509
  let changed = !1;
3550
- debugVerbose$2 && (debug$g(`
3510
+ debugVerbose$1 && (debug$f(`
3551
3511
 
3552
- NEW PATCH =============================================================`), debug$g(JSON.stringify(patch, null, 2)));
3512
+ NEW PATCH =============================================================`), debug$f(JSON.stringify(patch, null, 2)));
3553
3513
  try {
3554
3514
  switch (patch.type) {
3555
3515
  case "insert":
@@ -3565,7 +3525,7 @@ NEW PATCH =============================================================`), debug
3565
3525
  changed = diffMatchPatch(editor, patch);
3566
3526
  break;
3567
3527
  default:
3568
- debug$g("Unhandled patch", patch.type);
3528
+ debug$f("Unhandled patch", patch.type);
3569
3529
  }
3570
3530
  } catch (err) {
3571
3531
  console.error(err);
@@ -3580,9 +3540,9 @@ function diffMatchPatch(editor, patch) {
3580
3540
  childPath
3581
3541
  } = findBlockAndChildFromPath(editor, patch.path);
3582
3542
  if (!block)
3583
- return debug$g("Block not found"), !1;
3543
+ return debug$f("Block not found"), !1;
3584
3544
  if (!child || !childPath)
3585
- return debug$g("Child not found"), !1;
3545
+ return debug$f("Child not found"), !1;
3586
3546
  if (!(block && editor.isTextBlock(block) && patch.path.length === 4 && patch.path[1] === "children" && patch.path[3] === "text") || !Text.isText(child))
3587
3547
  return !1;
3588
3548
  const patches = parse(patch.value), [newValue] = apply(patches, child.text, {
@@ -3612,9 +3572,9 @@ function insertPatch(editor, patch, schemaTypes) {
3612
3572
  childPath: targetChildPath
3613
3573
  } = findBlockAndChildFromPath(editor, patch.path);
3614
3574
  if (!targetBlock || !targetBlockPath)
3615
- return debug$g("Block not found"), !1;
3575
+ return debug$f("Block not found"), !1;
3616
3576
  if (patch.path.length > 1 && patch.path[1] !== "children")
3617
- return debug$g("Ignoring patch targeting void value"), !1;
3577
+ return debug$f("Ignoring patch targeting void value"), !1;
3618
3578
  if (patch.path.length === 1) {
3619
3579
  const {
3620
3580
  items: items2,
@@ -3622,7 +3582,7 @@ function insertPatch(editor, patch, schemaTypes) {
3622
3582
  } = patch, blocksToInsert = toSlateValue(items2, {
3623
3583
  schemaTypes
3624
3584
  }, KEY_TO_SLATE_ELEMENT.get(editor)), targetBlockIndex = targetBlockPath[0], normalizedIdx2 = position2 === "after" ? targetBlockIndex + 1 : targetBlockIndex;
3625
- return debug$g(`Inserting blocks at path [${normalizedIdx2}]`), debugState(editor, "before"), Transforms.insertNodes(editor, blocksToInsert, {
3585
+ return debug$f(`Inserting blocks at path [${normalizedIdx2}]`), debugState(editor, "before"), Transforms.insertNodes(editor, blocksToInsert, {
3626
3586
  at: [normalizedIdx2]
3627
3587
  }), debugState(editor, "after"), !0;
3628
3588
  }
@@ -3631,14 +3591,14 @@ function insertPatch(editor, patch, schemaTypes) {
3631
3591
  position
3632
3592
  } = patch;
3633
3593
  if (!targetChild || !targetChildPath)
3634
- return debug$g("Child not found"), !1;
3594
+ return debug$f("Child not found"), !1;
3635
3595
  const childrenToInsert = targetBlock && toSlateValue([{
3636
3596
  ...targetBlock,
3637
3597
  children: items
3638
3598
  }], {
3639
3599
  schemaTypes
3640
3600
  }, KEY_TO_SLATE_ELEMENT.get(editor)), targetChildIndex = targetChildPath[1], normalizedIdx = position === "after" ? targetChildIndex + 1 : targetChildIndex, childInsertPath = [targetChildPath[0], normalizedIdx];
3641
- return debug$g(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && Element$1.isElement(childrenToInsert[0]) && Transforms.insertNodes(editor, childrenToInsert[0].children, {
3601
+ return debug$f(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && Element$1.isElement(childrenToInsert[0]) && Transforms.insertNodes(editor, childrenToInsert[0].children, {
3642
3602
  at: childInsertPath
3643
3603
  }), debugState(editor, "after"), !0;
3644
3604
  }
@@ -3652,14 +3612,14 @@ function setPatch(editor, patch) {
3652
3612
  childPath
3653
3613
  } = findBlockAndChildFromPath(editor, patch.path);
3654
3614
  if (!block)
3655
- return debug$g("Block not found"), !1;
3615
+ return debug$f("Block not found"), !1;
3656
3616
  const isTextBlock = editor.isTextBlock(block);
3657
3617
  if (isTextBlock && patch.path.length > 1 && patch.path[1] !== "children")
3658
- return debug$g("Ignoring setting void value"), !1;
3618
+ return debug$f("Ignoring setting void value"), !1;
3659
3619
  if (debugState(editor, "before"), isTextBlock && child && childPath) {
3660
3620
  if (Text.isText(value) && Text.isText(child)) {
3661
3621
  const newText = child.text;
3662
- value.text !== newText && (debug$g("Setting text property"), editor.apply({
3622
+ value.text !== newText && (debug$f("Setting text property"), editor.apply({
3663
3623
  type: "remove_text",
3664
3624
  path: childPath,
3665
3625
  offset: 0,
@@ -3671,7 +3631,7 @@ function setPatch(editor, patch) {
3671
3631
  text: value.text
3672
3632
  }), editor.onChange());
3673
3633
  } else
3674
- debug$g("Setting non-text property"), editor.apply({
3634
+ debug$f("Setting non-text property"), editor.apply({
3675
3635
  type: "set_node",
3676
3636
  path: childPath,
3677
3637
  properties: {},
@@ -3679,7 +3639,7 @@ function setPatch(editor, patch) {
3679
3639
  });
3680
3640
  return !0;
3681
3641
  } else if (Element$1.isElement(block) && patch.path.length === 1 && blockPath) {
3682
- debug$g("Setting block property");
3642
+ debug$f("Setting block property");
3683
3643
  const {
3684
3644
  children,
3685
3645
  ...nextRest
@@ -3696,7 +3656,7 @@ function setPatch(editor, patch) {
3696
3656
  ...prevRest
3697
3657
  },
3698
3658
  newProperties: nextRest
3699
- }), debug$g("Setting children"), block.children.forEach((c2, cIndex) => {
3659
+ }), debug$f("Setting children"), block.children.forEach((c2, cIndex) => {
3700
3660
  editor.apply({
3701
3661
  type: "remove_node",
3702
3662
  path: blockPath.concat(block.children.length - 1 - cIndex),
@@ -3722,7 +3682,7 @@ function setPatch(editor, patch) {
3722
3682
  }
3723
3683
  function unsetPatch(editor, patch) {
3724
3684
  if (patch.path.length === 0) {
3725
- debug$g("Removing everything"), debugState(editor, "before");
3685
+ debug$f("Removing everything"), debugState(editor, "before");
3726
3686
  const previousSelection = editor.selection;
3727
3687
  return Transforms.deselect(editor), editor.children.forEach((_child, i) => {
3728
3688
  Transforms.removeNodes(editor, {
@@ -3749,13 +3709,13 @@ function unsetPatch(editor, patch) {
3749
3709
  } = findBlockAndChildFromPath(editor, patch.path);
3750
3710
  if (patch.path.length === 1) {
3751
3711
  if (!block || !blockPath)
3752
- return debug$g("Block not found"), !1;
3712
+ return debug$f("Block not found"), !1;
3753
3713
  const blockIndex = blockPath[0];
3754
- return debug$g(`Removing block at path [${blockIndex}]`), debugState(editor, "before"), Transforms.removeNodes(editor, {
3714
+ return debug$f(`Removing block at path [${blockIndex}]`), debugState(editor, "before"), Transforms.removeNodes(editor, {
3755
3715
  at: [blockIndex]
3756
3716
  }), debugState(editor, "after"), !0;
3757
3717
  }
3758
- return editor.isTextBlock(block) && patch.path[1] === "children" && patch.path.length === 3 ? !child || !childPath ? (debug$g("Child not found"), !1) : (debug$g(`Unsetting child at path ${JSON.stringify(childPath)}`), debugState(editor, "before"), debugVerbose$2 && debug$g(`Removing child at path ${JSON.stringify(childPath)}`), Transforms.removeNodes(editor, {
3718
+ return editor.isTextBlock(block) && patch.path[1] === "children" && patch.path.length === 3 ? !child || !childPath ? (debug$f("Child not found"), !1) : (debug$f(`Unsetting child at path ${JSON.stringify(childPath)}`), debugState(editor, "before"), debugVerbose$1 && debug$f(`Removing child at path ${JSON.stringify(childPath)}`), Transforms.removeNodes(editor, {
3759
3719
  at: childPath
3760
3720
  }), debugState(editor, "after"), !0) : !1;
3761
3721
  }
@@ -3763,7 +3723,7 @@ function isKeyedSegment(segment) {
3763
3723
  return typeof segment == "object" && "_key" in segment;
3764
3724
  }
3765
3725
  function debugState(editor, stateName) {
3766
- debugVerbose$2 && (debug$g(`Children ${stateName}:`, JSON.stringify(editor.children, null, 2)), debug$g(`Selection ${stateName}: `, JSON.stringify(editor.selection, null, 2)));
3726
+ debugVerbose$1 && (debug$f(`Children ${stateName}:`, JSON.stringify(editor.children, null, 2)), debug$f(`Selection ${stateName}: `, JSON.stringify(editor.selection, null, 2)));
3767
3727
  }
3768
3728
  function findBlockFromPath(editor, path) {
3769
3729
  let blockIndex = -1;
@@ -3805,7 +3765,7 @@ function findBlockAndChildFromPath(editor, path) {
3805
3765
  childPath: void 0
3806
3766
  };
3807
3767
  }
3808
- const debug$f = debugWithName("plugin:withPatches");
3768
+ const debug$e = debugWithName("plugin:withPatches");
3809
3769
  function createWithPatches({
3810
3770
  editorActor,
3811
3771
  patchFunctions,
@@ -3831,7 +3791,7 @@ function createWithPatches({
3831
3791
  withoutPatching(editor, () => {
3832
3792
  withoutSaving(editor, () => {
3833
3793
  patches.forEach((patch) => {
3834
- debug$f.enabled && debug$f(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
3794
+ debug$e.enabled && debug$e(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
3835
3795
  });
3836
3796
  });
3837
3797
  });
@@ -3844,10 +3804,10 @@ function createWithPatches({
3844
3804
  remotePatches.length !== 0 && (bufferedPatches = bufferedPatches.concat(remotePatches), handleBufferedRemotePatches());
3845
3805
  };
3846
3806
  return subscriptions.push(() => {
3847
- debug$f("Subscribing to remote patches");
3807
+ debug$e("Subscribing to remote patches");
3848
3808
  const sub = editorActor.on("patches", handlePatches);
3849
3809
  return () => {
3850
- debug$f("Unsubscribing to remote patches"), sub.unsubscribe();
3810
+ debug$e("Unsubscribing to remote patches"), sub.unsubscribe();
3851
3811
  };
3852
3812
  }), editor.apply = (operation) => {
3853
3813
  if (editorActor.getSnapshot().context.readOnly)
@@ -3900,7 +3860,7 @@ function createWithPatches({
3900
3860
  }, editor;
3901
3861
  };
3902
3862
  }
3903
- const debug$e = debugWithName("plugin:withPlaceholderBlock");
3863
+ const debug$d = debugWithName("plugin:withPlaceholderBlock");
3904
3864
  function createWithPlaceholderBlock(editorActor) {
3905
3865
  return function(editor) {
3906
3866
  const {
@@ -3920,7 +3880,7 @@ function createWithPlaceholderBlock(editorActor) {
3920
3880
  const node = op.node;
3921
3881
  if (op.path[0] === 0 && Editor.isVoid(editor, node)) {
3922
3882
  const nextPath = Path.next(op.path);
3923
- editor.children[nextPath[0]] || (debug$e("Adding placeholder block"), Editor.insertNode(editor, editor.pteCreateTextBlock({
3883
+ editor.children[nextPath[0]] || (debug$d("Adding placeholder block"), Editor.insertNode(editor, editor.pteCreateTextBlock({
3924
3884
  decorators: []
3925
3885
  })));
3926
3886
  }
@@ -3930,7 +3890,7 @@ function createWithPlaceholderBlock(editorActor) {
3930
3890
  }, editor;
3931
3891
  };
3932
3892
  }
3933
- const debug$d = debugWithName("plugin:withPortableTextBlockStyle");
3893
+ const debug$c = debugWithName("plugin:withPortableTextBlockStyle");
3934
3894
  function createWithPortableTextBlockStyle(editorActor, types) {
3935
3895
  const defaultStyle = types.styles[0].value;
3936
3896
  return function(editor) {
@@ -3943,7 +3903,7 @@ function createWithPortableTextBlockStyle(editorActor, types) {
3943
3903
  if (op.type === "split_node" && op.path.length === 1 && editor.isTextBlock(op.properties) && op.properties.style !== defaultStyle && op.path[0] === path[0] && !Path.equals(path, op.path)) {
3944
3904
  const [child] = Editor.node(editor, [op.path[0] + 1, 0]);
3945
3905
  if (Text.isText(child) && child.text === "") {
3946
- debug$d(`Normalizing split node to ${defaultStyle} style`, op), editorActor.send({
3906
+ debug$c(`Normalizing split node to ${defaultStyle} style`, op), editorActor.send({
3947
3907
  type: "normalizing"
3948
3908
  }), Transforms.setNodes(editor, {
3949
3909
  style: defaultStyle
@@ -3965,12 +3925,12 @@ function createWithPortableTextBlockStyle(editorActor, types) {
3965
3925
  at: editor.selection,
3966
3926
  match: (node) => editor.isTextBlock(node)
3967
3927
  })].forEach(([node, path]) => {
3968
- editor.isTextBlock(node) && node.style === blockStyle ? (debug$d(`Unsetting block style '${blockStyle}'`), Transforms.setNodes(editor, {
3928
+ editor.isTextBlock(node) && node.style === blockStyle ? (debug$c(`Unsetting block style '${blockStyle}'`), Transforms.setNodes(editor, {
3969
3929
  ...node,
3970
3930
  style: defaultStyle
3971
3931
  }, {
3972
3932
  at: path
3973
- })) : (blockStyle ? debug$d(`Setting style '${blockStyle}'`) : debug$d("Setting default style", defaultStyle), Transforms.setNodes(editor, {
3933
+ })) : (blockStyle ? debug$c(`Setting style '${blockStyle}'`) : debug$c("Setting default style", defaultStyle), Transforms.setNodes(editor, {
3974
3934
  ...node,
3975
3935
  style: blockStyle || defaultStyle
3976
3936
  }, {
@@ -3980,11 +3940,11 @@ function createWithPortableTextBlockStyle(editorActor, types) {
3980
3940
  }, editor;
3981
3941
  };
3982
3942
  }
3983
- const debug$c = debugWithName("plugin:withPortableTextLists"), MAX_LIST_LEVEL = 10;
3943
+ const debug$b = debugWithName("plugin:withPortableTextLists"), MAX_LIST_LEVEL = 10;
3984
3944
  function createWithPortableTextLists(types) {
3985
3945
  return function(editor) {
3986
3946
  return editor.pteToggleListItem = (listItemStyle) => {
3987
- editor.pteHasListStyle(listItemStyle) ? (debug$c(`Remove list item '${listItemStyle}'`), editor.pteUnsetListItem(listItemStyle)) : (debug$c(`Add list item '${listItemStyle}'`), editor.pteSetListItem(listItemStyle));
3947
+ editor.pteHasListStyle(listItemStyle) ? (debug$b(`Remove list item '${listItemStyle}'`), editor.pteUnsetListItem(listItemStyle)) : (debug$b(`Add list item '${listItemStyle}'`), editor.pteSetListItem(listItemStyle));
3988
3948
  }, editor.pteUnsetListItem = (listItemStyle) => {
3989
3949
  editor.selection && [...Editor.nodes(editor, {
3990
3950
  at: editor.selection,
@@ -4000,7 +3960,7 @@ function createWithPortableTextLists(types) {
4000
3960
  listItem: void 0,
4001
3961
  level: void 0
4002
3962
  };
4003
- debug$c(`Unsetting list '${listItemStyle}'`), Transforms.setNodes(editor, newNode, {
3963
+ debug$b(`Unsetting list '${listItemStyle}'`), Transforms.setNodes(editor, newNode, {
4004
3964
  at: path
4005
3965
  });
4006
3966
  }
@@ -4010,7 +3970,7 @@ function createWithPortableTextLists(types) {
4010
3970
  at: editor.selection,
4011
3971
  match: (node) => editor.isTextBlock(node)
4012
3972
  })].forEach(([node, path]) => {
4013
- debug$c(`Setting list '${listItemStyle}'`), Transforms.setNodes(editor, {
3973
+ debug$b(`Setting list '${listItemStyle}'`), Transforms.setNodes(editor, {
4014
3974
  ...node,
4015
3975
  level: 1,
4016
3976
  listItem: listItemStyle || types.lists[0] && types.lists[0].value
@@ -4026,7 +3986,7 @@ function createWithPortableTextLists(types) {
4026
3986
  match: (node) => Element$1.isElement(node) && editor.isListBlock(node) && node.children.length === 1 && Text.isText(node.children[0]) && node.children[0].text === ""
4027
3987
  })];
4028
3988
  return selectedBlocks.length === 0 ? !1 : (selectedBlocks.forEach(([node, path]) => {
4029
- Element$1.isElement(node) && (debug$c("Unset list"), Transforms.setNodes(editor, {
3989
+ Element$1.isElement(node) && (debug$b("Unset list"), Transforms.setNodes(editor, {
4030
3990
  ...node,
4031
3991
  level: void 0,
4032
3992
  listItem: void 0
@@ -4044,7 +4004,7 @@ function createWithPortableTextLists(types) {
4044
4004
  return selectedBlocks.length === 0 ? !1 : (selectedBlocks.forEach(([node, path]) => {
4045
4005
  if (editor.isListBlock(node)) {
4046
4006
  let level = node.level || 1;
4047
- reverse ? (level--, debug$c("Decrementing list level", Math.min(MAX_LIST_LEVEL, Math.max(1, level)))) : (level++, debug$c("Incrementing list level", Math.min(MAX_LIST_LEVEL, Math.max(1, level)))), Transforms.setNodes(editor, {
4007
+ reverse ? (level--, debug$b("Decrementing list level", Math.min(MAX_LIST_LEVEL, Math.max(1, level)))) : (level++, debug$b("Incrementing list level", Math.min(MAX_LIST_LEVEL, Math.max(1, level)))), Transforms.setNodes(editor, {
4048
4008
  level: Math.min(MAX_LIST_LEVEL, Math.max(1, level))
4049
4009
  }, {
4050
4010
  at: path
@@ -4090,7 +4050,7 @@ function getNextSpan({
4090
4050
  }
4091
4051
  return nextSpan;
4092
4052
  }
4093
- const debug$b = debugWithName("plugin:withPortableTextMarkModel");
4053
+ const debug$a = debugWithName("plugin:withPortableTextMarkModel");
4094
4054
  function createWithPortableTextMarkModel(editorActor, types) {
4095
4055
  return function(editor) {
4096
4056
  const {
@@ -4104,7 +4064,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4104
4064
  for (const [child, childPath] of children) {
4105
4065
  const nextNode = node.children[childPath[1] + 1];
4106
4066
  if (editor.isTextSpan(child) && editor.isTextSpan(nextNode) && child.marks?.every((mark) => nextNode.marks?.includes(mark)) && nextNode.marks?.every((mark) => child.marks?.includes(mark))) {
4107
- debug$b("Merging spans", JSON.stringify(child, null, 2), JSON.stringify(nextNode, null, 2)), editorActor.send({
4067
+ debug$a("Merging spans", JSON.stringify(child, null, 2), JSON.stringify(nextNode, null, 2)), editorActor.send({
4108
4068
  type: "normalizing"
4109
4069
  }), Transforms.mergeNodes(editor, {
4110
4070
  at: [childPath[0], childPath[1] + 1],
@@ -4117,7 +4077,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4117
4077
  }
4118
4078
  }
4119
4079
  if (editor.isTextBlock(node) && !Array.isArray(node.markDefs)) {
4120
- debug$b("Adding .markDefs to block node"), editorActor.send({
4080
+ debug$a("Adding .markDefs to block node"), editorActor.send({
4121
4081
  type: "normalizing"
4122
4082
  }), Transforms.setNodes(editor, {
4123
4083
  markDefs: []
@@ -4129,7 +4089,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4129
4089
  return;
4130
4090
  }
4131
4091
  if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
4132
- debug$b("Adding .marks to span node"), editorActor.send({
4092
+ debug$a("Adding .marks to span node"), editorActor.send({
4133
4093
  type: "normalizing"
4134
4094
  }), Transforms.setNodes(editor, {
4135
4095
  marks: []
@@ -4143,7 +4103,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4143
4103
  if (editor.isTextSpan(node)) {
4144
4104
  const blockPath = Path.parent(path), [block] = Editor.node(editor, blockPath), decorators2 = types.decorators.map((decorator) => decorator.value), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
4145
4105
  if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
4146
- debug$b("Removing annotations from empty span node"), editorActor.send({
4106
+ debug$a("Removing annotations from empty span node"), editorActor.send({
4147
4107
  type: "normalizing"
4148
4108
  }), Transforms.setNodes(editor, {
4149
4109
  marks: node.marks?.filter((mark) => decorators2.includes(mark))
@@ -4161,7 +4121,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4161
4121
  if (editor.isTextSpan(child)) {
4162
4122
  const marks = child.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !node.markDefs?.find((def) => def._key === mark));
4163
4123
  if (orphanedAnnotations.length > 0) {
4164
- debug$b("Removing orphaned annotations from span node"), editorActor.send({
4124
+ debug$a("Removing orphaned annotations from span node"), editorActor.send({
4165
4125
  type: "normalizing"
4166
4126
  }), Transforms.setNodes(editor, {
4167
4127
  marks: marks.filter((mark) => !orphanedAnnotations.includes(mark))
@@ -4179,7 +4139,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4179
4139
  if (editor.isTextBlock(block)) {
4180
4140
  const decorators2 = types.decorators.map((decorator) => decorator.value), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
4181
4141
  if (orphanedAnnotations.length > 0) {
4182
- debug$b("Removing orphaned annotations from span node"), editorActor.send({
4142
+ debug$a("Removing orphaned annotations from span node"), editorActor.send({
4183
4143
  type: "normalizing"
4184
4144
  }), Transforms.setNodes(editor, {
4185
4145
  marks: marks.filter((mark) => !orphanedAnnotations.includes(mark))
@@ -4197,7 +4157,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4197
4157
  for (const markDef of markDefs)
4198
4158
  markDefKeys.has(markDef._key) || (markDefKeys.add(markDef._key), newMarkDefs.push(markDef));
4199
4159
  if (markDefs.length !== newMarkDefs.length) {
4200
- debug$b("Removing duplicate markDefs"), editorActor.send({
4160
+ debug$a("Removing duplicate markDefs"), editorActor.send({
4201
4161
  type: "normalizing"
4202
4162
  }), Transforms.setNodes(editor, {
4203
4163
  markDefs: newMarkDefs
@@ -4212,7 +4172,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4212
4172
  if (editor.isTextBlock(node) && !editor.operations.some((op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1)) {
4213
4173
  const newMarkDefs = (node.markDefs || []).filter((def) => node.children.find((child) => Text.isText(child) && Array.isArray(child.marks) && child.marks.includes(def._key)));
4214
4174
  if (node.markDefs && !isEqual(newMarkDefs, node.markDefs)) {
4215
- debug$b("Removing markDef not in use"), editorActor.send({
4175
+ debug$a("Removing markDef not in use"), editorActor.send({
4216
4176
  type: "normalizing"
4217
4177
  }), Transforms.setNodes(editor, {
4218
4178
  markDefs: newMarkDefs
@@ -4429,7 +4389,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
4429
4389
  const [targetBlock, targetPath] = Editor.node(editor, [op.path[0] - 1]);
4430
4390
  if (editor.isTextBlock(targetBlock)) {
4431
4391
  const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq([...oldDefs, ...op.properties.markDefs]);
4432
- debug$b("Copying markDefs over to merged block", op), Transforms.setNodes(editor, {
4392
+ debug$a("Copying markDefs over to merged block", op), Transforms.setNodes(editor, {
4433
4393
  markDefs: newMarkDefs
4434
4394
  }, {
4435
4395
  at: targetPath,
@@ -4581,7 +4541,7 @@ const toggleDecoratorActionImplementation = ({
4581
4541
  decorator: action.decorator
4582
4542
  }
4583
4543
  });
4584
- }, debug$a = debugWithName("plugin:withPortableTextSelections"), debugVerbose$1 = debug$a.enabled && !1;
4544
+ }, debug$9 = debugWithName("plugin:withPortableTextSelections"), debugVerbose = debug$9.enabled && !1;
4585
4545
  function createWithPortableTextSelections(editorActor, types) {
4586
4546
  let prevSelection = null;
4587
4547
  return function(editor) {
@@ -4597,7 +4557,7 @@ function createWithPortableTextSelections(editorActor, types) {
4597
4557
  ptRange = toPortableTextRange(value, editor.selection, types), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
4598
4558
  }
4599
4559
  }
4600
- debugVerbose$1 && debug$a(`Emitting selection ${JSON.stringify(ptRange || null)} (${JSON.stringify(editor.selection)})`), ptRange ? editorActor.send({
4560
+ debugVerbose && debug$9(`Emitting selection ${JSON.stringify(ptRange || null)} (${JSON.stringify(editor.selection)})`), ptRange ? editorActor.send({
4601
4561
  type: "selection",
4602
4562
  selection: ptRange
4603
4563
  }) : editorActor.send({
@@ -4615,7 +4575,7 @@ function createWithPortableTextSelections(editorActor, types) {
4615
4575
  }, editor;
4616
4576
  };
4617
4577
  }
4618
- const debug$9 = debugWithName("plugin:withSchemaTypes");
4578
+ const debug$8 = debugWithName("plugin:withSchemaTypes");
4619
4579
  function createWithSchemaTypes({
4620
4580
  editorActor,
4621
4581
  schemaTypes
@@ -4628,7 +4588,7 @@ function createWithSchemaTypes({
4628
4588
  return editor.normalizeNode = (entry) => {
4629
4589
  const [node, path] = entry;
4630
4590
  if (node._type === void 0 && path.length === 2) {
4631
- debug$9("Setting span type on text node without a type");
4591
+ debug$8("Setting span type on text node without a type");
4632
4592
  const span = node, key = span._key || editorActor.getSnapshot().context.keyGenerator();
4633
4593
  editorActor.send({
4634
4594
  type: "normalizing"
@@ -4644,7 +4604,7 @@ function createWithSchemaTypes({
4644
4604
  return;
4645
4605
  }
4646
4606
  if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
4647
- debug$9("Setting missing key on child node without a key");
4607
+ debug$8("Setting missing key on child node without a key");
4648
4608
  const key = editorActor.getSnapshot().context.keyGenerator();
4649
4609
  editorActor.send({
4650
4610
  type: "normalizing"
@@ -4661,7 +4621,7 @@ function createWithSchemaTypes({
4661
4621
  }, editor;
4662
4622
  };
4663
4623
  }
4664
- const debug$8 = debugWithName("plugin:withUtils");
4624
+ const debug$7 = debugWithName("plugin:withUtils");
4665
4625
  function createWithUtils({
4666
4626
  editorActor,
4667
4627
  schemaTypes
@@ -4676,14 +4636,14 @@ function createWithUtils({
4676
4636
  depth: 2
4677
4637
  });
4678
4638
  if (!textNode || !Text.isText(textNode) || textNode.text.length === 0) {
4679
- debug$8("pteExpandToWord: Can't expand to word here");
4639
+ debug$7("pteExpandToWord: Can't expand to word here");
4680
4640
  return;
4681
4641
  }
4682
4642
  const {
4683
4643
  focus
4684
4644
  } = selection, focusOffset = focus.offset, charsBefore = textNode.text.slice(0, focusOffset), charsAfter = textNode.text.slice(focusOffset, -1), isEmpty = (str) => str.match(/\s/g), whiteSpaceBeforeIndex = charsBefore.split("").reverse().findIndex((str) => isEmpty(str)), newStartOffset = whiteSpaceBeforeIndex > -1 ? charsBefore.length - whiteSpaceBeforeIndex : 0, whiteSpaceAfterIndex = charsAfter.split("").findIndex((obj) => isEmpty(obj)), newEndOffset = charsBefore.length + (whiteSpaceAfterIndex > -1 ? whiteSpaceAfterIndex : charsAfter.length + 1);
4685
4645
  if (!(newStartOffset === newEndOffset || Number.isNaN(newStartOffset) || Number.isNaN(newEndOffset))) {
4686
- debug$8("pteExpandToWord: Expanding to focused word"), Transforms.setSelection(editor, {
4646
+ debug$7("pteExpandToWord: Expanding to focused word"), Transforms.setSelection(editor, {
4687
4647
  anchor: {
4688
4648
  ...selection.anchor,
4689
4649
  offset: newStartOffset
@@ -4695,7 +4655,7 @@ function createWithUtils({
4695
4655
  });
4696
4656
  return;
4697
4657
  }
4698
- debug$8("pteExpandToWord: Can't expand to word here");
4658
+ debug$7("pteExpandToWord: Can't expand to word here");
4699
4659
  }
4700
4660
  }, editor.pteCreateTextBlock = (options) => toSlateValue([{
4701
4661
  _type: schemaTypes.block.name,
@@ -4721,16 +4681,10 @@ function createWithUtils({
4721
4681
  })[0], editor;
4722
4682
  };
4723
4683
  }
4724
- const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, options) => {
4684
+ const withPlugins = (editor, options) => {
4725
4685
  const e = editor, {
4726
4686
  editorActor
4727
- } = options, schemaTypes = editorActor.getSnapshot().context.schema;
4728
- e.destroy ? e.destroy() : originalFnMap.set(e, {
4729
- apply: e.apply,
4730
- onChange: e.onChange,
4731
- normalizeNode: e.normalizeNode
4732
- });
4733
- const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(editorActor, schemaTypes), withSchemaTypes = createWithSchemaTypes({
4687
+ } = options, schemaTypes = editorActor.getSnapshot().context.schema, operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(editorActor, schemaTypes), withSchemaTypes = createWithSchemaTypes({
4734
4688
  editorActor,
4735
4689
  schemaTypes
4736
4690
  }), withPatches = createWithPatches({
@@ -4745,38 +4699,21 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4745
4699
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor, schemaTypes), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor, schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
4746
4700
  editorActor,
4747
4701
  schemaTypes
4748
- }), withPortableTextSelections = createWithPortableTextSelections(editorActor, schemaTypes), withEventListeners = createWithEventListeners(editorActor, options.subscriptions);
4749
- return e.destroy = () => {
4750
- const originalFunctions = originalFnMap.get(e);
4751
- if (!originalFunctions)
4752
- throw new Error("Could not find pristine versions of editor functions");
4753
- e.apply = originalFunctions.apply, e.history = {
4754
- undos: [],
4755
- redos: []
4756
- }, e.normalizeNode = originalFunctions.normalizeNode, e.onChange = originalFunctions.onChange;
4757
- }, withEventListeners(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPortableTextLists(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e))))))))))));
4758
- }, debug$7 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
4702
+ }), withPortableTextSelections = createWithPortableTextSelections(editorActor, schemaTypes);
4703
+ return createWithEventListeners(editorActor, options.subscriptions)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPortableTextLists(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e))))))))))));
4704
+ }, debug$6 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
4759
4705
  function createSlateEditor(config) {
4760
4706
  const existingSlateEditor = slateEditors.get(config.editorActor);
4761
4707
  if (existingSlateEditor)
4762
- return debug$7("Reusing existing Slate editor instance", config.editorActor.id), existingSlateEditor;
4763
- debug$7("Creating new Slate editor instance", config.editorActor.id);
4764
- let unsubscriptions = [], subscriptions = [];
4765
- const instance = withPlugins(withReact(createEditor()), {
4708
+ return debug$6("Reusing existing Slate editor instance", config.editorActor.id), existingSlateEditor;
4709
+ debug$6("Creating new Slate editor instance", config.editorActor.id);
4710
+ const unsubscriptions = [], subscriptions = [], instance = withPlugins(withReact(createEditor$1()), {
4766
4711
  editorActor: config.editorActor,
4767
4712
  subscriptions
4768
4713
  });
4769
4714
  KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {});
4770
4715
  for (const subscription of subscriptions)
4771
4716
  unsubscriptions.push(subscription());
4772
- config.editorActor.subscribe((snapshot) => {
4773
- if (snapshot.status !== "active") {
4774
- debug$7("Destroying Slate editor"), instance.destroy();
4775
- for (const unsubscribe of unsubscriptions)
4776
- unsubscribe();
4777
- subscriptions = [], unsubscriptions = [];
4778
- }
4779
- });
4780
4717
  const initialValue = [instance.pteCreateTextBlock({
4781
4718
  decorators: []
4782
4719
  })], slateEditor = {
@@ -4785,7 +4722,7 @@ function createSlateEditor(config) {
4785
4722
  };
4786
4723
  return slateEditors.set(config.editorActor, slateEditor), slateEditor;
4787
4724
  }
4788
- const EditorActorContext = createContext({}), debug$6 = debugWithName("API:editable");
4725
+ const debug$5 = debugWithName("API:editable");
4789
4726
  function createEditableAPI(editor, editorActor) {
4790
4727
  const types = editorActor.getSnapshot().context.schema;
4791
4728
  return {
@@ -4866,7 +4803,7 @@ function createEditableAPI(editor, editorActor) {
4866
4803
  }], {
4867
4804
  schemaTypes: editorActor.getSnapshot().context.schema
4868
4805
  })[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode = child._type === types.span.name, focusNode = Node.get(editor, focusChildPath);
4869
- return isSpanNode && focusNode._type !== types.span.name && (debug$6("Inserting span child next to inline object child, moving selection + 1"), editor.move({
4806
+ return isSpanNode && focusNode._type !== types.span.name && (debug$5("Inserting span child next to inline object child, moving selection + 1"), editor.move({
4870
4807
  distance: 1,
4871
4808
  unit: "character"
4872
4809
  })), Transforms.insertNodes(editor, child, {
@@ -4998,18 +4935,18 @@ function createEditableAPI(editor, editorActor) {
4998
4935
  throw new Error("Invalid range");
4999
4936
  if (range) {
5000
4937
  if (!options?.mode || options?.mode === "selected") {
5001
- debug$6("Deleting content in selection"), Transforms.delete(editor, {
4938
+ debug$5("Deleting content in selection"), Transforms.delete(editor, {
5002
4939
  at: range,
5003
4940
  hanging: !0,
5004
4941
  voids: !0
5005
4942
  }), editor.onChange();
5006
4943
  return;
5007
4944
  }
5008
- options?.mode === "blocks" && (debug$6("Deleting blocks touched by selection"), Transforms.removeNodes(editor, {
4945
+ options?.mode === "blocks" && (debug$5("Deleting blocks touched by selection"), Transforms.removeNodes(editor, {
5009
4946
  at: range,
5010
4947
  voids: !0,
5011
4948
  match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && Element$1.isElement(node)
5012
- })), options?.mode === "children" && (debug$6("Deleting children touched by selection"), Transforms.removeNodes(editor, {
4949
+ })), options?.mode === "children" && (debug$5("Deleting children touched by selection"), Transforms.removeNodes(editor, {
5013
4950
  at: range,
5014
4951
  voids: !0,
5015
4952
  match: (node) => node._type === types.span.name || // Text children
@@ -5172,7 +5109,7 @@ const addAnnotationActionImplementation = ({
5172
5109
  action
5173
5110
  }) => {
5174
5111
  const editor = action.editor;
5175
- if (debug$6("Removing annotation", action.annotation.name), !!editor.selection)
5112
+ if (debug$5("Removing annotation", action.annotation.name), !!editor.selection)
5176
5113
  if (Range.isCollapsed(editor.selection)) {
5177
5114
  const [block, blockPath] = Editor.node(editor, editor.selection, {
5178
5115
  depth: 1
@@ -5716,22 +5653,7 @@ function performDefaultAction({
5716
5653
  });
5717
5654
  }
5718
5655
  }
5719
- const networkLogic = fromCallback(({
5720
- sendBack
5721
- }) => {
5722
- const onlineHandler = () => {
5723
- sendBack({
5724
- type: "online"
5725
- });
5726
- }, offlineHandler = () => {
5727
- sendBack({
5728
- type: "offline"
5729
- });
5730
- };
5731
- return window && (window.addEventListener("online", onlineHandler), window.addEventListener("offline", offlineHandler)), () => {
5732
- window && (window.removeEventListener("online", onlineHandler), window.removeEventListener("offline", offlineHandler));
5733
- };
5734
- }), editorMachine = setup({
5656
+ const editorMachine = setup({
5735
5657
  types: {
5736
5658
  context: {},
5737
5659
  events: {},
@@ -5830,9 +5752,6 @@ const networkLogic = fromCallback(({
5830
5752
  actionIntends: [defaultAction]
5831
5753
  });
5832
5754
  })
5833
- },
5834
- actors: {
5835
- networkLogic
5836
5755
  }
5837
5756
  }).createMachine({
5838
5757
  id: "editor",
@@ -5843,14 +5762,10 @@ const networkLogic = fromCallback(({
5843
5762
  keyGenerator: input.keyGenerator,
5844
5763
  pendingEvents: [],
5845
5764
  schema: input.schema,
5846
- readOnly: !1,
5847
- maxBlocks: void 0,
5765
+ readOnly: input.readOnly ?? !1,
5766
+ maxBlocks: input.maxBlocks,
5848
5767
  value: input.value
5849
5768
  }),
5850
- invoke: {
5851
- id: "networkLogic",
5852
- src: "networkLogic"
5853
- },
5854
5769
  on: {
5855
5770
  "annotation.add": {
5856
5771
  actions: emit(({
@@ -5924,16 +5839,6 @@ const networkLogic = fromCallback(({
5924
5839
  event
5925
5840
  }) => event)
5926
5841
  },
5927
- online: {
5928
- actions: emit({
5929
- type: "online"
5930
- })
5931
- },
5932
- offline: {
5933
- actions: emit({
5934
- type: "offline"
5935
- })
5936
- },
5937
5842
  loading: {
5938
5843
  actions: emit({
5939
5844
  type: "loading"
@@ -5963,11 +5868,16 @@ const networkLogic = fromCallback(({
5963
5868
  })
5964
5869
  },
5965
5870
  "toggle readOnly": {
5966
- actions: assign({
5871
+ actions: [assign({
5967
5872
  readOnly: ({
5968
5873
  context
5969
5874
  }) => !context.readOnly
5970
- })
5875
+ }), emit(({
5876
+ context
5877
+ }) => ({
5878
+ type: "readOnly toggled",
5879
+ readOnly: context.readOnly
5880
+ }))]
5971
5881
  },
5972
5882
  "update maxBlocks": {
5973
5883
  actions: assign({
@@ -6058,47 +5968,62 @@ const networkLogic = fromCallback(({
6058
5968
  }
6059
5969
  }
6060
5970
  }
6061
- }), PortableTextEditorSelectionContext = createContext(null), usePortableTextEditorSelection = () => {
6062
- const selection = useContext(PortableTextEditorSelectionContext);
6063
- if (selection === void 0)
6064
- throw new Error("The `usePortableTextEditorSelection` hook must be used inside the <PortableTextEditor> component's context.");
6065
- return selection;
6066
- }, debug$5 = debugWithName("component:PortableTextEditor:SelectionProvider"), debugVerbose = debug$5.enabled && !1;
6067
- function PortableTextEditorSelectionProvider(props) {
6068
- const $ = c(6), [selection, setSelection] = useState(null);
6069
- let t0, t1;
6070
- $[0] !== props.editorActor ? (t0 = () => {
6071
- debug$5("Subscribing to selection changes");
6072
- const subscription = props.editorActor.on("selection", (event) => {
6073
- startTransition(() => {
6074
- debugVerbose && debug$5("Setting selection"), setSelection(event.selection);
6075
- });
6076
- });
6077
- return () => {
6078
- debug$5("Unsubscribing to selection changes"), subscription.unsubscribe();
6079
- };
6080
- }, t1 = [props.editorActor], $[0] = props.editorActor, $[1] = t0, $[2] = t1) : (t0 = $[1], t1 = $[2]), useEffect(t0, t1);
6081
- let t2;
6082
- return $[3] !== props.children || $[4] !== selection ? (t2 = /* @__PURE__ */ jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children }), $[3] = props.children, $[4] = selection, $[5] = t2) : t2 = $[5], t2;
6083
- }
6084
- const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
6085
- let table;
6086
- return () => {
6087
- if (table)
6088
- return table;
6089
- table = [];
6090
- for (let i = 0; i < 256; ++i)
6091
- table[i] = (i + 256).toString(16).slice(1);
6092
- return table;
5971
+ });
5972
+ function createEditor(config) {
5973
+ const editorActor = createActor(editorMachine, {
5974
+ input: editorConfigToMachineInput(config)
5975
+ });
5976
+ editorActor.start();
5977
+ const slateEditor = createSlateEditor({
5978
+ editorActor
5979
+ }), editable = createEditableAPI(slateEditor.instance, editorActor);
5980
+ return {
5981
+ send: (event) => {
5982
+ editorActor.send(event);
5983
+ },
5984
+ on: (event, listener) => editorActor.on(
5985
+ event,
5986
+ // @ts-ignore
5987
+ listener
5988
+ ),
5989
+ editable,
5990
+ _internal: {
5991
+ editorActor,
5992
+ slateEditor
5993
+ }
6093
5994
  };
6094
- })();
6095
- function whatwgRNG(length = 16) {
6096
- const rnds8 = new Uint8Array(length);
6097
- return getRandomValues(rnds8), rnds8;
6098
5995
  }
6099
- function randomKey(length) {
6100
- const table = getByteHexTable();
6101
- return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
5996
+ function useEditor(config) {
5997
+ const editorActor = useActorRef(editorMachine, {
5998
+ input: editorConfigToMachineInput(config)
5999
+ }), slateEditor = createSlateEditor({
6000
+ editorActor
6001
+ }), editable = useMemo(() => createEditableAPI(slateEditor.instance, editorActor), [slateEditor.instance, editorActor]), send = useCallback((event) => {
6002
+ editorActor.send(event);
6003
+ }, [editorActor]), on = useCallback((event_0, listener) => editorActor.on(
6004
+ event_0,
6005
+ // @ts-ignore
6006
+ listener
6007
+ ), [editorActor]);
6008
+ return useMemo(() => ({
6009
+ send,
6010
+ on,
6011
+ editable,
6012
+ _internal: {
6013
+ editorActor,
6014
+ slateEditor
6015
+ }
6016
+ }), [send, on, editable, editorActor, slateEditor]);
6017
+ }
6018
+ function editorConfigToMachineInput(config) {
6019
+ return {
6020
+ behaviors: config.behaviors,
6021
+ keyGenerator: config.keyGenerator ?? defaultKeyGenerator,
6022
+ maxBlocks: config.maxBlocks,
6023
+ readOnly: config.readOnly,
6024
+ schema: config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : getPortableTextMemberSchemaTypes(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)),
6025
+ value: config.initialValue
6026
+ };
6102
6027
  }
6103
6028
  const debug$4 = debugWithName("component:PortableTextEditor");
6104
6029
  class PortableTextEditor extends Component {
@@ -6111,66 +6036,49 @@ class PortableTextEditor extends Component {
6111
6036
  * A lookup table for all the relevant schema types for this portable text type.
6112
6037
  */
6113
6038
  /**
6039
+ * The editor instance
6040
+ */
6041
+ /*
6114
6042
  * The editor API (currently implemented with Slate).
6115
6043
  */
6116
6044
  constructor(props) {
6117
- if (super(props), props.editor) {
6118
- const editor = props.editor;
6119
- this.editorActor = editor._internal.editorActor, this.slateEditor = editor._internal.slateEditor, this.editorActor.start(), this.schemaTypes = this.editorActor.getSnapshot().context.schema;
6120
- } else {
6121
- if (!props.schemaType)
6122
- throw new Error('PortableTextEditor: missing "schemaType" property');
6123
- props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.schemaTypes = getPortableTextMemberSchemaTypes(props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)), this.editorActor = createActor(editorMachine, {
6124
- input: {
6125
- keyGenerator: props.keyGenerator || defaultKeyGenerator,
6126
- schema: this.schemaTypes,
6127
- value: props.value
6128
- }
6129
- }), this.editorActor.start(), this.slateEditor = createSlateEditor({
6130
- editorActor: this.editorActor
6131
- }), props.readOnly && this.editorActor.send({
6132
- type: "toggle readOnly"
6133
- }), props.maxBlocks && this.editorActor.send({
6134
- type: "update maxBlocks",
6135
- maxBlocks: props.maxBlocks === void 0 ? void 0 : Number.parseInt(props.maxBlocks.toString(), 10)
6136
- });
6137
- }
6138
- this.editable = createEditableAPI(this.slateEditor.instance, this.editorActor);
6045
+ super(props), props.editor ? this.editor = props.editor : this.editor = createEditor({
6046
+ keyGenerator: props.keyGenerator ?? defaultKeyGenerator,
6047
+ schema: props.schemaType,
6048
+ initialValue: props.value,
6049
+ maxBlocks: props.maxBlocks === void 0 ? void 0 : Number.parseInt(props.maxBlocks.toString(), 10),
6050
+ readOnly: props.readOnly
6051
+ }), this.schemaTypes = this.editor._internal.editorActor.getSnapshot().context.schema, this.editable = this.editor.editable;
6139
6052
  }
6140
6053
  componentDidUpdate(prevProps) {
6141
- !this.props.editor && !prevProps.editor && this.props.schemaType !== prevProps.schemaType && (this.schemaTypes = getPortableTextMemberSchemaTypes(this.props.schemaType.hasOwnProperty("jsonType") ? this.props.schemaType : compileType(this.props.schemaType)), this.editorActor.send({
6054
+ !this.props.editor && !prevProps.editor && this.props.schemaType !== prevProps.schemaType && (this.schemaTypes = getPortableTextMemberSchemaTypes(this.props.schemaType.hasOwnProperty("jsonType") ? this.props.schemaType : compileType(this.props.schemaType)), this.editor._internal.editorActor.send({
6142
6055
  type: "update schema",
6143
6056
  schema: this.schemaTypes
6144
- })), !this.props.editor && !prevProps.editor && (this.props.readOnly !== prevProps.readOnly && this.editorActor.send({
6057
+ })), !this.props.editor && !prevProps.editor && (this.props.readOnly !== prevProps.readOnly && this.editor._internal.editorActor.send({
6145
6058
  type: "toggle readOnly"
6146
- }), this.props.maxBlocks !== prevProps.maxBlocks && this.editorActor.send({
6059
+ }), this.props.maxBlocks !== prevProps.maxBlocks && this.editor._internal.editorActor.send({
6147
6060
  type: "update maxBlocks",
6148
6061
  maxBlocks: this.props.maxBlocks === void 0 ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10)
6149
- }), this.props.value !== prevProps.value && this.editorActor.send({
6062
+ }), this.props.value !== prevProps.value && this.editor._internal.editorActor.send({
6150
6063
  type: "update value",
6151
6064
  value: this.props.value
6152
6065
  }), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this));
6153
6066
  }
6154
6067
  setEditable = (editable) => {
6155
- this.editable = {
6156
- ...this.editable,
6068
+ this.editor.editable = {
6069
+ ...this.editor.editable,
6157
6070
  ...editable
6158
6071
  };
6159
6072
  };
6160
- getValue = () => {
6161
- if (this.editable)
6162
- return this.editable.getValue();
6163
- };
6164
6073
  render() {
6165
6074
  const legacyPatches = this.props.editor ? void 0 : this.props.incomingPatches$ ?? this.props.patches$;
6166
6075
  return /* @__PURE__ */ jsxs(Fragment, { children: [
6167
- legacyPatches ? /* @__PURE__ */ jsx(RoutePatchesObservableToEditorActor, { editorActor: this.editorActor, patches$: legacyPatches }) : null,
6168
- /* @__PURE__ */ jsx(EditorActorContext.Provider, { value: this.editorActor, children: /* @__PURE__ */ jsx(Slate, { editor: this.slateEditor.instance, initialValue: this.slateEditor.initialValue, children: /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsxs(PortableTextEditorSelectionProvider, { editorActor: this.editorActor, children: [
6169
- /* @__PURE__ */ jsx(Synchronizer, { editorActor: this.editorActor, getValue: this.getValue, onChange: (change) => {
6170
- this.props.editor || this.props.onChange(change), this.change$.next(change);
6171
- } }),
6172
- this.props.children
6173
- ] }) }) }) })
6076
+ legacyPatches ? /* @__PURE__ */ jsx(RoutePatchesObservableToEditorActor, { editorActor: this.editor._internal.editorActor, patches$: legacyPatches }) : null,
6077
+ /* @__PURE__ */ jsx(RouteEventsToChanges, { editorActor: this.editor._internal.editorActor, onChange: (change) => {
6078
+ this.props.editor || this.props.onChange(change), this.change$.next(change);
6079
+ } }),
6080
+ /* @__PURE__ */ jsx(Synchronizer, { editorActor: this.editor._internal.editorActor, getValue: this.editor.editable.getValue, portableTextEditor: this, slateEditor: this.editor._internal.slateEditor.instance }),
6081
+ /* @__PURE__ */ jsx(EditorActorContext.Provider, { value: this.editor._internal.editorActor, children: /* @__PURE__ */ jsx(Slate, { editor: this.editor._internal.slateEditor.instance, initialValue: this.editor._internal.slateEditor.initialValue, children: /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsx(PortableTextEditorSelectionProvider, { editorActor: this.editor._internal.editorActor, children: this.props.children }) }) }) })
6174
6082
  ] });
6175
6083
  }
6176
6084
  // Static API methods
@@ -6238,6 +6146,79 @@ function RoutePatchesObservableToEditorActor(props) {
6238
6146
  };
6239
6147
  }, t1 = [props.editorActor, props.patches$], $[0] = props.editorActor, $[1] = props.patches$, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), useEffect(t0, t1), null;
6240
6148
  }
6149
+ function RouteEventsToChanges(props) {
6150
+ const $ = c(6);
6151
+ let t0;
6152
+ $[0] !== props ? (t0 = (change) => props.onChange(change), $[0] = props, $[1] = t0) : t0 = $[1];
6153
+ const handleChange = useEffectEvent(t0);
6154
+ let t1, t2;
6155
+ return $[2] !== handleChange || $[3] !== props.editorActor ? (t1 = () => {
6156
+ debug$4("Subscribing to editor changes");
6157
+ const sub = props.editorActor.on("*", (event) => {
6158
+ bb5: switch (event.type) {
6159
+ case "patch": {
6160
+ handleChange(event);
6161
+ break bb5;
6162
+ }
6163
+ case "loading": {
6164
+ handleChange({
6165
+ type: "loading",
6166
+ isLoading: !0
6167
+ });
6168
+ break bb5;
6169
+ }
6170
+ case "done loading": {
6171
+ handleChange({
6172
+ type: "loading",
6173
+ isLoading: !1
6174
+ });
6175
+ break bb5;
6176
+ }
6177
+ case "focused": {
6178
+ handleChange({
6179
+ type: "focus",
6180
+ event: event.event
6181
+ });
6182
+ break bb5;
6183
+ }
6184
+ case "value changed": {
6185
+ handleChange({
6186
+ type: "value",
6187
+ value: event.value
6188
+ });
6189
+ break bb5;
6190
+ }
6191
+ case "invalid value": {
6192
+ handleChange({
6193
+ type: "invalidValue",
6194
+ resolution: event.resolution,
6195
+ value: event.value
6196
+ });
6197
+ break bb5;
6198
+ }
6199
+ case "error": {
6200
+ handleChange({
6201
+ ...event,
6202
+ level: "warning"
6203
+ });
6204
+ break bb5;
6205
+ }
6206
+ case "annotation.add":
6207
+ case "annotation.remove":
6208
+ case "annotation.toggle":
6209
+ case "focus":
6210
+ case "patches":
6211
+ case "readOnly toggled":
6212
+ break bb5;
6213
+ default:
6214
+ handleChange(event);
6215
+ }
6216
+ });
6217
+ return () => {
6218
+ debug$4("Unsubscribing to changes"), sub.unsubscribe();
6219
+ };
6220
+ }, t2 = [props.editorActor, handleChange], $[2] = handleChange, $[3] = props.editorActor, $[4] = t1, $[5] = t2) : (t1 = $[4], t2 = $[5]), useEffect(t1, t2), null;
6221
+ }
6241
6222
  const debug$3 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (props) => {
6242
6223
  const {
6243
6224
  editorActor,
@@ -6695,9 +6676,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6695
6676
  ...restProps
6696
6677
  } = props, portableTextEditor = usePortableTextEditor(), ref = useRef(null), [editableElement, setEditableElement] = useState(null), [hasInvalidValue, setHasInvalidValue] = useState(!1), [rangeDecorationState, setRangeDecorationsState] = useState([]);
6697
6678
  useImperativeHandle(forwardedRef, () => ref.current);
6698
- const rangeDecorationsRef = useRef(rangeDecorations), editorActor = useContext(EditorActorContext), readOnly = useSelector(editorActor, (s) => s.context.readOnly), {
6699
- schemaTypes
6700
- } = portableTextEditor, slateEditor = useSlate(), blockTypeName = schemaTypes.block.name;
6679
+ const rangeDecorationsRef = useRef(rangeDecorations), editorActor = useContext(EditorActorContext), readOnly = useSelector(editorActor, (s) => s.context.readOnly), schemaTypes = useSelector(editorActor, (s_0) => s_0.context.schema), slateEditor = useSlate(), blockTypeName = schemaTypes.block.name;
6701
6680
  useMemo(() => {
6702
6681
  const withInsertData = createWithInsertData(editorActor, schemaTypes);
6703
6682
  if (readOnly)
@@ -6933,46 +6912,56 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6933
6912
  ) : null;
6934
6913
  });
6935
6914
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
6936
- function useEditor(config) {
6937
- const $ = c(21), t0 = config.keyGenerator ?? defaultKeyGenerator;
6938
- let t1;
6939
- $[0] !== config.schema || $[1] !== config.schemaDefinition ? (t1 = config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : getPortableTextMemberSchemaTypes(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)), $[0] = config.schema, $[1] = config.schemaDefinition, $[2] = t1) : t1 = $[2];
6915
+ const EditorContext = React.createContext(void 0);
6916
+ function EditorProvider(props) {
6917
+ const $ = c(30), editor = useEditor(props.config), editorActor = editor._internal.editorActor, slateEditor = editor._internal.slateEditor, editable = editor.editable;
6918
+ let t0, t1;
6919
+ $[0] !== editor ? (t1 = new PortableTextEditor({
6920
+ editor
6921
+ }), $[0] = editor, $[1] = t1) : t1 = $[1], t0 = t1;
6922
+ const portableTextEditor = t0;
6940
6923
  let t2;
6941
- $[3] !== config.behaviors || $[4] !== config.initialValue || $[5] !== t0 || $[6] !== t1 ? (t2 = {
6942
- input: {
6943
- behaviors: config.behaviors,
6944
- keyGenerator: t0,
6945
- schema: t1,
6946
- value: config.initialValue
6947
- }
6948
- }, $[3] = config.behaviors, $[4] = config.initialValue, $[5] = t0, $[6] = t1, $[7] = t2) : t2 = $[7];
6949
- const editorActor = useActorRef(editorMachine, t2);
6924
+ $[2] !== portableTextEditor.change$ ? (t2 = (change) => {
6925
+ portableTextEditor.change$.next(change);
6926
+ }, $[2] = portableTextEditor.change$, $[3] = t2) : t2 = $[3];
6950
6927
  let t3;
6951
- $[8] !== editorActor ? (t3 = createSlateEditor({
6952
- editorActor
6953
- }), $[8] = editorActor, $[9] = t3) : t3 = $[9];
6954
- const slateEditor = t3, readOnly = useSelector(editorActor, _temp);
6955
- let t4, t5;
6956
- $[10] !== editorActor ? (t4 = (event) => {
6957
- editorActor.send(event);
6958
- }, t5 = (event_0, listener) => editorActor.on(event_0, listener), $[10] = editorActor, $[11] = t4, $[12] = t5) : (t4 = $[11], t5 = $[12]);
6928
+ $[4] !== editorActor || $[5] !== t2 ? (t3 = /* @__PURE__ */ jsx(RouteEventsToChanges, { editorActor, onChange: t2 }), $[4] = editorActor, $[5] = t2, $[6] = t3) : t3 = $[6];
6929
+ let t4;
6930
+ $[7] !== editable.getValue || $[8] !== editorActor || $[9] !== portableTextEditor || $[10] !== slateEditor.instance ? (t4 = /* @__PURE__ */ jsx(Synchronizer, { editorActor, getValue: editable.getValue, portableTextEditor, slateEditor: slateEditor.instance }), $[7] = editable.getValue, $[8] = editorActor, $[9] = portableTextEditor, $[10] = slateEditor.instance, $[11] = t4) : t4 = $[11];
6931
+ let t5;
6932
+ $[12] !== editorActor || $[13] !== props.children ? (t5 = /* @__PURE__ */ jsx(PortableTextEditorSelectionProvider, { editorActor, children: props.children }), $[12] = editorActor, $[13] = props.children, $[14] = t5) : t5 = $[14];
6959
6933
  let t6;
6960
- $[13] !== editorActor || $[14] !== slateEditor ? (t6 = {
6961
- editorActor,
6962
- slateEditor
6963
- }, $[13] = editorActor, $[14] = slateEditor, $[15] = t6) : t6 = $[15];
6934
+ $[15] !== portableTextEditor || $[16] !== t5 ? (t6 = /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: portableTextEditor, children: t5 }), $[15] = portableTextEditor, $[16] = t5, $[17] = t6) : t6 = $[17];
6964
6935
  let t7;
6965
- return $[16] !== readOnly || $[17] !== t4 || $[18] !== t5 || $[19] !== t6 ? (t7 = {
6966
- send: t4,
6967
- on: t5,
6968
- readOnly,
6969
- _internal: t6
6970
- }, $[16] = readOnly, $[17] = t4, $[18] = t5, $[19] = t6, $[20] = t7) : t7 = $[20], t7;
6936
+ $[18] !== slateEditor.initialValue || $[19] !== slateEditor.instance || $[20] !== t6 ? (t7 = /* @__PURE__ */ jsx(Slate, { editor: slateEditor.instance, initialValue: slateEditor.initialValue, children: t6 }), $[18] = slateEditor.initialValue, $[19] = slateEditor.instance, $[20] = t6, $[21] = t7) : t7 = $[21];
6937
+ let t8;
6938
+ $[22] !== editorActor || $[23] !== t7 ? (t8 = /* @__PURE__ */ jsx(EditorActorContext.Provider, { value: editorActor, children: t7 }), $[22] = editorActor, $[23] = t7, $[24] = t8) : t8 = $[24];
6939
+ let t9;
6940
+ return $[25] !== editor || $[26] !== t3 || $[27] !== t4 || $[28] !== t8 ? (t9 = /* @__PURE__ */ jsxs(EditorContext.Provider, { value: editor, children: [
6941
+ t3,
6942
+ t4,
6943
+ t8
6944
+ ] }), $[25] = editor, $[26] = t3, $[27] = t4, $[28] = t8, $[29] = t9) : t9 = $[29], t9;
6945
+ }
6946
+ function useEditorContext() {
6947
+ const editor = React.useContext(EditorContext);
6948
+ if (!editor)
6949
+ throw new Error("No Editor set. Use EditorProvider to set one.");
6950
+ return editor;
6971
6951
  }
6972
- function _temp(s) {
6973
- return s.context.readOnly;
6952
+ function EditorEventListener(props) {
6953
+ const $ = c(4), editor = useEditorContext(), on = useEffectEvent(props.on);
6954
+ let t0, t1;
6955
+ return $[0] !== editor || $[1] !== on ? (t0 = () => {
6956
+ const subscription = editor.on("*", on);
6957
+ return () => {
6958
+ subscription.unsubscribe();
6959
+ };
6960
+ }, t1 = [editor, on], $[0] = editor, $[1] = on, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), useEffect(t0, t1), null;
6974
6961
  }
6975
6962
  export {
6963
+ EditorEventListener,
6964
+ EditorProvider,
6976
6965
  PortableTextEditable,
6977
6966
  PortableTextEditor,
6978
6967
  coreBehavior,
@@ -6984,6 +6973,7 @@ export {
6984
6973
  editorMachine,
6985
6974
  defaultKeyGenerator as keyGenerator,
6986
6975
  useEditor,
6976
+ useEditorContext,
6987
6977
  usePortableTextEditor,
6988
6978
  usePortableTextEditorSelection
6989
6979
  };