@portabletext/editor 1.33.5 → 1.34.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.
Files changed (50) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +14 -8
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs +20 -12
  4. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  5. package/lib/_chunks-cjs/plugin.event-listener.cjs +95 -70
  6. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
  7. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs +103 -46
  8. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs.map +1 -1
  9. package/lib/_chunks-es/behavior.core.js +14 -8
  10. package/lib/_chunks-es/behavior.core.js.map +1 -1
  11. package/lib/_chunks-es/behavior.markdown.js +20 -12
  12. package/lib/_chunks-es/behavior.markdown.js.map +1 -1
  13. package/lib/_chunks-es/plugin.event-listener.js +101 -75
  14. package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
  15. package/lib/_chunks-es/util.block-offsets-to-selection.js +102 -46
  16. package/lib/_chunks-es/util.block-offsets-to-selection.js.map +1 -1
  17. package/lib/behaviors/index.d.cts +5 -34
  18. package/lib/behaviors/index.d.ts +5 -34
  19. package/lib/index.d.cts +300 -1460
  20. package/lib/index.d.ts +300 -1460
  21. package/lib/plugins/index.cjs +60 -43
  22. package/lib/plugins/index.cjs.map +1 -1
  23. package/lib/plugins/index.d.cts +300 -1460
  24. package/lib/plugins/index.d.ts +300 -1460
  25. package/lib/plugins/index.js +63 -45
  26. package/lib/plugins/index.js.map +1 -1
  27. package/lib/selectors/index.d.cts +12 -0
  28. package/lib/selectors/index.d.ts +12 -0
  29. package/lib/utils/index.cjs +23 -1
  30. package/lib/utils/index.cjs.map +1 -1
  31. package/lib/utils/index.d.cts +11 -0
  32. package/lib/utils/index.d.ts +11 -0
  33. package/lib/utils/index.js +25 -2
  34. package/lib/utils/index.js.map +1 -1
  35. package/package.json +6 -6
  36. package/src/behavior-actions/behavior.action.block.set.ts +48 -5
  37. package/src/behavior-actions/behavior.action.block.unset.ts +77 -3
  38. package/src/behavior-actions/behavior.action.decorator.add.ts +30 -8
  39. package/src/behavior-actions/behavior.actions.ts +1 -18
  40. package/src/behaviors/behavior.core.lists.ts +18 -14
  41. package/src/behaviors/behavior.markdown-emphasis.ts +82 -41
  42. package/src/behaviors/behavior.markdown.ts +14 -12
  43. package/src/behaviors/behavior.types.ts +2 -14
  44. package/src/converters/converter.portable-text.deserialize.test.ts +3 -2
  45. package/src/internal-utils/parse-blocks.test.ts +455 -0
  46. package/src/internal-utils/parse-blocks.ts +202 -87
  47. package/src/utils/index.ts +1 -0
  48. package/src/utils/util.child-selection-point-to-block-offset.ts +55 -0
  49. package/src/behavior-actions/behavior.action.text-block.set.ts +0 -25
  50. package/src/behavior-actions/behavior.action.text-block.unset.ts +0 -17
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
- var reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), useEffectEvent = require("use-effect-event"), jsxRuntime = require("react/jsx-runtime"), slateReact = require("slate-react"), react = require("@xstate/react"), debug$e = require("debug"), slate = require("slate"), xstate = require("xstate"), isEqual = require("lodash/isEqual.js"), patches = require("@portabletext/patches"), types = require("@sanity/types"), flatten = require("lodash/flatten.js"), isPlainObject = require("lodash/isPlainObject.js"), uniq = require("lodash/uniq.js"), getRandomValues = require("get-random-values-esm"), util_blockOffsetsToSelection = require("./util.block-offsets-to-selection.cjs"), util_sliceBlocks = require("./util.slice-blocks.cjs"), blockTools = require("@portabletext/block-tools"), toHtml = require("@portabletext/to-html"), schema = require("@sanity/schema"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), selector_isAtTheStartOfBlock = require("./selector.is-at-the-start-of-block.cjs"), selector_isActiveStyle = require("./selector.is-active-style.cjs"), startCase = require("lodash.startcase"), behavior_core = require("./behavior.core.cjs"), rxjs = require("rxjs");
2
+ var reactCompilerRuntime = require("react-compiler-runtime"), React = require("react"), useEffectEvent = require("use-effect-event"), jsxRuntime = require("react/jsx-runtime"), slateReact = require("slate-react"), react = require("@xstate/react"), debug$e = require("debug"), slate = require("slate"), xstate = require("xstate"), isEqual = require("lodash/isEqual.js"), patches = require("@portabletext/patches"), types = require("@sanity/types"), flatten = require("lodash/flatten.js"), isPlainObject = require("lodash/isPlainObject.js"), uniq = require("lodash/uniq.js"), getRandomValues = require("get-random-values-esm"), util_blockOffsetsToSelection = require("./util.block-offsets-to-selection.cjs"), util_sliceBlocks = require("./util.slice-blocks.cjs"), blockTools = require("@portabletext/block-tools"), toHtml = require("@portabletext/to-html"), schema = require("@sanity/schema"), get = require("lodash/get.js"), isUndefined = require("lodash/isUndefined.js"), omitBy = require("lodash/omitBy.js"), selector_isAtTheStartOfBlock = require("./selector.is-at-the-start-of-block.cjs"), selector_isActiveStyle = require("./selector.is-active-style.cjs"), omit = require("lodash/omit.js"), startCase = require("lodash.startcase"), behavior_core = require("./behavior.core.cjs"), rxjs = require("rxjs");
3
3
  function _interopDefaultCompat(e) {
4
4
  return e && typeof e == "object" && "default" in e ? e : { default: e };
5
5
  }
6
- var React__default = /* @__PURE__ */ _interopDefaultCompat(React), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$e), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy), startCase__default = /* @__PURE__ */ _interopDefaultCompat(startCase);
6
+ var React__default = /* @__PURE__ */ _interopDefaultCompat(React), debug__default = /* @__PURE__ */ _interopDefaultCompat(debug$e), isEqual__default = /* @__PURE__ */ _interopDefaultCompat(isEqual), flatten__default = /* @__PURE__ */ _interopDefaultCompat(flatten), isPlainObject__default = /* @__PURE__ */ _interopDefaultCompat(isPlainObject), uniq__default = /* @__PURE__ */ _interopDefaultCompat(uniq), getRandomValues__default = /* @__PURE__ */ _interopDefaultCompat(getRandomValues), get__default = /* @__PURE__ */ _interopDefaultCompat(get), isUndefined__default = /* @__PURE__ */ _interopDefaultCompat(isUndefined), omitBy__default = /* @__PURE__ */ _interopDefaultCompat(omitBy), omit__default = /* @__PURE__ */ _interopDefaultCompat(omit), startCase__default = /* @__PURE__ */ _interopDefaultCompat(startCase);
7
7
  function createEditorSchema(portableTextType) {
8
8
  if (!portableTextType)
9
9
  throw new Error("Parameter 'portabletextType' missing (required)");
@@ -2876,10 +2876,21 @@ const decoratorAddActionImplementation = ({
2876
2876
  context,
2877
2877
  action
2878
2878
  }) => {
2879
- const editor = action.editor, mark = action.decorator, selection = action.selection ? toSlateRange(action.selection, action.editor) ?? editor.selection : editor.selection;
2879
+ const editor = action.editor, mark = action.decorator, value = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), manualAnchor = action.offsets?.anchor ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
2880
+ value,
2881
+ blockOffset: action.offsets.anchor,
2882
+ direction: "backward"
2883
+ }) : void 0, manualFocus = action.offsets?.focus ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
2884
+ value,
2885
+ blockOffset: action.offsets.focus,
2886
+ direction: "forward"
2887
+ }) : void 0, manualSelection = manualAnchor && manualFocus ? {
2888
+ anchor: manualAnchor,
2889
+ focus: manualFocus
2890
+ } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
2880
2891
  if (!selection)
2881
2892
  return;
2882
- const value = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? util_sliceBlocks.spanSelectionPointToBlockOffset({
2893
+ const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? util_sliceBlocks.spanSelectionPointToBlockOffset({
2883
2894
  value,
2884
2895
  selectionPoint: editorSelection.anchor
2885
2896
  }) : void 0, focusOffset = editorSelection ? util_sliceBlocks.spanSelectionPointToBlockOffset({
@@ -3982,6 +3993,7 @@ function insertBlock({
3982
3993
  }
3983
3994
  }
3984
3995
  const blockSetBehaviorActionImplementation = ({
3996
+ context,
3985
3997
  action
3986
3998
  }) => {
3987
3999
  const location = toSlateRange({
@@ -3995,17 +4007,40 @@ const blockSetBehaviorActionImplementation = ({
3995
4007
  }
3996
4008
  }, action.editor);
3997
4009
  if (!location)
3998
- return;
4010
+ throw new Error(`Unable to convert ${JSON.stringify(action.at)} into a Slate Range`);
4011
+ const block = slate.Editor.node(action.editor, location, {
4012
+ depth: 1
4013
+ })?.[0];
4014
+ if (!block)
4015
+ throw new Error(`Unable to find block at ${JSON.stringify(action.at)}`);
4016
+ const parsedBlock = fromSlateValue([block], context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(action.editor)).at(0);
4017
+ if (!parsedBlock)
4018
+ throw new Error(`Unable to parse block at ${JSON.stringify(action.at)}`);
3999
4019
  const {
4000
- at,
4001
- editor,
4002
- type,
4003
- ...payload
4004
- } = action;
4005
- slate.Transforms.setNodes(action.editor, payload, {
4020
+ _type,
4021
+ ...filteredProps
4022
+ } = action.props, updatedBlock = util_blockOffsetsToSelection.parseBlock({
4023
+ context,
4024
+ block: {
4025
+ ...parsedBlock,
4026
+ ...filteredProps
4027
+ },
4028
+ options: {
4029
+ refreshKeys: !1
4030
+ }
4031
+ });
4032
+ if (!updatedBlock)
4033
+ throw new Error(`Unable to update block at ${JSON.stringify(action.at)}`);
4034
+ const slateBlock = toSlateValue([updatedBlock], {
4035
+ schemaTypes: context.schema
4036
+ })?.at(0);
4037
+ if (!slateBlock)
4038
+ throw new Error("Unable to convert block to Slate value");
4039
+ slate.Transforms.setNodes(action.editor, slateBlock, {
4006
4040
  at: location
4007
4041
  });
4008
4042
  }, blockUnsetBehaviorActionImplementation = ({
4043
+ context,
4009
4044
  action
4010
4045
  }) => {
4011
4046
  const location = toSlateRange({
@@ -4018,7 +4053,53 @@ const blockSetBehaviorActionImplementation = ({
4018
4053
  offset: 0
4019
4054
  }
4020
4055
  }, action.editor);
4021
- location && slate.Transforms.unsetNodes(action.editor, action.props, {
4056
+ if (!location)
4057
+ throw new Error(`Unable to convert ${JSON.stringify(action.at)} into a Slate Range`);
4058
+ const block = slate.Editor.node(action.editor, location, {
4059
+ depth: 1
4060
+ })?.[0];
4061
+ if (!block)
4062
+ throw new Error(`Unable to find block at ${JSON.stringify(action.at)}`);
4063
+ const parsedBlock = fromSlateValue([block], context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(action.editor)).at(0);
4064
+ if (!parsedBlock)
4065
+ throw new Error(`Unable to parse block at ${JSON.stringify(action.at)}`);
4066
+ if (util_blockOffsetsToSelection.isTextBlock(context.schema, parsedBlock)) {
4067
+ const propsToRemove = action.props.filter((prop) => prop !== "_type"), updatedTextBlock = util_blockOffsetsToSelection.parseBlock({
4068
+ context,
4069
+ block: omit__default.default(parsedBlock, propsToRemove),
4070
+ options: {
4071
+ refreshKeys: !1
4072
+ }
4073
+ });
4074
+ if (!updatedTextBlock)
4075
+ throw new Error(`Unable to update block at ${JSON.stringify(action.at)}`);
4076
+ const propsToSet = {};
4077
+ for (const prop of propsToRemove)
4078
+ prop in updatedTextBlock ? propsToSet[prop] = updatedTextBlock[prop] : propsToSet[prop] = void 0;
4079
+ slate.Transforms.setNodes(action.editor, propsToSet, {
4080
+ at: location
4081
+ });
4082
+ return;
4083
+ }
4084
+ const updatedBlockObject = util_blockOffsetsToSelection.parseBlock({
4085
+ context,
4086
+ block: omit__default.default(parsedBlock, action.props.filter((prop) => prop !== "_type")),
4087
+ options: {
4088
+ refreshKeys: !1
4089
+ }
4090
+ });
4091
+ if (!updatedBlockObject)
4092
+ throw new Error(`Unable to update block at ${JSON.stringify(action.at)}`);
4093
+ const {
4094
+ _type,
4095
+ _key,
4096
+ ...props
4097
+ } = updatedBlockObject;
4098
+ slate.Transforms.setNodes(action.editor, {
4099
+ _type,
4100
+ _key,
4101
+ value: props
4102
+ }, {
4022
4103
  at: location
4023
4104
  });
4024
4105
  }, dataTransferSetActionImplementation = ({
@@ -4198,48 +4279,6 @@ const blockSetBehaviorActionImplementation = ({
4198
4279
  editor: action.editor,
4199
4280
  schema: context.schema
4200
4281
  });
4201
- }, textBlockSetActionImplementation = ({
4202
- action
4203
- }) => {
4204
- const at = toSlateRange({
4205
- anchor: {
4206
- path: action.at,
4207
- offset: 0
4208
- },
4209
- focus: {
4210
- path: action.at,
4211
- offset: 0
4212
- }
4213
- }, action.editor);
4214
- slate.Transforms.setNodes(action.editor, {
4215
- ...action.style ? {
4216
- style: action.style
4217
- } : {},
4218
- ...action.listItem ? {
4219
- listItem: action.listItem
4220
- } : {},
4221
- ...action.level ? {
4222
- level: action.level
4223
- } : {}
4224
- }, {
4225
- at
4226
- });
4227
- }, textBlockUnsetActionImplementation = ({
4228
- action
4229
- }) => {
4230
- const at = toSlateRange({
4231
- anchor: {
4232
- path: action.at,
4233
- offset: 0
4234
- },
4235
- focus: {
4236
- path: action.at,
4237
- offset: 0
4238
- }
4239
- }, action.editor);
4240
- slate.Transforms.unsetNodes(action.editor, action.props, {
4241
- at
4242
- });
4243
4282
  }, behaviorActionImplementations = {
4244
4283
  "annotation.add": addAnnotationActionImplementation,
4245
4284
  "annotation.remove": removeAnnotationActionImplementation,
@@ -4445,9 +4484,7 @@ const blockSetBehaviorActionImplementation = ({
4445
4484
  },
4446
4485
  "style.toggle": toggleStyleActionImplementation,
4447
4486
  "style.add": addStyleActionImplementation,
4448
- "style.remove": removeStyleActionImplementation,
4449
- "text block.set": textBlockSetActionImplementation,
4450
- "text block.unset": textBlockUnsetActionImplementation
4487
+ "style.remove": removeStyleActionImplementation
4451
4488
  };
4452
4489
  function performAction({
4453
4490
  context,
@@ -4769,25 +4806,13 @@ function performDefaultAction({
4769
4806
  });
4770
4807
  break;
4771
4808
  }
4772
- case "style.toggle": {
4809
+ default: {
4773
4810
  behaviorActionImplementations["style.toggle"]({
4774
4811
  context,
4775
4812
  action
4776
4813
  });
4777
4814
  break;
4778
4815
  }
4779
- case "text block.set": {
4780
- behaviorActionImplementations["text block.set"]({
4781
- context,
4782
- action
4783
- });
4784
- break;
4785
- }
4786
- default:
4787
- behaviorActionImplementations["text block.unset"]({
4788
- context,
4789
- action
4790
- });
4791
4816
  }
4792
4817
  }
4793
4818
  function createWithEventListeners(editorActor, subscriptions) {