@portabletext/editor 1.10.0 → 1.10.2

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.esm.js CHANGED
@@ -13,7 +13,7 @@ import { c } from "react-compiler-runtime";
13
13
  import { styled } from "styled-components";
14
14
  import uniq from "lodash/uniq.js";
15
15
  import { Subject } from "rxjs";
16
- import { fromCallback, setup, assign, assertEvent, emit, enqueueActions, createActor } from "xstate";
16
+ import { setup, assign, assertEvent, emit, enqueueActions, createActor } from "xstate";
17
17
  import throttle from "lodash/throttle.js";
18
18
  import { useEffectEvent } from "use-effect-event";
19
19
  import debounce from "lodash/debounce.js";
@@ -2957,11 +2957,10 @@ function _updateBlock(slateEditor, currentBlock, oldBlock, currentBlockIndex) {
2957
2957
  }
2958
2958
  const debug$i = debugWithName("component:PortableTextEditor:Synchronizer"), debugVerbose$3 = debug$i.enabled && !1, FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === "test" ? 500 : 1e3;
2959
2959
  function Synchronizer(props) {
2960
- const $ = c(35), portableTextEditor = usePortableTextEditor(), readOnly = useSelector(props.editorActor, _temp$1), {
2960
+ const $ = c(28), portableTextEditor = usePortableTextEditor(), readOnly = useSelector(props.editorActor, _temp$1), value = useSelector(props.editorActor, _temp2), {
2961
2961
  editorActor,
2962
2962
  getValue,
2963
- onChange,
2964
- value
2963
+ onChange
2965
2964
  } = props;
2966
2965
  let t0;
2967
2966
  $[0] === Symbol.for("react.memo_cache_sentinel") ? (t0 = [], $[0] = t0) : t0 = $[0];
@@ -3013,52 +3012,38 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
3013
3012
  });
3014
3013
  debug$i("Subscribing to editor changes");
3015
3014
  const sub = editorActor.on("*", (event) => {
3016
- bb20: switch (event.type) {
3015
+ bb22: switch (event.type) {
3017
3016
  case "patch": {
3018
3017
  IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, !0), pendingPatches.current.push(event.patch), onFlushPendingPatchesThrottled(), handleChange(event);
3019
- break bb20;
3018
+ break bb22;
3020
3019
  }
3021
3020
  case "loading": {
3022
3021
  handleChange({
3023
3022
  type: "loading",
3024
3023
  isLoading: !0
3025
3024
  });
3026
- break bb20;
3025
+ break bb22;
3027
3026
  }
3028
3027
  case "done loading": {
3029
3028
  handleChange({
3030
3029
  type: "loading",
3031
3030
  isLoading: !1
3032
3031
  });
3033
- break bb20;
3032
+ break bb22;
3034
3033
  }
3035
3034
  case "focused": {
3036
3035
  handleChange({
3037
3036
  type: "focus",
3038
3037
  event: event.event
3039
3038
  });
3040
- break bb20;
3041
- }
3042
- case "offline": {
3043
- handleChange({
3044
- type: "connection",
3045
- value: "offline"
3046
- });
3047
- break bb20;
3048
- }
3049
- case "online": {
3050
- handleChange({
3051
- type: "connection",
3052
- value: "online"
3053
- });
3054
- break bb20;
3039
+ break bb22;
3055
3040
  }
3056
3041
  case "value changed": {
3057
3042
  handleChange({
3058
3043
  type: "value",
3059
3044
  value: event.value
3060
3045
  });
3061
- break bb20;
3046
+ break bb22;
3062
3047
  }
3063
3048
  case "invalid value": {
3064
3049
  handleChange({
@@ -3066,21 +3051,21 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
3066
3051
  resolution: event.resolution,
3067
3052
  value: event.value
3068
3053
  });
3069
- break bb20;
3054
+ break bb22;
3070
3055
  }
3071
3056
  case "error": {
3072
3057
  handleChange({
3073
3058
  ...event,
3074
3059
  level: "warning"
3075
3060
  });
3076
- break bb20;
3061
+ break bb22;
3077
3062
  }
3078
3063
  case "annotation.add":
3079
3064
  case "annotation.remove":
3080
3065
  case "annotation.toggle":
3081
3066
  case "focus":
3082
3067
  case "patches":
3083
- break bb20;
3068
+ break bb22;
3084
3069
  default:
3085
3070
  handleChange(event);
3086
3071
  }
@@ -3089,25 +3074,16 @@ ${JSON.stringify(pendingPatches.current, null, 2)}`);
3089
3074
  debug$i("Unsubscribing to changes"), sub.unsubscribe();
3090
3075
  };
3091
3076
  }, t9 = [editorActor, handleChange, onFlushPendingPatches, slateEditor], $[17] = editorActor, $[18] = handleChange, $[19] = onFlushPendingPatches, $[20] = slateEditor, $[21] = t8, $[22] = t9) : (t8 = $[21], t9 = $[22]), useEffect(t8, t9);
3092
- let t10;
3093
- $[23] !== syncValue || $[24] !== value ? (t10 = () => {
3094
- debug$i("Editor is online, syncing from props.value"), syncValue(value);
3095
- }, $[23] = syncValue, $[24] = value, $[25] = t10) : t10 = $[25];
3096
- const handleOnline = t10;
3097
- let t11, t12;
3098
- $[26] !== editorActor || $[27] !== handleOnline ? (t11 = () => {
3099
- const subscription = editorActor.on("online", handleOnline);
3100
- return () => {
3101
- subscription.unsubscribe();
3102
- };
3103
- }, t12 = [handleOnline, editorActor], $[26] = editorActor, $[27] = handleOnline, $[28] = t11, $[29] = t12) : (t11 = $[28], t12 = $[29]), useEffect(t11, t12);
3104
3077
  const isInitialValueFromProps = useRef(!0);
3105
- let t13, t14;
3106
- return $[30] !== editorActor || $[31] !== syncValue || $[32] !== value ? (t13 = () => {
3078
+ let t10, t11;
3079
+ return $[23] !== editorActor || $[24] !== syncValue || $[25] !== value ? (t10 = () => {
3107
3080
  debug$i("Value from props changed, syncing new value"), syncValue(value), isInitialValueFromProps.current && (editorActor.send({
3108
3081
  type: "ready"
3109
3082
  }), isInitialValueFromProps.current = !1);
3110
- }, t14 = [editorActor, syncValue, value], $[30] = editorActor, $[31] = syncValue, $[32] = value, $[33] = t13, $[34] = t14) : (t13 = $[33], t14 = $[34]), useEffect(t13, t14), null;
3083
+ }, t11 = [editorActor, syncValue, value], $[23] = editorActor, $[24] = syncValue, $[25] = value, $[26] = t10, $[27] = t11) : (t10 = $[26], t11 = $[27]), useEffect(t10, t11), null;
3084
+ }
3085
+ function _temp2(s_0) {
3086
+ return s_0.context.value;
3111
3087
  }
3112
3088
  function _temp$1(s) {
3113
3089
  return s.context.readOnly;
@@ -5714,22 +5690,7 @@ function performDefaultAction({
5714
5690
  });
5715
5691
  }
5716
5692
  }
5717
- const networkLogic = fromCallback(({
5718
- sendBack
5719
- }) => {
5720
- const onlineHandler = () => {
5721
- sendBack({
5722
- type: "online"
5723
- });
5724
- }, offlineHandler = () => {
5725
- sendBack({
5726
- type: "offline"
5727
- });
5728
- };
5729
- return window.addEventListener("online", onlineHandler), window.addEventListener("offline", offlineHandler), () => {
5730
- window.removeEventListener("online", onlineHandler), window.removeEventListener("offline", offlineHandler);
5731
- };
5732
- }), editorMachine = setup({
5693
+ const editorMachine = setup({
5733
5694
  types: {
5734
5695
  context: {},
5735
5696
  events: {},
@@ -5828,9 +5789,6 @@ const networkLogic = fromCallback(({
5828
5789
  actionIntends: [defaultAction]
5829
5790
  });
5830
5791
  })
5831
- },
5832
- actors: {
5833
- networkLogic
5834
5792
  }
5835
5793
  }).createMachine({
5836
5794
  id: "editor",
@@ -5842,12 +5800,9 @@ const networkLogic = fromCallback(({
5842
5800
  pendingEvents: [],
5843
5801
  schema: input.schema,
5844
5802
  readOnly: !1,
5845
- maxBlocks: void 0
5803
+ maxBlocks: void 0,
5804
+ value: input.value
5846
5805
  }),
5847
- invoke: {
5848
- id: "networkLogic",
5849
- src: "networkLogic"
5850
- },
5851
5806
  on: {
5852
5807
  "annotation.add": {
5853
5808
  actions: emit(({
@@ -5921,16 +5876,6 @@ const networkLogic = fromCallback(({
5921
5876
  event
5922
5877
  }) => event)
5923
5878
  },
5924
- online: {
5925
- actions: emit({
5926
- type: "online"
5927
- })
5928
- },
5929
- offline: {
5930
- actions: emit({
5931
- type: "offline"
5932
- })
5933
- },
5934
5879
  loading: {
5935
5880
  actions: emit({
5936
5881
  type: "loading"
@@ -5952,6 +5897,13 @@ const networkLogic = fromCallback(({
5952
5897
  "update schema": {
5953
5898
  actions: "assign schema"
5954
5899
  },
5900
+ "update value": {
5901
+ actions: assign({
5902
+ value: ({
5903
+ event
5904
+ }) => event.value
5905
+ })
5906
+ },
5955
5907
  "toggle readOnly": {
5956
5908
  actions: assign({
5957
5909
  readOnly: ({
@@ -6113,7 +6065,8 @@ class PortableTextEditor extends Component {
6113
6065
  props.incomingPatches$ && console.warn("The prop 'incomingPatches$' is deprecated and renamed to 'patches$'"), this.schemaTypes = getPortableTextMemberSchemaTypes(props.schemaType.hasOwnProperty("jsonType") ? props.schemaType : compileType(props.schemaType)), this.editorActor = createActor(editorMachine, {
6114
6066
  input: {
6115
6067
  keyGenerator: props.keyGenerator || defaultKeyGenerator,
6116
- schema: this.schemaTypes
6068
+ schema: this.schemaTypes,
6069
+ value: props.value
6117
6070
  }
6118
6071
  }), this.editorActor.start(), this.slateEditor = createSlateEditor({
6119
6072
  editorActor: this.editorActor
@@ -6135,7 +6088,10 @@ class PortableTextEditor extends Component {
6135
6088
  }), this.props.maxBlocks !== prevProps.maxBlocks && this.editorActor.send({
6136
6089
  type: "update maxBlocks",
6137
6090
  maxBlocks: this.props.maxBlocks === void 0 ? void 0 : Number.parseInt(this.props.maxBlocks.toString(), 10)
6138
- })), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this);
6091
+ }), this.props.value !== prevProps.value && this.editorActor.send({
6092
+ type: "update value",
6093
+ value: this.props.value
6094
+ }), this.props.editorRef !== prevProps.editorRef && this.props.editorRef && (this.props.editorRef.current = this));
6139
6095
  }
6140
6096
  setEditable = (editable) => {
6141
6097
  this.editable = {
@@ -6154,7 +6110,7 @@ class PortableTextEditor extends Component {
6154
6110
  /* @__PURE__ */ jsx(EditorActorContext.Provider, { value: this.editorActor, children: /* @__PURE__ */ jsx(Slate, { editor: this.slateEditor.instance, initialValue: this.slateEditor.initialValue, children: /* @__PURE__ */ jsx(PortableTextEditorContext.Provider, { value: this, children: /* @__PURE__ */ jsxs(PortableTextEditorSelectionProvider, { editorActor: this.editorActor, children: [
6155
6111
  /* @__PURE__ */ jsx(Synchronizer, { editorActor: this.editorActor, getValue: this.getValue, onChange: (change) => {
6156
6112
  this.props.editor || this.props.onChange(change), this.change$.next(change);
6157
- }, value: this.props.value }),
6113
+ } }),
6158
6114
  this.props.children
6159
6115
  ] }) }) }) })
6160
6116
  ] });
@@ -6920,39 +6876,40 @@ const debug = debugWithName("component:Editable"), PLACEHOLDER_STYLE = {
6920
6876
  });
6921
6877
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
6922
6878
  function useEditor(config) {
6923
- const $ = c(20), t0 = config.keyGenerator ?? defaultKeyGenerator;
6879
+ const $ = c(21), t0 = config.keyGenerator ?? defaultKeyGenerator;
6924
6880
  let t1;
6925
6881
  $[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];
6926
6882
  let t2;
6927
- $[3] !== config.behaviors || $[4] !== t0 || $[5] !== t1 ? (t2 = {
6883
+ $[3] !== config.behaviors || $[4] !== config.initialValue || $[5] !== t0 || $[6] !== t1 ? (t2 = {
6928
6884
  input: {
6929
6885
  behaviors: config.behaviors,
6930
6886
  keyGenerator: t0,
6931
- schema: t1
6887
+ schema: t1,
6888
+ value: config.initialValue
6932
6889
  }
6933
- }, $[3] = config.behaviors, $[4] = t0, $[5] = t1, $[6] = t2) : t2 = $[6];
6890
+ }, $[3] = config.behaviors, $[4] = config.initialValue, $[5] = t0, $[6] = t1, $[7] = t2) : t2 = $[7];
6934
6891
  const editorActor = useActorRef(editorMachine, t2);
6935
6892
  let t3;
6936
- $[7] !== editorActor ? (t3 = createSlateEditor({
6893
+ $[8] !== editorActor ? (t3 = createSlateEditor({
6937
6894
  editorActor
6938
- }), $[7] = editorActor, $[8] = t3) : t3 = $[8];
6895
+ }), $[8] = editorActor, $[9] = t3) : t3 = $[9];
6939
6896
  const slateEditor = t3, readOnly = useSelector(editorActor, _temp);
6940
6897
  let t4, t5;
6941
- $[9] !== editorActor ? (t4 = (event) => {
6898
+ $[10] !== editorActor ? (t4 = (event) => {
6942
6899
  editorActor.send(event);
6943
- }, t5 = (event_0, listener) => editorActor.on(event_0, listener), $[9] = editorActor, $[10] = t4, $[11] = t5) : (t4 = $[10], t5 = $[11]);
6900
+ }, t5 = (event_0, listener) => editorActor.on(event_0, listener), $[10] = editorActor, $[11] = t4, $[12] = t5) : (t4 = $[11], t5 = $[12]);
6944
6901
  let t6;
6945
- $[12] !== editorActor || $[13] !== slateEditor ? (t6 = {
6902
+ $[13] !== editorActor || $[14] !== slateEditor ? (t6 = {
6946
6903
  editorActor,
6947
6904
  slateEditor
6948
- }, $[12] = editorActor, $[13] = slateEditor, $[14] = t6) : t6 = $[14];
6905
+ }, $[13] = editorActor, $[14] = slateEditor, $[15] = t6) : t6 = $[15];
6949
6906
  let t7;
6950
- return $[15] !== readOnly || $[16] !== t4 || $[17] !== t5 || $[18] !== t6 ? (t7 = {
6907
+ return $[16] !== readOnly || $[17] !== t4 || $[18] !== t5 || $[19] !== t6 ? (t7 = {
6951
6908
  send: t4,
6952
6909
  on: t5,
6953
6910
  readOnly,
6954
6911
  _internal: t6
6955
- }, $[15] = readOnly, $[16] = t4, $[17] = t5, $[18] = t6, $[19] = t7) : t7 = $[19], t7;
6912
+ }, $[16] = readOnly, $[17] = t4, $[18] = t5, $[19] = t6, $[20] = t7) : t7 = $[20], t7;
6956
6913
  }
6957
6914
  function _temp(s) {
6958
6915
  return s.context.readOnly;