@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
@@ -5,7 +5,7 @@ import { jsx, jsxs, Fragment } from "react/jsx-runtime";
5
5
  import { withReact, ReactEditor, Slate } from "slate-react";
6
6
  import { useSelector, useActorRef } from "@xstate/react";
7
7
  import debug$f from "debug";
8
- import { Editor, Element, Range, Point, Text, Operation, Transforms, Path, Node, createEditor as createEditor$1, deleteBackward, deleteForward, insertText } from "slate";
8
+ import { Editor, Element, Range, Point, Text, Operation, Transforms, Path, Node, createEditor as createEditor$1, select, deleteFragment, insertText, deleteBackward, deleteForward } from "slate";
9
9
  import { setup, emit, assign, fromCallback, assertEvent, enqueueActions, createActor } from "xstate";
10
10
  import isEqual from "lodash/isEqual.js";
11
11
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
@@ -13,6 +13,7 @@ import { defineType, defineField, isKeySegment, isPortableTextTextBlock, isPorta
13
13
  import flatten from "lodash/flatten.js";
14
14
  import isPlainObject from "lodash/isPlainObject.js";
15
15
  import uniq from "lodash/uniq.js";
16
+ import { parseBlock } from "./parse-blocks.js";
16
17
  import { sliceBlocks } from "./util.slice-blocks.js";
17
18
  import { htmlToBlocks } from "@portabletext/block-tools";
18
19
  import { toHTML } from "@portabletext/to-html";
@@ -2236,83 +2237,7 @@ const converterJson = {
2236
2237
  reason: "No application/x-portable-text Converter found"
2237
2238
  };
2238
2239
  }
2239
- };
2240
- function isTypedObject(object) {
2241
- return isRecord(object) && typeof object._type == "string";
2242
- }
2243
- function isRecord(value) {
2244
- return !!value && (typeof value == "object" || typeof value == "function");
2245
- }
2246
- function parseBlock({
2247
- context,
2248
- block,
2249
- options
2250
- }) {
2251
- if (!isTypedObject(block) || block._type !== context.schema.block.name && !context.schema.blockObjects.some((blockObject) => blockObject.name === block._type))
2252
- return;
2253
- if (block._type !== context.schema.block.name) {
2254
- const _key = options.refreshKeys ? context.keyGenerator() : typeof block._key == "string" ? block._key : context.keyGenerator();
2255
- return {
2256
- ...block,
2257
- _key
2258
- };
2259
- }
2260
- if (!isPortableTextTextBlock(block))
2261
- return {
2262
- _type: context.schema.block.name,
2263
- _key: options.refreshKeys ? context.keyGenerator() : typeof block._key == "string" ? block._key : context.keyGenerator(),
2264
- children: [{
2265
- _key: context.keyGenerator(),
2266
- _type: context.schema.span.name,
2267
- text: "",
2268
- marks: []
2269
- }],
2270
- markDefs: [],
2271
- style: context.schema.styles[0].value
2272
- };
2273
- const markDefKeyMap = /* @__PURE__ */ new Map(), markDefs = (block.markDefs ?? []).flatMap((markDef) => {
2274
- if (context.schema.annotations.some((annotation) => annotation.name === markDef._type)) {
2275
- const _key = options.refreshKeys ? context.keyGenerator() : markDef._key;
2276
- return markDefKeyMap.set(markDef._key, _key), [{
2277
- ...markDef,
2278
- _key
2279
- }];
2280
- }
2281
- return [];
2282
- }), children = block.children.flatMap((child) => {
2283
- if (!isTypedObject(child))
2284
- return [];
2285
- if (child._type !== context.schema.span.name && !context.schema.inlineObjects.some((inlineObject) => inlineObject.name === child._type))
2286
- return [];
2287
- if (!isPortableTextSpan$1(child))
2288
- return [{
2289
- ...child,
2290
- _key: options.refreshKeys ? context.keyGenerator() : child._key
2291
- }];
2292
- const marks = (child.marks ?? []).flatMap((mark) => markDefKeyMap.has(mark) ? [markDefKeyMap.get(mark)] : context.schema.decorators.some((decorator) => decorator.value === mark) ? [mark] : []);
2293
- return [{
2294
- ...child,
2295
- _key: options.refreshKeys ? context.keyGenerator() : child._key,
2296
- marks
2297
- }];
2298
- }), parsedBlock = {
2299
- ...block,
2300
- _key: options.refreshKeys ? context.keyGenerator() : block._key,
2301
- children: children.length > 0 ? children : [{
2302
- _key: context.keyGenerator(),
2303
- _type: context.schema.span.name,
2304
- text: "",
2305
- marks: []
2306
- }],
2307
- markDefs
2308
- };
2309
- if (!context.schema.styles.find((style) => style.value === block.style)) {
2310
- const defaultStyle = context.schema.styles[0].value;
2311
- defaultStyle !== void 0 ? parsedBlock.style = defaultStyle : delete parsedBlock.style;
2312
- }
2313
- return context.schema.lists.find((list) => list.value === block.listItem) || (delete parsedBlock.listItem, delete parsedBlock.level), parsedBlock;
2314
- }
2315
- const converterPortableText = {
2240
+ }, converterPortableText = {
2316
2241
  mimeType: "application/x-portable-text",
2317
2242
  serialize: ({
2318
2243
  context,
@@ -2742,7 +2667,7 @@ function createWithEventListeners(editorActor, subscriptions) {
2742
2667
  insertData,
2743
2668
  insertSoftBreak,
2744
2669
  insertText: insertText2,
2745
- select,
2670
+ select: select2,
2746
2671
  setFragmentData
2747
2672
  } = editor;
2748
2673
  return editor.deleteBackward = (unit) => {
@@ -2827,7 +2752,7 @@ function createWithEventListeners(editorActor, subscriptions) {
2827
2752
  });
2828
2753
  }, editor.select = (location) => {
2829
2754
  if (isApplyingBehaviorActions(editor)) {
2830
- select(location);
2755
+ select2(location);
2831
2756
  return;
2832
2757
  }
2833
2758
  const range = Editor.range(editor, location);
@@ -2839,7 +2764,7 @@ function createWithEventListeners(editorActor, subscriptions) {
2839
2764
  },
2840
2765
  editor,
2841
2766
  defaultActionCallback: () => {
2842
- select(location);
2767
+ select2(location);
2843
2768
  }
2844
2769
  });
2845
2770
  }, editor.setFragmentData = (dataTransfer, originEvent) => {
@@ -3125,15 +3050,20 @@ function setPatch(editor, patch) {
3125
3050
  node: c2
3126
3051
  });
3127
3052
  });
3128
- } else if (block && "value" in block) {
3129
- const newVal = applyAll([block.value], [patch])[0];
3130
- return Transforms.setNodes(editor, {
3131
- ...block,
3132
- value: newVal
3133
- }, {
3134
- at: blockPath
3135
- }), !0;
3136
- }
3053
+ } else if (block && "value" in block)
3054
+ if (patch.path.length > 1 && patch.path[1] !== "children") {
3055
+ const newVal = applyAll(block.value, [{
3056
+ ...patch,
3057
+ path: patch.path.slice(1)
3058
+ }]);
3059
+ Transforms.setNodes(editor, {
3060
+ ...block,
3061
+ value: newVal
3062
+ }, {
3063
+ at: blockPath
3064
+ });
3065
+ } else
3066
+ return !1;
3137
3067
  return debugState(editor, "after"), !0;
3138
3068
  }
3139
3069
  function unsetPatch(editor, patch) {
@@ -4815,6 +4745,13 @@ const blockSetBehaviorActionImplementation = ({
4815
4745
  action
4816
4746
  }) => {
4817
4747
  action.dataTransfer.setData(action.mimeType, action.data);
4748
+ }, deleteActionImplementation = ({
4749
+ action
4750
+ }) => {
4751
+ const range = toSlateRange(action.selection, action.editor);
4752
+ if (!range)
4753
+ throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(action.selection)}`);
4754
+ select(action.editor, range), deleteFragment(action.editor);
4818
4755
  }, insertBlockObjectActionImplementation = ({
4819
4756
  context,
4820
4757
  action
@@ -4924,16 +4861,10 @@ const blockSetBehaviorActionImplementation = ({
4924
4861
  always: !0
4925
4862
  });
4926
4863
  }, insertSoftBreakActionImplementation = ({
4927
- context,
4928
4864
  action
4929
4865
  }) => {
4930
- insertBreakActionImplementation({
4931
- context,
4932
- action: {
4933
- ...action,
4934
- type: "insert.break"
4935
- }
4936
- });
4866
+ insertText(action.editor, `
4867
+ `);
4937
4868
  }, insertInlineObjectActionImplementation = ({
4938
4869
  context,
4939
4870
  action
@@ -5085,6 +5016,7 @@ const blockSetBehaviorActionImplementation = ({
5085
5016
  }) => {
5086
5017
  ReactEditor.focus(action.editor);
5087
5018
  },
5019
+ delete: deleteActionImplementation,
5088
5020
  "delete.backward": ({
5089
5021
  action
5090
5022
  }) => {
@@ -5393,6 +5325,13 @@ function performDefaultAction({
5393
5325
  });
5394
5326
  break;
5395
5327
  }
5328
+ case "delete": {
5329
+ behaviorActionImplementations.delete({
5330
+ context,
5331
+ action
5332
+ });
5333
+ break;
5334
+ }
5396
5335
  case "delete.backward": {
5397
5336
  behaviorActionImplementations["delete.backward"]({
5398
5337
  context,
@@ -6668,6 +6607,16 @@ class PortableTextEditor extends Component {
6668
6607
  static toggleMark = (editor, mark) => {
6669
6608
  debug("Host toggling mark", mark), editor.editable?.toggleMark(mark);
6670
6609
  };
6610
+ /**
6611
+ * @deprecated
6612
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6613
+ *
6614
+ * ```
6615
+ * import * as selectors from '@portabletext/editor/selectors'
6616
+ * const editor = useEditor()
6617
+ * const selectedSlice = useEditorSelector(editor, selectors.getSelectedSlice)
6618
+ * ```
6619
+ */
6671
6620
  static getFragment = (editor) => (debug("Host getting fragment"), editor.editable?.getFragment());
6672
6621
  static undo = (editor) => {
6673
6622
  debug("Host undoing"), editor.editable?.undo();
@@ -6675,6 +6624,16 @@ class PortableTextEditor extends Component {
6675
6624
  static redo = (editor) => {
6676
6625
  debug("Host redoing"), editor.editable?.redo();
6677
6626
  };
6627
+ /**
6628
+ * @deprecated
6629
+ * Use built-in selectors or write your own: https://www.portabletext.org/reference/selectors/
6630
+ *
6631
+ * ```
6632
+ * import * as selectors from '@portabletext/editor/selectors'
6633
+ * const editor = useEditor()
6634
+ * const isOverlapping = useEditorSelector(editor, selectors.isOverlappingSelection(selectionB))
6635
+ * ```
6636
+ */
6678
6637
  static isSelectionsOverlapping = (editor, selectionA, selectionB) => editor.editable?.isSelectionsOverlapping(selectionA, selectionB);
6679
6638
  }
6680
6639
  function RoutePatchesObservableToEditorActor(props) {