@portabletext/editor 1.28.0 → 1.30.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 (61) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +40 -37
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/parse-blocks.cjs +79 -0
  4. package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -0
  5. package/lib/_chunks-cjs/plugin.event-listener.cjs +55 -97
  6. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
  7. package/lib/_chunks-cjs/selector.get-selection-start-point.cjs +15 -0
  8. package/lib/_chunks-cjs/selector.get-selection-start-point.cjs.map +1 -0
  9. package/lib/_chunks-es/behavior.core.js +40 -37
  10. package/lib/_chunks-es/behavior.core.js.map +1 -1
  11. package/lib/_chunks-es/parse-blocks.js +80 -0
  12. package/lib/_chunks-es/parse-blocks.js.map +1 -0
  13. package/lib/_chunks-es/plugin.event-listener.js +57 -98
  14. package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
  15. package/lib/_chunks-es/selector.get-selection-start-point.js +16 -0
  16. package/lib/_chunks-es/selector.get-selection-start-point.js.map +1 -0
  17. package/lib/behaviors/index.d.cts +196 -124
  18. package/lib/behaviors/index.d.ts +196 -124
  19. package/lib/index.d.cts +248 -0
  20. package/lib/index.d.ts +248 -0
  21. package/lib/plugins/index.cjs +249 -1
  22. package/lib/plugins/index.cjs.map +1 -1
  23. package/lib/plugins/index.d.cts +246 -1
  24. package/lib/plugins/index.d.ts +246 -1
  25. package/lib/plugins/index.js +257 -3
  26. package/lib/plugins/index.js.map +1 -1
  27. package/lib/selectors/index.cjs +28 -1
  28. package/lib/selectors/index.cjs.map +1 -1
  29. package/lib/selectors/index.d.cts +21 -0
  30. package/lib/selectors/index.d.ts +21 -0
  31. package/lib/selectors/index.js +28 -0
  32. package/lib/selectors/index.js.map +1 -1
  33. package/lib/utils/index.cjs +70 -1
  34. package/lib/utils/index.cjs.map +1 -1
  35. package/lib/utils/index.d.cts +168 -2
  36. package/lib/utils/index.d.ts +168 -2
  37. package/lib/utils/index.js +71 -1
  38. package/lib/utils/index.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/behavior-actions/behavior.action.delete.ts +18 -0
  41. package/src/behavior-actions/behavior.action.insert-break.ts +3 -8
  42. package/src/behavior-actions/behavior.actions.ts +9 -0
  43. package/src/behaviors/_exports/index.ts +1 -0
  44. package/src/behaviors/behavior.core.deserialize.ts +52 -38
  45. package/src/behaviors/behavior.core.ts +4 -11
  46. package/src/behaviors/behavior.types.ts +4 -0
  47. package/src/editor/PortableTextEditor.tsx +20 -0
  48. package/src/internal-utils/__tests__/patchToOperations.test.ts +19 -21
  49. package/src/internal-utils/applyPatch.ts +11 -3
  50. package/src/plugins/index.ts +2 -0
  51. package/src/plugins/plugin.behavior.tsx +22 -0
  52. package/src/plugins/plugin.one-line.tsx +225 -0
  53. package/src/selectors/index.ts +3 -0
  54. package/src/selectors/selector.get-selection-end-point.ts +17 -0
  55. package/src/selectors/selector.get-selection-start-point.ts +17 -0
  56. package/src/selectors/selector.is-overlapping-selection.ts +46 -0
  57. package/src/utils/index.ts +4 -0
  58. package/src/utils/util.is-span.ts +12 -0
  59. package/src/utils/util.is-text-block.ts +12 -0
  60. package/src/utils/util.merge-text-blocks.ts +36 -0
  61. package/src/utils/util.split-text-block.ts +55 -0
@@ -1,5 +1,5 @@
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$f = 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"), 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"), startCase = require("lodash.startcase"), selector_isAtTheStartOfBlock = require("./selector.is-at-the-start-of-block.cjs"), util_isEmptyTextBlock = require("./util.is-empty-text-block.cjs"), behavior_core = require("./behavior.core.cjs"), getRandomValues = require("get-random-values-esm"), 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$f = 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"), parseBlocks = require("./parse-blocks.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"), startCase = require("lodash.startcase"), selector_isAtTheStartOfBlock = require("./selector.is-at-the-start-of-block.cjs"), util_isEmptyTextBlock = require("./util.is-empty-text-block.cjs"), behavior_core = require("./behavior.core.cjs"), getRandomValues = require("get-random-values-esm"), rxjs = require("rxjs");
3
3
  function _interopDefaultCompat(e) {
4
4
  return e && typeof e == "object" && "default" in e ? e : { default: e };
5
5
  }
@@ -2214,83 +2214,7 @@ const converterJson = {
2214
2214
  reason: "No application/x-portable-text Converter found"
2215
2215
  };
2216
2216
  }
2217
- };
2218
- function isTypedObject(object) {
2219
- return isRecord(object) && typeof object._type == "string";
2220
- }
2221
- function isRecord(value) {
2222
- return !!value && (typeof value == "object" || typeof value == "function");
2223
- }
2224
- function parseBlock({
2225
- context,
2226
- block,
2227
- options
2228
- }) {
2229
- if (!isTypedObject(block) || block._type !== context.schema.block.name && !context.schema.blockObjects.some((blockObject) => blockObject.name === block._type))
2230
- return;
2231
- if (block._type !== context.schema.block.name) {
2232
- const _key = options.refreshKeys ? context.keyGenerator() : typeof block._key == "string" ? block._key : context.keyGenerator();
2233
- return {
2234
- ...block,
2235
- _key
2236
- };
2237
- }
2238
- if (!types.isPortableTextTextBlock(block))
2239
- return {
2240
- _type: context.schema.block.name,
2241
- _key: options.refreshKeys ? context.keyGenerator() : typeof block._key == "string" ? block._key : context.keyGenerator(),
2242
- children: [{
2243
- _key: context.keyGenerator(),
2244
- _type: context.schema.span.name,
2245
- text: "",
2246
- marks: []
2247
- }],
2248
- markDefs: [],
2249
- style: context.schema.styles[0].value
2250
- };
2251
- const markDefKeyMap = /* @__PURE__ */ new Map(), markDefs = (block.markDefs ?? []).flatMap((markDef) => {
2252
- if (context.schema.annotations.some((annotation) => annotation.name === markDef._type)) {
2253
- const _key = options.refreshKeys ? context.keyGenerator() : markDef._key;
2254
- return markDefKeyMap.set(markDef._key, _key), [{
2255
- ...markDef,
2256
- _key
2257
- }];
2258
- }
2259
- return [];
2260
- }), children = block.children.flatMap((child) => {
2261
- if (!isTypedObject(child))
2262
- return [];
2263
- if (child._type !== context.schema.span.name && !context.schema.inlineObjects.some((inlineObject) => inlineObject.name === child._type))
2264
- return [];
2265
- if (!types.isPortableTextSpan(child))
2266
- return [{
2267
- ...child,
2268
- _key: options.refreshKeys ? context.keyGenerator() : child._key
2269
- }];
2270
- const marks = (child.marks ?? []).flatMap((mark) => markDefKeyMap.has(mark) ? [markDefKeyMap.get(mark)] : context.schema.decorators.some((decorator) => decorator.value === mark) ? [mark] : []);
2271
- return [{
2272
- ...child,
2273
- _key: options.refreshKeys ? context.keyGenerator() : child._key,
2274
- marks
2275
- }];
2276
- }), parsedBlock = {
2277
- ...block,
2278
- _key: options.refreshKeys ? context.keyGenerator() : block._key,
2279
- children: children.length > 0 ? children : [{
2280
- _key: context.keyGenerator(),
2281
- _type: context.schema.span.name,
2282
- text: "",
2283
- marks: []
2284
- }],
2285
- markDefs
2286
- };
2287
- if (!context.schema.styles.find((style) => style.value === block.style)) {
2288
- const defaultStyle = context.schema.styles[0].value;
2289
- defaultStyle !== void 0 ? parsedBlock.style = defaultStyle : delete parsedBlock.style;
2290
- }
2291
- return context.schema.lists.find((list) => list.value === block.listItem) || (delete parsedBlock.listItem, delete parsedBlock.level), parsedBlock;
2292
- }
2293
- const converterPortableText = {
2217
+ }, converterPortableText = {
2294
2218
  mimeType: "application/x-portable-text",
2295
2219
  serialize: ({
2296
2220
  context,
@@ -2326,7 +2250,7 @@ const converterPortableText = {
2326
2250
  reason: "Data is not an array"
2327
2251
  };
2328
2252
  const parsedBlocks = blocks.flatMap((block) => {
2329
- const parsedBlock = parseBlock({
2253
+ const parsedBlock = parseBlocks.parseBlock({
2330
2254
  context,
2331
2255
  block,
2332
2256
  options: {
@@ -3103,15 +3027,20 @@ function setPatch(editor, patch) {
3103
3027
  node: c
3104
3028
  });
3105
3029
  });
3106
- } else if (block && "value" in block) {
3107
- const newVal = patches.applyAll([block.value], [patch])[0];
3108
- return slate.Transforms.setNodes(editor, {
3109
- ...block,
3110
- value: newVal
3111
- }, {
3112
- at: blockPath
3113
- }), !0;
3114
- }
3030
+ } else if (block && "value" in block)
3031
+ if (patch.path.length > 1 && patch.path[1] !== "children") {
3032
+ const newVal = patches.applyAll(block.value, [{
3033
+ ...patch,
3034
+ path: patch.path.slice(1)
3035
+ }]);
3036
+ slate.Transforms.setNodes(editor, {
3037
+ ...block,
3038
+ value: newVal
3039
+ }, {
3040
+ at: blockPath
3041
+ });
3042
+ } else
3043
+ return !1;
3115
3044
  return debugState(editor, "after"), !0;
3116
3045
  }
3117
3046
  function unsetPatch(editor, patch) {
@@ -4793,6 +4722,13 @@ const blockSetBehaviorActionImplementation = ({
4793
4722
  action
4794
4723
  }) => {
4795
4724
  action.dataTransfer.setData(action.mimeType, action.data);
4725
+ }, deleteActionImplementation = ({
4726
+ action
4727
+ }) => {
4728
+ const range = toSlateRange(action.selection, action.editor);
4729
+ if (!range)
4730
+ throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(action.selection)}`);
4731
+ slate.select(action.editor, range), slate.deleteFragment(action.editor);
4796
4732
  }, insertBlockObjectActionImplementation = ({
4797
4733
  context,
4798
4734
  action
@@ -4902,16 +4838,10 @@ const blockSetBehaviorActionImplementation = ({
4902
4838
  always: !0
4903
4839
  });
4904
4840
  }, insertSoftBreakActionImplementation = ({
4905
- context,
4906
4841
  action
4907
4842
  }) => {
4908
- insertBreakActionImplementation({
4909
- context,
4910
- action: {
4911
- ...action,
4912
- type: "insert.break"
4913
- }
4914
- });
4843
+ slate.insertText(action.editor, `
4844
+ `);
4915
4845
  }, insertInlineObjectActionImplementation = ({
4916
4846
  context,
4917
4847
  action
@@ -4981,7 +4911,7 @@ const blockSetBehaviorActionImplementation = ({
4981
4911
  context,
4982
4912
  action
4983
4913
  }) => {
4984
- const parsedBlock = parseBlock({
4914
+ const parsedBlock = parseBlocks.parseBlock({
4985
4915
  block: action.block,
4986
4916
  context,
4987
4917
  options: {
@@ -5063,6 +4993,7 @@ const blockSetBehaviorActionImplementation = ({
5063
4993
  }) => {
5064
4994
  slateReact.ReactEditor.focus(action.editor);
5065
4995
  },
4996
+ delete: deleteActionImplementation,
5066
4997
  "delete.backward": ({
5067
4998
  action
5068
4999
  }) => {
@@ -5371,6 +5302,13 @@ function performDefaultAction({
5371
5302
  });
5372
5303
  break;
5373
5304
  }
5305
+ case "delete": {
5306
+ behaviorActionImplementations.delete({
5307
+ context,
5308
+ action
5309
+ });
5310
+ break;
5311
+ }
5374
5312
  case "delete.backward": {
5375
5313
  behaviorActionImplementations["delete.backward"]({
5376
5314
  context,
@@ -6646,6 +6584,16 @@ class PortableTextEditor extends React.Component {
6646
6584
  static toggleMark = (editor, mark) => {
6647
6585
  debug("Host toggling mark", mark), editor.editable?.toggleMark(mark);
6648
6586
  };
6587
+ /**
6588
+ * @deprecated
6589
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6590
+ *
6591
+ * ```
6592
+ * import * as selectors from '@portabletext/editor/selectors'
6593
+ * const editor = useEditor()
6594
+ * const selectedSlice = useEditorSelector(editor, selectors.getSelectedSlice)
6595
+ * ```
6596
+ */
6649
6597
  static getFragment = (editor) => (debug("Host getting fragment"), editor.editable?.getFragment());
6650
6598
  static undo = (editor) => {
6651
6599
  debug("Host undoing"), editor.editable?.undo();
@@ -6653,6 +6601,16 @@ class PortableTextEditor extends React.Component {
6653
6601
  static redo = (editor) => {
6654
6602
  debug("Host redoing"), editor.editable?.redo();
6655
6603
  };
6604
+ /**
6605
+ * @deprecated
6606
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6607
+ *
6608
+ * ```
6609
+ * import * as selectors from '@portabletext/editor/selectors'
6610
+ * const editor = useEditor()
6611
+ * const isOverlapping = useEditorSelector(editor, selectors.isOverlappingSelection(selectionB))
6612
+ * ```
6613
+ */
6656
6614
  static isSelectionsOverlapping = (editor, selectionA, selectionB) => editor.editable?.isSelectionsOverlapping(selectionA, selectionB);
6657
6615
  }
6658
6616
  function RoutePatchesObservableToEditorActor(props) {