@portabletext/editor 1.17.0 → 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 +40 -38
  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 +40 -38
  13. package/lib/behaviors/index.js.map +1 -1
  14. package/lib/index.cjs +301 -351
  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 +301 -351
  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 +10 -10
  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 +52 -41
  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
@@ -1,5 +1,7 @@
1
1
  import {useSelector} from '@xstate/react'
2
+ import type {PortableTextSlateEditor} from '../types/editor'
2
3
  import type {Editor} from './create-editor'
4
+ import type {EditorActor} from './editor-machine'
3
5
  import type {EditorSnapshot} from './editor-snapshot'
4
6
  import {getActiveDecorators} from './get-active-decorators'
5
7
  import {getValue} from './get-value'
@@ -9,12 +11,12 @@ function defaultCompare<T>(a: T, b: T) {
9
11
  }
10
12
 
11
13
  /**
12
- * @alpha
14
+ * @public
13
15
  */
14
16
  export type EditorSelector<TSelected> = (snapshot: EditorSnapshot) => TSelected
15
17
 
16
18
  /**
17
- * @alpha
19
+ * @public
18
20
  */
19
21
  export function useEditorSelector<TSelected>(
20
22
  editor: Editor,
@@ -23,20 +25,35 @@ export function useEditorSelector<TSelected>(
23
25
  ) {
24
26
  return useSelector(
25
27
  editor._internal.editorActor,
26
- (snapshot) => {
27
- const context = {
28
- activeDecorators: getActiveDecorators({
29
- schema: snapshot.context.schema,
30
- slateEditorInstance: editor._internal.slateEditor.instance,
31
- }),
32
- keyGenerator: snapshot.context.keyGenerator,
33
- schema: snapshot.context.schema,
34
- selection: snapshot.context.selection,
35
- value: getValue(editor),
36
- }
28
+ (editorActorSnapshot) => {
29
+ const snapshot = getEditorSnapshot({
30
+ editorActorSnapshot,
31
+ slateEditorInstance: editor._internal.slateEditor.instance,
32
+ })
37
33
 
38
- return selector({context})
34
+ return selector(snapshot)
39
35
  },
40
36
  compare,
41
37
  )
42
38
  }
39
+
40
+ export function getEditorSnapshot({
41
+ editorActorSnapshot,
42
+ slateEditorInstance,
43
+ }: {
44
+ editorActorSnapshot: ReturnType<EditorActor['getSnapshot']>
45
+ slateEditorInstance: PortableTextSlateEditor
46
+ }): EditorSnapshot {
47
+ return {
48
+ context: {
49
+ activeDecorators: getActiveDecorators({
50
+ schema: editorActorSnapshot.context.schema,
51
+ slateEditorInstance,
52
+ }),
53
+ keyGenerator: editorActorSnapshot.context.keyGenerator,
54
+ schema: editorActorSnapshot.context.schema,
55
+ selection: editorActorSnapshot.context.selection,
56
+ value: getValue({editorActorSnapshot, slateEditorInstance}),
57
+ },
58
+ }
59
+ }
@@ -3,7 +3,7 @@ import type {EditorSelection} from '../types/editor'
3
3
  import type {EditorSchema} from './define-schema'
4
4
 
5
5
  /**
6
- * @alpha
6
+ * @public
7
7
  */
8
8
  export type EditorContext = {
9
9
  activeDecorators: Array<string>
@@ -14,7 +14,7 @@ export type EditorContext = {
14
14
  }
15
15
 
16
16
  /**
17
- * @alpha
17
+ * @public
18
18
  */
19
19
  export type EditorSnapshot = {
20
20
  context: EditorContext
@@ -1,11 +1,18 @@
1
+ import type {PortableTextSlateEditor} from '../types/editor'
1
2
  import {fromSlateValue} from '../utils/values'
2
3
  import {KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
3
- import type {Editor} from './create-editor'
4
+ import type {EditorActor} from './editor-machine'
4
5
 
5
- export function getValue(editor: Editor) {
6
+ export function getValue({
7
+ editorActorSnapshot,
8
+ slateEditorInstance,
9
+ }: {
10
+ editorActorSnapshot: ReturnType<EditorActor['getSnapshot']>
11
+ slateEditorInstance: PortableTextSlateEditor
12
+ }) {
6
13
  return fromSlateValue(
7
- editor._internal.slateEditor.instance.children,
8
- editor._internal.editorActor.getSnapshot().context.schema.block.name,
9
- KEY_TO_VALUE_ELEMENT.get(editor._internal.slateEditor.instance),
14
+ slateEditorInstance.children,
15
+ editorActorSnapshot.context.schema.block.name,
16
+ KEY_TO_VALUE_ELEMENT.get(slateEditorInstance),
10
17
  )
11
18
  }
@@ -8,6 +8,7 @@ export const PortableTextEditorContext =
8
8
  createContext<PortableTextEditor | null>(null)
9
9
 
10
10
  /**
11
+ * @deprecated Use `useEditor` to get the current editor instance.
11
12
  * @public
12
13
  * Get the current editor object from the React context.
13
14
  */
@@ -16,6 +16,7 @@ const PortableTextEditorSelectionContext =
16
16
  createContext<EditorSelection | null>(null)
17
17
 
18
18
  /**
19
+ * @deprecated Use `useEditorSelector` to get the current editor selection.
19
20
  * @public
20
21
  * Get the current editor selection from the React context.
21
22
  */
@@ -4,7 +4,7 @@ import type {EditorSelector} from '../editor/editor-selector'
4
4
  import {getSelectedBlocks} from './selectors'
5
5
 
6
6
  /**
7
- * @alpha
7
+ * @public
8
8
  */
9
9
  export const getActiveListItem: EditorSelector<
10
10
  PortableTextListBlock['listItem'] | undefined
@@ -4,7 +4,7 @@ import type {EditorSelector} from '../editor/editor-selector'
4
4
  import {getSelectedBlocks} from './selectors'
5
5
 
6
6
  /**
7
- * @alpha
7
+ * @public
8
8
  */
9
9
  export const getActiveStyle: EditorSelector<PortableTextTextBlock['style']> = ({
10
10
  context,
@@ -8,7 +8,7 @@ import {
8
8
  import type {EditorSelector} from '../editor/editor-selector'
9
9
 
10
10
  /**
11
- * @alpha
11
+ * @public
12
12
  */
13
13
  export const getSelectedSpans: EditorSelector<
14
14
  Array<{
@@ -4,7 +4,7 @@ import {isKeyedSegment} from '../editor/utils/utils.is-keyed-segment'
4
4
  import {reverseSelection} from '../editor/utils/utils.reverse-selection'
5
5
 
6
6
  /**
7
- * @alpha
7
+ * @public
8
8
  */
9
9
  export const getSelectionText: EditorSelector<string> = ({context}) => {
10
10
  let text = ''
@@ -5,7 +5,7 @@ import {reverseSelection} from '../editor/utils/utils.reverse-selection'
5
5
  import {getSelectionText} from './selector.get-selection-text'
6
6
 
7
7
  /**
8
- * @alpha
8
+ * @public
9
9
  */
10
10
  export const getBlockTextBefore: EditorSelector<string> = ({context}) => {
11
11
  if (!context.selection) {
@@ -4,7 +4,7 @@ import {getSelectedSpans} from './selector.get-selected-spans'
4
4
  import {getSelectedBlocks} from './selectors'
5
5
 
6
6
  /**
7
- * @alpha
7
+ * @public
8
8
  */
9
9
  export function isActiveAnnotation(
10
10
  annotation: string,
@@ -3,7 +3,7 @@ import {getSelectedSpans} from './selector.get-selected-spans'
3
3
  import {isSelectionExpanded} from './selector.is-selection-expanded'
4
4
 
5
5
  /**
6
- * @alpha
6
+ * @public
7
7
  */
8
8
  export function isActiveDecorator(decorator: string): EditorSelector<boolean> {
9
9
  return (snapshot) => {
@@ -2,7 +2,7 @@ import type {EditorSelector} from '../editor/editor-selector'
2
2
  import {getActiveListItem} from './selector.get-active-list-item'
3
3
 
4
4
  /**
5
- * @alpha
5
+ * @public
6
6
  */
7
7
  export function isActiveListItem(listItem: string): EditorSelector<boolean> {
8
8
  return (snapshot) => {
@@ -2,7 +2,7 @@ import type {EditorSelector} from '../editor/editor-selector'
2
2
  import {getActiveStyle} from './selector.get-active-style'
3
3
 
4
4
  /**
5
- * @alpha
5
+ * @public
6
6
  */
7
7
  export function isActiveStyle(style: string): EditorSelector<boolean> {
8
8
  return (snapshot) => {
@@ -1,7 +1,7 @@
1
1
  import type {EditorSelector} from '../editor/editor-selector'
2
2
 
3
3
  /**
4
- * @alpha
4
+ * @public
5
5
  */
6
6
  export const isSelectionCollapsed: EditorSelector<boolean> = ({context}) => {
7
7
  return (
@@ -2,7 +2,7 @@ import type {EditorSelector} from '../editor/editor-selector'
2
2
  import {isSelectionCollapsed} from './selector.is-selection-collapsed'
3
3
 
4
4
  /**
5
- * @alpha
5
+ * @public
6
6
  */
7
7
  export const isSelectionExpanded: EditorSelector<boolean> = ({context}) => {
8
8
  return !isSelectionCollapsed({context})
@@ -13,7 +13,7 @@ import {createGuards} from '../behavior-actions/behavior.guards'
13
13
  import type {EditorSelector} from '../editor/editor-selector'
14
14
 
15
15
  /**
16
- * @alpha
16
+ * @public
17
17
  */
18
18
  export const getFocusBlock: EditorSelector<
19
19
  {node: PortableTextBlock; path: [KeyedSegment]} | undefined
@@ -32,7 +32,7 @@ export const getFocusBlock: EditorSelector<
32
32
  }
33
33
 
34
34
  /**
35
- * @alpha
35
+ * @public
36
36
  */
37
37
  export const getFocusListBlock: EditorSelector<
38
38
  {node: PortableTextListBlock; path: [KeyedSegment]} | undefined
@@ -46,7 +46,7 @@ export const getFocusListBlock: EditorSelector<
46
46
  }
47
47
 
48
48
  /**
49
- * @alpha
49
+ * @public
50
50
  */
51
51
  export const getFocusTextBlock: EditorSelector<
52
52
  {node: PortableTextTextBlock; path: [KeyedSegment]} | undefined
@@ -59,7 +59,7 @@ export const getFocusTextBlock: EditorSelector<
59
59
  }
60
60
 
61
61
  /**
62
- * @alpha
62
+ * @public
63
63
  */
64
64
  export const getFocusBlockObject: EditorSelector<
65
65
  {node: PortableTextObject; path: [KeyedSegment]} | undefined
@@ -72,7 +72,7 @@ export const getFocusBlockObject: EditorSelector<
72
72
  }
73
73
 
74
74
  /**
75
- * @alpha
75
+ * @public
76
76
  */
77
77
  export const getFocusChild: EditorSelector<
78
78
  | {
@@ -103,7 +103,7 @@ export const getFocusChild: EditorSelector<
103
103
  }
104
104
 
105
105
  /**
106
- * @alpha
106
+ * @public
107
107
  */
108
108
  export const getFocusSpan: EditorSelector<
109
109
  | {node: PortableTextSpan; path: [KeyedSegment, 'children', KeyedSegment]}
@@ -117,7 +117,7 @@ export const getFocusSpan: EditorSelector<
117
117
  }
118
118
 
119
119
  /**
120
- * @alpha
120
+ * @public
121
121
  */
122
122
  export const getFirstBlock: EditorSelector<
123
123
  {node: PortableTextBlock; path: [KeyedSegment]} | undefined
@@ -128,7 +128,7 @@ export const getFirstBlock: EditorSelector<
128
128
  }
129
129
 
130
130
  /**
131
- * @alpha
131
+ * @public
132
132
  */
133
133
  export const getLastBlock: EditorSelector<
134
134
  {node: PortableTextBlock; path: [KeyedSegment]} | undefined
@@ -141,7 +141,7 @@ export const getLastBlock: EditorSelector<
141
141
  }
142
142
 
143
143
  /**
144
- * @alpha
144
+ * @public
145
145
  */
146
146
  export const getSelectedBlocks: EditorSelector<
147
147
  Array<{node: PortableTextBlock; path: [KeyedSegment]}>
@@ -195,7 +195,7 @@ export const getSelectedBlocks: EditorSelector<
195
195
  }
196
196
 
197
197
  /**
198
- * @alpha
198
+ * @public
199
199
  */
200
200
  export const getSelectionStartBlock: EditorSelector<
201
201
  | {
@@ -224,7 +224,7 @@ export const getSelectionStartBlock: EditorSelector<
224
224
  }
225
225
 
226
226
  /**
227
- * @alpha
227
+ * @public
228
228
  */
229
229
  export const getSelectionEndBlock: EditorSelector<
230
230
  | {
@@ -253,7 +253,7 @@ export const getSelectionEndBlock: EditorSelector<
253
253
  }
254
254
 
255
255
  /**
256
- * @alpha
256
+ * @public
257
257
  */
258
258
  export const getPreviousBlock: EditorSelector<
259
259
  {node: PortableTextBlock; path: [KeyedSegment]} | undefined
@@ -284,7 +284,7 @@ export const getPreviousBlock: EditorSelector<
284
284
  }
285
285
 
286
286
  /**
287
- * @alpha
287
+ * @public
288
288
  */
289
289
  export const getNextBlock: EditorSelector<
290
290
  {node: PortableTextBlock; path: [KeyedSegment]} | undefined
@@ -113,9 +113,9 @@ export interface History {
113
113
  undos: HistoryItem[]
114
114
  }
115
115
 
116
- /** @beta */
116
+ /** @public */
117
117
  export type EditorSelectionPoint = {path: Path; offset: number}
118
- /** @beta */
118
+ /** @public */
119
119
  export type EditorSelection = {
120
120
  anchor: EditorSelectionPoint
121
121
  focus: EditorSelectionPoint