@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.js CHANGED
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var types = require("@sanity/types"), schema = require("@sanity/schema"), startCase = require("lodash.startcase"), jsxRuntime = require("react/jsx-runtime"), react$1 = require("@xstate/react"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), reactCompilerRuntime = require("react-compiler-runtime"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), throttle = require("lodash/throttle.js"), useEffectEvent = require("use-effect-event"), debounce = require("lodash/debounce.js"), patches = require("@portabletext/patches"), flatten = require("lodash/flatten.js"), isPlainObject = require("lodash/isPlainObject.js"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), getRandomValues = require("get-random-values-esm"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools");
3
+ var types = require("@sanity/types"), schema = require("@sanity/schema"), startCase = require("lodash.startcase"), jsxRuntime = require("react/jsx-runtime"), react = require("@xstate/react"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), React = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), reactCompilerRuntime = require("react-compiler-runtime"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), useEffectEvent = require("use-effect-event"), throttle = require("lodash/throttle.js"), debounce = require("lodash/debounce.js"), patches = require("@portabletext/patches"), flatten = require("lodash/flatten.js"), isPlainObject = require("lodash/isPlainObject.js"), getRandomValues = require("get-random-values-esm"), xstate = require("xstate"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
7
- var startCase__default = /* @__PURE__ */ _interopDefaultCompat(startCase), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$m), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), throttle__default = /* @__PURE__ */ _interopDefaultCompat(throttle), debounce__default = /* @__PURE__ */ _interopDefaultCompat(debounce), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues);
7
+ var startCase__default = /* @__PURE__ */ _interopDefaultCompat(startCase), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), noop__default = /* @__PURE__ */ _interopDefaultCompat(noop), React__default = /* @__PURE__ */ _interopDefaultCompat(React), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$m), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), throttle__default = /* @__PURE__ */ _interopDefaultCompat(throttle), debounce__default = /* @__PURE__ */ _interopDefaultCompat(debounce), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy);
8
8
  function defineBehavior(behavior) {
9
9
  return behavior;
10
10
  }
@@ -296,6 +296,65 @@ const breakingBlockObject = {
296
296
  blockObjects: coreBlockObjectBehaviors,
297
297
  lists: coreListBehaviors
298
298
  };
299
+ function createLinkBehaviors(config) {
300
+ const pasteLinkOnSelection = {
301
+ on: "paste",
302
+ guard: ({
303
+ context,
304
+ event
305
+ }) => {
306
+ const selectionCollapsed = selectionIsCollapsed(context), text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
307
+ url,
308
+ schema: context.schema
309
+ }) : void 0;
310
+ return annotation && !selectionCollapsed ? {
311
+ annotation
312
+ } : !1;
313
+ },
314
+ actions: [(_, {
315
+ annotation
316
+ }) => [{
317
+ type: "annotation.add",
318
+ annotation
319
+ }]]
320
+ }, pasteLinkAtCaret = {
321
+ on: "paste",
322
+ guard: ({
323
+ context,
324
+ event
325
+ }) => {
326
+ const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
327
+ if (!focusSpan || !selectionCollapsed)
328
+ return !1;
329
+ const text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
330
+ url,
331
+ schema: context.schema
332
+ }) : void 0;
333
+ return url && annotation && selectionCollapsed ? {
334
+ focusSpan,
335
+ annotation,
336
+ url
337
+ } : !1;
338
+ },
339
+ actions: [(_, {
340
+ annotation,
341
+ url
342
+ }) => [{
343
+ type: "insert span",
344
+ text: url,
345
+ annotations: [annotation]
346
+ }]]
347
+ };
348
+ return [pasteLinkOnSelection, pasteLinkAtCaret];
349
+ }
350
+ function looksLikeUrl(text) {
351
+ let looksLikeUrl2 = !1;
352
+ try {
353
+ new URL(text), looksLikeUrl2 = !0;
354
+ } catch {
355
+ }
356
+ return looksLikeUrl2;
357
+ }
299
358
  function isPortableTextSpan(node) {
300
359
  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"));
301
360
  }
@@ -552,65 +611,6 @@ function createMarkdownBehaviors(config) {
552
611
  };
553
612
  return [automaticBlockquoteOnSpace, automaticBreak, automaticHeadingOnSpace, clearStyleOnBackspace, automaticListOnSpace];
554
613
  }
555
- function createLinkBehaviors(config) {
556
- const pasteLinkOnSelection = {
557
- on: "paste",
558
- guard: ({
559
- context,
560
- event
561
- }) => {
562
- const selectionCollapsed = selectionIsCollapsed(context), text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
563
- url,
564
- schema: context.schema
565
- }) : void 0;
566
- return annotation && !selectionCollapsed ? {
567
- annotation
568
- } : !1;
569
- },
570
- actions: [(_, {
571
- annotation
572
- }) => [{
573
- type: "annotation.add",
574
- annotation
575
- }]]
576
- }, pasteLinkAtCaret = {
577
- on: "paste",
578
- guard: ({
579
- context,
580
- event
581
- }) => {
582
- const focusSpan = getFocusSpan(context), selectionCollapsed = selectionIsCollapsed(context);
583
- if (!focusSpan || !selectionCollapsed)
584
- return !1;
585
- const text = event.clipboardData.getData("text/plain"), url = looksLikeUrl(text) ? text : void 0, annotation = url !== void 0 ? config.linkAnnotation?.({
586
- url,
587
- schema: context.schema
588
- }) : void 0;
589
- return url && annotation && selectionCollapsed ? {
590
- focusSpan,
591
- annotation,
592
- url
593
- } : !1;
594
- },
595
- actions: [(_, {
596
- annotation,
597
- url
598
- }) => [{
599
- type: "insert span",
600
- text: url,
601
- annotations: [annotation]
602
- }]]
603
- };
604
- return [pasteLinkOnSelection, pasteLinkAtCaret];
605
- }
606
- function looksLikeUrl(text) {
607
- let looksLikeUrl2 = !1;
608
- try {
609
- new URL(text), looksLikeUrl2 = !0;
610
- } catch {
611
- }
612
- return looksLikeUrl2;
613
- }
614
614
  function getPortableTextMemberSchemaTypes(portableTextType) {
615
615
  if (!portableTextType)
616
616
  throw new Error("Parameter 'portabletextType' missing (required)");
@@ -1140,15 +1140,15 @@ const debug$l = debugWithName("components:DraggableBlock"), DraggableBlock = (t0
1140
1140
  element,
1141
1141
  readOnly,
1142
1142
  blockRef
1143
- } = t0, editor = slateReact.useSlateStatic(), dragGhostRef = react.useRef(), [isDragOver, setIsDragOver] = react.useState(!1);
1143
+ } = t0, editor = slateReact.useSlateStatic(), dragGhostRef = React.useRef(), [isDragOver, setIsDragOver] = React.useState(!1);
1144
1144
  let t1, t2;
1145
1145
  $[0] !== editor || $[1] !== element ? (t2 = slate.Editor.isVoid(editor, element), $[0] = editor, $[1] = element, $[2] = t2) : t2 = $[2], t1 = t2;
1146
1146
  const isVoid = t1;
1147
1147
  let t3, t4;
1148
1148
  $[3] !== editor || $[4] !== element ? (t4 = slate.Editor.isInline(editor, element), $[3] = editor, $[4] = element, $[5] = t4) : t4 = $[5], t3 = t4;
1149
- const isInline = t3, [blockElement, setBlockElement] = react.useState(null);
1149
+ const isInline = t3, [blockElement, setBlockElement] = React.useState(null);
1150
1150
  let t5, t6;
1151
- $[6] !== blockRef || $[7] !== editor || $[8] !== element ? (t5 = () => setBlockElement(blockRef ? blockRef.current : slateReact.ReactEditor.toDOMNode(editor, element)), t6 = [editor, element, blockRef], $[6] = blockRef, $[7] = editor, $[8] = element, $[9] = t5, $[10] = t6) : (t5 = $[9], t6 = $[10]), react.useEffect(t5, t6);
1151
+ $[6] !== blockRef || $[7] !== editor || $[8] !== element ? (t5 = () => setBlockElement(blockRef ? blockRef.current : slateReact.ReactEditor.toDOMNode(editor, element)), t6 = [editor, element, blockRef], $[6] = blockRef, $[7] = editor, $[8] = element, $[9] = t5, $[10] = t6) : (t5 = $[9], t6 = $[10]), React.useEffect(t5, t6);
1152
1152
  let t7;
1153
1153
  $[11] !== blockElement || $[12] !== editor || $[13] !== element ? (t7 = (event) => {
1154
1154
  const isMyDragOver = IS_DRAGGING_BLOCK_ELEMENT.get(editor);
@@ -1279,9 +1279,9 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = {
1279
1279
  renderStyle,
1280
1280
  spellCheck
1281
1281
  }) => {
1282
- const editor = slateReact.useSlateStatic(), selected = slateReact.useSelected(), blockRef = react.useRef(null), inlineBlockObjectRef = react.useRef(null), focused = selected && editor.selection && slate.Range.isCollapsed(editor.selection) || !1, value = react.useMemo(() => fromSlateValue([element], schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0], [editor, element, schemaTypes.block.name]);
1282
+ const editor = slateReact.useSlateStatic(), selected = slateReact.useSelected(), blockRef = React.useRef(null), inlineBlockObjectRef = React.useRef(null), focused = selected && editor.selection && slate.Range.isCollapsed(editor.selection) || !1, value = React.useMemo(() => fromSlateValue([element], schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0], [editor, element, schemaTypes.block.name]);
1283
1283
  let renderedBlock = children, className;
1284
- const blockPath = react.useMemo(() => [{
1284
+ const blockPath = React.useMemo(() => [{
1285
1285
  _key: element._key
1286
1286
  }], [element]);
1287
1287
  if (typeof element._type != "string")
@@ -1402,8 +1402,8 @@ const EMPTY_ANNOTATIONS = [], inlineBlockStyle = {
1402
1402
  ] }, element._key);
1403
1403
  };
1404
1404
  Element.displayName = "Element";
1405
- const PortableTextEditorContext = react.createContext(null), usePortableTextEditor = () => {
1406
- const editor = react.useContext(PortableTextEditorContext);
1405
+ const PortableTextEditorContext = React.createContext(null), usePortableTextEditor = () => {
1406
+ const editor = React.useContext(PortableTextEditorContext);
1407
1407
  if (!editor)
1408
1408
  throw new Error("The `usePortableTextEditor` hook must be used inside the <PortableTextEditor> component's context.");
1409
1409
  return editor;
@@ -2752,19 +2752,20 @@ function useSyncValue(props) {
2752
2752
  const {
2753
2753
  editorActor,
2754
2754
  portableTextEditor,
2755
- readOnly
2756
- } = props, schemaTypes = editorActor.getSnapshot().context.schema, previousValue = react.useRef(), slateEditor = slateReact.useSlate(), updateValueFunctionRef = react.useRef(), updateFromCurrentValue = react.useCallback(() => {
2755
+ readOnly,
2756
+ slateEditor
2757
+ } = props, schemaTypes = editorActor.getSnapshot().context.schema, previousValue = React.useRef(), updateValueFunctionRef = React.useRef(), updateFromCurrentValue = React.useCallback(() => {
2757
2758
  const currentValue = CURRENT_VALUE.get(portableTextEditor);
2758
2759
  if (previousValue.current === currentValue) {
2759
2760
  debug$j("Value is the same object as previous, not need to sync");
2760
2761
  return;
2761
2762
  }
2762
2763
  updateValueFunctionRef.current && currentValue && (debug$j("Updating the value debounced"), updateValueFunctionRef.current(currentValue));
2763
- }, [portableTextEditor]), updateValueDebounced = react.useMemo(() => debounce__default.default(updateFromCurrentValue, 1e3, {
2764
+ }, [portableTextEditor]), updateValueDebounced = React.useMemo(() => debounce__default.default(updateFromCurrentValue, 1e3, {
2764
2765
  trailing: !0,
2765
2766
  leading: !1
2766
2767
  }), [updateFromCurrentValue]);
2767
- return react.useMemo(() => {
2768
+ return React.useMemo(() => {
2768
2769
  const updateFunction = (value) => {
2769
2770
  CURRENT_VALUE.set(portableTextEditor, value);
2770
2771
  const isProcessingLocalChanges = isChangingLocally(slateEditor), isProcessingRemoteChanges = isChangingRemotely(slateEditor);
@@ -2936,27 +2937,29 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
2936
2937
  }
2937
2938
  const debug$i = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$3 = debug$i.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
2938
2939
  function Synchronizer(props) {
2939
- const $ = reactCompilerRuntime.c(35), portableTextEditor = usePortableTextEditor(), readOnly = react$1.useSelector(props.editorActor, _temp$1), value = react$1.useSelector(props.editorActor, _temp2), {
2940
+ const $ = reactCompilerRuntime.c(26), readOnly = react.useSelector(props.editorActor, _temp), value = react.useSelector(props.editorActor, _temp2), {
2940
2941
  editorActor,
2941
2942
  getValue,
2942
- onChange
2943
+ portableTextEditor,
2944
+ slateEditor
2943
2945
  } = props;
2944
2946
  let t0;
2945
2947
  $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = [], $[0] = t0) : t0 = $[0];
2946
- const pendingPatches = react.useRef(t0);
2948
+ const pendingPatches = React.useRef(t0);
2947
2949
  let t1;
2948
- $[1] !== editorActor || $[2] !== portableTextEditor || $[3] !== readOnly ? (t1 = {
2950
+ $[1] !== editorActor || $[2] !== portableTextEditor || $[3] !== readOnly || $[4] !== slateEditor ? (t1 = {
2949
2951
  editorActor,
2950
2952
  portableTextEditor,
2951
- readOnly
2952
- }, $[1] = editorActor, $[2] = portableTextEditor, $[3] = readOnly, $[4] = t1) : t1 = $[4];
2953
- const syncValue = useSyncValue(t1), slateEditor = slateReact.useSlate();
2953
+ readOnly,
2954
+ slateEditor
2955
+ }, $[1] = editorActor, $[2] = portableTextEditor, $[3] = readOnly, $[4] = slateEditor, $[5] = t1) : t1 = $[5];
2956
+ const syncValue = useSyncValue(t1);
2954
2957
  let t2, t3;
2955
- $[5] !== slateEditor ? (t2 = () => {
2958
+ $[6] !== slateEditor ? (t2 = () => {
2956
2959
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !1);
2957
- }, t3 = [slateEditor], $[5] = slateEditor, $[6] = t2, $[7] = t3) : (t2 = $[6], t3 = $[7]), react.useEffect(t2, t3);
2960
+ }, t3 = [slateEditor], $[6] = slateEditor, $[7] = t2, $[8] = t3) : (t2 = $[7], t3 = $[8]), React.useEffect(t2, t3);
2958
2961
  let t4;
2959
- $[8] !== editorActor || $[9] !== getValue || $[10] !== slateEditor ? (t4 = () => {
2962
+ $[9] !== editorActor || $[10] !== getValue || $[11] !== slateEditor ? (t4 = () => {
2960
2963
  if (pendingPatches.current.length > 0) {
2961
2964
  debug$i("Flushing pending patches"), debugVerbose$3 && debug$i(`Patches:
2962
2965
  ${JSON.stringify(pendingPatches.current, null, 2)}`);
@@ -2968,17 +2971,14 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
2968
2971
  }), pendingPatches.current = [];
2969
2972
  }
2970
2973
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !1);
2971
- }, $[8] = editorActor, $[9] = getValue, $[10] = slateEditor, $[11] = t4) : t4 = $[11];
2974
+ }, $[9] = editorActor, $[10] = getValue, $[11] = slateEditor, $[12] = t4) : t4 = $[12];
2972
2975
  const onFlushPendingPatches = t4;
2973
2976
  let t5, t6;
2974
- $[12] !== onFlushPendingPatches ? (t5 = () => () => {
2977
+ $[13] !== onFlushPendingPatches ? (t5 = () => () => {
2975
2978
  onFlushPendingPatches();
2976
- }, t6 = [onFlushPendingPatches], $[12] = onFlushPendingPatches, $[13] = t5, $[14] = t6) : (t5 = $[13], t6 = $[14]), react.useEffect(t5, t6);
2977
- let t7;
2978
- $[15] !== onChange ? (t7 = (change) => onChange(change), $[15] = onChange, $[16] = t7) : t7 = $[16];
2979
- const handleChange = useEffectEvent.useEffectEvent(t7);
2980
- let t8, t9;
2981
- $[17] !== editorActor || $[18] !== handleChange || $[19] !== onFlushPendingPatches || $[20] !== slateEditor ? (t8 = () => {
2979
+ }, t6 = [onFlushPendingPatches], $[13] = onFlushPendingPatches, $[14] = t5, $[15] = t6) : (t5 = $[14], t6 = $[15]), React.useEffect(t5, t6);
2980
+ let t7, t8;
2981
+ $[16] !== editorActor || $[17] !== onFlushPendingPatches || $[18] !== slateEditor ? (t7 = () => {
2982
2982
  const onFlushPendingPatchesThrottled = throttle__default.default(() => {
2983
2983
  if (slate.Editor.isNormalizing(slateEditor)) {
2984
2984
  onFlushPendingPatches();
@@ -2989,116 +2989,76 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
2989
2989
  leading: !1,
2990
2990
  trailing: !0
2991
2991
  });
2992
- debug$i("Subscribing to editor changes");
2993
- const sub = editorActor.on("*", (event) => {
2994
- bb22: switch (event.type) {
2995
- case "patch": {
2996
- IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !0), pendingPatches.current.push(event.patch), onFlushPendingPatchesThrottled(), handleChange(event);
2997
- break bb22;
2998
- }
2999
- case "loading": {
3000
- handleChange({
3001
- type: "loading",
3002
- isLoading: !0
3003
- });
3004
- break bb22;
3005
- }
3006
- case "done loading": {
3007
- handleChange({
3008
- type: "loading",
3009
- isLoading: !1
3010
- });
3011
- break bb22;
3012
- }
3013
- case "focused": {
3014
- handleChange({
3015
- type: "focus",
3016
- event: event.event
3017
- });
3018
- break bb22;
3019
- }
3020
- case "offline": {
3021
- handleChange({
3022
- type: "connection",
3023
- value: "offline"
3024
- });
3025
- break bb22;
3026
- }
3027
- case "online": {
3028
- handleChange({
3029
- type: "connection",
3030
- value: "online"
3031
- });
3032
- break bb22;
3033
- }
3034
- case "value changed": {
3035
- handleChange({
3036
- type: "value",
3037
- value: event.value
3038
- });
3039
- break bb22;
3040
- }
3041
- case "invalid value": {
3042
- handleChange({
3043
- type: "invalidValue",
3044
- resolution: event.resolution,
3045
- value: event.value
3046
- });
3047
- break bb22;
3048
- }
3049
- case "error": {
3050
- handleChange({
3051
- ...event,
3052
- level: "warning"
3053
- });
3054
- break bb22;
3055
- }
3056
- case "annotation.add":
3057
- case "annotation.remove":
3058
- case "annotation.toggle":
3059
- case "focus":
3060
- case "patches":
3061
- break bb22;
3062
- default:
3063
- handleChange(event);
3064
- }
2992
+ debug$i("Subscribing to patch events");
2993
+ const sub = editorActor.on("patch", (event) => {
2994
+ IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !0), pendingPatches.current.push(event.patch), onFlushPendingPatchesThrottled();
3065
2995
  });
3066
2996
  return () => {
3067
- debug$i("Unsubscribing to changes"), sub.unsubscribe();
3068
- };
3069
- }, t9 = [editorActor, handleChange, onFlushPendingPatches, slateEditor], $[17] = editorActor, $[18] = handleChange, $[19] = onFlushPendingPatches, $[20] = slateEditor, $[21] = t8, $[22] = t9) : (t8 = $[21], t9 = $[22]), react.useEffect(t8, t9);
3070
- let t10;
3071
- $[23] !== syncValue || $[24] !== value ? (t10 = () => {
3072
- debug$i("Editor is online, syncing from props.value"), syncValue(value);
3073
- }, $[23] = syncValue, $[24] = value, $[25] = t10) : t10 = $[25];
3074
- const handleOnline = t10;
3075
- let t11, t12;
3076
- $[26] !== editorActor || $[27] !== handleOnline ? (t11 = () => {
3077
- const subscription = editorActor.on("online", handleOnline);
3078
- return () => {
3079
- subscription.unsubscribe();
2997
+ debug$i("Unsubscribing to patch events"), sub.unsubscribe();
3080
2998
  };
3081
- }, t12 = [handleOnline, editorActor], $[26] = editorActor, $[27] = handleOnline, $[28] = t11, $[29] = t12) : (t11 = $[28], t12 = $[29]), react.useEffect(t11, t12);
3082
- const isInitialValueFromProps = react.useRef(!0);
3083
- let t13, t14;
3084
- return $[30] !== editorActor || $[31] !== syncValue || $[32] !== value ? (t13 = () => {
2999
+ }, t8 = [editorActor, onFlushPendingPatches, slateEditor], $[16] = editorActor, $[17] = onFlushPendingPatches, $[18] = slateEditor, $[19] = t7, $[20] = t8) : (t7 = $[19], t8 = $[20]), React.useEffect(t7, t8);
3000
+ const isInitialValueFromProps = React.useRef(!0);
3001
+ let t10, t9;
3002
+ return $[21] !== editorActor || $[22] !== syncValue || $[23] !== value ? (t9 = () => {
3085
3003
  debug$i("Value from props changed, syncing new value"), syncValue(value), isInitialValueFromProps.current && (editorActor.send({
3086
3004
  type: "ready"
3087
3005
  }), isInitialValueFromProps.current = !1);
3088
- }, t14 = [editorActor, syncValue, value], $[30] = editorActor, $[31] = syncValue, $[32] = value, $[33] = t13, $[34] = t14) : (t13 = $[33], t14 = $[34]), react.useEffect(t13, t14), null;
3006
+ }, t10 = [editorActor, syncValue, value], $[21] = editorActor, $[22] = syncValue, $[23] = value, $[24] = t10, $[25] = t9) : (t10 = $[24], t9 = $[25]), React.useEffect(t9, t10), null;
3089
3007
  }
3090
3008
  function _temp2(s_0) {
3091
3009
  return s_0.context.value;
3092
3010
  }
3093
- function _temp$1(s) {
3011
+ function _temp(s) {
3094
3012
  return s.context.readOnly;
3095
3013
  }
3096
3014
  Synchronizer.displayName = "Synchronizer";
3097
- const debug$h = debugWithName("operationToPatches");
3015
+ const EditorActorContext = React.createContext({}), PortableTextEditorSelectionContext = React.createContext(null), usePortableTextEditorSelection = () => {
3016
+ const selection = React.useContext(PortableTextEditorSelectionContext);
3017
+ if (selection === void 0)
3018
+ throw new Error("The `usePortableTextEditorSelection` hook must be used inside the <PortableTextEditor> component's context.");
3019
+ return selection;
3020
+ }, debug$h = debugWithName("component:PortableTextEditor:SelectionProvider"), debugVerbose$2 = debug$h.enabled && !1;
3021
+ function PortableTextEditorSelectionProvider(props) {
3022
+ const $ = reactCompilerRuntime.c(6), [selection, setSelection] = React.useState(null);
3023
+ let t0, t1;
3024
+ $[0] !== props.editorActor ? (t0 = () => {
3025
+ debug$h("Subscribing to selection changes");
3026
+ const subscription = props.editorActor.on("selection", (event) => {
3027
+ React.startTransition(() => {
3028
+ debugVerbose$2 && debug$h("Setting selection"), setSelection(event.selection);
3029
+ });
3030
+ });
3031
+ return () => {
3032
+ debug$h("Unsubscribing to selection changes"), subscription.unsubscribe();
3033
+ };
3034
+ }, t1 = [props.editorActor], $[0] = props.editorActor, $[1] = t0, $[2] = t1) : (t0 = $[1], t1 = $[2]), React.useEffect(t0, t1);
3035
+ let t2;
3036
+ return $[3] !== props.children || $[4] !== selection ? (t2 = /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children }), $[3] = props.children, $[4] = selection, $[5] = t2) : t2 = $[5], t2;
3037
+ }
3038
+ const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
3039
+ let table;
3040
+ return () => {
3041
+ if (table)
3042
+ return table;
3043
+ table = [];
3044
+ for (let i = 0; i < 256; ++i)
3045
+ table[i] = (i + 256).toString(16).slice(1);
3046
+ return table;
3047
+ };
3048
+ })();
3049
+ function whatwgRNG(length = 16) {
3050
+ const rnds8 = new Uint8Array(length);
3051
+ return getRandomValues__default.default(rnds8), rnds8;
3052
+ }
3053
+ function randomKey(length) {
3054
+ const table = getByteHexTable();
3055
+ return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
3056
+ }
3057
+ const debug$g = debugWithName("operationToPatches");
3098
3058
  function createOperationToPatches(types2) {
3099
3059
  const textBlockName = types2.block.name;
3100
3060
  function insertTextPatch(editor, operation, beforeValue) {
3101
- debug$h.enabled && debug$h("Operation", JSON.stringify(operation, null, 2));
3061
+ debug$g.enabled && debug$g("Operation", JSON.stringify(operation, null, 2));
3102
3062
  const block = editor.isTextBlock(editor.children[operation.path[0]]) && editor.children[operation.path[0]];
3103
3063
  if (!block)
3104
3064
  throw new Error("Could not find block");
@@ -3191,7 +3151,7 @@ function createOperationToPatches(types2) {
3191
3151
  _key: block.children[operation.path[1] - 1]._key
3192
3152
  }])];
3193
3153
  }
3194
- return debug$h("Something was inserted into a void block. Not producing editor patches."), [];
3154
+ return debug$g("Something was inserted into a void block. Not producing editor patches."), [];
3195
3155
  }
3196
3156
  function splitNodePatch(editor, operation, beforeValue) {
3197
3157
  const patches$1 = [], splitBlock = editor.children[operation.path[0]];
@@ -3249,9 +3209,9 @@ function createOperationToPatches(types2) {
3249
3209
  _key: block._key
3250
3210
  }, "children", {
3251
3211
  _key: spanToRemove._key
3252
- }])] : (debug$h("Span not found in editor trying to remove node"), []);
3212
+ }])] : (debug$g("Span not found in editor trying to remove node"), []);
3253
3213
  } else
3254
- return debug$h("Not creating patch inside object block"), [];
3214
+ return debug$g("Not creating patch inside object block"), [];
3255
3215
  }
3256
3216
  function mergeNodePatch(editor, operation, beforeValue) {
3257
3217
  const patches$1 = [], block = beforeValue[operation.path[0]], updatedBlock = editor.children[operation.path[0]];
@@ -3277,7 +3237,7 @@ function createOperationToPatches(types2) {
3277
3237
  _key: removedSpan._key
3278
3238
  }])) : console.warn(`Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`, JSON.stringify(block, null, 2)));
3279
3239
  } else
3280
- debug$h("Void nodes can't be merged, not creating any patches");
3240
+ debug$g("Void nodes can't be merged, not creating any patches");
3281
3241
  return patches$1;
3282
3242
  }
3283
3243
  function moveNodePatch(editor, operation, beforeValue) {
@@ -3522,13 +3482,13 @@ function createWithObjectKeys(editorActor, schemaTypes) {
3522
3482
  }, editor;
3523
3483
  };
3524
3484
  }
3525
- const debug$g = debugWithName("applyPatches"), debugVerbose$2 = debug$g.enabled && !0;
3485
+ const debug$f = debugWithName("applyPatches"), debugVerbose$1 = debug$f.enabled && !0;
3526
3486
  function createApplyPatch(schemaTypes) {
3527
3487
  return (editor, patch) => {
3528
3488
  let changed = !1;
3529
- debugVerbose$2 && (debug$g(`
3489
+ debugVerbose$1 && (debug$f(`
3530
3490
 
3531
- NEW PATCH =============================================================`), debug$g(JSON.stringify(patch, null, 2)));
3491
+ NEW PATCH =============================================================`), debug$f(JSON.stringify(patch, null, 2)));
3532
3492
  try {
3533
3493
  switch (patch.type) {
3534
3494
  case "insert":
@@ -3544,7 +3504,7 @@ NEW PATCH =============================================================`), debug
3544
3504
  changed = diffMatchPatch(editor, patch);
3545
3505
  break;
3546
3506
  default:
3547
- debug$g("Unhandled patch", patch.type);
3507
+ debug$f("Unhandled patch", patch.type);
3548
3508
  }
3549
3509
  } catch (err) {
3550
3510
  console.error(err);
@@ -3559,9 +3519,9 @@ function diffMatchPatch(editor, patch) {
3559
3519
  childPath
3560
3520
  } = findBlockAndChildFromPath(editor, patch.path);
3561
3521
  if (!block)
3562
- return debug$g("Block not found"), !1;
3522
+ return debug$f("Block not found"), !1;
3563
3523
  if (!child || !childPath)
3564
- return debug$g("Child not found"), !1;
3524
+ return debug$f("Child not found"), !1;
3565
3525
  if (!(block && editor.isTextBlock(block) && patch.path.length === 4 && patch.path[1] === "children" && patch.path[3] === "text") || !slate.Text.isText(child))
3566
3526
  return !1;
3567
3527
  const patches2 = parse(patch.value), [newValue] = apply(patches2, child.text, {
@@ -3591,9 +3551,9 @@ function insertPatch(editor, patch, schemaTypes) {
3591
3551
  childPath: targetChildPath
3592
3552
  } = findBlockAndChildFromPath(editor, patch.path);
3593
3553
  if (!targetBlock || !targetBlockPath)
3594
- return debug$g("Block not found"), !1;
3554
+ return debug$f("Block not found"), !1;
3595
3555
  if (patch.path.length > 1 && patch.path[1] !== "children")
3596
- return debug$g("Ignoring patch targeting void value"), !1;
3556
+ return debug$f("Ignoring patch targeting void value"), !1;
3597
3557
  if (patch.path.length === 1) {
3598
3558
  const {
3599
3559
  items: items2,
@@ -3601,7 +3561,7 @@ function insertPatch(editor, patch, schemaTypes) {
3601
3561
  } = patch, blocksToInsert = toSlateValue(items2, {
3602
3562
  schemaTypes
3603
3563
  }, KEY_TO_SLATE_ELEMENT.get(editor)), targetBlockIndex = targetBlockPath[0], normalizedIdx2 = position2 === "after" ? targetBlockIndex + 1 : targetBlockIndex;
3604
- return debug$g(`Inserting blocks at path [${normalizedIdx2}]`), debugState(editor, "before"), slate.Transforms.insertNodes(editor, blocksToInsert, {
3564
+ return debug$f(`Inserting blocks at path [${normalizedIdx2}]`), debugState(editor, "before"), slate.Transforms.insertNodes(editor, blocksToInsert, {
3605
3565
  at: [normalizedIdx2]
3606
3566
  }), debugState(editor, "after"), !0;
3607
3567
  }
@@ -3610,14 +3570,14 @@ function insertPatch(editor, patch, schemaTypes) {
3610
3570
  position
3611
3571
  } = patch;
3612
3572
  if (!targetChild || !targetChildPath)
3613
- return debug$g("Child not found"), !1;
3573
+ return debug$f("Child not found"), !1;
3614
3574
  const childrenToInsert = targetBlock && toSlateValue([{
3615
3575
  ...targetBlock,
3616
3576
  children: items
3617
3577
  }], {
3618
3578
  schemaTypes
3619
3579
  }, KEY_TO_SLATE_ELEMENT.get(editor)), targetChildIndex = targetChildPath[1], normalizedIdx = position === "after" ? targetChildIndex + 1 : targetChildIndex, childInsertPath = [targetChildPath[0], normalizedIdx];
3620
- return debug$g(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && slate.Element.isElement(childrenToInsert[0]) && slate.Transforms.insertNodes(editor, childrenToInsert[0].children, {
3580
+ return debug$f(`Inserting children at path ${childInsertPath}`), debugState(editor, "before"), childrenToInsert && slate.Element.isElement(childrenToInsert[0]) && slate.Transforms.insertNodes(editor, childrenToInsert[0].children, {
3621
3581
  at: childInsertPath
3622
3582
  }), debugState(editor, "after"), !0;
3623
3583
  }
@@ -3631,14 +3591,14 @@ function setPatch(editor, patch) {
3631
3591
  childPath
3632
3592
  } = findBlockAndChildFromPath(editor, patch.path);
3633
3593
  if (!block)
3634
- return debug$g("Block not found"), !1;
3594
+ return debug$f("Block not found"), !1;
3635
3595
  const isTextBlock = editor.isTextBlock(block);
3636
3596
  if (isTextBlock && patch.path.length > 1 && patch.path[1] !== "children")
3637
- return debug$g("Ignoring setting void value"), !1;
3597
+ return debug$f("Ignoring setting void value"), !1;
3638
3598
  if (debugState(editor, "before"), isTextBlock && child && childPath) {
3639
3599
  if (slate.Text.isText(value) && slate.Text.isText(child)) {
3640
3600
  const newText = child.text;
3641
- value.text !== newText && (debug$g("Setting text property"), editor.apply({
3601
+ value.text !== newText && (debug$f("Setting text property"), editor.apply({
3642
3602
  type: "remove_text",
3643
3603
  path: childPath,
3644
3604
  offset: 0,
@@ -3650,7 +3610,7 @@ function setPatch(editor, patch) {
3650
3610
  text: value.text
3651
3611
  }), editor.onChange());
3652
3612
  } else
3653
- debug$g("Setting non-text property"), editor.apply({
3613
+ debug$f("Setting non-text property"), editor.apply({
3654
3614
  type: "set_node",
3655
3615
  path: childPath,
3656
3616
  properties: {},
@@ -3658,7 +3618,7 @@ function setPatch(editor, patch) {
3658
3618
  });
3659
3619
  return !0;
3660
3620
  } else if (slate.Element.isElement(block) && patch.path.length === 1 && blockPath) {
3661
- debug$g("Setting block property");
3621
+ debug$f("Setting block property");
3662
3622
  const {
3663
3623
  children,
3664
3624
  ...nextRest
@@ -3675,7 +3635,7 @@ function setPatch(editor, patch) {
3675
3635
  ...prevRest
3676
3636
  },
3677
3637
  newProperties: nextRest
3678
- }), debug$g("Setting children"), block.children.forEach((c, cIndex) => {
3638
+ }), debug$f("Setting children"), block.children.forEach((c, cIndex) => {
3679
3639
  editor.apply({
3680
3640
  type: "remove_node",
3681
3641
  path: blockPath.concat(block.children.length - 1 - cIndex),
@@ -3701,7 +3661,7 @@ function setPatch(editor, patch) {
3701
3661
  }
3702
3662
  function unsetPatch(editor, patch) {
3703
3663
  if (patch.path.length === 0) {
3704
- debug$g("Removing everything"), debugState(editor, "before");
3664
+ debug$f("Removing everything"), debugState(editor, "before");
3705
3665
  const previousSelection = editor.selection;
3706
3666
  return slate.Transforms.deselect(editor), editor.children.forEach((_child, i) => {
3707
3667
  slate.Transforms.removeNodes(editor, {
@@ -3728,13 +3688,13 @@ function unsetPatch(editor, patch) {
3728
3688
  } = findBlockAndChildFromPath(editor, patch.path);
3729
3689
  if (patch.path.length === 1) {
3730
3690
  if (!block || !blockPath)
3731
- return debug$g("Block not found"), !1;
3691
+ return debug$f("Block not found"), !1;
3732
3692
  const blockIndex = blockPath[0];
3733
- return debug$g(`Removing block at path [${blockIndex}]`), debugState(editor, "before"), slate.Transforms.removeNodes(editor, {
3693
+ return debug$f(`Removing block at path [${blockIndex}]`), debugState(editor, "before"), slate.Transforms.removeNodes(editor, {
3734
3694
  at: [blockIndex]
3735
3695
  }), debugState(editor, "after"), !0;
3736
3696
  }
3737
- 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)}`), slate.Transforms.removeNodes(editor, {
3697
+ 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)}`), slate.Transforms.removeNodes(editor, {
3738
3698
  at: childPath
3739
3699
  }), debugState(editor, "after"), !0) : !1;
3740
3700
  }
@@ -3742,7 +3702,7 @@ function isKeyedSegment(segment) {
3742
3702
  return typeof segment == "object" && "_key" in segment;
3743
3703
  }
3744
3704
  function debugState(editor, stateName) {
3745
- debugVerbose$2 && (debug$g(`Children ${stateName}:`, JSON.stringify(editor.children, null, 2)), debug$g(`Selection ${stateName}: `, JSON.stringify(editor.selection, null, 2)));
3705
+ debugVerbose$1 && (debug$f(`Children ${stateName}:`, JSON.stringify(editor.children, null, 2)), debug$f(`Selection ${stateName}: `, JSON.stringify(editor.selection, null, 2)));
3746
3706
  }
3747
3707
  function findBlockFromPath(editor, path) {
3748
3708
  let blockIndex = -1;
@@ -3784,7 +3744,7 @@ function findBlockAndChildFromPath(editor, path) {
3784
3744
  childPath: void 0
3785
3745
  };
3786
3746
  }
3787
- const debug$f = debugWithName("plugin:withPatches");
3747
+ const debug$e = debugWithName("plugin:withPatches");
3788
3748
  function createWithPatches({
3789
3749
  editorActor,
3790
3750
  patchFunctions,
@@ -3810,7 +3770,7 @@ function createWithPatches({
3810
3770
  withoutPatching(editor, () => {
3811
3771
  withoutSaving(editor, () => {
3812
3772
  patches2.forEach((patch) => {
3813
- debug$f.enabled && debug$f(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
3773
+ debug$e.enabled && debug$e(`Handling remote patch ${JSON.stringify(patch)}`), changed = applyPatch(editor, patch);
3814
3774
  });
3815
3775
  });
3816
3776
  });
@@ -3823,10 +3783,10 @@ function createWithPatches({
3823
3783
  remotePatches.length !== 0 && (bufferedPatches = bufferedPatches.concat(remotePatches), handleBufferedRemotePatches());
3824
3784
  };
3825
3785
  return subscriptions.push(() => {
3826
- debug$f("Subscribing to remote patches");
3786
+ debug$e("Subscribing to remote patches");
3827
3787
  const sub = editorActor.on("patches", handlePatches);
3828
3788
  return () => {
3829
- debug$f("Unsubscribing to remote patches"), sub.unsubscribe();
3789
+ debug$e("Unsubscribing to remote patches"), sub.unsubscribe();
3830
3790
  };
3831
3791
  }), editor.apply = (operation) => {
3832
3792
  if (editorActor.getSnapshot().context.readOnly)
@@ -3879,7 +3839,7 @@ function createWithPatches({
3879
3839
  }, editor;
3880
3840
  };
3881
3841
  }
3882
- const debug$e = debugWithName("plugin:withPlaceholderBlock");
3842
+ const debug$d = debugWithName("plugin:withPlaceholderBlock");
3883
3843
  function createWithPlaceholderBlock(editorActor) {
3884
3844
  return function(editor) {
3885
3845
  const {
@@ -3899,7 +3859,7 @@ function createWithPlaceholderBlock(editorActor) {
3899
3859
  const node = op.node;
3900
3860
  if (op.path[0] === 0 && slate.Editor.isVoid(editor, node)) {
3901
3861
  const nextPath = slate.Path.next(op.path);
3902
- editor.children[nextPath[0]] || (debug$e("Adding placeholder block"), slate.Editor.insertNode(editor, editor.pteCreateTextBlock({
3862
+ editor.children[nextPath[0]] || (debug$d("Adding placeholder block"), slate.Editor.insertNode(editor, editor.pteCreateTextBlock({
3903
3863
  decorators: []
3904
3864
  })));
3905
3865
  }
@@ -3909,7 +3869,7 @@ function createWithPlaceholderBlock(editorActor) {
3909
3869
  }, editor;
3910
3870
  };
3911
3871
  }
3912
- const debug$d = debugWithName("plugin:withPortableTextBlockStyle");
3872
+ const debug$c = debugWithName("plugin:withPortableTextBlockStyle");
3913
3873
  function createWithPortableTextBlockStyle(editorActor, types2) {
3914
3874
  const defaultStyle = types2.styles[0].value;
3915
3875
  return function(editor) {
@@ -3922,7 +3882,7 @@ function createWithPortableTextBlockStyle(editorActor, types2) {
3922
3882
  if (op.type === "split_node" && op.path.length === 1 && editor.isTextBlock(op.properties) && op.properties.style !== defaultStyle && op.path[0] === path[0] && !slate.Path.equals(path, op.path)) {
3923
3883
  const [child] = slate.Editor.node(editor, [op.path[0] + 1, 0]);
3924
3884
  if (slate.Text.isText(child) && child.text === "") {
3925
- debug$d(`Normalizing split node to ${defaultStyle} style`, op), editorActor.send({
3885
+ debug$c(`Normalizing split node to ${defaultStyle} style`, op), editorActor.send({
3926
3886
  type: "normalizing"
3927
3887
  }), slate.Transforms.setNodes(editor, {
3928
3888
  style: defaultStyle
@@ -3944,12 +3904,12 @@ function createWithPortableTextBlockStyle(editorActor, types2) {
3944
3904
  at: editor.selection,
3945
3905
  match: (node) => editor.isTextBlock(node)
3946
3906
  })].forEach(([node, path]) => {
3947
- editor.isTextBlock(node) && node.style === blockStyle ? (debug$d(`Unsetting block style '${blockStyle}'`), slate.Transforms.setNodes(editor, {
3907
+ editor.isTextBlock(node) && node.style === blockStyle ? (debug$c(`Unsetting block style '${blockStyle}'`), slate.Transforms.setNodes(editor, {
3948
3908
  ...node,
3949
3909
  style: defaultStyle
3950
3910
  }, {
3951
3911
  at: path
3952
- })) : (blockStyle ? debug$d(`Setting style '${blockStyle}'`) : debug$d("Setting default style", defaultStyle), slate.Transforms.setNodes(editor, {
3912
+ })) : (blockStyle ? debug$c(`Setting style '${blockStyle}'`) : debug$c("Setting default style", defaultStyle), slate.Transforms.setNodes(editor, {
3953
3913
  ...node,
3954
3914
  style: blockStyle || defaultStyle
3955
3915
  }, {
@@ -3959,11 +3919,11 @@ function createWithPortableTextBlockStyle(editorActor, types2) {
3959
3919
  }, editor;
3960
3920
  };
3961
3921
  }
3962
- const debug$c = debugWithName("plugin:withPortableTextLists"), MAX_LIST_LEVEL = 10;
3922
+ const debug$b = debugWithName("plugin:withPortableTextLists"), MAX_LIST_LEVEL = 10;
3963
3923
  function createWithPortableTextLists(types2) {
3964
3924
  return function(editor) {
3965
3925
  return editor.pteToggleListItem = (listItemStyle) => {
3966
- editor.pteHasListStyle(listItemStyle) ? (debug$c(`Remove list item '${listItemStyle}'`), editor.pteUnsetListItem(listItemStyle)) : (debug$c(`Add list item '${listItemStyle}'`), editor.pteSetListItem(listItemStyle));
3926
+ editor.pteHasListStyle(listItemStyle) ? (debug$b(`Remove list item '${listItemStyle}'`), editor.pteUnsetListItem(listItemStyle)) : (debug$b(`Add list item '${listItemStyle}'`), editor.pteSetListItem(listItemStyle));
3967
3927
  }, editor.pteUnsetListItem = (listItemStyle) => {
3968
3928
  editor.selection && [...slate.Editor.nodes(editor, {
3969
3929
  at: editor.selection,
@@ -3979,7 +3939,7 @@ function createWithPortableTextLists(types2) {
3979
3939
  listItem: void 0,
3980
3940
  level: void 0
3981
3941
  };
3982
- debug$c(`Unsetting list '${listItemStyle}'`), slate.Transforms.setNodes(editor, newNode, {
3942
+ debug$b(`Unsetting list '${listItemStyle}'`), slate.Transforms.setNodes(editor, newNode, {
3983
3943
  at: path
3984
3944
  });
3985
3945
  }
@@ -3989,7 +3949,7 @@ function createWithPortableTextLists(types2) {
3989
3949
  at: editor.selection,
3990
3950
  match: (node) => editor.isTextBlock(node)
3991
3951
  })].forEach(([node, path]) => {
3992
- debug$c(`Setting list '${listItemStyle}'`), slate.Transforms.setNodes(editor, {
3952
+ debug$b(`Setting list '${listItemStyle}'`), slate.Transforms.setNodes(editor, {
3993
3953
  ...node,
3994
3954
  level: 1,
3995
3955
  listItem: listItemStyle || types2.lists[0] && types2.lists[0].value
@@ -4005,7 +3965,7 @@ function createWithPortableTextLists(types2) {
4005
3965
  match: (node) => slate.Element.isElement(node) && editor.isListBlock(node) && node.children.length === 1 && slate.Text.isText(node.children[0]) && node.children[0].text === ""
4006
3966
  })];
4007
3967
  return selectedBlocks.length === 0 ? !1 : (selectedBlocks.forEach(([node, path]) => {
4008
- slate.Element.isElement(node) && (debug$c("Unset list"), slate.Transforms.setNodes(editor, {
3968
+ slate.Element.isElement(node) && (debug$b("Unset list"), slate.Transforms.setNodes(editor, {
4009
3969
  ...node,
4010
3970
  level: void 0,
4011
3971
  listItem: void 0
@@ -4023,7 +3983,7 @@ function createWithPortableTextLists(types2) {
4023
3983
  return selectedBlocks.length === 0 ? !1 : (selectedBlocks.forEach(([node, path]) => {
4024
3984
  if (editor.isListBlock(node)) {
4025
3985
  let level = node.level || 1;
4026
- 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)))), slate.Transforms.setNodes(editor, {
3986
+ 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)))), slate.Transforms.setNodes(editor, {
4027
3987
  level: Math.min(MAX_LIST_LEVEL, Math.max(1, level))
4028
3988
  }, {
4029
3989
  at: path
@@ -4069,7 +4029,7 @@ function getNextSpan({
4069
4029
  }
4070
4030
  return nextSpan;
4071
4031
  }
4072
- const debug$b = debugWithName("plugin:withPortableTextMarkModel");
4032
+ const debug$a = debugWithName("plugin:withPortableTextMarkModel");
4073
4033
  function createWithPortableTextMarkModel(editorActor, types2) {
4074
4034
  return function(editor) {
4075
4035
  const {
@@ -4083,7 +4043,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4083
4043
  for (const [child, childPath] of children) {
4084
4044
  const nextNode = node.children[childPath[1] + 1];
4085
4045
  if (editor.isTextSpan(child) && editor.isTextSpan(nextNode) && child.marks?.every((mark) => nextNode.marks?.includes(mark)) && nextNode.marks?.every((mark) => child.marks?.includes(mark))) {
4086
- debug$b("Merging spans", JSON.stringify(child, null, 2), JSON.stringify(nextNode, null, 2)), editorActor.send({
4046
+ debug$a("Merging spans", JSON.stringify(child, null, 2), JSON.stringify(nextNode, null, 2)), editorActor.send({
4087
4047
  type: "normalizing"
4088
4048
  }), slate.Transforms.mergeNodes(editor, {
4089
4049
  at: [childPath[0], childPath[1] + 1],
@@ -4096,7 +4056,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4096
4056
  }
4097
4057
  }
4098
4058
  if (editor.isTextBlock(node) && !Array.isArray(node.markDefs)) {
4099
- debug$b("Adding .markDefs to block node"), editorActor.send({
4059
+ debug$a("Adding .markDefs to block node"), editorActor.send({
4100
4060
  type: "normalizing"
4101
4061
  }), slate.Transforms.setNodes(editor, {
4102
4062
  markDefs: []
@@ -4108,7 +4068,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4108
4068
  return;
4109
4069
  }
4110
4070
  if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
4111
- debug$b("Adding .marks to span node"), editorActor.send({
4071
+ debug$a("Adding .marks to span node"), editorActor.send({
4112
4072
  type: "normalizing"
4113
4073
  }), slate.Transforms.setNodes(editor, {
4114
4074
  marks: []
@@ -4122,7 +4082,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4122
4082
  if (editor.isTextSpan(node)) {
4123
4083
  const blockPath = slate.Path.parent(path), [block] = slate.Editor.node(editor, blockPath), decorators2 = types2.decorators.map((decorator) => decorator.value), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
4124
4084
  if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
4125
- debug$b("Removing annotations from empty span node"), editorActor.send({
4085
+ debug$a("Removing annotations from empty span node"), editorActor.send({
4126
4086
  type: "normalizing"
4127
4087
  }), slate.Transforms.setNodes(editor, {
4128
4088
  marks: node.marks?.filter((mark) => decorators2.includes(mark))
@@ -4140,7 +4100,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4140
4100
  if (editor.isTextSpan(child)) {
4141
4101
  const marks = child.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !node.markDefs?.find((def) => def._key === mark));
4142
4102
  if (orphanedAnnotations.length > 0) {
4143
- debug$b("Removing orphaned annotations from span node"), editorActor.send({
4103
+ debug$a("Removing orphaned annotations from span node"), editorActor.send({
4144
4104
  type: "normalizing"
4145
4105
  }), slate.Transforms.setNodes(editor, {
4146
4106
  marks: marks.filter((mark) => !orphanedAnnotations.includes(mark))
@@ -4158,7 +4118,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4158
4118
  if (editor.isTextBlock(block)) {
4159
4119
  const decorators2 = types2.decorators.map((decorator) => decorator.value), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
4160
4120
  if (orphanedAnnotations.length > 0) {
4161
- debug$b("Removing orphaned annotations from span node"), editorActor.send({
4121
+ debug$a("Removing orphaned annotations from span node"), editorActor.send({
4162
4122
  type: "normalizing"
4163
4123
  }), slate.Transforms.setNodes(editor, {
4164
4124
  marks: marks.filter((mark) => !orphanedAnnotations.includes(mark))
@@ -4176,7 +4136,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4176
4136
  for (const markDef of markDefs)
4177
4137
  markDefKeys.has(markDef._key) || (markDefKeys.add(markDef._key), newMarkDefs.push(markDef));
4178
4138
  if (markDefs.length !== newMarkDefs.length) {
4179
- debug$b("Removing duplicate markDefs"), editorActor.send({
4139
+ debug$a("Removing duplicate markDefs"), editorActor.send({
4180
4140
  type: "normalizing"
4181
4141
  }), slate.Transforms.setNodes(editor, {
4182
4142
  markDefs: newMarkDefs
@@ -4191,7 +4151,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4191
4151
  if (editor.isTextBlock(node) && !editor.operations.some((op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1)) {
4192
4152
  const newMarkDefs = (node.markDefs || []).filter((def) => node.children.find((child) => slate.Text.isText(child) && Array.isArray(child.marks) && child.marks.includes(def._key)));
4193
4153
  if (node.markDefs && !isEqual__default.default(newMarkDefs, node.markDefs)) {
4194
- debug$b("Removing markDef not in use"), editorActor.send({
4154
+ debug$a("Removing markDef not in use"), editorActor.send({
4195
4155
  type: "normalizing"
4196
4156
  }), slate.Transforms.setNodes(editor, {
4197
4157
  markDefs: newMarkDefs
@@ -4408,7 +4368,7 @@ function createWithPortableTextMarkModel(editorActor, types2) {
4408
4368
  const [targetBlock, targetPath] = slate.Editor.node(editor, [op.path[0] - 1]);
4409
4369
  if (editor.isTextBlock(targetBlock)) {
4410
4370
  const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq__default.default([...oldDefs, ...op.properties.markDefs]);
4411
- debug$b("Copying markDefs over to merged block", op), slate.Transforms.setNodes(editor, {
4371
+ debug$a("Copying markDefs over to merged block", op), slate.Transforms.setNodes(editor, {
4412
4372
  markDefs: newMarkDefs
4413
4373
  }, {
4414
4374
  at: targetPath,
@@ -4560,7 +4520,7 @@ const toggleDecoratorActionImplementation = ({
4560
4520
  decorator: action.decorator
4561
4521
  }
4562
4522
  });
4563
- }, debug$a = debugWithName("plugin:withPortableTextSelections"), debugVerbose$1 = debug$a.enabled && !1;
4523
+ }, debug$9 = debugWithName("plugin:withPortableTextSelections"), debugVerbose = debug$9.enabled && !1;
4564
4524
  function createWithPortableTextSelections(editorActor, types2) {
4565
4525
  let prevSelection = null;
4566
4526
  return function(editor) {
@@ -4576,7 +4536,7 @@ function createWithPortableTextSelections(editorActor, types2) {
4576
4536
  ptRange = toPortableTextRange(value, editor.selection, types2), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
4577
4537
  }
4578
4538
  }
4579
- debugVerbose$1 && debug$a(`Emitting selection ${JSON.stringify(ptRange || null)} (${JSON.stringify(editor.selection)})`), ptRange ? editorActor.send({
4539
+ debugVerbose && debug$9(`Emitting selection ${JSON.stringify(ptRange || null)} (${JSON.stringify(editor.selection)})`), ptRange ? editorActor.send({
4580
4540
  type: "selection",
4581
4541
  selection: ptRange
4582
4542
  }) : editorActor.send({
@@ -4594,7 +4554,7 @@ function createWithPortableTextSelections(editorActor, types2) {
4594
4554
  }, editor;
4595
4555
  };
4596
4556
  }
4597
- const debug$9 = debugWithName("plugin:withSchemaTypes");
4557
+ const debug$8 = debugWithName("plugin:withSchemaTypes");
4598
4558
  function createWithSchemaTypes({
4599
4559
  editorActor,
4600
4560
  schemaTypes
@@ -4607,7 +4567,7 @@ function createWithSchemaTypes({
4607
4567
  return editor.normalizeNode = (entry) => {
4608
4568
  const [node, path] = entry;
4609
4569
  if (node._type === void 0 && path.length === 2) {
4610
- debug$9("Setting span type on text node without a type");
4570
+ debug$8("Setting span type on text node without a type");
4611
4571
  const span = node, key = span._key || editorActor.getSnapshot().context.keyGenerator();
4612
4572
  editorActor.send({
4613
4573
  type: "normalizing"
@@ -4623,7 +4583,7 @@ function createWithSchemaTypes({
4623
4583
  return;
4624
4584
  }
4625
4585
  if (node._key === void 0 && (path.length === 1 || path.length === 2)) {
4626
- debug$9("Setting missing key on child node without a key");
4586
+ debug$8("Setting missing key on child node without a key");
4627
4587
  const key = editorActor.getSnapshot().context.keyGenerator();
4628
4588
  editorActor.send({
4629
4589
  type: "normalizing"
@@ -4640,7 +4600,7 @@ function createWithSchemaTypes({
4640
4600
  }, editor;
4641
4601
  };
4642
4602
  }
4643
- const debug$8 = debugWithName("plugin:withUtils");
4603
+ const debug$7 = debugWithName("plugin:withUtils");
4644
4604
  function createWithUtils({
4645
4605
  editorActor,
4646
4606
  schemaTypes
@@ -4655,14 +4615,14 @@ function createWithUtils({
4655
4615
  depth: 2
4656
4616
  });
4657
4617
  if (!textNode || !slate.Text.isText(textNode) || textNode.text.length === 0) {
4658
- debug$8("pteExpandToWord: Can't expand to word here");
4618
+ debug$7("pteExpandToWord: Can't expand to word here");
4659
4619
  return;
4660
4620
  }
4661
4621
  const {
4662
4622
  focus
4663
4623
  } = 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);
4664
4624
  if (!(newStartOffset === newEndOffset || Number.isNaN(newStartOffset) || Number.isNaN(newEndOffset))) {
4665
- debug$8("pteExpandToWord: Expanding to focused word"), slate.Transforms.setSelection(editor, {
4625
+ debug$7("pteExpandToWord: Expanding to focused word"), slate.Transforms.setSelection(editor, {
4666
4626
  anchor: {
4667
4627
  ...selection.anchor,
4668
4628
  offset: newStartOffset
@@ -4674,7 +4634,7 @@ function createWithUtils({
4674
4634
  });
4675
4635
  return;
4676
4636
  }
4677
- debug$8("pteExpandToWord: Can't expand to word here");
4637
+ debug$7("pteExpandToWord: Can't expand to word here");
4678
4638
  }
4679
4639
  }, editor.pteCreateTextBlock = (options) => toSlateValue([{
4680
4640
  _type: schemaTypes.block.name,
@@ -4700,16 +4660,10 @@ function createWithUtils({
4700
4660
  })[0], editor;
4701
4661
  };
4702
4662
  }
4703
- const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, options) => {
4663
+ const withPlugins = (editor, options) => {
4704
4664
  const e = editor, {
4705
4665
  editorActor
4706
- } = options, schemaTypes = editorActor.getSnapshot().context.schema;
4707
- e.destroy ? e.destroy() : originalFnMap.set(e, {
4708
- apply: e.apply,
4709
- onChange: e.onChange,
4710
- normalizeNode: e.normalizeNode
4711
- });
4712
- const operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(editorActor, schemaTypes), withSchemaTypes = createWithSchemaTypes({
4666
+ } = options, schemaTypes = editorActor.getSnapshot().context.schema, operationToPatches = createOperationToPatches(schemaTypes), withObjectKeys = createWithObjectKeys(editorActor, schemaTypes), withSchemaTypes = createWithSchemaTypes({
4713
4667
  editorActor,
4714
4668
  schemaTypes
4715
4669
  }), withPatches = createWithPatches({
@@ -4724,38 +4678,21 @@ const originalFnMap = /* @__PURE__ */ new WeakMap(), withPlugins = (editor, opti
4724
4678
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor, schemaTypes), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor, schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
4725
4679
  editorActor,
4726
4680
  schemaTypes
4727
- }), withPortableTextSelections = createWithPortableTextSelections(editorActor, schemaTypes), withEventListeners = createWithEventListeners(editorActor, options.subscriptions);
4728
- return e.destroy = () => {
4729
- const originalFunctions = originalFnMap.get(e);
4730
- if (!originalFunctions)
4731
- throw new Error("Could not find pristine versions of editor functions");
4732
- e.apply = originalFunctions.apply, e.history = {
4733
- undos: [],
4734
- redos: []
4735
- }, e.normalizeNode = originalFunctions.normalizeNode, e.onChange = originalFunctions.onChange;
4736
- }, withEventListeners(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPortableTextLists(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e))))))))))));
4737
- }, debug$7 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
4681
+ }), withPortableTextSelections = createWithPortableTextSelections(editorActor, schemaTypes);
4682
+ return createWithEventListeners(editorActor, options.subscriptions)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPortableTextLists(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e))))))))))));
4683
+ }, debug$6 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
4738
4684
  function createSlateEditor(config) {
4739
4685
  const existingSlateEditor = slateEditors.get(config.editorActor);
4740
4686
  if (existingSlateEditor)
4741
- return debug$7("Reusing existing Slate editor instance", config.editorActor.id), existingSlateEditor;
4742
- debug$7("Creating new Slate editor instance", config.editorActor.id);
4743
- let unsubscriptions = [], subscriptions = [];
4744
- const instance = withPlugins(slateReact.withReact(slate.createEditor()), {
4687
+ return debug$6("Reusing existing Slate editor instance", config.editorActor.id), existingSlateEditor;
4688
+ debug$6("Creating new Slate editor instance", config.editorActor.id);
4689
+ const unsubscriptions = [], subscriptions = [], instance = withPlugins(slateReact.withReact(slate.createEditor()), {
4745
4690
  editorActor: config.editorActor,
4746
4691
  subscriptions
4747
4692
  });
4748
4693
  KEY_TO_VALUE_ELEMENT.set(instance, {}), KEY_TO_SLATE_ELEMENT.set(instance, {});
4749
4694
  for (const subscription of subscriptions)
4750
4695
  unsubscriptions.push(subscription());
4751
- config.editorActor.subscribe((snapshot) => {
4752
- if (snapshot.status !== "active") {
4753
- debug$7("Destroying Slate editor"), instance.destroy();
4754
- for (const unsubscribe of unsubscriptions)
4755
- unsubscribe();
4756
- subscriptions = [], unsubscriptions = [];
4757
- }
4758
- });
4759
4696
  const initialValue = [instance.pteCreateTextBlock({
4760
4697
  decorators: []
4761
4698
  })], slateEditor = {
@@ -4764,7 +4701,7 @@ function createSlateEditor(config) {
4764
4701
  };
4765
4702
  return slateEditors.set(config.editorActor, slateEditor), slateEditor;
4766
4703
  }
4767
- const EditorActorContext = react.createContext({}), debug$6 = debugWithName("API:editable");
4704
+ const debug$5 = debugWithName("API:editable");
4768
4705
  function createEditableAPI(editor, editorActor) {
4769
4706
  const types2 = editorActor.getSnapshot().context.schema;
4770
4707
  return {
@@ -4845,7 +4782,7 @@ function createEditableAPI(editor, editorActor) {
4845
4782
  }], {
4846
4783
  schemaTypes: editorActor.getSnapshot().context.schema
4847
4784
  })[0].children[0], focusChildPath = editor.selection.focus.path.slice(0, 2), isSpanNode = child._type === types2.span.name, focusNode = slate.Node.get(editor, focusChildPath);
4848
- return isSpanNode && focusNode._type !== types2.span.name && (debug$6("Inserting span child next to inline object child, moving selection + 1"), editor.move({
4785
+ return isSpanNode && focusNode._type !== types2.span.name && (debug$5("Inserting span child next to inline object child, moving selection + 1"), editor.move({
4849
4786
  distance: 1,
4850
4787
  unit: "character"
4851
4788
  })), slate.Transforms.insertNodes(editor, child, {
@@ -4977,18 +4914,18 @@ function createEditableAPI(editor, editorActor) {
4977
4914
  throw new Error("Invalid range");
4978
4915
  if (range) {
4979
4916
  if (!options?.mode || options?.mode === "selected") {
4980
- debug$6("Deleting content in selection"), slate.Transforms.delete(editor, {
4917
+ debug$5("Deleting content in selection"), slate.Transforms.delete(editor, {
4981
4918
  at: range,
4982
4919
  hanging: !0,
4983
4920
  voids: !0
4984
4921
  }), editor.onChange();
4985
4922
  return;
4986
4923
  }
4987
- options?.mode === "blocks" && (debug$6("Deleting blocks touched by selection"), slate.Transforms.removeNodes(editor, {
4924
+ options?.mode === "blocks" && (debug$5("Deleting blocks touched by selection"), slate.Transforms.removeNodes(editor, {
4988
4925
  at: range,
4989
4926
  voids: !0,
4990
4927
  match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && slate.Element.isElement(node)
4991
- })), options?.mode === "children" && (debug$6("Deleting children touched by selection"), slate.Transforms.removeNodes(editor, {
4928
+ })), options?.mode === "children" && (debug$5("Deleting children touched by selection"), slate.Transforms.removeNodes(editor, {
4992
4929
  at: range,
4993
4930
  voids: !0,
4994
4931
  match: (node) => node._type === types2.span.name || // Text children
@@ -5151,7 +5088,7 @@ const addAnnotationActionImplementation = ({
5151
5088
  action
5152
5089
  }) => {
5153
5090
  const editor = action.editor;
5154
- if (debug$6("Removing annotation", action.annotation.name), !!editor.selection)
5091
+ if (debug$5("Removing annotation", action.annotation.name), !!editor.selection)
5155
5092
  if (slate.Range.isCollapsed(editor.selection)) {
5156
5093
  const [block, blockPath] = slate.Editor.node(editor, editor.selection, {
5157
5094
  depth: 1
@@ -5695,22 +5632,7 @@ function performDefaultAction({
5695
5632
  });
5696
5633
  }
5697
5634
  }
5698
- const networkLogic = xstate.fromCallback(({
5699
- sendBack
5700
- }) => {
5701
- const onlineHandler = () => {
5702
- sendBack({
5703
- type: "online"
5704
- });
5705
- }, offlineHandler = () => {
5706
- sendBack({
5707
- type: "offline"
5708
- });
5709
- };
5710
- return window && (window.addEventListener("online", onlineHandler), window.addEventListener("offline", offlineHandler)), () => {
5711
- window && (window.removeEventListener("online", onlineHandler), window.removeEventListener("offline", offlineHandler));
5712
- };
5713
- }), editorMachine = xstate.setup({
5635
+ const editorMachine = xstate.setup({
5714
5636
  types: {
5715
5637
  context: {},
5716
5638
  events: {},
@@ -5809,9 +5731,6 @@ const networkLogic = xstate.fromCallback(({
5809
5731
  actionIntends: [defaultAction]
5810
5732
  });
5811
5733
  })
5812
- },
5813
- actors: {
5814
- networkLogic
5815
5734
  }
5816
5735
  }).createMachine({
5817
5736
  id: "editor",
@@ -5822,14 +5741,10 @@ const networkLogic = xstate.fromCallback(({
5822
5741
  keyGenerator: input.keyGenerator,
5823
5742
  pendingEvents: [],
5824
5743
  schema: input.schema,
5825
- readOnly: !1,
5826
- maxBlocks: void 0,
5744
+ readOnly: input.readOnly ?? !1,
5745
+ maxBlocks: input.maxBlocks,
5827
5746
  value: input.value
5828
5747
  }),
5829
- invoke: {
5830
- id: "networkLogic",
5831
- src: "networkLogic"
5832
- },
5833
5748
  on: {
5834
5749
  "annotation.add": {
5835
5750
  actions: xstate.emit(({
@@ -5903,16 +5818,6 @@ const networkLogic = xstate.fromCallback(({
5903
5818
  event
5904
5819
  }) => event)
5905
5820
  },
5906
- online: {
5907
- actions: xstate.emit({
5908
- type: "online"
5909
- })
5910
- },
5911
- offline: {
5912
- actions: xstate.emit({
5913
- type: "offline"
5914
- })
5915
- },
5916
5821
  loading: {
5917
5822
  actions: xstate.emit({
5918
5823
  type: "loading"
@@ -5942,11 +5847,16 @@ const networkLogic = xstate.fromCallback(({
5942
5847
  })
5943
5848
  },
5944
5849
  "toggle readOnly": {
5945
- actions: xstate.assign({
5850
+ actions: [xstate.assign({
5946
5851
  readOnly: ({
5947
5852
  context
5948
5853
  }) => !context.readOnly
5949
- })
5854
+ }), xstate.emit(({
5855
+ context
5856
+ }) => ({
5857
+ type: "readOnly toggled",
5858
+ readOnly: context.readOnly
5859
+ }))]
5950
5860
  },
5951
5861
  "update maxBlocks": {
5952
5862
  actions: xstate.assign({
@@ -6037,50 +5947,65 @@ const networkLogic = xstate.fromCallback(({
6037
5947
  }
6038
5948
  }
6039
5949
  }
6040
- }), PortableTextEditorSelectionContext = react.createContext(null), usePortableTextEditorSelection = () => {
6041
- const selection = react.useContext(PortableTextEditorSelectionContext);
6042
- if (selection === void 0)
6043
- throw new Error("The `usePortableTextEditorSelection` hook must be used inside the <PortableTextEditor> component's context.");
6044
- return selection;
6045
- }, debug$5 = debugWithName("component:PortableTextEditor:SelectionProvider"), debugVerbose = debug$5.enabled && !1;
6046
- function PortableTextEditorSelectionProvider(props) {
6047
- const $ = reactCompilerRuntime.c(6), [selection, setSelection] = react.useState(null);
6048
- let t0, t1;
6049
- $[0] !== props.editorActor ? (t0 = () => {
6050
- debug$5("Subscribing to selection changes");
6051
- const subscription = props.editorActor.on("selection", (event) => {
6052
- react.startTransition(() => {
6053
- debugVerbose && debug$5("Setting selection"), setSelection(event.selection);
6054
- });
6055
- });
6056
- return () => {
6057
- debug$5("Unsubscribing to selection changes"), subscription.unsubscribe();
6058
- };
6059
- }, t1 = [props.editorActor], $[0] = props.editorActor, $[1] = t0, $[2] = t1) : (t0 = $[1], t1 = $[2]), react.useEffect(t0, t1);
6060
- let t2;
6061
- return $[3] !== props.children || $[4] !== selection ? (t2 = /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorSelectionContext.Provider, { value: selection, children: props.children }), $[3] = props.children, $[4] = selection, $[5] = t2) : t2 = $[5], t2;
6062
- }
6063
- const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
6064
- let table;
6065
- return () => {
6066
- if (table)
6067
- return table;
6068
- table = [];
6069
- for (let i = 0; i < 256; ++i)
6070
- table[i] = (i + 256).toString(16).slice(1);
6071
- return table;
5950
+ });
5951
+ function createEditor(config) {
5952
+ const editorActor = xstate.createActor(editorMachine, {
5953
+ input: editorConfigToMachineInput(config)
5954
+ });
5955
+ editorActor.start();
5956
+ const slateEditor = createSlateEditor({
5957
+ editorActor
5958
+ }), editable = createEditableAPI(slateEditor.instance, editorActor);
5959
+ return {
5960
+ send: (event) => {
5961
+ editorActor.send(event);
5962
+ },
5963
+ on: (event, listener) => editorActor.on(
5964
+ event,
5965
+ // @ts-ignore
5966
+ listener
5967
+ ),
5968
+ editable,
5969
+ _internal: {
5970
+ editorActor,
5971
+ slateEditor
5972
+ }
6072
5973
  };
6073
- })();
6074
- function whatwgRNG(length = 16) {
6075
- const rnds8 = new Uint8Array(length);
6076
- return getRandomValues__default.default(rnds8), rnds8;
6077
5974
  }
6078
- function randomKey(length) {
6079
- const table = getByteHexTable();
6080
- return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
5975
+ function useEditor(config) {
5976
+ const editorActor = react.useActorRef(editorMachine, {
5977
+ input: editorConfigToMachineInput(config)
5978
+ }), slateEditor = createSlateEditor({
5979
+ editorActor
5980
+ }), editable = React.useMemo(() => createEditableAPI(slateEditor.instance, editorActor), [slateEditor.instance, editorActor]), send = React.useCallback((event) => {
5981
+ editorActor.send(event);
5982
+ }, [editorActor]), on = React.useCallback((event_0, listener) => editorActor.on(
5983
+ event_0,
5984
+ // @ts-ignore
5985
+ listener
5986
+ ), [editorActor]);
5987
+ return React.useMemo(() => ({
5988
+ send,
5989
+ on,
5990
+ editable,
5991
+ _internal: {
5992
+ editorActor,
5993
+ slateEditor
5994
+ }
5995
+ }), [send, on, editable, editorActor, slateEditor]);
5996
+ }
5997
+ function editorConfigToMachineInput(config) {
5998
+ return {
5999
+ behaviors: config.behaviors,
6000
+ keyGenerator: config.keyGenerator ?? defaultKeyGenerator,
6001
+ maxBlocks: config.maxBlocks,
6002
+ readOnly: config.readOnly,
6003
+ schema: config.schemaDefinition ? compileSchemaDefinition(config.schemaDefinition) : getPortableTextMemberSchemaTypes(config.schema.hasOwnProperty("jsonType") ? config.schema : compileType(config.schema)),
6004
+ value: config.initialValue
6005
+ };
6081
6006
  }
6082
6007
  const debug$4 = debugWithName("component:PortableTextEditor");
6083
- class PortableTextEditor extends react.Component {
6008
+ class PortableTextEditor extends React.Component {
6084
6009
  static displayName = "PortableTextEditor";
6085
6010
  /**
6086
6011
  * An observable of all the editor changes.
@@ -6090,66 +6015,49 @@ class PortableTextEditor extends react.Component {
6090
6015
  * A lookup table for all the relevant schema types for this portable text type.
6091
6016
  */
6092
6017
  /**
6018
+ * The editor instance
6019
+ */
6020
+ /*
6093
6021
  * The editor API (currently implemented with Slate).
6094
6022
  */
6095
6023
  constructor(props) {
6096
- if (super(props), props.editor) {
6097
- const editor = props.editor;
6098
- this.editorActor = editor._internal.editorActor, this.slateEditor = editor._internal.slateEditor, this.editorActor.start(), this.schemaTypes = this.editorActor.getSnapshot().context.schema;
6099
- } else {
6100
- if (!props.schemaType)
6101
- throw new Error('PortableTextEditor: missing "schemaType" property');
6102
- 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 = xstate.createActor(editorMachine, {
6103
- input: {
6104
- keyGenerator: props.keyGenerator || defaultKeyGenerator,
6105
- schema: this.schemaTypes,
6106
- value: props.value
6107
- }
6108
- }), this.editorActor.start(), this.slateEditor = createSlateEditor({
6109
- editorActor: this.editorActor
6110
- }), props.readOnly && this.editorActor.send({
6111
- type: "toggle readOnly"
6112
- }), props.maxBlocks && this.editorActor.send({
6113
- type: "update maxBlocks",
6114
- maxBlocks: props.maxBlocks === void 0 ? void 0 : Number.parseInt(props.maxBlocks.toString(), 10)
6115
- });
6116
- }
6117
- this.editable = createEditableAPI(this.slateEditor.instance, this.editorActor);
6024
+ super(props), props.editor ? this.editor = props.editor : this.editor = createEditor({
6025
+ keyGenerator: props.keyGenerator ?? defaultKeyGenerator,
6026
+ schema: props.schemaType,
6027
+ initialValue: props.value,
6028
+ maxBlocks: props.maxBlocks === void 0 ? void 0 : Number.parseInt(props.maxBlocks.toString(), 10),
6029
+ readOnly: props.readOnly
6030
+ }), this.schemaTypes = this.editor._internal.editorActor.getSnapshot().context.schema, this.editable = this.editor.editable;
6118
6031
  }
6119
6032
  componentDidUpdate(prevProps) {
6120
- !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({
6033
+ !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({
6121
6034
  type: "update schema",
6122
6035
  schema: this.schemaTypes
6123
- })), !this.props.editor && !prevProps.editor && (this.props.readOnly !== prevProps.readOnly && this.editorActor.send({
6036
+ })), !this.props.editor && !prevProps.editor && (this.props.readOnly !== prevProps.readOnly && this.editor._internal.editorActor.send({
6124
6037
  type: "toggle readOnly"
6125
- }), this.props.maxBlocks !== prevProps.maxBlocks && this.editorActor.send({
6038
+ }), this.props.maxBlocks !== prevProps.maxBlocks && this.editor._internal.editorActor.send({
6126
6039
  type: "update maxBlocks",
6127
6040
  maxBlocks: this.props.maxBlocks === void 0 ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10)
6128
- }), this.props.value !== prevProps.value && this.editorActor.send({
6041
+ }), this.props.value !== prevProps.value && this.editor._internal.editorActor.send({
6129
6042
  type: "update value",
6130
6043
  value: this.props.value
6131
6044
  }), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this));
6132
6045
  }
6133
6046
  setEditable = (editable) => {
6134
- this.editable = {
6135
- ...this.editable,
6047
+ this.editor.editable = {
6048
+ ...this.editor.editable,
6136
6049
  ...editable
6137
6050
  };
6138
6051
  };
6139
- getValue = () => {
6140
- if (this.editable)
6141
- return this.editable.getValue();
6142
- };
6143
6052
  render() {
6144
6053
  const legacyPatches = this.props.editor ? void 0 : this.props.incomingPatches$ ?? this.props.patches$;
6145
6054
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
6146
- legacyPatches ? /* @__PURE__ */ jsxRuntime.jsx(RoutePatchesObservableToEditorActor, { editorActor: this.editorActor, patches$: legacyPatches }) : null,
6147
- /* @__PURE__ */ jsxRuntime.jsx(EditorActorContext.Provider, { value: this.editorActor, children: /* @__PURE__ */ jsxRuntime.jsx(slateReact.Slate, { editor: this.slateEditor.instance, initialValue: this.slateEditor.initialValue, children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsxRuntime.jsxs(PortableTextEditorSelectionProvider, { editorActor: this.editorActor, children: [
6148
- /* @__PURE__ */ jsxRuntime.jsx(Synchronizer, { editorActor: this.editorActor, getValue: this.getValue, onChange: (change) => {
6149
- this.props.editor || this.props.onChange(change), this.change$.next(change);
6150
- } }),
6151
- this.props.children
6152
- ] }) }) }) })
6055
+ legacyPatches ? /* @__PURE__ */ jsxRuntime.jsx(RoutePatchesObservableToEditorActor, { editorActor: this.editor._internal.editorActor, patches$: legacyPatches }) : null,
6056
+ /* @__PURE__ */ jsxRuntime.jsx(RouteEventsToChanges, { editorActor: this.editor._internal.editorActor, onChange: (change) => {
6057
+ this.props.editor || this.props.onChange(change), this.change$.next(change);
6058
+ } }),
6059
+ /* @__PURE__ */ jsxRuntime.jsx(Synchronizer, { editorActor: this.editor._internal.editorActor, getValue: this.editor.editable.getValue, portableTextEditor: this, slateEditor: this.editor._internal.slateEditor.instance }),
6060
+ /* @__PURE__ */ jsxRuntime.jsx(EditorActorContext.Provider, { value: this.editor._internal.editorActor, children: /* @__PURE__ */ jsxRuntime.jsx(slateReact.Slate, { editor: this.editor._internal.slateEditor.instance, initialValue: this.editor._internal.slateEditor.initialValue, children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorSelectionProvider, { editorActor: this.editor._internal.editorActor, children: this.props.children }) }) }) })
6153
6061
  ] });
6154
6062
  }
6155
6063
  // Static API methods
@@ -6215,7 +6123,80 @@ function RoutePatchesObservableToEditorActor(props) {
6215
6123
  return () => {
6216
6124
  subscription.unsubscribe();
6217
6125
  };
6218
- }, t1 = [props.editorActor, props.patches$], $[0] = props.editorActor, $[1] = props.patches$, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), react.useEffect(t0, t1), null;
6126
+ }, t1 = [props.editorActor, props.patches$], $[0] = props.editorActor, $[1] = props.patches$, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), React.useEffect(t0, t1), null;
6127
+ }
6128
+ function RouteEventsToChanges(props) {
6129
+ const $ = reactCompilerRuntime.c(6);
6130
+ let t0;
6131
+ $[0] !== props ? (t0 = (change) => props.onChange(change), $[0] = props, $[1] = t0) : t0 = $[1];
6132
+ const handleChange = useEffectEvent.useEffectEvent(t0);
6133
+ let t1, t2;
6134
+ return $[2] !== handleChange || $[3] !== props.editorActor ? (t1 = () => {
6135
+ debug$4("Subscribing to editor changes");
6136
+ const sub = props.editorActor.on("*", (event) => {
6137
+ bb5: switch (event.type) {
6138
+ case "patch": {
6139
+ handleChange(event);
6140
+ break bb5;
6141
+ }
6142
+ case "loading": {
6143
+ handleChange({
6144
+ type: "loading",
6145
+ isLoading: !0
6146
+ });
6147
+ break bb5;
6148
+ }
6149
+ case "done loading": {
6150
+ handleChange({
6151
+ type: "loading",
6152
+ isLoading: !1
6153
+ });
6154
+ break bb5;
6155
+ }
6156
+ case "focused": {
6157
+ handleChange({
6158
+ type: "focus",
6159
+ event: event.event
6160
+ });
6161
+ break bb5;
6162
+ }
6163
+ case "value changed": {
6164
+ handleChange({
6165
+ type: "value",
6166
+ value: event.value
6167
+ });
6168
+ break bb5;
6169
+ }
6170
+ case "invalid value": {
6171
+ handleChange({
6172
+ type: "invalidValue",
6173
+ resolution: event.resolution,
6174
+ value: event.value
6175
+ });
6176
+ break bb5;
6177
+ }
6178
+ case "error": {
6179
+ handleChange({
6180
+ ...event,
6181
+ level: "warning"
6182
+ });
6183
+ break bb5;
6184
+ }
6185
+ case "annotation.add":
6186
+ case "annotation.remove":
6187
+ case "annotation.toggle":
6188
+ case "focus":
6189
+ case "patches":
6190
+ case "readOnly toggled":
6191
+ break bb5;
6192
+ default:
6193
+ handleChange(event);
6194
+ }
6195
+ });
6196
+ return () => {
6197
+ debug$4("Unsubscribing to changes"), sub.unsubscribe();
6198
+ };
6199
+ }, t2 = [props.editorActor, handleChange], $[2] = handleChange, $[3] = props.editorActor, $[4] = t1, $[5] = t2) : (t1 = $[4], t2 = $[5]), React.useEffect(t1, t2), null;
6219
6200
  }
6220
6201
  const debug$3 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (props) => {
6221
6202
  const {
@@ -6227,22 +6208,22 @@ const debug$3 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
6227
6208
  renderChild,
6228
6209
  renderDecorator,
6229
6210
  renderAnnotation
6230
- } = props, spanRef = react.useRef(null), portableTextEditor = usePortableTextEditor(), blockSelected = slateReact.useSelected(), [focused, setFocused] = react.useState(!1), [selected, setSelected] = react.useState(!1), block = children.props.parent, path = react.useMemo(() => block ? [{
6211
+ } = props, spanRef = React.useRef(null), portableTextEditor = usePortableTextEditor(), blockSelected = slateReact.useSelected(), [focused, setFocused] = React.useState(!1), [selected, setSelected] = React.useState(!1), block = children.props.parent, path = React.useMemo(() => block ? [{
6231
6212
  _key: block?._key
6232
6213
  }, "children", {
6233
6214
  _key: leaf._key
6234
- }] : [], [block, leaf._key]), decoratorValues = react.useMemo(() => schemaTypes.decorators.map((dec) => dec.value), [schemaTypes.decorators]), marks = react.useMemo(() => uniq__default.default((leaf.marks || EMPTY_MARKS).filter((mark) => decoratorValues.includes(mark))), [decoratorValues, leaf.marks]), annotationMarks = Array.isArray(leaf.marks) ? leaf.marks : EMPTY_MARKS, annotations = react.useMemo(() => annotationMarks.map((mark_0) => !decoratorValues.includes(mark_0) && block?.markDefs?.find((def) => def._key === mark_0)).filter(Boolean), [annotationMarks, block, decoratorValues]), shouldTrackSelectionAndFocus = annotations.length > 0 && blockSelected;
6235
- react.useEffect(() => {
6215
+ }] : [], [block, leaf._key]), decoratorValues = React.useMemo(() => schemaTypes.decorators.map((dec) => dec.value), [schemaTypes.decorators]), marks = React.useMemo(() => uniq__default.default((leaf.marks || EMPTY_MARKS).filter((mark) => decoratorValues.includes(mark))), [decoratorValues, leaf.marks]), annotationMarks = Array.isArray(leaf.marks) ? leaf.marks : EMPTY_MARKS, annotations = React.useMemo(() => annotationMarks.map((mark_0) => !decoratorValues.includes(mark_0) && block?.markDefs?.find((def) => def._key === mark_0)).filter(Boolean), [annotationMarks, block, decoratorValues]), shouldTrackSelectionAndFocus = annotations.length > 0 && blockSelected;
6216
+ React.useEffect(() => {
6236
6217
  if (!shouldTrackSelectionAndFocus) {
6237
6218
  setFocused(!1);
6238
6219
  return;
6239
6220
  }
6240
6221
  const sel = PortableTextEditor.getSelection(portableTextEditor);
6241
- sel && isEqual__default.default(sel.focus.path, path) && PortableTextEditor.isCollapsedSelection(portableTextEditor) && react.startTransition(() => {
6222
+ sel && isEqual__default.default(sel.focus.path, path) && PortableTextEditor.isCollapsedSelection(portableTextEditor) && React.startTransition(() => {
6242
6223
  setFocused(!0);
6243
6224
  });
6244
6225
  }, [shouldTrackSelectionAndFocus, path, portableTextEditor]);
6245
- const setSelectedFromRange = react.useCallback(() => {
6226
+ const setSelectedFromRange = React.useCallback(() => {
6246
6227
  if (!shouldTrackSelectionAndFocus)
6247
6228
  return;
6248
6229
  debug$3("Setting selection and focus from range");
@@ -6257,7 +6238,7 @@ const debug$3 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
6257
6238
  } else
6258
6239
  setSelected(!1);
6259
6240
  }, [shouldTrackSelectionAndFocus]);
6260
- react.useEffect(() => {
6241
+ React.useEffect(() => {
6261
6242
  if (!shouldTrackSelectionAndFocus)
6262
6243
  return;
6263
6244
  const onBlur = editorActor.on("blur", () => {
@@ -6271,8 +6252,8 @@ const debug$3 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
6271
6252
  return () => {
6272
6253
  onBlur.unsubscribe(), onFocus.unsubscribe(), onSelection.unsubscribe();
6273
6254
  };
6274
- }, [editorActor, path, portableTextEditor, setSelectedFromRange, shouldTrackSelectionAndFocus]), react.useEffect(() => setSelectedFromRange(), [setSelectedFromRange]);
6275
- const content = react.useMemo(() => {
6255
+ }, [editorActor, path, portableTextEditor, setSelectedFromRange, shouldTrackSelectionAndFocus]), React.useEffect(() => setSelectedFromRange(), [setSelectedFromRange]);
6256
+ const content = React.useMemo(() => {
6276
6257
  let returnedChildren = children;
6277
6258
  if (slate.Text.isText(leaf) && leaf._type === schemaTypes.span.name && (marks.forEach((mark_1) => {
6278
6259
  const schemaType = schemaTypes.decorators.find((dec_0) => dec_0.value === mark_1);
@@ -6338,7 +6319,7 @@ const debug$3 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
6338
6319
  }
6339
6320
  return returnedChildren;
6340
6321
  }, [annotations, block, children, focused, leaf, marks, path, renderAnnotation, renderChild, renderDecorator, schemaTypes.annotations, schemaTypes.decorators, schemaTypes.span, selected]);
6341
- return react.useMemo(() => /* @__PURE__ */ jsxRuntime.jsx("span", { ...attributes, ref: spanRef, children: content }, leaf._key), [leaf, attributes, content]);
6322
+ return React.useMemo(() => /* @__PURE__ */ jsxRuntime.jsx("span", { ...attributes, ref: spanRef, children: content }, leaf._key), [leaf, attributes, content]);
6342
6323
  };
6343
6324
  Leaf.displayName = "Leaf";
6344
6325
  const debug$2 = debugWithName("plugin:withHotKeys"), DEFAULT_HOTKEYS = {
@@ -6651,7 +6632,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6651
6632
  pointerEvents: "none",
6652
6633
  left: 0,
6653
6634
  right: 0
6654
- }, PortableTextEditable = react.forwardRef(function(props, forwardedRef) {
6635
+ }, PortableTextEditable = React.forwardRef(function(props, forwardedRef) {
6655
6636
  const {
6656
6637
  hotkeys,
6657
6638
  onBlur,
@@ -6672,19 +6653,17 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6672
6653
  scrollSelectionIntoView,
6673
6654
  spellCheck,
6674
6655
  ...restProps
6675
- } = props, portableTextEditor = usePortableTextEditor(), ref = react.useRef(null), [editableElement, setEditableElement] = react.useState(null), [hasInvalidValue, setHasInvalidValue] = react.useState(!1), [rangeDecorationState, setRangeDecorationsState] = react.useState([]);
6676
- react.useImperativeHandle(forwardedRef, () => ref.current);
6677
- const rangeDecorationsRef = react.useRef(rangeDecorations), editorActor = react.useContext(EditorActorContext), readOnly = react$1.useSelector(editorActor, (s) => s.context.readOnly), {
6678
- schemaTypes
6679
- } = portableTextEditor, slateEditor = slateReact.useSlate(), blockTypeName = schemaTypes.block.name;
6680
- react.useMemo(() => {
6656
+ } = props, portableTextEditor = usePortableTextEditor(), ref = React.useRef(null), [editableElement, setEditableElement] = React.useState(null), [hasInvalidValue, setHasInvalidValue] = React.useState(!1), [rangeDecorationState, setRangeDecorationsState] = React.useState([]);
6657
+ React.useImperativeHandle(forwardedRef, () => ref.current);
6658
+ const rangeDecorationsRef = React.useRef(rangeDecorations), editorActor = React.useContext(EditorActorContext), readOnly = react.useSelector(editorActor, (s) => s.context.readOnly), schemaTypes = react.useSelector(editorActor, (s_0) => s_0.context.schema), slateEditor = slateReact.useSlate(), blockTypeName = schemaTypes.block.name;
6659
+ React.useMemo(() => {
6681
6660
  const withInsertData = createWithInsertData(editorActor, schemaTypes);
6682
6661
  if (readOnly)
6683
6662
  return debug("Editable is in read only mode"), withInsertData(slateEditor);
6684
6663
  const withHotKeys = createWithHotkeys(editorActor, portableTextEditor, hotkeys);
6685
6664
  return debug("Editable is in edit mode"), withInsertData(withHotKeys(slateEditor));
6686
6665
  }, [editorActor, hotkeys, portableTextEditor, readOnly, schemaTypes, slateEditor]);
6687
- const renderElement = react.useCallback((eProps) => /* @__PURE__ */ jsxRuntime.jsx(Element, { ...eProps, readOnly, renderBlock, renderChild, renderListItem, renderStyle, schemaTypes, spellCheck }), [schemaTypes, spellCheck, readOnly, renderBlock, renderChild, renderListItem, renderStyle]), renderLeaf = react.useCallback((lProps) => {
6666
+ const renderElement = React.useCallback((eProps) => /* @__PURE__ */ jsxRuntime.jsx(Element, { ...eProps, readOnly, renderBlock, renderChild, renderListItem, renderStyle, schemaTypes, spellCheck }), [schemaTypes, spellCheck, readOnly, renderBlock, renderChild, renderListItem, renderStyle]), renderLeaf = React.useCallback((lProps) => {
6688
6667
  if (lProps.leaf._type === "span") {
6689
6668
  let rendered = /* @__PURE__ */ jsxRuntime.jsx(Leaf, { ...lProps, editorActor, schemaTypes, renderAnnotation, renderChild, renderDecorator, readOnly });
6690
6669
  if (renderPlaceholder && lProps.leaf.placeholder && lProps.text.text === "")
@@ -6698,7 +6677,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6698
6677
  })), rendered;
6699
6678
  }
6700
6679
  return lProps.children;
6701
- }, [editorActor, readOnly, renderAnnotation, renderChild, renderDecorator, renderPlaceholder, schemaTypes]), restoreSelectionFromProps = react.useCallback(() => {
6680
+ }, [editorActor, readOnly, renderAnnotation, renderChild, renderDecorator, renderPlaceholder, schemaTypes]), restoreSelectionFromProps = React.useCallback(() => {
6702
6681
  if (propsSelection) {
6703
6682
  debug(`Selection from props ${JSON.stringify(propsSelection)}`);
6704
6683
  const normalizedSelection = normalizeSelection(propsSelection, fromSlateValue(slateEditor.children, blockTypeName));
@@ -6711,7 +6690,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6711
6690
  }), slateEditor.onChange());
6712
6691
  }
6713
6692
  }
6714
- }, [blockTypeName, editorActor, propsSelection, slateEditor]), syncRangeDecorations = react.useCallback((operation) => {
6693
+ }, [blockTypeName, editorActor, propsSelection, slateEditor]), syncRangeDecorations = React.useCallback((operation) => {
6715
6694
  if (rangeDecorations && rangeDecorations.length > 0) {
6716
6695
  const newSlateRanges = [];
6717
6696
  if (rangeDecorations.forEach((rangeDecorationItem) => {
@@ -6744,7 +6723,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6744
6723
  }
6745
6724
  setRangeDecorationsState((rangeDecorationState_0) => rangeDecorationState_0.length > 0 ? [] : rangeDecorationState_0);
6746
6725
  }, [portableTextEditor, rangeDecorations, schemaTypes, slateEditor]);
6747
- react.useEffect(() => {
6726
+ React.useEffect(() => {
6748
6727
  const onReady = editorActor.on("ready", () => {
6749
6728
  restoreSelectionFromProps();
6750
6729
  }), onInvalidValue = editorActor.on("invalid value", () => {
@@ -6755,21 +6734,21 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6755
6734
  return () => {
6756
6735
  onReady.unsubscribe(), onInvalidValue.unsubscribe(), onValueChanged.unsubscribe();
6757
6736
  };
6758
- }, [editorActor, restoreSelectionFromProps]), react.useEffect(() => {
6737
+ }, [editorActor, restoreSelectionFromProps]), React.useEffect(() => {
6759
6738
  propsSelection && !hasInvalidValue && restoreSelectionFromProps();
6760
6739
  }, [hasInvalidValue, propsSelection, restoreSelectionFromProps]);
6761
- const [syncedRangeDecorations, setSyncedRangeDecorations] = react.useState(!1);
6762
- react.useEffect(() => {
6740
+ const [syncedRangeDecorations, setSyncedRangeDecorations] = React.useState(!1);
6741
+ React.useEffect(() => {
6763
6742
  syncedRangeDecorations || (setSyncedRangeDecorations(!0), syncRangeDecorations());
6764
- }, [syncRangeDecorations, syncedRangeDecorations]), react.useEffect(() => {
6743
+ }, [syncRangeDecorations, syncedRangeDecorations]), React.useEffect(() => {
6765
6744
  isEqual__default.default(rangeDecorations, rangeDecorationsRef.current) || syncRangeDecorations(), rangeDecorationsRef.current = rangeDecorations;
6766
- }, [rangeDecorations, syncRangeDecorations]), react.useEffect(() => {
6745
+ }, [rangeDecorations, syncRangeDecorations]), React.useEffect(() => {
6767
6746
  const teardown = withSyncRangeDecorations(slateEditor, syncRangeDecorations);
6768
6747
  return () => teardown();
6769
6748
  }, [slateEditor, syncRangeDecorations]);
6770
- const handleCopy = react.useCallback((event) => {
6749
+ const handleCopy = React.useCallback((event) => {
6771
6750
  onCopy && onCopy(event) !== void 0 && event.preventDefault();
6772
- }, [onCopy]), handlePaste = react.useCallback((event_0) => {
6751
+ }, [onCopy]), handlePaste = React.useCallback((event_0) => {
6773
6752
  const value_0 = PortableTextEditor.getValue(portableTextEditor), path = toPortableTextRange(value_0, slateEditor.selection, schemaTypes)?.focus.path || [], onPasteResult = onPaste?.({
6774
6753
  event: event_0,
6775
6754
  value: value_0,
@@ -6794,7 +6773,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6794
6773
  },
6795
6774
  editor: slateEditor
6796
6775
  })), debug("No result from custom paste handler, pasting normally");
6797
- }, [editorActor, onPaste, portableTextEditor, schemaTypes, slateEditor]), handleOnFocus = react.useCallback((event_1) => {
6776
+ }, [editorActor, onPaste, portableTextEditor, schemaTypes, slateEditor]), handleOnFocus = React.useCallback((event_1) => {
6798
6777
  if (onFocus && onFocus(event_1), !event_1.isDefaultPrevented()) {
6799
6778
  const selection = PortableTextEditor.getSelection(portableTextEditor);
6800
6779
  selection === null && (slate.Transforms.select(slateEditor, slate.Editor.start(slateEditor, [])), slateEditor.onChange()), editorActor.send({
@@ -6807,20 +6786,20 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6807
6786
  selection
6808
6787
  });
6809
6788
  }
6810
- }, [editorActor, onFocus, portableTextEditor, slateEditor]), handleClick = react.useCallback((event_2) => {
6789
+ }, [editorActor, onFocus, portableTextEditor, slateEditor]), handleClick = React.useCallback((event_2) => {
6811
6790
  onClick && onClick(event_2);
6812
6791
  const focusBlockPath = slateEditor.selection ? slateEditor.selection.focus.path.slice(0, 1) : void 0, focusBlock = focusBlockPath ? slate.Node.descendant(slateEditor, focusBlockPath) : void 0, [_, lastNodePath] = slate.Node.last(slateEditor, []), lastBlockPath = lastNodePath.slice(0, 1), lastNodeFocused = focusBlockPath ? slate.Path.equals(lastBlockPath, focusBlockPath) : !1, lastBlockIsVoid = focusBlock ? !slateEditor.isTextBlock(focusBlock) : !1;
6813
6792
  slateEditor.selection && slate.Range.isCollapsed(slateEditor.selection) && lastNodeFocused && lastBlockIsVoid && (slate.Transforms.insertNodes(slateEditor, slateEditor.pteCreateTextBlock({
6814
6793
  decorators: []
6815
6794
  })), slateEditor.onChange());
6816
- }, [onClick, slateEditor]), handleOnBlur = react.useCallback((event_3) => {
6795
+ }, [onClick, slateEditor]), handleOnBlur = React.useCallback((event_3) => {
6817
6796
  onBlur && onBlur(event_3), event_3.isPropagationStopped() || editorActor.send({
6818
6797
  type: "blur",
6819
6798
  event: event_3
6820
6799
  });
6821
- }, [editorActor, onBlur]), handleOnBeforeInput = react.useCallback((event_4) => {
6800
+ }, [editorActor, onBlur]), handleOnBeforeInput = React.useCallback((event_4) => {
6822
6801
  onBeforeInput && onBeforeInput(event_4);
6823
- }, [onBeforeInput]), validateSelection = react.useCallback(() => {
6802
+ }, [onBeforeInput]), validateSelection = React.useCallback(() => {
6824
6803
  if (!slateEditor.selection)
6825
6804
  return;
6826
6805
  const root = slateReact.ReactEditor.findDocumentOrShadowRoot(slateEditor), {
@@ -6839,7 +6818,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6839
6818
  debug("Could not resolve selection, selecting top document"), slate.Transforms.deselect(slateEditor), slateEditor.children.length > 0 && slate.Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
6840
6819
  }
6841
6820
  }, [ref, slateEditor]);
6842
- react.useEffect(() => {
6821
+ React.useEffect(() => {
6843
6822
  if (editableElement) {
6844
6823
  const mutationObserver = new MutationObserver(validateSelection);
6845
6824
  return mutationObserver.observe(editableElement, {
@@ -6853,14 +6832,14 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6853
6832
  };
6854
6833
  }
6855
6834
  }, [validateSelection, editableElement]);
6856
- const handleKeyDown = react.useCallback((event_5) => {
6835
+ const handleKeyDown = React.useCallback((event_5) => {
6857
6836
  props.onKeyDown && props.onKeyDown(event_5), event_5.isDefaultPrevented() || slateEditor.pteWithHotKeys(event_5);
6858
- }, [props, slateEditor]), scrollSelectionIntoViewToSlate = react.useMemo(() => {
6837
+ }, [props, slateEditor]), scrollSelectionIntoViewToSlate = React.useMemo(() => {
6859
6838
  if (scrollSelectionIntoView !== void 0)
6860
6839
  return scrollSelectionIntoView === null ? noop__default.default : (_editor, domRange) => {
6861
6840
  scrollSelectionIntoView(portableTextEditor, domRange);
6862
6841
  };
6863
- }, [portableTextEditor, scrollSelectionIntoView]), decorate = react.useCallback(([, path_0]) => {
6842
+ }, [portableTextEditor, scrollSelectionIntoView]), decorate = React.useCallback(([, path_0]) => {
6864
6843
  if (isEqualToEmptyEditor(slateEditor.children, schemaTypes))
6865
6844
  return [{
6866
6845
  anchor: {
@@ -6887,7 +6866,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6887
6866
  }) || slate.Range.includes(item, path_0));
6888
6867
  return result_1.length > 0 ? result_1 : [];
6889
6868
  }, [slateEditor, schemaTypes, rangeDecorationState]);
6890
- return react.useEffect(() => {
6869
+ return React.useEffect(() => {
6891
6870
  ref.current = slateReact.ReactEditor.toDOMNode(slateEditor, slateEditor), setEditableElement(ref.current);
6892
6871
  }, [slateEditor, ref]), portableTextEditor ? hasInvalidValue ? null : /* @__PURE__ */ jsxRuntime.jsx(
6893
6872
  slateReact.Editable,
@@ -6912,45 +6891,55 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6912
6891
  ) : null;
6913
6892
  });
6914
6893
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
6915
- function useEditor(config) {
6916
- const $ = reactCompilerRuntime.c(21), t0 = config.keyGenerator ?? defaultKeyGenerator;
6917
- let t1;
6918
- $[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];
6894
+ const EditorContext = React__default.default.createContext(void 0);
6895
+ function EditorProvider(props) {
6896
+ const $ = reactCompilerRuntime.c(30), editor = useEditor(props.config), editorActor = editor._internal.editorActor, slateEditor = editor._internal.slateEditor, editable = editor.editable;
6897
+ let t0, t1;
6898
+ $[0] !== editor ? (t1 = new PortableTextEditor({
6899
+ editor
6900
+ }), $[0] = editor, $[1] = t1) : t1 = $[1], t0 = t1;
6901
+ const portableTextEditor = t0;
6919
6902
  let t2;
6920
- $[3] !== config.behaviors || $[4] !== config.initialValue || $[5] !== t0 || $[6] !== t1 ? (t2 = {
6921
- input: {
6922
- behaviors: config.behaviors,
6923
- keyGenerator: t0,
6924
- schema: t1,
6925
- value: config.initialValue
6926
- }
6927
- }, $[3] = config.behaviors, $[4] = config.initialValue, $[5] = t0, $[6] = t1, $[7] = t2) : t2 = $[7];
6928
- const editorActor = react$1.useActorRef(editorMachine, t2);
6903
+ $[2] !== portableTextEditor.change$ ? (t2 = (change) => {
6904
+ portableTextEditor.change$.next(change);
6905
+ }, $[2] = portableTextEditor.change$, $[3] = t2) : t2 = $[3];
6929
6906
  let t3;
6930
- $[8] !== editorActor ? (t3 = createSlateEditor({
6931
- editorActor
6932
- }), $[8] = editorActor, $[9] = t3) : t3 = $[9];
6933
- const slateEditor = t3, readOnly = react$1.useSelector(editorActor, _temp);
6934
- let t4, t5;
6935
- $[10] !== editorActor ? (t4 = (event) => {
6936
- editorActor.send(event);
6937
- }, t5 = (event_0, listener) => editorActor.on(event_0, listener), $[10] = editorActor, $[11] = t4, $[12] = t5) : (t4 = $[11], t5 = $[12]);
6907
+ $[4] !== editorActor || $[5] !== t2 ? (t3 = /* @__PURE__ */ jsxRuntime.jsx(RouteEventsToChanges, { editorActor, onChange: t2 }), $[4] = editorActor, $[5] = t2, $[6] = t3) : t3 = $[6];
6908
+ let t4;
6909
+ $[7] !== editable.getValue || $[8] !== editorActor || $[9] !== portableTextEditor || $[10] !== slateEditor.instance ? (t4 = /* @__PURE__ */ jsxRuntime.jsx(Synchronizer, { editorActor, getValue: editable.getValue, portableTextEditor, slateEditor: slateEditor.instance }), $[7] = editable.getValue, $[8] = editorActor, $[9] = portableTextEditor, $[10] = slateEditor.instance, $[11] = t4) : t4 = $[11];
6910
+ let t5;
6911
+ $[12] !== editorActor || $[13] !== props.children ? (t5 = /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorSelectionProvider, { editorActor, children: props.children }), $[12] = editorActor, $[13] = props.children, $[14] = t5) : t5 = $[14];
6938
6912
  let t6;
6939
- $[13] !== editorActor || $[14] !== slateEditor ? (t6 = {
6940
- editorActor,
6941
- slateEditor
6942
- }, $[13] = editorActor, $[14] = slateEditor, $[15] = t6) : t6 = $[15];
6913
+ $[15] !== portableTextEditor || $[16] !== t5 ? (t6 = /* @__PURE__ */ jsxRuntime.jsx(PortableTextEditorContext.Provider, { value: portableTextEditor, children: t5 }), $[15] = portableTextEditor, $[16] = t5, $[17] = t6) : t6 = $[17];
6943
6914
  let t7;
6944
- return $[16] !== readOnly || $[17] !== t4 || $[18] !== t5 || $[19] !== t6 ? (t7 = {
6945
- send: t4,
6946
- on: t5,
6947
- readOnly,
6948
- _internal: t6
6949
- }, $[16] = readOnly, $[17] = t4, $[18] = t5, $[19] = t6, $[20] = t7) : t7 = $[20], t7;
6915
+ $[18] !== slateEditor.initialValue || $[19] !== slateEditor.instance || $[20] !== t6 ? (t7 = /* @__PURE__ */ jsxRuntime.jsx(slateReact.Slate, { editor: slateEditor.instance, initialValue: slateEditor.initialValue, children: t6 }), $[18] = slateEditor.initialValue, $[19] = slateEditor.instance, $[20] = t6, $[21] = t7) : t7 = $[21];
6916
+ let t8;
6917
+ $[22] !== editorActor || $[23] !== t7 ? (t8 = /* @__PURE__ */ jsxRuntime.jsx(EditorActorContext.Provider, { value: editorActor, children: t7 }), $[22] = editorActor, $[23] = t7, $[24] = t8) : t8 = $[24];
6918
+ let t9;
6919
+ return $[25] !== editor || $[26] !== t3 || $[27] !== t4 || $[28] !== t8 ? (t9 = /* @__PURE__ */ jsxRuntime.jsxs(EditorContext.Provider, { value: editor, children: [
6920
+ t3,
6921
+ t4,
6922
+ t8
6923
+ ] }), $[25] = editor, $[26] = t3, $[27] = t4, $[28] = t8, $[29] = t9) : t9 = $[29], t9;
6924
+ }
6925
+ function useEditorContext() {
6926
+ const editor = React__default.default.useContext(EditorContext);
6927
+ if (!editor)
6928
+ throw new Error("No Editor set. Use EditorProvider to set one.");
6929
+ return editor;
6950
6930
  }
6951
- function _temp(s) {
6952
- return s.context.readOnly;
6931
+ function EditorEventListener(props) {
6932
+ const $ = reactCompilerRuntime.c(4), editor = useEditorContext(), on = useEffectEvent.useEffectEvent(props.on);
6933
+ let t0, t1;
6934
+ return $[0] !== editor || $[1] !== on ? (t0 = () => {
6935
+ const subscription = editor.on("*", on);
6936
+ return () => {
6937
+ subscription.unsubscribe();
6938
+ };
6939
+ }, t1 = [editor, on], $[0] = editor, $[1] = on, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), React.useEffect(t0, t1), null;
6953
6940
  }
6941
+ exports.EditorEventListener = EditorEventListener;
6942
+ exports.EditorProvider = EditorProvider;
6954
6943
  exports.PortableTextEditable = PortableTextEditable;
6955
6944
  exports.PortableTextEditor = PortableTextEditor;
6956
6945
  exports.coreBehavior = coreBehavior;
@@ -6962,6 +6951,7 @@ exports.defineSchema = defineSchema;
6962
6951
  exports.editorMachine = editorMachine;
6963
6952
  exports.keyGenerator = defaultKeyGenerator;
6964
6953
  exports.useEditor = useEditor;
6954
+ exports.useEditorContext = useEditorContext;
6965
6955
  exports.usePortableTextEditor = usePortableTextEditor;
6966
6956
  exports.usePortableTextEditorSelection = usePortableTextEditorSelection;
6967
6957
  //# sourceMappingURL=index.js.map