@portabletext/editor 1.1.12-canary.2 → 1.1.12-canary.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var jsxRuntime = require("react/jsx-runtime"), reactCompilerRuntime = require("react-compiler-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), types = require("@sanity/types"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), useEffectEvent = require("use-effect-event"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content");
3
+ var jsxRuntime = require("react/jsx-runtime"), isEqual = require("lodash/isEqual.js"), noop = require("lodash/noop.js"), react = require("react"), slate = require("slate"), slateReact = require("slate-react"), debug$m = require("debug"), types = require("@sanity/types"), reactCompilerRuntime = require("react-compiler-runtime"), styledComponents = require("styled-components"), uniq = require("lodash/uniq.js"), rxjs = require("rxjs"), xstate = require("xstate"), schema = require("@sanity/schema"), patches = require("@portabletext/patches"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), flatten = require("lodash/flatten.js"), isHotkeyEsm = require("is-hotkey-esm"), blockTools = require("@sanity/block-tools"), isPlainObject = require("lodash/isPlainObject.js"), throttle = require("lodash/throttle.js"), useEffectEvent = require("use-effect-event"), debounce = require("lodash/debounce.js"), content = require("@sanity/util/content");
4
4
  function _interopDefaultCompat(e) {
5
5
  return e && typeof e == "object" && "default" in e ? e : { default: e };
6
6
  }
@@ -779,6 +779,25 @@ function getFocusBlockObject(context) {
779
779
  path: focusBlock.path
780
780
  } : void 0;
781
781
  }
782
+ function getFocusChild(context) {
783
+ const focusBlock = getFocusTextBlock(context);
784
+ if (!focusBlock)
785
+ return;
786
+ const key = context.selection && types.isKeySegment(context.selection.focus.path[2]) ? context.selection.focus.path[2]._key : void 0, node = key ? focusBlock.node.children.find((span) => span._key === key) : void 0;
787
+ return node && key ? {
788
+ node,
789
+ path: [...focusBlock.path, "children", {
790
+ _key: key
791
+ }]
792
+ } : void 0;
793
+ }
794
+ function getFocusSpan(context) {
795
+ const focusChild = getFocusChild(context);
796
+ return focusChild && types.isPortableTextSpan(focusChild.node) ? {
797
+ node: focusChild.node,
798
+ path: focusChild.path
799
+ } : void 0;
800
+ }
782
801
  function getSelectionStartBlock(context) {
783
802
  const key = context.selection.backward ? types.isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : types.isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
784
803
  return node && key ? {
@@ -845,14 +864,7 @@ function getNextBlock(context) {
845
864
  function isEmptyTextBlock(block) {
846
865
  return block.children.length === 1 && block.children[0].text === "";
847
866
  }
848
- const softReturn = {
849
- on: "insert soft break",
850
- actions: [() => [{
851
- type: "insert text",
852
- text: `
853
- `
854
- }]]
855
- }, breakingVoidBlock = {
867
+ const breakingVoidBlock = {
856
868
  on: "insert break",
857
869
  guard: ({
858
870
  context
@@ -939,7 +951,50 @@ const softReturn = {
939
951
  }
940
952
  }
941
953
  }]]
942
- }, coreBehaviors = [softReturn, breakingVoidBlock, deletingEmptyTextBlockAfterBlockObject, deletingEmptyTextBlockBeforeBlockObject], debug$k = debugWithName("operationToPatches");
954
+ }, coreBlockObjectBehaviors = [breakingVoidBlock, deletingEmptyTextBlockAfterBlockObject, deletingEmptyTextBlockBeforeBlockObject], clearListOnBackspace = {
955
+ on: "delete backward",
956
+ guard: ({
957
+ context
958
+ }) => {
959
+ const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
960
+ return !selectionCollapsed || !focusTextBlock || !focusSpan ? !1 : focusTextBlock.node.children[0]._key === focusSpan.node._key && context.selection.focus.offset === 0 && focusTextBlock.node.level === 1 ? {
961
+ focusTextBlock
962
+ } : !1;
963
+ },
964
+ actions: [(_, {
965
+ focusTextBlock
966
+ }) => [{
967
+ type: "unset block",
968
+ props: ["listItem", "level"],
969
+ paths: [focusTextBlock.path]
970
+ }]]
971
+ }, unindentListOnBackspace = {
972
+ on: "delete backward",
973
+ guard: ({
974
+ context
975
+ }) => {
976
+ const selectionCollapsed = selectionIsCollapsed(context), focusTextBlock = getFocusTextBlock(context), focusSpan = getFocusSpan(context);
977
+ return !selectionCollapsed || !focusTextBlock || !focusSpan ? !1 : focusTextBlock.node.children[0]._key === focusSpan.node._key && context.selection.focus.offset === 0 && focusTextBlock.node.level !== void 0 && focusTextBlock.node.level > 1 ? {
978
+ focusTextBlock,
979
+ level: focusTextBlock.node.level - 1
980
+ } : !1;
981
+ },
982
+ actions: [(_, {
983
+ focusTextBlock,
984
+ level
985
+ }) => [{
986
+ type: "set block",
987
+ level,
988
+ paths: [focusTextBlock.path]
989
+ }]]
990
+ }, coreListBehaviors = [clearListOnBackspace, unindentListOnBackspace], softReturn = {
991
+ on: "insert soft break",
992
+ actions: [() => [{
993
+ type: "insert text",
994
+ text: `
995
+ `
996
+ }]]
997
+ }, coreBehaviors = [softReturn, ...coreBlockObjectBehaviors, ...coreListBehaviors], debug$k = debugWithName("operationToPatches");
943
998
  function createOperationToPatches(types2) {
944
999
  const textBlockName = types2.block.name;
945
1000
  function insertTextPatch(editor, operation, beforeValue) {
@@ -4684,28 +4739,20 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
4684
4739
  }
4685
4740
  const debug$4 = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$1 = debug$4.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
4686
4741
  function Synchronizer(props) {
4687
- const $ = reactCompilerRuntime.c(36), portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), {
4742
+ const portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), {
4688
4743
  editorActor,
4689
4744
  getValue,
4690
4745
  onChange,
4691
4746
  value
4692
- } = props;
4693
- let t0;
4694
- $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = [], $[0] = t0) : t0 = $[0];
4695
- const pendingPatches = react.useRef(t0);
4696
- let t1;
4697
- $[1] !== editorActor || $[2] !== portableTextEditor || $[3] !== readOnly ? (t1 = {
4747
+ } = props, pendingPatches = react.useRef([]), syncValue = useSyncValue({
4698
4748
  editorActor,
4699
4749
  portableTextEditor,
4700
4750
  readOnly
4701
- }, $[1] = editorActor, $[2] = portableTextEditor, $[3] = readOnly, $[4] = t1) : t1 = $[4];
4702
- const syncValue = useSyncValue(t1), slateEditor = slateReact.useSlate();
4703
- let t2, t3;
4704
- $[5] !== slateEditor ? (t2 = () => {
4751
+ }), slateEditor = slateReact.useSlate();
4752
+ react.useEffect(() => {
4705
4753
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !1);
4706
- }, t3 = [slateEditor], $[5] = slateEditor, $[6] = t2, $[7] = t3) : (t2 = $[6], t3 = $[7]), react.useEffect(t2, t3);
4707
- let t4;
4708
- $[8] !== getValue || $[9] !== editorActor || $[10] !== slateEditor ? (t4 = () => {
4754
+ }, [slateEditor]);
4755
+ const onFlushPendingPatches = react.useCallback(() => {
4709
4756
  if (pendingPatches.current.length > 0) {
4710
4757
  debug$4("Flushing pending patches"), debugVerbose$1 && debug$4(`Patches:
4711
4758
  ${JSON.stringify(pendingPatches.current, null, 2)}`);
@@ -4717,68 +4764,61 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
4717
4764
  }), pendingPatches.current = [];
4718
4765
  }
4719
4766
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !1);
4720
- }, $[8] = getValue, $[9] = editorActor, $[10] = slateEditor, $[11] = t4) : t4 = $[11];
4721
- const onFlushPendingPatches = t4;
4722
- let t5, t6;
4723
- $[12] !== onFlushPendingPatches ? (t5 = () => () => {
4767
+ }, [editorActor, slateEditor, getValue]), onFlushPendingPatchesThrottled = react.useMemo(() => throttle__default.default(() => {
4768
+ if (slate.Editor.isNormalizing(slateEditor)) {
4769
+ onFlushPendingPatches();
4770
+ return;
4771
+ }
4772
+ onFlushPendingPatchesThrottled();
4773
+ }, FLUSH_PATCHES_THROTTLED_MS, {
4774
+ leading: !1,
4775
+ trailing: !0
4776
+ }), [onFlushPendingPatches, slateEditor]);
4777
+ react.useEffect(() => () => {
4724
4778
  onFlushPendingPatches();
4725
- }, t6 = [onFlushPendingPatches], $[12] = onFlushPendingPatches, $[13] = t5, $[14] = t6) : (t5 = $[13], t6 = $[14]), react.useEffect(t5, t6);
4726
- let t7;
4727
- $[15] !== onChange ? (t7 = (change) => onChange(change), $[15] = onChange, $[16] = t7) : t7 = $[16];
4728
- const handleChange = useEffectEvent.useEffectEvent(t7);
4729
- let t8, t9;
4730
- $[17] !== slateEditor || $[18] !== onFlushPendingPatches || $[19] !== editorActor || $[20] !== handleChange ? (t8 = () => {
4731
- const onFlushPendingPatchesThrottled = throttle__default.default(() => {
4732
- if (slate.Editor.isNormalizing(slateEditor)) {
4733
- onFlushPendingPatches();
4734
- return;
4735
- }
4736
- onFlushPendingPatchesThrottled();
4737
- }, FLUSH_PATCHES_THROTTLED_MS, {
4738
- leading: !1,
4739
- trailing: !0
4740
- });
4779
+ }, [onFlushPendingPatches]);
4780
+ const handleChange = useEffectEvent.useEffectEvent((change) => onChange(change));
4781
+ react.useEffect(() => {
4741
4782
  debug$4("Subscribing to editor changes");
4742
4783
  const sub = editorActor.on("*", (event) => {
4743
- bb18: switch (event.type) {
4744
- case "patch": {
4784
+ switch (event.type) {
4785
+ case "patch":
4745
4786
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !0), pendingPatches.current.push(event.patch), onFlushPendingPatchesThrottled(), handleChange(event);
4746
- break bb18;
4747
- }
4787
+ break;
4748
4788
  case "loading": {
4749
4789
  handleChange({
4750
4790
  type: "loading",
4751
4791
  isLoading: !0
4752
4792
  });
4753
- break bb18;
4793
+ break;
4754
4794
  }
4755
4795
  case "done loading": {
4756
4796
  handleChange({
4757
4797
  type: "loading",
4758
4798
  isLoading: !1
4759
4799
  });
4760
- break bb18;
4800
+ break;
4761
4801
  }
4762
4802
  case "offline": {
4763
4803
  handleChange({
4764
4804
  type: "connection",
4765
4805
  value: "offline"
4766
4806
  });
4767
- break bb18;
4807
+ break;
4768
4808
  }
4769
4809
  case "online": {
4770
4810
  handleChange({
4771
4811
  type: "connection",
4772
4812
  value: "online"
4773
4813
  });
4774
- break bb18;
4814
+ break;
4775
4815
  }
4776
4816
  case "value changed": {
4777
4817
  handleChange({
4778
4818
  type: "value",
4779
4819
  value: event.value
4780
4820
  });
4781
- break bb18;
4821
+ break;
4782
4822
  }
4783
4823
  case "invalid value": {
4784
4824
  handleChange({
@@ -4786,14 +4826,14 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
4786
4826
  resolution: event.resolution,
4787
4827
  value: event.value
4788
4828
  });
4789
- break bb18;
4829
+ break;
4790
4830
  }
4791
4831
  case "error": {
4792
4832
  handleChange({
4793
4833
  ...event,
4794
4834
  level: "warning"
4795
4835
  });
4796
- break bb18;
4836
+ break;
4797
4837
  }
4798
4838
  default:
4799
4839
  handleChange(event);
@@ -4802,28 +4842,24 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
4802
4842
  return () => {
4803
4843
  debug$4("Unsubscribing to changes"), sub.unsubscribe();
4804
4844
  };
4805
- }, t9 = [editorActor, handleChange, onFlushPendingPatches, slateEditor], $[17] = slateEditor, $[18] = onFlushPendingPatches, $[19] = editorActor, $[20] = handleChange, $[21] = t8, $[22] = t9) : (t8 = $[21], t9 = $[22]), react.useEffect(t8, t9);
4806
- let t10;
4807
- $[23] !== syncValue || $[24] !== value ? (t10 = () => {
4845
+ }, [handleChange, editorActor, onFlushPendingPatchesThrottled, slateEditor]);
4846
+ const handleOnline = react.useCallback(() => {
4808
4847
  debug$4("Editor is online, syncing from props.value"), syncValue(value);
4809
- }, $[23] = syncValue, $[24] = value, $[25] = t10) : t10 = $[25];
4810
- const handleOnline = t10;
4811
- let t11, t12;
4812
- $[26] !== editorActor || $[27] !== portableTextEditor.props.patches$ || $[28] !== handleOnline ? (t11 = () => {
4848
+ }, [syncValue, value]);
4849
+ react.useEffect(() => {
4813
4850
  const subscription = editorActor.on("online", () => {
4814
4851
  portableTextEditor.props.patches$ && handleOnline();
4815
4852
  });
4816
4853
  return () => {
4817
4854
  subscription.unsubscribe();
4818
4855
  };
4819
- }, t12 = [handleOnline, editorActor, portableTextEditor.props.patches$], $[26] = editorActor, $[27] = portableTextEditor.props.patches$, $[28] = handleOnline, $[29] = t11, $[30] = t12) : (t11 = $[29], t12 = $[30]), react.useEffect(t11, t12);
4856
+ }, [handleOnline, editorActor, portableTextEditor.props.patches$]);
4820
4857
  const isInitialValueFromProps = react.useRef(!0);
4821
- let t13, t14;
4822
- return $[31] !== syncValue || $[32] !== value || $[33] !== editorActor ? (t13 = () => {
4858
+ return react.useEffect(() => {
4823
4859
  debug$4("Value from props changed, syncing new value"), syncValue(value), isInitialValueFromProps.current && (editorActor.send({
4824
4860
  type: "ready"
4825
4861
  }), isInitialValueFromProps.current = !1);
4826
- }, t14 = [editorActor, syncValue, value], $[31] = syncValue, $[32] = value, $[33] = editorActor, $[34] = t13, $[35] = t14) : (t13 = $[34], t14 = $[35]), react.useEffect(t13, t14), null;
4862
+ }, [editorActor, syncValue, value]), null;
4827
4863
  }
4828
4864
  Synchronizer.displayName = "Synchronizer";
4829
4865
  const EditorActorContext = react.createContext({}), insertBreakActionImplementation = ({
@@ -5647,14 +5683,6 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5647
5683
  return react.useMemo(() => /* @__PURE__ */ jsxRuntime.jsx("span", { ...attributes, ref: spanRef, children: content2 }, leaf._key), [leaf, attributes, content2]);
5648
5684
  };
5649
5685
  Leaf.displayName = "Leaf";
5650
- function withSyncRangeDecorations(slateEditor, syncRangeDecorations) {
5651
- const originalApply = slateEditor.apply;
5652
- return slateEditor.apply = (op) => {
5653
- originalApply(op), op.type !== "set_selection" && syncRangeDecorations(op);
5654
- }, () => {
5655
- slateEditor.apply = originalApply;
5656
- };
5657
- }
5658
5686
  const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5659
5687
  position: "absolute",
5660
5688
  userSelect: "none",
@@ -5662,9 +5690,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5662
5690
  left: 0,
5663
5691
  right: 0
5664
5692
  }, PortableTextEditable = react.forwardRef(function(props, forwardedRef) {
5665
- const $ = reactCompilerRuntime.c(122);
5666
- let rangeDecorations, hotkeys, spellCheck, renderBlock, renderChild, renderListItem, renderStyle, renderAnnotation, renderDecorator, renderPlaceholder, propsSelection, onCopy, onPaste, onFocus, onClick, onBlur, onBeforeInput, scrollSelectionIntoView, restProps;
5667
- $[0] !== props ? ({
5693
+ const {
5668
5694
  hotkeys,
5669
5695
  onBlur,
5670
5696
  onFocus,
@@ -5684,30 +5710,13 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5684
5710
  scrollSelectionIntoView,
5685
5711
  spellCheck,
5686
5712
  ...restProps
5687
- } = props, $[0] = props, $[1] = rangeDecorations, $[2] = hotkeys, $[3] = spellCheck, $[4] = renderBlock, $[5] = renderChild, $[6] = renderListItem, $[7] = renderStyle, $[8] = renderAnnotation, $[9] = renderDecorator, $[10] = renderPlaceholder, $[11] = propsSelection, $[12] = onCopy, $[13] = onPaste, $[14] = onFocus, $[15] = onClick, $[16] = onBlur, $[17] = onBeforeInput, $[18] = scrollSelectionIntoView, $[19] = restProps) : (rangeDecorations = $[1], hotkeys = $[2], spellCheck = $[3], renderBlock = $[4], renderChild = $[5], renderListItem = $[6], renderStyle = $[7], renderAnnotation = $[8], renderDecorator = $[9], renderPlaceholder = $[10], propsSelection = $[11], onCopy = $[12], onPaste = $[13], onFocus = $[14], onClick = $[15], onBlur = $[16], onBeforeInput = $[17], scrollSelectionIntoView = $[18], restProps = $[19]);
5688
- const portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), ref = react.useRef(null), [editableElement, setEditableElement] = react.useState(null), [hasInvalidValue, setHasInvalidValue] = react.useState(!1);
5689
- let t0;
5690
- $[20] === Symbol.for("react.memo_cache_sentinel") ? (t0 = [], $[20] = t0) : t0 = $[20];
5691
- const [rangeDecorationState, setRangeDecorationsState] = react.useState(t0);
5692
- let t1;
5693
- $[21] === Symbol.for("react.memo_cache_sentinel") ? (t1 = () => ref.current, $[21] = t1) : t1 = $[21], react.useImperativeHandle(forwardedRef, t1);
5713
+ } = props, portableTextEditor = usePortableTextEditor(), readOnly = usePortableTextEditorReadOnlyStatus(), ref = react.useRef(null), [editableElement, setEditableElement] = react.useState(null), [hasInvalidValue, setHasInvalidValue] = react.useState(!1), [rangeDecorationState, setRangeDecorationsState] = react.useState([]);
5714
+ react.useImperativeHandle(forwardedRef, () => ref.current);
5694
5715
  const rangeDecorationsRef = react.useRef(rangeDecorations), editorActor = react.useContext(EditorActorContext), {
5695
5716
  schemaTypes
5696
- } = portableTextEditor, slateEditor = slateReact.useSlate(), blockTypeName = schemaTypes.block.name;
5697
- bb0: {
5698
- const withInsertData = createWithInsertData(editorActor, schemaTypes);
5699
- if (readOnly) {
5700
- debug("Editable is in read only mode"), withInsertData(slateEditor);
5701
- break bb0;
5702
- }
5703
- const withHotKeys = createWithHotkeys(portableTextEditor, hotkeys);
5704
- debug("Editable is in edit mode"), withInsertData(withHotKeys(slateEditor));
5705
- }
5706
- let t3;
5707
- $[22] !== readOnly || $[23] !== renderBlock || $[24] !== renderChild || $[25] !== renderListItem || $[26] !== renderStyle || $[27] !== schemaTypes || $[28] !== spellCheck ? (t3 = (eProps) => /* @__PURE__ */ jsxRuntime.jsx(Element, { ...eProps, readOnly, renderBlock, renderChild, renderListItem, renderStyle, schemaTypes, spellCheck }), $[22] = readOnly, $[23] = renderBlock, $[24] = renderChild, $[25] = renderListItem, $[26] = renderStyle, $[27] = schemaTypes, $[28] = spellCheck, $[29] = t3) : t3 = $[29];
5708
- const renderElement = t3;
5709
- let t4;
5710
- $[30] !== editorActor || $[31] !== schemaTypes || $[32] !== renderAnnotation || $[33] !== renderChild || $[34] !== renderDecorator || $[35] !== readOnly || $[36] !== renderPlaceholder ? (t4 = (lProps) => {
5717
+ } = portableTextEditor, slateEditor = slateReact.useSlate(), blockTypeName = schemaTypes.block.name, withInsertData = react.useMemo(() => createWithInsertData(editorActor, schemaTypes), [editorActor, schemaTypes]), withHotKeys = react.useMemo(() => createWithHotkeys(portableTextEditor, hotkeys), [hotkeys, portableTextEditor]);
5718
+ react.useMemo(() => readOnly ? (debug("Editable is in read only mode"), withInsertData(slateEditor)) : (debug("Editable is in edit mode"), withInsertData(withHotKeys(slateEditor))), [readOnly, slateEditor, withHotKeys, withInsertData]);
5719
+ 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) => {
5711
5720
  if (lProps.leaf._type === "span") {
5712
5721
  let rendered = /* @__PURE__ */ jsxRuntime.jsx(Leaf, { ...lProps, editorActor, schemaTypes, renderAnnotation, renderChild, renderDecorator, readOnly });
5713
5722
  if (renderPlaceholder && lProps.leaf.placeholder && lProps.text.text === "")
@@ -5721,10 +5730,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5721
5730
  })), rendered;
5722
5731
  }
5723
5732
  return lProps.children;
5724
- }, $[30] = editorActor, $[31] = schemaTypes, $[32] = renderAnnotation, $[33] = renderChild, $[34] = renderDecorator, $[35] = readOnly, $[36] = renderPlaceholder, $[37] = t4) : t4 = $[37];
5725
- const renderLeaf = t4;
5726
- let t5;
5727
- $[38] !== propsSelection || $[39] !== slateEditor || $[40] !== blockTypeName || $[41] !== editorActor ? (t5 = () => {
5733
+ }, [editorActor, readOnly, renderAnnotation, renderChild, renderDecorator, renderPlaceholder, schemaTypes]), restoreSelectionFromProps = react.useCallback(() => {
5728
5734
  if (propsSelection) {
5729
5735
  debug(`Selection from props ${JSON.stringify(propsSelection)}`);
5730
5736
  const normalizedSelection = normalizeSelection(propsSelection, fromSlateValue(slateEditor.children, blockTypeName));
@@ -5737,10 +5743,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5737
5743
  }), slateEditor.onChange());
5738
5744
  }
5739
5745
  }
5740
- }, $[38] = propsSelection, $[39] = slateEditor, $[40] = blockTypeName, $[41] = editorActor, $[42] = t5) : t5 = $[42];
5741
- const restoreSelectionFromProps = t5;
5742
- let t6;
5743
- $[43] !== rangeDecorations || $[44] !== slateEditor || $[45] !== portableTextEditor || $[46] !== schemaTypes ? (t6 = (operation) => {
5746
+ }, [blockTypeName, editorActor, propsSelection, slateEditor]), syncRangeDecorations = react.useCallback((operation) => {
5744
5747
  if (rangeDecorations && rangeDecorations.length > 0) {
5745
5748
  const newSlateRanges = [];
5746
5749
  if (rangeDecorations.forEach((rangeDecorationItem) => {
@@ -5772,10 +5775,8 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5772
5775
  }
5773
5776
  }
5774
5777
  setRangeDecorationsState((rangeDecorationState_0) => rangeDecorationState_0.length > 0 ? [] : rangeDecorationState_0);
5775
- }, $[43] = rangeDecorations, $[44] = slateEditor, $[45] = portableTextEditor, $[46] = schemaTypes, $[47] = t6) : t6 = $[47];
5776
- const syncRangeDecorations = t6;
5777
- let t7, t8;
5778
- $[48] !== editorActor || $[49] !== restoreSelectionFromProps ? (t7 = () => {
5778
+ }, [portableTextEditor, rangeDecorations, schemaTypes, slateEditor]);
5779
+ react.useEffect(() => {
5779
5780
  const onReady = editorActor.on("ready", () => {
5780
5781
  restoreSelectionFromProps();
5781
5782
  }), onInvalidValue = editorActor.on("invalid value", () => {
@@ -5786,32 +5787,22 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5786
5787
  return () => {
5787
5788
  onReady.unsubscribe(), onInvalidValue.unsubscribe(), onValueChanged.unsubscribe();
5788
5789
  };
5789
- }, t8 = [editorActor, restoreSelectionFromProps], $[48] = editorActor, $[49] = restoreSelectionFromProps, $[50] = t7, $[51] = t8) : (t7 = $[50], t8 = $[51]), react.useEffect(t7, t8);
5790
- let t9, t10;
5791
- $[52] !== propsSelection || $[53] !== hasInvalidValue || $[54] !== restoreSelectionFromProps ? (t9 = () => {
5790
+ }, [editorActor, restoreSelectionFromProps]), react.useEffect(() => {
5792
5791
  propsSelection && !hasInvalidValue && restoreSelectionFromProps();
5793
- }, t10 = [hasInvalidValue, propsSelection, restoreSelectionFromProps], $[52] = propsSelection, $[53] = hasInvalidValue, $[54] = restoreSelectionFromProps, $[55] = t9, $[56] = t10) : (t9 = $[55], t10 = $[56]), react.useEffect(t9, t10);
5794
- const [syncedRangeDecorations, setSyncedRangeDecorations] = react.useState(!1);
5795
- let t11, t12;
5796
- $[57] !== syncedRangeDecorations || $[58] !== syncRangeDecorations ? (t11 = () => {
5792
+ }, [hasInvalidValue, propsSelection, restoreSelectionFromProps]);
5793
+ const originalApply = react.useMemo(() => slateEditor.apply, [slateEditor]), [syncedRangeDecorations, setSyncedRangeDecorations] = react.useState(!1);
5794
+ react.useEffect(() => {
5797
5795
  syncedRangeDecorations || (setSyncedRangeDecorations(!0), syncRangeDecorations());
5798
- }, t12 = [syncRangeDecorations, syncedRangeDecorations], $[57] = syncedRangeDecorations, $[58] = syncRangeDecorations, $[59] = t11, $[60] = t12) : (t11 = $[59], t12 = $[60]), react.useEffect(t11, t12);
5799
- let t13, t14;
5800
- $[61] !== rangeDecorations || $[62] !== syncRangeDecorations ? (t13 = () => {
5796
+ }, [syncRangeDecorations, syncedRangeDecorations]), react.useEffect(() => {
5801
5797
  isEqual__default.default(rangeDecorations, rangeDecorationsRef.current) || syncRangeDecorations(), rangeDecorationsRef.current = rangeDecorations;
5802
- }, t14 = [rangeDecorations, syncRangeDecorations], $[61] = rangeDecorations, $[62] = syncRangeDecorations, $[63] = t13, $[64] = t14) : (t13 = $[63], t14 = $[64]), react.useEffect(t13, t14);
5803
- let t15, t16;
5804
- $[65] !== slateEditor || $[66] !== syncRangeDecorations ? (t15 = () => {
5805
- const teardown = withSyncRangeDecorations(slateEditor, syncRangeDecorations);
5806
- return () => teardown();
5807
- }, t16 = [slateEditor, syncRangeDecorations], $[65] = slateEditor, $[66] = syncRangeDecorations, $[67] = t15, $[68] = t16) : (t15 = $[67], t16 = $[68]), react.useEffect(t15, t16);
5808
- let t17;
5809
- $[69] !== onCopy ? (t17 = (event) => {
5798
+ }, [rangeDecorations, syncRangeDecorations]), react.useEffect(() => (slateEditor.apply = (op) => {
5799
+ originalApply(op), op.type !== "set_selection" && syncRangeDecorations(op);
5800
+ }, () => {
5801
+ slateEditor.apply = originalApply;
5802
+ }), [originalApply, slateEditor, syncRangeDecorations]);
5803
+ const handleCopy = react.useCallback((event) => {
5810
5804
  onCopy && onCopy(event) !== void 0 && event.preventDefault();
5811
- }, $[69] = onCopy, $[70] = t17) : t17 = $[70];
5812
- const handleCopy = t17;
5813
- let t18;
5814
- $[71] !== slateEditor || $[72] !== onPaste || $[73] !== portableTextEditor || $[74] !== schemaTypes || $[75] !== editorActor ? (t18 = (event_0) => {
5805
+ }, [onCopy]), handlePaste = react.useCallback((event_0) => {
5815
5806
  if (event_0.preventDefault(), !slateEditor.selection)
5816
5807
  return;
5817
5808
  if (!onPaste) {
@@ -5835,10 +5826,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5835
5826
  type: "done loading"
5836
5827
  });
5837
5828
  }));
5838
- }, $[71] = slateEditor, $[72] = onPaste, $[73] = portableTextEditor, $[74] = schemaTypes, $[75] = editorActor, $[76] = t18) : t18 = $[76];
5839
- const handlePaste = t18;
5840
- let t19;
5841
- $[77] !== onFocus || $[78] !== portableTextEditor || $[79] !== slateEditor || $[80] !== editorActor ? (t19 = (event_1) => {
5829
+ }, [editorActor, onPaste, portableTextEditor, schemaTypes, slateEditor]), handleOnFocus = react.useCallback((event_1) => {
5842
5830
  if (onFocus && onFocus(event_1), !event_1.isDefaultPrevented()) {
5843
5831
  const selection = PortableTextEditor.getSelection(portableTextEditor);
5844
5832
  selection === null && (slate.Transforms.select(slateEditor, slate.Editor.start(slateEditor, [])), slateEditor.onChange()), editorActor.send({
@@ -5851,10 +5839,7 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5851
5839
  selection
5852
5840
  });
5853
5841
  }
5854
- }, $[77] = onFocus, $[78] = portableTextEditor, $[79] = slateEditor, $[80] = editorActor, $[81] = t19) : t19 = $[81];
5855
- const handleOnFocus = t19;
5856
- let t20;
5857
- $[82] !== onClick || $[83] !== slateEditor ? (t20 = (event_2) => {
5842
+ }, [editorActor, onFocus, portableTextEditor, slateEditor]), handleClick = react.useCallback((event_2) => {
5858
5843
  if (onClick && onClick(event_2), slateEditor.selection && event_2.target === event_2.currentTarget) {
5859
5844
  const [lastBlock, path_0] = slate.Node.last(slateEditor, []), focusPath = slateEditor.selection.focus.path.slice(0, 1), lastPath = path_0.slice(0, 1);
5860
5845
  if (slate.Path.equals(focusPath, lastPath)) {
@@ -5864,23 +5849,33 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5864
5849
  })), slateEditor.onChange());
5865
5850
  }
5866
5851
  }
5867
- }, $[82] = onClick, $[83] = slateEditor, $[84] = t20) : t20 = $[84];
5868
- const handleClick = t20;
5869
- let t21;
5870
- $[85] !== onBlur || $[86] !== editorActor ? (t21 = (event_3) => {
5852
+ }, [onClick, slateEditor]), handleOnBlur = react.useCallback((event_3) => {
5871
5853
  onBlur && onBlur(event_3), event_3.isPropagationStopped() || editorActor.send({
5872
5854
  type: "blur",
5873
5855
  event: event_3
5874
5856
  });
5875
- }, $[85] = onBlur, $[86] = editorActor, $[87] = t21) : t21 = $[87];
5876
- const handleOnBlur = t21;
5877
- let t22;
5878
- $[88] !== onBeforeInput ? (t22 = (event_4) => {
5857
+ }, [editorActor, onBlur]), handleOnBeforeInput = react.useCallback((event_4) => {
5879
5858
  onBeforeInput && onBeforeInput(event_4);
5880
- }, $[88] = onBeforeInput, $[89] = t22) : t22 = $[89];
5881
- const handleOnBeforeInput = t22, validateSelection = useValidateSelection(ref, slateEditor);
5882
- let t23, t24;
5883
- $[90] !== editableElement || $[91] !== validateSelection ? (t23 = () => {
5859
+ }, [onBeforeInput]), validateSelection = react.useCallback(() => {
5860
+ if (!slateEditor.selection)
5861
+ return;
5862
+ const root = slateReact.ReactEditor.findDocumentOrShadowRoot(slateEditor), {
5863
+ activeElement
5864
+ } = root;
5865
+ if (ref.current !== activeElement)
5866
+ return;
5867
+ const domSelection = slateReact.ReactEditor.getWindow(slateEditor).getSelection();
5868
+ if (!domSelection || domSelection.rangeCount === 0)
5869
+ return;
5870
+ const existingDOMRange = domSelection.getRangeAt(0);
5871
+ try {
5872
+ const newDOMRange = slateReact.ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
5873
+ (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
5874
+ } catch {
5875
+ debug("Could not resolve selection, selecting top document"), slate.Transforms.deselect(slateEditor), slateEditor.children.length > 0 && slate.Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
5876
+ }
5877
+ }, [ref, slateEditor]);
5878
+ react.useEffect(() => {
5884
5879
  if (editableElement) {
5885
5880
  const mutationObserver = new MutationObserver(validateSelection);
5886
5881
  return mutationObserver.observe(editableElement, {
@@ -5893,31 +5888,15 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5893
5888
  mutationObserver.disconnect();
5894
5889
  };
5895
5890
  }
5896
- }, t24 = [validateSelection, editableElement], $[90] = editableElement, $[91] = validateSelection, $[92] = t23, $[93] = t24) : (t23 = $[92], t24 = $[93]), react.useEffect(t23, t24);
5897
- let t25;
5898
- $[94] !== props || $[95] !== slateEditor ? (t25 = (event_5) => {
5891
+ }, [validateSelection, editableElement]);
5892
+ const handleKeyDown = react.useCallback((event_5) => {
5899
5893
  props.onKeyDown && props.onKeyDown(event_5), event_5.isDefaultPrevented() || slateEditor.pteWithHotKeys(event_5);
5900
- }, $[94] = props, $[95] = slateEditor, $[96] = t25) : t25 = $[96];
5901
- const handleKeyDown = t25;
5902
- let t26;
5903
- bb1: {
5904
- if (scrollSelectionIntoView === void 0) {
5905
- t26 = void 0;
5906
- break bb1;
5907
- }
5908
- if (scrollSelectionIntoView === null) {
5909
- t26 = noop__default.default;
5910
- break bb1;
5911
- }
5912
- let t272;
5913
- $[97] !== scrollSelectionIntoView || $[98] !== portableTextEditor ? (t272 = (_editor, domRange) => {
5914
- scrollSelectionIntoView(portableTextEditor, domRange);
5915
- }, $[97] = scrollSelectionIntoView, $[98] = portableTextEditor, $[99] = t272) : t272 = $[99], t26 = t272;
5916
- }
5917
- const scrollSelectionIntoViewToSlate = t26;
5918
- let t27;
5919
- $[100] !== slateEditor.children || $[101] !== schemaTypes || $[102] !== rangeDecorationState ? (t27 = (t282) => {
5920
- const [, path_1] = t282;
5894
+ }, [props, slateEditor]), scrollSelectionIntoViewToSlate = react.useMemo(() => {
5895
+ if (scrollSelectionIntoView !== void 0)
5896
+ return scrollSelectionIntoView === null ? noop__default.default : (_editor, domRange) => {
5897
+ scrollSelectionIntoView(portableTextEditor, domRange);
5898
+ };
5899
+ }, [portableTextEditor, scrollSelectionIntoView]), decorate = react.useCallback(([, path_1]) => {
5921
5900
  if (isEqualToEmptyEditor(slateEditor.children, schemaTypes))
5922
5901
  return [{
5923
5902
  anchor: {
@@ -5943,38 +5922,32 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
5943
5922
  }
5944
5923
  }) || slate.Range.includes(item, path_1));
5945
5924
  return result_1.length > 0 ? result_1 : [];
5946
- }, $[100] = slateEditor.children, $[101] = schemaTypes, $[102] = rangeDecorationState, $[103] = t27) : t27 = $[103];
5947
- const decorate = t27;
5948
- let t28, t29;
5949
- if ($[104] !== slateEditor ? (t28 = () => {
5925
+ }, [slateEditor, schemaTypes, rangeDecorationState]);
5926
+ return react.useEffect(() => {
5950
5927
  ref.current = slateReact.ReactEditor.toDOMNode(slateEditor, slateEditor), setEditableElement(ref.current);
5951
- }, t29 = [slateEditor, ref], $[104] = slateEditor, $[105] = t28, $[106] = t29) : (t28 = $[105], t29 = $[106]), react.useEffect(t28, t29), !portableTextEditor)
5952
- return null;
5953
- let t30;
5954
- return $[107] !== hasInvalidValue || $[108] !== restProps || $[109] !== decorate || $[110] !== handleOnBlur || $[111] !== handleCopy || $[112] !== handleClick || $[113] !== handleOnBeforeInput || $[114] !== handleOnFocus || $[115] !== handleKeyDown || $[116] !== handlePaste || $[117] !== readOnly || $[118] !== renderElement || $[119] !== renderLeaf || $[120] !== scrollSelectionIntoViewToSlate ? (t30 = hasInvalidValue ? null : /* @__PURE__ */ jsxRuntime.jsx(slateReact.Editable, { ...restProps, autoFocus: !1, className: restProps.className || "pt-editable", decorate, onBlur: handleOnBlur, onCopy: handleCopy, onClick: handleClick, onDOMBeforeInput: handleOnBeforeInput, onFocus: handleOnFocus, onKeyDown: handleKeyDown, onPaste: handlePaste, readOnly, renderPlaceholder: void 0, renderElement, renderLeaf, scrollSelectionIntoView: scrollSelectionIntoViewToSlate }), $[107] = hasInvalidValue, $[108] = restProps, $[109] = decorate, $[110] = handleOnBlur, $[111] = handleCopy, $[112] = handleClick, $[113] = handleOnBeforeInput, $[114] = handleOnFocus, $[115] = handleKeyDown, $[116] = handlePaste, $[117] = readOnly, $[118] = renderElement, $[119] = renderLeaf, $[120] = scrollSelectionIntoViewToSlate, $[121] = t30) : t30 = $[121], t30;
5928
+ }, [slateEditor, ref]), portableTextEditor ? hasInvalidValue ? null : /* @__PURE__ */ jsxRuntime.jsx(
5929
+ slateReact.Editable,
5930
+ {
5931
+ ...restProps,
5932
+ autoFocus: !1,
5933
+ className: restProps.className || "pt-editable",
5934
+ decorate,
5935
+ onBlur: handleOnBlur,
5936
+ onCopy: handleCopy,
5937
+ onClick: handleClick,
5938
+ onDOMBeforeInput: handleOnBeforeInput,
5939
+ onFocus: handleOnFocus,
5940
+ onKeyDown: handleKeyDown,
5941
+ onPaste: handlePaste,
5942
+ readOnly,
5943
+ renderPlaceholder: void 0,
5944
+ renderElement,
5945
+ renderLeaf,
5946
+ scrollSelectionIntoView: scrollSelectionIntoViewToSlate
5947
+ }
5948
+ ) : null;
5955
5949
  });
5956
5950
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
5957
- function useValidateSelection(ref, slateEditor) {
5958
- return react.useCallback(() => {
5959
- if (!slateEditor.selection)
5960
- return;
5961
- const root = slateReact.ReactEditor.findDocumentOrShadowRoot(slateEditor), {
5962
- activeElement
5963
- } = root;
5964
- if (ref.current !== activeElement)
5965
- return;
5966
- const domSelection = slateReact.ReactEditor.getWindow(slateEditor).getSelection();
5967
- if (!domSelection || domSelection.rangeCount === 0)
5968
- return;
5969
- const existingDOMRange = domSelection.getRangeAt(0);
5970
- try {
5971
- const newDOMRange = slateReact.ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
5972
- (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
5973
- } catch {
5974
- debug("Could not resolve selection, selecting top document"), slate.Transforms.deselect(slateEditor), slateEditor.children.length > 0 && slate.Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
5975
- }
5976
- }, [ref, slateEditor]);
5977
- }
5978
5951
  exports.PortableTextEditable = PortableTextEditable;
5979
5952
  exports.PortableTextEditor = PortableTextEditor;
5980
5953
  exports.editorMachine = editorMachine;