@portabletext/editor 1.44.6 → 1.44.8

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.cjs CHANGED
@@ -817,7 +817,14 @@ const slateOperationCallback = ({
817
817
  }
818
818
  newRange !== null && rangeDecorationState.push({
819
819
  ...newRange || slateRange,
820
- rangeDecoration: decoratedRange.rangeDecoration
820
+ rangeDecoration: {
821
+ ...decoratedRange.rangeDecoration,
822
+ selection: editorProvider.slateRangeToSelection({
823
+ schema: context.schema,
824
+ editor: context.slateEditor,
825
+ range: newRange
826
+ })
827
+ }
821
828
  });
822
829
  }
823
830
  return rangeDecorationState;
@@ -827,19 +834,37 @@ const slateOperationCallback = ({
827
834
  readOnly: ({
828
835
  event
829
836
  }) => (xstate.assertEvent(event, "update read only"), event.readOnly)
837
+ }),
838
+ "increment update count": xstate.assign({
839
+ updateCount: ({
840
+ context
841
+ }) => context.updateCount + 1
830
842
  })
831
843
  },
832
844
  actors: {
833
845
  "slate operation listener": xstate.fromCallback(slateOperationCallback)
834
846
  },
835
847
  guards: {
848
+ "has pending range decorations": ({
849
+ context
850
+ }) => context.pendingRangeDecorations.length > 0,
836
851
  "has range decorations": ({
837
852
  context
838
853
  }) => context.decoratedRanges.length > 0,
839
854
  "has different decorations": ({
840
855
  context,
841
856
  event
842
- }) => (xstate.assertEvent(event, "range decorations updated"), !isEqual__default.default(context.pendingRangeDecorations, event.rangeDecorations)),
857
+ }) => {
858
+ xstate.assertEvent(event, "range decorations updated");
859
+ const existingRangeDecorations = context.decoratedRanges.map((decoratedRange) => ({
860
+ anchor: decoratedRange.rangeDecoration.selection?.anchor,
861
+ focus: decoratedRange.rangeDecoration.selection?.focus
862
+ })), newRangeDecorations = event.rangeDecorations.map((rangeDecoration) => ({
863
+ anchor: rangeDecoration.selection?.anchor,
864
+ focus: rangeDecoration.selection?.focus
865
+ }));
866
+ return !isEqual__default.default(existingRangeDecorations, newRangeDecorations);
867
+ },
843
868
  "not read only": ({
844
869
  context
845
870
  }) => !context.readOnly
@@ -853,7 +878,8 @@ const slateOperationCallback = ({
853
878
  pendingRangeDecorations: input.rangeDecorations,
854
879
  decoratedRanges: [],
855
880
  schema: input.schema,
856
- slateEditor: input.slateEditor
881
+ slateEditor: input.slateEditor,
882
+ updateCount: 0
857
883
  }),
858
884
  invoke: {
859
885
  src: "slate operation listener",
@@ -875,10 +901,13 @@ const slateOperationCallback = ({
875
901
  "range decorations updated": {
876
902
  actions: ["update pending range decorations"]
877
903
  },
878
- ready: {
904
+ ready: [{
879
905
  target: "ready",
880
- actions: ["set up initial range decorations"]
881
- }
906
+ guard: "has pending range decorations",
907
+ actions: ["set up initial range decorations", "increment update count"]
908
+ }, {
909
+ target: "ready"
910
+ }]
882
911
  }
883
912
  },
884
913
  ready: {
@@ -887,7 +916,7 @@ const slateOperationCallback = ({
887
916
  "range decorations updated": {
888
917
  target: ".idle",
889
918
  guard: "has different decorations",
890
- actions: ["update range decorations", "update pending range decorations"]
919
+ actions: ["update range decorations", "increment update count"]
891
920
  }
892
921
  },
893
922
  states: {
@@ -910,7 +939,7 @@ const slateOperationCallback = ({
910
939
  }
911
940
  });
912
941
  function createDecorate(rangeDecorationActor) {
913
- return function([, path]) {
942
+ return function([node, path]) {
914
943
  if (editorProvider.isEqualToEmptyEditor(rangeDecorationActor.getSnapshot().context.slateEditor.children, rangeDecorationActor.getSnapshot().context.schema))
915
944
  return [{
916
945
  anchor: {
@@ -925,7 +954,10 @@ function createDecorate(rangeDecorationActor) {
925
954
  }];
926
955
  if (path.length === 0)
927
956
  return [];
928
- const result = rangeDecorationActor.getSnapshot().context.decoratedRanges.filter((item) => slate.Range.isCollapsed(item) ? path.length !== 2 ? !1 : slate.Path.equals(item.focus.path, path) && slate.Path.equals(item.anchor.path, path) : slate.Range.intersection(item, {
957
+ if (!slate.Element.isElement(node) || node.children.length === 0)
958
+ return [];
959
+ const blockIndex = path.at(0);
960
+ return blockIndex === void 0 ? [] : rangeDecorationActor.getSnapshot().context.decoratedRanges.filter((decoratedRange) => slate.Range.isCollapsed(decoratedRange) ? node.children.some((_, childIndex) => slate.Path.equals(decoratedRange.anchor.path, [blockIndex, childIndex]) && slate.Path.equals(decoratedRange.focus.path, [blockIndex, childIndex])) : slate.Range.intersection(decoratedRange, {
929
961
  anchor: {
930
962
  path,
931
963
  offset: 0
@@ -934,8 +966,7 @@ function createDecorate(rangeDecorationActor) {
934
966
  path,
935
967
  offset: 0
936
968
  }
937
- }) || slate.Range.includes(item, path));
938
- return result.length > 0 ? result : [];
969
+ }) || slate.Range.includes(decoratedRange, path));
939
970
  };
940
971
  }
941
972
  const debug = editorProvider.debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
@@ -984,7 +1015,9 @@ const debug = editorProvider.debugWithName("component:Editable"), PLACEHOLDER_ST
984
1015
  schema: schemaTypes,
985
1016
  rangeDecorations: rangeDecorations ?? []
986
1017
  }
987
- }), decorate = React.useMemo(() => createDecorate(rangeDecorationsActor), [rangeDecorationsActor]);
1018
+ });
1019
+ react.useSelector(rangeDecorationsActor, (s_1) => s_1.context.updateCount);
1020
+ const decorate = React.useMemo(() => createDecorate(rangeDecorationsActor), [rangeDecorationsActor]);
988
1021
  React.useEffect(() => {
989
1022
  rangeDecorationsActor.send({
990
1023
  type: "update read only",