@portabletext/editor 1.17.1 → 1.18.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 (56) hide show
  1. package/README.md +228 -261
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  4. package/lib/_chunks-cjs/selector.is-selection-collapsed.cjs.map +1 -1
  5. package/lib/_chunks-es/behavior.core.js.map +1 -1
  6. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  7. package/lib/_chunks-es/selector.is-selection-collapsed.js.map +1 -1
  8. package/lib/behaviors/index.cjs +4 -1
  9. package/lib/behaviors/index.cjs.map +1 -1
  10. package/lib/behaviors/index.d.cts +23 -23
  11. package/lib/behaviors/index.d.ts +23 -23
  12. package/lib/behaviors/index.js +4 -1
  13. package/lib/behaviors/index.js.map +1 -1
  14. package/lib/index.cjs +78 -69
  15. package/lib/index.cjs.map +1 -1
  16. package/lib/index.d.cts +123 -753
  17. package/lib/index.d.ts +123 -753
  18. package/lib/index.js +78 -69
  19. package/lib/index.js.map +1 -1
  20. package/lib/selectors/index.cjs.map +1 -1
  21. package/lib/selectors/index.d.cts +30 -30
  22. package/lib/selectors/index.d.ts +30 -30
  23. package/lib/selectors/index.js.map +1 -1
  24. package/package.json +8 -8
  25. package/src/behaviors/behavior.code-editor.ts +2 -2
  26. package/src/behaviors/behavior.core.ts +2 -2
  27. package/src/behaviors/behavior.emoji-picker.ts +8 -3
  28. package/src/behaviors/behavior.links.ts +2 -2
  29. package/src/behaviors/behavior.markdown.ts +2 -2
  30. package/src/behaviors/behavior.types.ts +10 -10
  31. package/src/editor/PortableTextEditor.tsx +2 -0
  32. package/src/editor/__tests__/self-solving.test.tsx +14 -0
  33. package/src/editor/components/Synchronizer.tsx +6 -1
  34. package/src/editor/create-editor.ts +14 -3
  35. package/src/editor/define-schema.ts +4 -4
  36. package/src/editor/editor-event-listener.tsx +1 -1
  37. package/src/editor/editor-machine.ts +42 -52
  38. package/src/editor/editor-provider.tsx +3 -3
  39. package/src/editor/editor-selector.ts +31 -14
  40. package/src/editor/editor-snapshot.ts +2 -2
  41. package/src/editor/get-value.ts +12 -5
  42. package/src/editor/hooks/usePortableTextEditor.ts +1 -0
  43. package/src/editor/hooks/usePortableTextEditorSelection.tsx +1 -0
  44. package/src/selectors/selector.get-active-list-item.ts +1 -1
  45. package/src/selectors/selector.get-active-style.ts +1 -1
  46. package/src/selectors/selector.get-selected-spans.ts +1 -1
  47. package/src/selectors/selector.get-selection-text.ts +1 -1
  48. package/src/selectors/selector.get-text-before.ts +1 -1
  49. package/src/selectors/selector.is-active-annotation.ts +1 -1
  50. package/src/selectors/selector.is-active-decorator.ts +1 -1
  51. package/src/selectors/selector.is-active-list-item.ts +1 -1
  52. package/src/selectors/selector.is-active-style.ts +1 -1
  53. package/src/selectors/selector.is-selection-collapsed.ts +1 -1
  54. package/src/selectors/selector.is-selection-expanded.ts +1 -1
  55. package/src/selectors/selectors.ts +13 -13
  56. package/src/types/editor.ts +2 -2
package/lib/index.js CHANGED
@@ -2626,7 +2626,12 @@ function Synchronizer(props) {
2626
2626
  type: "has pending patches"
2627
2627
  }), event.type === "mutation" && (syncActorRef.send({
2628
2628
  type: "mutation"
2629
- }), editorActor.send(event));
2629
+ }), editorActor.send({
2630
+ type: "mutation",
2631
+ patches: event.patches,
2632
+ snapshot: event.snapshot,
2633
+ value: event.snapshot
2634
+ }));
2630
2635
  });
2631
2636
  return () => {
2632
2637
  subscription.unsubscribe();
@@ -5770,11 +5775,12 @@ const editorMachine = setup({
5770
5775
  if (eventBehaviors.length === 0) {
5771
5776
  if (!defaultAction)
5772
5777
  return;
5773
- enqueue.raise({
5774
- type: "behavior action intends",
5775
- editor: event.editor,
5776
- actionIntends: [defaultAction]
5777
- });
5778
+ Editor.withoutNormalizing(event.editor, () => {
5779
+ performAction({
5780
+ context,
5781
+ action: defaultAction
5782
+ });
5783
+ }), event.editor.onChange();
5778
5784
  return;
5779
5785
  }
5780
5786
  const value = fromSlateValue(event.editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(event.editor)), selection = toPortableTextRange(value, event.editor.selection, context.schema), editorContext = {
@@ -5800,10 +5806,20 @@ const editorMachine = setup({
5800
5806
  event: event.behaviorEvent
5801
5807
  }, shouldRun));
5802
5808
  for (const actionIntends of actionIntendSets)
5803
- behaviorOverwritten = behaviorOverwritten || actionIntends.length > 0 && actionIntends.some((actionIntend) => actionIntend.type !== "effect"), enqueue.raise({
5804
- type: "behavior action intends",
5805
- editor: event.editor,
5806
- actionIntends
5809
+ behaviorOverwritten = behaviorOverwritten || actionIntends.length > 0 && actionIntends.some((actionIntend) => actionIntend.type !== "effect"), Editor.withoutNormalizing(event.editor, () => {
5810
+ for (const actionIntend of actionIntends) {
5811
+ const action = {
5812
+ ...actionIntend,
5813
+ editor: event.editor
5814
+ };
5815
+ performAction({
5816
+ context,
5817
+ action
5818
+ });
5819
+ }
5820
+ }), event.editor.onChange(), actionIntends.some((actionIntend) => actionIntend.type === "reselect") && enqueue.raise({
5821
+ type: "selection",
5822
+ selection: toPortableTextRange(event.editor.children, event.editor.selection, context.schema)
5807
5823
  });
5808
5824
  if (behaviorOverwritten) {
5809
5825
  event.nativeEvent?.preventDefault();
@@ -5813,11 +5829,12 @@ const editorMachine = setup({
5813
5829
  if (!behaviorOverwritten) {
5814
5830
  if (!defaultAction)
5815
5831
  return;
5816
- enqueue.raise({
5817
- type: "behavior action intends",
5818
- editor: event.editor,
5819
- actionIntends: [defaultAction]
5820
- });
5832
+ Editor.withoutNormalizing(event.editor, () => {
5833
+ performAction({
5834
+ context,
5835
+ action: defaultAction
5836
+ });
5837
+ }), event.editor.onChange();
5821
5838
  }
5822
5839
  })
5823
5840
  }
@@ -5909,34 +5926,6 @@ const editorMachine = setup({
5909
5926
  event
5910
5927
  }) => event.maxBlocks
5911
5928
  })
5912
- },
5913
- "behavior action intends": {
5914
- actions: [({
5915
- context,
5916
- event
5917
- }) => {
5918
- Editor.withoutNormalizing(event.editor, () => {
5919
- for (const actionIntend of event.actionIntends) {
5920
- const action = {
5921
- ...actionIntend,
5922
- editor: event.editor
5923
- };
5924
- performAction({
5925
- context,
5926
- action
5927
- });
5928
- }
5929
- }), event.editor.onChange();
5930
- }, enqueueActions(({
5931
- context,
5932
- event,
5933
- enqueue
5934
- }) => {
5935
- event.actionIntends.some((actionIntend) => actionIntend.type === "reselect") && enqueue.raise({
5936
- type: "selection",
5937
- selection: toPortableTextRange(event.editor.children, event.editor.selection, context.schema)
5938
- });
5939
- })]
5940
5929
  }
5941
5930
  },
5942
5931
  type: "parallel",
@@ -6097,7 +6086,48 @@ const editorMachine = setup({
6097
6086
  }
6098
6087
  }
6099
6088
  }
6100
- }), defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
6089
+ });
6090
+ function getValue({
6091
+ editorActorSnapshot,
6092
+ slateEditorInstance
6093
+ }) {
6094
+ return fromSlateValue(slateEditorInstance.children, editorActorSnapshot.context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(slateEditorInstance));
6095
+ }
6096
+ function defaultCompare(a, b) {
6097
+ return a === b;
6098
+ }
6099
+ function useEditorSelector(editor, selector, t0) {
6100
+ const $ = c(3), compare = t0 === void 0 ? defaultCompare : t0;
6101
+ let t1;
6102
+ return $[0] !== editor._internal.slateEditor.instance || $[1] !== selector ? (t1 = (editorActorSnapshot) => {
6103
+ const snapshot = getEditorSnapshot({
6104
+ editorActorSnapshot,
6105
+ slateEditorInstance: editor._internal.slateEditor.instance
6106
+ });
6107
+ return selector(snapshot);
6108
+ }, $[0] = editor._internal.slateEditor.instance, $[1] = selector, $[2] = t1) : t1 = $[2], useSelector(editor._internal.editorActor, t1, compare);
6109
+ }
6110
+ function getEditorSnapshot({
6111
+ editorActorSnapshot,
6112
+ slateEditorInstance
6113
+ }) {
6114
+ return {
6115
+ context: {
6116
+ activeDecorators: getActiveDecorators({
6117
+ schema: editorActorSnapshot.context.schema,
6118
+ slateEditorInstance
6119
+ }),
6120
+ keyGenerator: editorActorSnapshot.context.keyGenerator,
6121
+ schema: editorActorSnapshot.context.schema,
6122
+ selection: editorActorSnapshot.context.selection,
6123
+ value: getValue({
6124
+ editorActorSnapshot,
6125
+ slateEditorInstance
6126
+ })
6127
+ }
6128
+ };
6129
+ }
6130
+ const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
6101
6131
  let table;
6102
6132
  return () => {
6103
6133
  if (table)
@@ -6149,6 +6179,10 @@ function createEditorFromActor(editorActor) {
6149
6179
  editorActor
6150
6180
  }), editable = createEditableAPI(slateEditor.instance, editorActor);
6151
6181
  return {
6182
+ getSnapshot: () => getEditorSnapshot({
6183
+ editorActorSnapshot: editorActor.getSnapshot(),
6184
+ slateEditorInstance: slateEditor.instance
6185
+ }),
6152
6186
  send: (event) => {
6153
6187
  editorActor.send(event);
6154
6188
  },
@@ -7145,31 +7179,6 @@ function EditorEventListener(props) {
7145
7179
  };
7146
7180
  }, t1 = [editor, on], $[0] = editor, $[1] = on, $[2] = t0, $[3] = t1) : (t0 = $[2], t1 = $[3]), useEffect(t0, t1), null;
7147
7181
  }
7148
- function getValue(editor) {
7149
- return fromSlateValue(editor._internal.slateEditor.instance.children, editor._internal.editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor._internal.slateEditor.instance));
7150
- }
7151
- function defaultCompare(a, b) {
7152
- return a === b;
7153
- }
7154
- function useEditorSelector(editor, selector, t0) {
7155
- const $ = c(3), compare = t0 === void 0 ? defaultCompare : t0;
7156
- let t1;
7157
- return $[0] !== editor || $[1] !== selector ? (t1 = (snapshot) => {
7158
- const context = {
7159
- activeDecorators: getActiveDecorators({
7160
- schema: snapshot.context.schema,
7161
- slateEditorInstance: editor._internal.slateEditor.instance
7162
- }),
7163
- keyGenerator: snapshot.context.keyGenerator,
7164
- schema: snapshot.context.schema,
7165
- selection: snapshot.context.selection,
7166
- value: getValue(editor)
7167
- };
7168
- return selector({
7169
- context
7170
- });
7171
- }, $[0] = editor, $[1] = selector, $[2] = t1) : t1 = $[2], useSelector(editor._internal.editorActor, t1, compare);
7172
- }
7173
7182
  export {
7174
7183
  EditorEventListener,
7175
7184
  EditorProvider,