@portabletext/editor 1.33.2 → 1.33.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +11 -204
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs +7 -7
  4. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  5. package/lib/_chunks-cjs/plugin.event-listener.cjs +208 -33
  6. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
  7. package/lib/_chunks-cjs/selector.get-text-before.cjs +3 -3
  8. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  9. package/lib/_chunks-cjs/selector.is-active-style.cjs +246 -0
  10. package/lib/_chunks-cjs/selector.is-active-style.cjs.map +1 -0
  11. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs +31 -200
  12. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs.map +1 -1
  13. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs +7 -5
  14. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs.map +1 -1
  15. package/lib/_chunks-cjs/util.reverse-selection.cjs +0 -116
  16. package/lib/_chunks-cjs/util.reverse-selection.cjs.map +1 -1
  17. package/lib/_chunks-cjs/util.slice-blocks.cjs +138 -1
  18. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  19. package/lib/_chunks-es/behavior.core.js +9 -202
  20. package/lib/_chunks-es/behavior.core.js.map +1 -1
  21. package/lib/_chunks-es/behavior.markdown.js +1 -1
  22. package/lib/_chunks-es/plugin.event-listener.js +205 -31
  23. package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
  24. package/lib/_chunks-es/selector.get-text-before.js +2 -1
  25. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  26. package/lib/_chunks-es/selector.is-active-style.js +249 -0
  27. package/lib/_chunks-es/selector.is-active-style.js.map +1 -0
  28. package/lib/_chunks-es/selector.is-at-the-start-of-block.js +20 -189
  29. package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +1 -1
  30. package/lib/_chunks-es/util.block-offsets-to-selection.js +5 -3
  31. package/lib/_chunks-es/util.block-offsets-to-selection.js.map +1 -1
  32. package/lib/_chunks-es/util.reverse-selection.js +1 -117
  33. package/lib/_chunks-es/util.reverse-selection.js.map +1 -1
  34. package/lib/_chunks-es/util.slice-blocks.js +140 -3
  35. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  36. package/lib/index.d.cts +1 -115
  37. package/lib/index.d.ts +1 -115
  38. package/lib/plugins/index.cjs +19 -15
  39. package/lib/plugins/index.cjs.map +1 -1
  40. package/lib/plugins/index.d.cts +1 -115
  41. package/lib/plugins/index.d.ts +1 -115
  42. package/lib/plugins/index.js +9 -5
  43. package/lib/plugins/index.js.map +1 -1
  44. package/lib/selectors/index.cjs +16 -21
  45. package/lib/selectors/index.cjs.map +1 -1
  46. package/lib/selectors/index.d.cts +2 -0
  47. package/lib/selectors/index.d.ts +2 -0
  48. package/lib/selectors/index.js +7 -11
  49. package/lib/selectors/index.js.map +1 -1
  50. package/lib/utils/index.cjs +13 -13
  51. package/lib/utils/index.cjs.map +1 -1
  52. package/lib/utils/index.d.cts +2 -0
  53. package/lib/utils/index.d.ts +2 -0
  54. package/lib/utils/index.js +3 -3
  55. package/package.json +2 -2
  56. package/src/behaviors/behavior.core.annotations.ts +0 -24
  57. package/src/behaviors/behavior.core.decorators.ts +0 -19
  58. package/src/behaviors/behavior.core.insert-break.ts +4 -4
  59. package/src/behaviors/behavior.core.lists.ts +0 -30
  60. package/src/behaviors/behavior.core.ts +2 -17
  61. package/src/behaviors/behavior.default.ts +198 -0
  62. package/src/behaviors/behavior.foundational.ts +12 -12
  63. package/src/behaviors/behavior.markdown-emphasis.ts +4 -0
  64. package/src/converters/converter.text-html.serialize.test.ts +1 -1
  65. package/src/editor/PortableTextEditor.tsx +1 -1
  66. package/src/editor/editor-machine.ts +8 -8
  67. package/src/plugins/plugin.event-listener.tsx +1 -1
  68. package/src/selectors/selector.get-caret-word-selection.ts +9 -0
  69. package/src/selectors/selector.get-selection-text.test.ts +383 -36
  70. package/src/selectors/selector.get-selection-text.ts +13 -73
  71. package/src/utils/util.block-offset.test.ts +312 -0
  72. package/src/utils/util.block-offset.ts +39 -7
  73. package/src/utils/util.block-offsets-to-selection.ts +2 -0
  74. package/src/utils/util.slice-blocks.ts +12 -1
  75. package/lib/_chunks-cjs/selector.get-trimmed-selection.cjs +0 -97
  76. package/lib/_chunks-cjs/selector.get-trimmed-selection.cjs.map +0 -1
  77. package/lib/_chunks-es/selector.get-trimmed-selection.js +0 -100
  78. package/lib/_chunks-es/selector.get-trimmed-selection.js.map +0 -1
  79. package/src/behaviors/behavior.core.deserialize.ts +0 -60
  80. package/src/behaviors/behavior.core.serialize.ts +0 -44
  81. package/src/behaviors/behavior.core.style.ts +0 -19
@@ -15,7 +15,7 @@ import isPlainObject from "lodash/isPlainObject.js";
15
15
  import uniq from "lodash/uniq.js";
16
16
  import getRandomValues from "get-random-values-esm";
17
17
  import { parseBlock, blockOffsetsToSelection } from "./util.block-offsets-to-selection.js";
18
- import { sliceBlocks } from "./util.slice-blocks.js";
18
+ import { sliceBlocks, spanSelectionPointToBlockOffset } from "./util.slice-blocks.js";
19
19
  import { htmlToBlocks } from "@portabletext/block-tools";
20
20
  import { toHTML } from "@portabletext/to-html";
21
21
  import { Schema } from "@sanity/schema";
@@ -23,8 +23,7 @@ import get from "lodash/get.js";
23
23
  import isUndefined from "lodash/isUndefined.js";
24
24
  import omitBy from "lodash/omitBy.js";
25
25
  import { createGuards } from "./selector.is-at-the-start-of-block.js";
26
- import { spanSelectionPointToBlockOffset } from "./util.reverse-selection.js";
27
- import { getTrimmedSelection } from "./selector.get-trimmed-selection.js";
26
+ import { getTrimmedSelection, isActiveAnnotation, isActiveDecorator, isActiveListItem, isActiveStyle } from "./selector.is-active-style.js";
28
27
  import startCase from "lodash.startcase";
29
28
  import { defineBehavior, raise, coreBehaviors, isCustomBehaviorEvent } from "./behavior.core.js";
30
29
  import { Subject } from "rxjs";
@@ -5707,24 +5706,195 @@ function createSlateEditor(config) {
5707
5706
  };
5708
5707
  return slateEditors.set(config.editorActor, slateEditor), slateEditor;
5709
5708
  }
5710
- const keyIs = {
5711
- lineBreak: (event) => event.key === "Enter" && event.shiftKey
5712
- }, foundationalBehaviors = [
5713
- /**
5714
- * On WebKit, Shift+Enter results in an `insertParagraph` input event rather
5715
- * than an `insertLineBreak` input event. This Behavior makes sure we catch
5716
- * that `key.down` event beforehand and raise an `insert.soft break` manually.
5717
- */
5718
- defineBehavior({
5719
- on: "key.down",
5720
- guard: ({
5709
+ const toggleAnnotationOff = defineBehavior({
5710
+ on: "annotation.toggle",
5711
+ guard: ({
5712
+ context,
5713
+ event
5714
+ }) => isActiveAnnotation(event.annotation.name)({
5715
+ context
5716
+ }),
5717
+ actions: [({
5718
+ event
5719
+ }) => [raise({
5720
+ type: "annotation.remove",
5721
+ annotation: event.annotation
5722
+ })]]
5723
+ }), toggleAnnotationOn = defineBehavior({
5724
+ on: "annotation.toggle",
5725
+ guard: ({
5726
+ context,
5727
+ event
5728
+ }) => !isActiveAnnotation(event.annotation.name)({
5729
+ context
5730
+ }),
5731
+ actions: [({
5732
+ event
5733
+ }) => [raise({
5734
+ type: "annotation.add",
5735
+ annotation: event.annotation
5736
+ })]]
5737
+ }), toggleDecoratorOff = defineBehavior({
5738
+ on: "decorator.toggle",
5739
+ guard: ({
5740
+ context,
5741
+ event
5742
+ }) => isActiveDecorator(event.decorator)({
5743
+ context
5744
+ }),
5745
+ actions: [({
5746
+ event
5747
+ }) => [raise({
5748
+ type: "decorator.remove",
5749
+ decorator: event.decorator
5750
+ })]]
5751
+ }), toggleDecoratorOn = defineBehavior({
5752
+ on: "decorator.toggle",
5753
+ guard: ({
5754
+ context,
5755
+ event
5756
+ }) => !isActiveDecorator(event.decorator)({
5757
+ context
5758
+ }),
5759
+ actions: [({
5760
+ event
5761
+ }) => [raise({
5762
+ type: "decorator.add",
5763
+ decorator: event.decorator
5764
+ })]]
5765
+ }), toggleListItemOff = defineBehavior({
5766
+ on: "list item.toggle",
5767
+ guard: ({
5768
+ context,
5769
+ event
5770
+ }) => isActiveListItem(event.listItem)({
5771
+ context
5772
+ }),
5773
+ actions: [({
5774
+ event
5775
+ }) => [raise({
5776
+ type: "list item.remove",
5777
+ listItem: event.listItem
5778
+ })]]
5779
+ }), toggleListItemOn = defineBehavior({
5780
+ on: "list item.toggle",
5781
+ guard: ({
5782
+ context,
5783
+ event
5784
+ }) => !isActiveListItem(event.listItem)({
5785
+ context
5786
+ }),
5787
+ actions: [({
5788
+ event
5789
+ }) => [raise({
5790
+ type: "list item.add",
5791
+ listItem: event.listItem
5792
+ })]]
5793
+ }), toggleStyleOff = defineBehavior({
5794
+ on: "style.toggle",
5795
+ guard: ({
5796
+ context,
5797
+ event
5798
+ }) => isActiveStyle(event.style)({
5799
+ context
5800
+ }),
5801
+ actions: [({
5802
+ event
5803
+ }) => [raise({
5804
+ type: "style.remove",
5805
+ style: event.style
5806
+ })]]
5807
+ }), toggleStyleOn = defineBehavior({
5808
+ on: "style.toggle",
5809
+ guard: ({
5810
+ context,
5811
+ event
5812
+ }) => !isActiveStyle(event.style)({
5813
+ context
5814
+ }),
5815
+ actions: [({
5816
+ event
5817
+ }) => [raise({
5818
+ type: "style.add",
5819
+ style: event.style
5820
+ })]]
5821
+ }), raiseDeserializationSuccessOrFailure = defineBehavior({
5822
+ on: "deserialize",
5823
+ guard: ({
5824
+ context,
5825
+ event
5826
+ }) => {
5827
+ const deserializeEvents = context.converters.flatMap((converter) => {
5828
+ const data = event.dataTransfer.getData(converter.mimeType);
5829
+ return data ? [converter.deserialize({
5830
+ context,
5831
+ event: {
5832
+ type: "deserialize",
5833
+ data
5834
+ }
5835
+ })] : [];
5836
+ });
5837
+ return deserializeEvents.find((deserializeEvent) => deserializeEvent.type === "deserialization.success") || {
5838
+ type: "deserialization.failure",
5839
+ mimeType: "*/*",
5840
+ reason: deserializeEvents.map((deserializeEvent) => deserializeEvent.type === "deserialization.failure" ? deserializeEvent.reason : "").join(", ")
5841
+ };
5842
+ },
5843
+ actions: [({
5844
+ event
5845
+ }, deserializeEvent) => [raise({
5846
+ ...deserializeEvent,
5847
+ dataTransfer: event.dataTransfer
5848
+ })]]
5849
+ }), raiseInsertBlocks = defineBehavior({
5850
+ on: "deserialization.success",
5851
+ actions: [({
5852
+ event
5853
+ }) => [raise({
5854
+ type: "insert.blocks",
5855
+ blocks: event.data
5856
+ })]]
5857
+ }), raiseSerializationSuccessOrFailure = defineBehavior({
5858
+ on: "serialize",
5859
+ guard: ({
5860
+ context,
5861
+ event
5862
+ }) => {
5863
+ if (context.converters.length === 0)
5864
+ return !1;
5865
+ const serializeEvents = context.converters.map((converter) => converter.serialize({
5866
+ context,
5721
5867
  event
5722
- }) => keyIs.lineBreak(event.keyboardEvent),
5723
- actions: [() => [raise({
5724
- type: "insert.soft break"
5725
- })]]
5726
- })
5727
- ];
5868
+ }));
5869
+ return serializeEvents.length === 0 ? !1 : serializeEvents;
5870
+ },
5871
+ actions: [({
5872
+ event
5873
+ }, serializeEvents) => serializeEvents.map((serializeEvent) => raise({
5874
+ ...serializeEvent,
5875
+ dataTransfer: event.dataTransfer
5876
+ }))]
5877
+ }), raiseDataTransferSet = defineBehavior({
5878
+ on: "serialization.success",
5879
+ actions: [({
5880
+ event
5881
+ }) => [raise({
5882
+ type: "data transfer.set",
5883
+ data: event.data,
5884
+ dataTransfer: event.dataTransfer,
5885
+ mimeType: event.mimeType
5886
+ })]]
5887
+ }), defaultBehaviors = [toggleAnnotationOff, toggleAnnotationOn, toggleDecoratorOff, toggleDecoratorOn, toggleListItemOff, toggleListItemOn, toggleStyleOff, toggleStyleOn, raiseDeserializationSuccessOrFailure, raiseInsertBlocks, raiseSerializationSuccessOrFailure, raiseDataTransferSet], keyIs = {
5888
+ lineBreak: (event) => event.key === "Enter" && event.shiftKey
5889
+ }, raiseSoftBreak = defineBehavior({
5890
+ on: "key.down",
5891
+ guard: ({
5892
+ event
5893
+ }) => keyIs.lineBreak(event.keyboardEvent),
5894
+ actions: [() => [raise({
5895
+ type: "insert.soft break"
5896
+ })]]
5897
+ }), foundationalBehaviors = [raiseSoftBreak];
5728
5898
  function getActiveDecorators({
5729
5899
  schema,
5730
5900
  slateEditorInstance
@@ -5778,7 +5948,7 @@ const editorMachine = setup({
5778
5948
  "assign behaviors": assign({
5779
5949
  behaviors: ({
5780
5950
  event
5781
- }) => (assertEvent(event, "update behaviors"), /* @__PURE__ */ new Set([...foundationalBehaviors, ...event.behaviors]))
5951
+ }) => (assertEvent(event, "update behaviors"), /* @__PURE__ */ new Set([...event.behaviors]))
5782
5952
  }),
5783
5953
  "assign schema": assign({
5784
5954
  schema: ({
@@ -5825,7 +5995,7 @@ const editorMachine = setup({
5825
5995
  const defaultAction = event.type === "custom behavior event" || event.behaviorEvent.type === "copy" || event.behaviorEvent.type === "deserialize" || event.behaviorEvent.type === "key.down" || event.behaviorEvent.type === "key.up" || event.behaviorEvent.type === "paste" || event.behaviorEvent.type === "serialize" ? void 0 : {
5826
5996
  ...event.behaviorEvent,
5827
5997
  editor: event.editor
5828
- }, defaultActionCallback = event.type === "behavior event" ? event.defaultActionCallback : void 0, eventBehaviors = [...context.behaviors.values()].filter((behavior) => behavior.on === event.behaviorEvent.type);
5998
+ }, defaultActionCallback = event.type === "behavior event" ? event.defaultActionCallback : void 0, eventBehaviors = [...foundationalBehaviors, ...context.behaviors.values(), ...defaultBehaviors].filter((behavior) => behavior.on === event.behaviorEvent.type);
5829
5999
  if (eventBehaviors.length === 0) {
5830
6000
  if (defaultActionCallback) {
5831
6001
  withApplyingBehaviorActions(event.editor, () => {
@@ -5935,7 +6105,7 @@ const editorMachine = setup({
5935
6105
  context: ({
5936
6106
  input
5937
6107
  }) => ({
5938
- behaviors: /* @__PURE__ */ new Set([...foundationalBehaviors, ...input.behaviors ?? coreBehaviors]),
6108
+ behaviors: /* @__PURE__ */ new Set([...input.behaviors ?? coreBehaviors]),
5939
6109
  converters: new Set(input.converters ?? []),
5940
6110
  keyGenerator: input.keyGenerator,
5941
6111
  pendingEvents: [],
@@ -6797,12 +6967,12 @@ function RoutePatchesObservableToEditorActor(props) {
6797
6967
  }, t1 = [props.editorActor, props.patches$], $[0] = props.editorActor, $[1] = props.patches$, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), useEffect(t0, t1), null;
6798
6968
  }
6799
6969
  function RouteEventsToChanges(props) {
6800
- const $ = c(6);
6970
+ const $ = c(7);
6801
6971
  let t0;
6802
6972
  $[0] !== props ? (t0 = (change) => props.onChange(change), $[0] = props, $[1] = t0) : t0 = $[1];
6803
6973
  const handleChange = useEffectEvent(t0);
6804
- let t1, t2;
6805
- return $[2] !== handleChange || $[3] !== props.editorActor ? (t1 = () => {
6974
+ let t1;
6975
+ $[2] !== handleChange || $[3] !== props.editorActor ? (t1 = () => {
6806
6976
  debug("Subscribing to editor changes");
6807
6977
  const sub = props.editorActor.on("*", (event) => {
6808
6978
  bb5: switch (event.type) {
@@ -6879,7 +7049,9 @@ function RouteEventsToChanges(props) {
6879
7049
  return () => {
6880
7050
  debug("Unsubscribing to changes"), sub.unsubscribe();
6881
7051
  };
6882
- }, t2 = [props.editorActor, handleChange], $[2] = handleChange, $[3] = props.editorActor, $[4] = t1, $[5] = t2) : (t1 = $[4], t2 = $[5]), useEffect(t1, t2), null;
7052
+ }, $[2] = handleChange, $[3] = props.editorActor, $[4] = t1) : t1 = $[4];
7053
+ let t2;
7054
+ return $[5] !== props.editorActor ? (t2 = [props.editorActor], $[5] = props.editorActor, $[6] = t2) : t2 = $[6], useEffect(t1, t2), null;
6883
7055
  }
6884
7056
  const EditorContext = React.createContext(void 0);
6885
7057
  function EditorProvider(props) {
@@ -6919,14 +7091,16 @@ function useEditor() {
6919
7091
  return editor;
6920
7092
  }
6921
7093
  function EventListenerPlugin(props) {
6922
- const $ = c(4), editor = useEditor(), on = useEffectEvent(props.on);
6923
- let t0, t1;
6924
- return $[0] !== editor || $[1] !== on ? (t0 = () => {
7094
+ const $ = c(5), editor = useEditor(), on = useEffectEvent(props.on);
7095
+ let t0;
7096
+ $[0] !== editor || $[1] !== on ? (t0 = () => {
6925
7097
  const subscription = editor.on("*", on);
6926
7098
  return () => {
6927
7099
  subscription.unsubscribe();
6928
7100
  };
6929
- }, t1 = [editor, on], $[0] = editor, $[1] = on, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), useEffect(t0, t1), null;
7101
+ }, $[0] = editor, $[1] = on, $[2] = t0) : t0 = $[2];
7102
+ let t1;
7103
+ return $[3] !== editor ? (t1 = [editor], $[3] = editor, $[4] = t1) : t1 = $[4], useEffect(t0, t1), null;
6930
7104
  }
6931
7105
  export {
6932
7106
  EditorActorContext,