@portabletext/editor 3.3.2 → 3.3.4

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 (307) hide show
  1. package/lib/_chunks-dts/index.d.ts +7 -1
  2. package/package.json +16 -19
  3. package/src/behaviors/_exports/index.ts +0 -1
  4. package/src/behaviors/behavior.abstract.annotation.ts +0 -77
  5. package/src/behaviors/behavior.abstract.decorator.ts +0 -39
  6. package/src/behaviors/behavior.abstract.delete.ts +0 -273
  7. package/src/behaviors/behavior.abstract.deserialize.ts +0 -232
  8. package/src/behaviors/behavior.abstract.insert.ts +0 -525
  9. package/src/behaviors/behavior.abstract.keyboard.ts +0 -189
  10. package/src/behaviors/behavior.abstract.list-item.ts +0 -70
  11. package/src/behaviors/behavior.abstract.move.ts +0 -79
  12. package/src/behaviors/behavior.abstract.select.ts +0 -118
  13. package/src/behaviors/behavior.abstract.serialize.ts +0 -96
  14. package/src/behaviors/behavior.abstract.split.ts +0 -170
  15. package/src/behaviors/behavior.abstract.style.ts +0 -55
  16. package/src/behaviors/behavior.abstract.ts +0 -139
  17. package/src/behaviors/behavior.config.ts +0 -7
  18. package/src/behaviors/behavior.core.annotations.ts +0 -62
  19. package/src/behaviors/behavior.core.block-element.ts +0 -116
  20. package/src/behaviors/behavior.core.block-objects.ts +0 -285
  21. package/src/behaviors/behavior.core.decorators.ts +0 -44
  22. package/src/behaviors/behavior.core.dnd.ts +0 -356
  23. package/src/behaviors/behavior.core.insert-break.ts +0 -266
  24. package/src/behaviors/behavior.core.insert.ts +0 -52
  25. package/src/behaviors/behavior.core.lists.ts +0 -691
  26. package/src/behaviors/behavior.core.ts +0 -44
  27. package/src/behaviors/behavior.perform-event.ts +0 -354
  28. package/src/behaviors/behavior.types.action.ts +0 -102
  29. package/src/behaviors/behavior.types.behavior.ts +0 -83
  30. package/src/behaviors/behavior.types.event.ts +0 -667
  31. package/src/behaviors/behavior.types.guard.ts +0 -11
  32. package/src/behaviors/index.ts +0 -17
  33. package/src/converters/converter.json.ts +0 -53
  34. package/src/converters/converter.portable-text.deserialize.test.ts +0 -680
  35. package/src/converters/converter.portable-text.ts +0 -75
  36. package/src/converters/converter.text-html.deserialize.test.ts +0 -406
  37. package/src/converters/converter.text-html.serialize.test.ts +0 -246
  38. package/src/converters/converter.text-html.ts +0 -87
  39. package/src/converters/converter.text-markdown.ts +0 -67
  40. package/src/converters/converter.text-plain.test.ts +0 -245
  41. package/src/converters/converter.text-plain.ts +0 -126
  42. package/src/converters/converter.types.ts +0 -72
  43. package/src/converters/converters.core.ts +0 -18
  44. package/src/editor/Editable.tsx +0 -1012
  45. package/src/editor/PortableTextEditor.tsx +0 -791
  46. package/src/editor/components/drop-indicator.tsx +0 -17
  47. package/src/editor/components/render-block-object.tsx +0 -121
  48. package/src/editor/components/render-default-object.tsx +0 -21
  49. package/src/editor/components/render-element.tsx +0 -88
  50. package/src/editor/components/render-inline-object.tsx +0 -129
  51. package/src/editor/components/render-leaf.tsx +0 -64
  52. package/src/editor/components/render-span.tsx +0 -303
  53. package/src/editor/components/render-text-block.tsx +0 -265
  54. package/src/editor/components/render-text.tsx +0 -18
  55. package/src/editor/components/use-core-block-element-behaviors.ts +0 -39
  56. package/src/editor/create-editor.ts +0 -323
  57. package/src/editor/create-slate-editor.tsx +0 -64
  58. package/src/editor/editor-actor-context.ts +0 -4
  59. package/src/editor/editor-context.tsx +0 -7
  60. package/src/editor/editor-dom.ts +0 -220
  61. package/src/editor/editor-machine.ts +0 -751
  62. package/src/editor/editor-provider.tsx +0 -111
  63. package/src/editor/editor-schema.ts +0 -6
  64. package/src/editor/editor-selector.ts +0 -89
  65. package/src/editor/editor-snapshot.ts +0 -68
  66. package/src/editor/event-to-change.tsx +0 -49
  67. package/src/editor/hooks/usePortableTextEditor.ts +0 -25
  68. package/src/editor/hooks/usePortableTextEditorSelection.tsx +0 -28
  69. package/src/editor/mutation-machine.ts +0 -322
  70. package/src/editor/plugins/create-with-event-listeners.ts +0 -271
  71. package/src/editor/plugins/createWithEditableAPI.ts +0 -529
  72. package/src/editor/plugins/createWithHotKeys.ts +0 -68
  73. package/src/editor/plugins/createWithObjectKeys.ts +0 -289
  74. package/src/editor/plugins/createWithPatches.ts +0 -272
  75. package/src/editor/plugins/createWithPortableTextMarkModel.ts +0 -559
  76. package/src/editor/plugins/createWithSchemaTypes.ts +0 -121
  77. package/src/editor/plugins/slate-plugin.update-selection.ts +0 -51
  78. package/src/editor/plugins/slate-plugin.update-value.ts +0 -46
  79. package/src/editor/plugins/with-plugins.ts +0 -69
  80. package/src/editor/range-decorations-machine.ts +0 -421
  81. package/src/editor/relay-actor-context.ts +0 -4
  82. package/src/editor/relay-machine.ts +0 -152
  83. package/src/editor/sync-machine.ts +0 -961
  84. package/src/editor/use-editor.ts +0 -27
  85. package/src/editor/validate-selection-machine.test.ts +0 -47
  86. package/src/editor/validate-selection-machine.ts +0 -149
  87. package/src/editor/weakMaps.ts +0 -15
  88. package/src/editor/with-normalizing-node.ts +0 -14
  89. package/src/editor/with-performing-behavior-operation.ts +0 -21
  90. package/src/editor/withChanges.ts +0 -13
  91. package/src/editor/without-normalizing-conditional.ts +0 -13
  92. package/src/editor/withoutPatching.ts +0 -14
  93. package/src/editor.ts +0 -59
  94. package/src/history/behavior.operation.history.redo.ts +0 -67
  95. package/src/history/behavior.operation.history.undo.ts +0 -71
  96. package/src/history/event.history.undo.test.tsx +0 -672
  97. package/src/history/history.preserving-keys.test.tsx +0 -112
  98. package/src/history/remote-patches.ts +0 -20
  99. package/src/history/slate-plugin.history.ts +0 -142
  100. package/src/history/slate-plugin.redoing.ts +0 -21
  101. package/src/history/slate-plugin.undoing.ts +0 -21
  102. package/src/history/slate-plugin.without-history.ts +0 -23
  103. package/src/history/transform-operation.ts +0 -245
  104. package/src/history/undo-redo-collaboration.test.tsx +0 -541
  105. package/src/history/undo-redo.feature +0 -125
  106. package/src/history/undo-redo.test.tsx +0 -195
  107. package/src/history/undo-step.ts +0 -148
  108. package/src/index.ts +0 -98
  109. package/src/internal-utils/__tests__/ranges.test.ts +0 -23
  110. package/src/internal-utils/__tests__/values.test.ts +0 -110
  111. package/src/internal-utils/apply-operation-to-portable-text.test.ts +0 -1861
  112. package/src/internal-utils/apply-operation-to-portable-text.ts +0 -615
  113. package/src/internal-utils/applyPatch.ts +0 -644
  114. package/src/internal-utils/block-keys.ts +0 -9
  115. package/src/internal-utils/build-index-maps.test.ts +0 -464
  116. package/src/internal-utils/build-index-maps.ts +0 -131
  117. package/src/internal-utils/collapse-selection.ts +0 -36
  118. package/src/internal-utils/compound-client-rect.ts +0 -28
  119. package/src/internal-utils/create-placeholder-block.ts +0 -21
  120. package/src/internal-utils/create-test-snapshot.ts +0 -28
  121. package/src/internal-utils/debug.ts +0 -12
  122. package/src/internal-utils/editor-selection.test.ts +0 -44
  123. package/src/internal-utils/editor-selection.ts +0 -56
  124. package/src/internal-utils/event-position.ts +0 -318
  125. package/src/internal-utils/global-scope.ts +0 -27
  126. package/src/internal-utils/globally-scoped-context.ts +0 -39
  127. package/src/internal-utils/is-hotkey.test.ts +0 -114
  128. package/src/internal-utils/is-hotkey.ts +0 -209
  129. package/src/internal-utils/mime-type.ts +0 -1
  130. package/src/internal-utils/move-range-by-operation.ts +0 -19
  131. package/src/internal-utils/operation-to-patches.test.ts +0 -522
  132. package/src/internal-utils/operation-to-patches.ts +0 -571
  133. package/src/internal-utils/portable-text-node.ts +0 -209
  134. package/src/internal-utils/schema.ts +0 -8
  135. package/src/internal-utils/selection-block-keys.ts +0 -20
  136. package/src/internal-utils/selection-focus-text.ts +0 -40
  137. package/src/internal-utils/selection-text.test.ts +0 -32
  138. package/src/internal-utils/selection-text.ts +0 -21
  139. package/src/internal-utils/selection.ts +0 -77
  140. package/src/internal-utils/sibling-utils.ts +0 -55
  141. package/src/internal-utils/slate-utils.test.tsx +0 -121
  142. package/src/internal-utils/slate-utils.ts +0 -417
  143. package/src/internal-utils/split-string.ts +0 -12
  144. package/src/internal-utils/stop-actor.ts +0 -43
  145. package/src/internal-utils/string-overlap.test.ts +0 -14
  146. package/src/internal-utils/string-overlap.ts +0 -28
  147. package/src/internal-utils/string-utils.ts +0 -7
  148. package/src/internal-utils/text-block-key.test.ts +0 -41
  149. package/src/internal-utils/text-block-key.ts +0 -26
  150. package/src/internal-utils/text-marks.test.ts +0 -41
  151. package/src/internal-utils/text-marks.ts +0 -22
  152. package/src/internal-utils/text-selection.test.ts +0 -211
  153. package/src/internal-utils/text-selection.ts +0 -121
  154. package/src/internal-utils/to-slate-range.test.ts +0 -278
  155. package/src/internal-utils/to-slate-range.ts +0 -171
  156. package/src/internal-utils/validateValue.ts +0 -443
  157. package/src/internal-utils/value-annotations.ts +0 -33
  158. package/src/internal-utils/values.test.ts +0 -282
  159. package/src/internal-utils/values.ts +0 -266
  160. package/src/keyboard-shortcuts/default-keyboard-shortcuts.ts +0 -146
  161. package/src/operations/behavior.operation.annotation.add.ts +0 -99
  162. package/src/operations/behavior.operation.annotation.remove.ts +0 -150
  163. package/src/operations/behavior.operation.block.set.ts +0 -104
  164. package/src/operations/behavior.operation.block.unset.ts +0 -54
  165. package/src/operations/behavior.operation.child.set.ts +0 -107
  166. package/src/operations/behavior.operation.child.unset.ts +0 -116
  167. package/src/operations/behavior.operation.decorator.add.ts +0 -131
  168. package/src/operations/behavior.operation.delete.ts +0 -294
  169. package/src/operations/behavior.operation.insert.block.ts +0 -495
  170. package/src/operations/behavior.operation.insert.child.ts +0 -129
  171. package/src/operations/behavior.operation.insert.text.ts +0 -8
  172. package/src/operations/behavior.operation.move.backward.ts +0 -12
  173. package/src/operations/behavior.operation.move.block.ts +0 -44
  174. package/src/operations/behavior.operation.move.forward.ts +0 -11
  175. package/src/operations/behavior.operation.select.ts +0 -27
  176. package/src/operations/behavior.operations.ts +0 -221
  177. package/src/plugins/_exports/index.ts +0 -1
  178. package/src/plugins/index.ts +0 -3
  179. package/src/plugins/plugin.behavior.tsx +0 -24
  180. package/src/plugins/plugin.editor-ref.tsx +0 -17
  181. package/src/plugins/plugin.event-listener.tsx +0 -68
  182. package/src/plugins/plugin.internal.auto-close-brackets.test.tsx +0 -71
  183. package/src/plugins/plugin.internal.auto-close-brackets.ts +0 -62
  184. package/src/plugins/plugin.internal.change-ref.tsx +0 -19
  185. package/src/plugins/plugin.internal.portable-text-editor-ref.tsx +0 -16
  186. package/src/plugins/plugin.internal.slate-editor-ref.tsx +0 -15
  187. package/src/priority/priority.core.ts +0 -3
  188. package/src/priority/priority.sort.test.ts +0 -319
  189. package/src/priority/priority.sort.ts +0 -123
  190. package/src/priority/priority.types.ts +0 -24
  191. package/src/selectors/_exports/index.ts +0 -1
  192. package/src/selectors/drag-selection.test.ts +0 -578
  193. package/src/selectors/drag-selection.ts +0 -118
  194. package/src/selectors/index.ts +0 -54
  195. package/src/selectors/selector.get-active-annotation-marks.ts +0 -12
  196. package/src/selectors/selector.get-active-annotations.ts +0 -36
  197. package/src/selectors/selector.get-active-decorators.ts +0 -29
  198. package/src/selectors/selector.get-active-list-item.ts +0 -38
  199. package/src/selectors/selector.get-active-style.ts +0 -38
  200. package/src/selectors/selector.get-anchor-block.ts +0 -22
  201. package/src/selectors/selector.get-anchor-child.ts +0 -36
  202. package/src/selectors/selector.get-anchor-span.ts +0 -17
  203. package/src/selectors/selector.get-anchor-text-block.ts +0 -18
  204. package/src/selectors/selector.get-block-offsets.ts +0 -34
  205. package/src/selectors/selector.get-caret-word-selection.test.ts +0 -284
  206. package/src/selectors/selector.get-caret-word-selection.ts +0 -134
  207. package/src/selectors/selector.get-first-block.ts +0 -14
  208. package/src/selectors/selector.get-focus-block-object.ts +0 -18
  209. package/src/selectors/selector.get-focus-block.ts +0 -23
  210. package/src/selectors/selector.get-focus-child.ts +0 -36
  211. package/src/selectors/selector.get-focus-inline-object.ts +0 -17
  212. package/src/selectors/selector.get-focus-list-block.ts +0 -18
  213. package/src/selectors/selector.get-focus-span.ts +0 -18
  214. package/src/selectors/selector.get-focus-text-block.ts +0 -18
  215. package/src/selectors/selector.get-last-block.ts +0 -16
  216. package/src/selectors/selector.get-mark-state.test.ts +0 -325
  217. package/src/selectors/selector.get-mark-state.ts +0 -263
  218. package/src/selectors/selector.get-next-block.ts +0 -29
  219. package/src/selectors/selector.get-next-inline-object.ts +0 -53
  220. package/src/selectors/selector.get-next-inline-objects.ts +0 -50
  221. package/src/selectors/selector.get-next-span.ts +0 -56
  222. package/src/selectors/selector.get-previous-block.ts +0 -29
  223. package/src/selectors/selector.get-previous-inline-object.ts +0 -50
  224. package/src/selectors/selector.get-previous-inline-objects.ts +0 -47
  225. package/src/selectors/selector.get-previous-span.ts +0 -53
  226. package/src/selectors/selector.get-selected-blocks.ts +0 -61
  227. package/src/selectors/selector.get-selected-spans.test.ts +0 -347
  228. package/src/selectors/selector.get-selected-spans.ts +0 -155
  229. package/src/selectors/selector.get-selected-text-blocks.ts +0 -73
  230. package/src/selectors/selector.get-selected-value.test.ts +0 -834
  231. package/src/selectors/selector.get-selected-value.ts +0 -66
  232. package/src/selectors/selector.get-selection-end-block.ts +0 -33
  233. package/src/selectors/selector.get-selection-end-child.ts +0 -33
  234. package/src/selectors/selector.get-selection-end-point.ts +0 -17
  235. package/src/selectors/selector.get-selection-start-block.ts +0 -33
  236. package/src/selectors/selector.get-selection-start-child.ts +0 -33
  237. package/src/selectors/selector.get-selection-start-point.ts +0 -17
  238. package/src/selectors/selector.get-selection-text.test.ts +0 -421
  239. package/src/selectors/selector.get-selection-text.ts +0 -27
  240. package/src/selectors/selector.get-selection.ts +0 -9
  241. package/src/selectors/selector.get-text-after.ts +0 -46
  242. package/src/selectors/selector.get-text-before.ts +0 -46
  243. package/src/selectors/selector.get-value.ts +0 -11
  244. package/src/selectors/selector.is-active-annotation.test.ts +0 -320
  245. package/src/selectors/selector.is-active-annotation.ts +0 -52
  246. package/src/selectors/selector.is-active-decorator.test.ts +0 -136
  247. package/src/selectors/selector.is-active-decorator.ts +0 -24
  248. package/src/selectors/selector.is-active-list-item.ts +0 -13
  249. package/src/selectors/selector.is-active-style.ts +0 -13
  250. package/src/selectors/selector.is-at-the-end-of-block.ts +0 -30
  251. package/src/selectors/selector.is-at-the-start-of-block.ts +0 -30
  252. package/src/selectors/selector.is-overlapping-selection.test.ts +0 -304
  253. package/src/selectors/selector.is-overlapping-selection.ts +0 -181
  254. package/src/selectors/selector.is-point-after-selection.ts +0 -97
  255. package/src/selectors/selector.is-point-before-selection.ts +0 -97
  256. package/src/selectors/selector.is-selecting-entire-blocks.ts +0 -43
  257. package/src/selectors/selector.is-selection-collapsed.ts +0 -17
  258. package/src/selectors/selector.is-selection-expanded.test.ts +0 -63
  259. package/src/selectors/selector.is-selection-expanded.ts +0 -9
  260. package/src/test/_exports/index.ts +0 -1
  261. package/src/test/gherkin-parameter-types.ts +0 -112
  262. package/src/test/index.ts +0 -1
  263. package/src/test/vitest/_exports/index.ts +0 -1
  264. package/src/test/vitest/index.ts +0 -3
  265. package/src/test/vitest/step-context.ts +0 -13
  266. package/src/test/vitest/step-definitions.tsx +0 -960
  267. package/src/test/vitest/test-editor.tsx +0 -198
  268. package/src/type-utils.ts +0 -29
  269. package/src/types/block-offset.ts +0 -9
  270. package/src/types/block-with-optional-key.ts +0 -25
  271. package/src/types/editor.ts +0 -509
  272. package/src/types/options.ts +0 -13
  273. package/src/types/paths.ts +0 -35
  274. package/src/types/slate-editor.ts +0 -50
  275. package/src/types/slate.ts +0 -27
  276. package/src/utils/_exports/index.ts +0 -1
  277. package/src/utils/asserters.ts +0 -9
  278. package/src/utils/index.ts +0 -24
  279. package/src/utils/key-generator.ts +0 -33
  280. package/src/utils/parse-blocks.test.ts +0 -836
  281. package/src/utils/parse-blocks.ts +0 -504
  282. package/src/utils/util.at-the-beginning-of-block.ts +0 -32
  283. package/src/utils/util.block-offset-to-block-selection-point.ts +0 -28
  284. package/src/utils/util.block-offset-to-selection-point.ts +0 -33
  285. package/src/utils/util.block-offset.test.ts +0 -375
  286. package/src/utils/util.block-offset.ts +0 -136
  287. package/src/utils/util.block-offsets-to-selection.ts +0 -38
  288. package/src/utils/util.child-selection-point-to-block-offset.ts +0 -51
  289. package/src/utils/util.get-block-end-point.ts +0 -35
  290. package/src/utils/util.get-block-start-point.ts +0 -31
  291. package/src/utils/util.get-selection-end-point.ts +0 -20
  292. package/src/utils/util.get-selection-start-point.ts +0 -20
  293. package/src/utils/util.get-text-block-text.ts +0 -8
  294. package/src/utils/util.is-empty-text-block.ts +0 -21
  295. package/src/utils/util.is-equal-selection-points.ts +0 -13
  296. package/src/utils/util.is-equal-selections.ts +0 -20
  297. package/src/utils/util.is-keyed-segment.ts +0 -8
  298. package/src/utils/util.is-selection-collapsed.ts +0 -16
  299. package/src/utils/util.is-selection-expanded.ts +0 -13
  300. package/src/utils/util.merge-text-blocks.ts +0 -40
  301. package/src/utils/util.reverse-selection.ts +0 -26
  302. package/src/utils/util.selection-point-to-block-offset.ts +0 -30
  303. package/src/utils/util.selection-point.ts +0 -22
  304. package/src/utils/util.slice-blocks.ts +0 -221
  305. package/src/utils/util.slice-text-block.test.ts +0 -190
  306. package/src/utils/util.slice-text-block.ts +0 -89
  307. package/src/utils/util.split-text-block.ts +0 -54
@@ -1,111 +0,0 @@
1
- import type React from 'react'
2
- import {useEffect, useState} from 'react'
3
- import {Slate} from 'slate-react'
4
- import type {EditorConfig} from '../editor'
5
- import {stopActor} from '../internal-utils/stop-actor'
6
- import {createInternalEditor} from './create-editor'
7
- import {EditorActorContext} from './editor-actor-context'
8
- import {EditorContext} from './editor-context'
9
- import {eventToChange} from './event-to-change'
10
- import {PortableTextEditorContext} from './hooks/usePortableTextEditor'
11
- import {
12
- PortableTextEditor,
13
- type PortableTextEditorProps,
14
- } from './PortableTextEditor'
15
- import {RelayActorContext} from './relay-actor-context'
16
-
17
- /**
18
- * @public
19
- */
20
- export type EditorProviderProps = {
21
- initialConfig: EditorConfig
22
- children?: React.ReactNode
23
- }
24
-
25
- /**
26
- * @public
27
- * The EditorProvider component is used to set up the editor context and configure the Portable Text Editor.
28
- * @example
29
- * ```tsx
30
- * import {EditorProvider} from '@portabletext/editor'
31
- *
32
- * function App() {
33
- * return (
34
- * <EditorProvider initialConfig={{ ... }} >
35
- * ...
36
- * </EditorProvider>
37
- * )
38
- * }
39
- *
40
- * ```
41
- * @group Components
42
- */
43
- export function EditorProvider(props: EditorProviderProps) {
44
- const [{internalEditor, portableTextEditor}] = useState(() => {
45
- const internalEditor = createInternalEditor(props.initialConfig)
46
- const portableTextEditor = new PortableTextEditor({
47
- editor: internalEditor.editor,
48
- } as unknown as PortableTextEditorProps)
49
-
50
- return {internalEditor, portableTextEditor}
51
- })
52
-
53
- useEffect(() => {
54
- const unsubscribers: Array<() => void> = []
55
-
56
- for (const subscription of internalEditor.subscriptions) {
57
- unsubscribers.push(subscription())
58
- }
59
-
60
- const relayActorSubscription = internalEditor.actors.relayActor.on(
61
- '*',
62
- (event) => {
63
- const change = eventToChange(event)
64
-
65
- if (change) {
66
- portableTextEditor.change$.next(change)
67
- }
68
- },
69
- )
70
- unsubscribers.push(relayActorSubscription.unsubscribe)
71
-
72
- internalEditor.actors.editorActor.start()
73
- internalEditor.actors.editorActor.send({
74
- type: 'add slate editor',
75
- editor: internalEditor.editor._internal.slateEditor.instance,
76
- })
77
- internalEditor.actors.mutationActor.start()
78
- internalEditor.actors.relayActor.start()
79
- internalEditor.actors.syncActor.start()
80
-
81
- return () => {
82
- for (const unsubscribe of unsubscribers) {
83
- unsubscribe()
84
- }
85
-
86
- stopActor(internalEditor.actors.editorActor)
87
- stopActor(internalEditor.actors.mutationActor)
88
- stopActor(internalEditor.actors.relayActor)
89
- stopActor(internalEditor.actors.syncActor)
90
- }
91
- }, [internalEditor, portableTextEditor])
92
-
93
- return (
94
- <EditorContext.Provider value={internalEditor.editor}>
95
- <EditorActorContext.Provider value={internalEditor.actors.editorActor}>
96
- <RelayActorContext.Provider value={internalEditor.actors.relayActor}>
97
- <Slate
98
- editor={internalEditor.editor._internal.slateEditor.instance}
99
- initialValue={
100
- internalEditor.editor._internal.slateEditor.initialValue
101
- }
102
- >
103
- <PortableTextEditorContext.Provider value={portableTextEditor}>
104
- {props.children}
105
- </PortableTextEditorContext.Provider>
106
- </Slate>
107
- </RelayActorContext.Provider>
108
- </EditorActorContext.Provider>
109
- </EditorContext.Provider>
110
- )
111
- }
@@ -1,6 +0,0 @@
1
- import type {Schema} from '@portabletext/schema'
2
-
3
- /**
4
- * @public
5
- */
6
- export type EditorSchema = Schema
@@ -1,89 +0,0 @@
1
- import {useSelector} from '@xstate/react'
2
- import type {Editor} from '../editor'
3
- import {slateRangeToSelection} from '../internal-utils/slate-utils'
4
- import type {PortableTextSlateEditor} from '../types/slate-editor'
5
- import type {InternalEditor} from './create-editor'
6
- import type {EditorActor} from './editor-machine'
7
- import type {EditorSnapshot} from './editor-snapshot'
8
-
9
- function defaultCompare<T>(a: T, b: T) {
10
- return a === b
11
- }
12
-
13
- /**
14
- * @public
15
- */
16
- export type EditorSelector<TSelected> = (snapshot: EditorSnapshot) => TSelected
17
-
18
- /**
19
- * @public
20
- * Hook to select a value from the editor state.
21
- * @example
22
- * Pass a selector as the second argument
23
- * ```tsx
24
- * import { useEditorSelector } from '@portabletext/editor'
25
- *
26
- * function MyComponent(editor) {
27
- * const value = useEditorSelector(editor, selector)
28
- * }
29
- * ```
30
- * @example
31
- * Pass an inline selector as the second argument.
32
- * In this case, use the editor context to obtain the schema.
33
- * ```tsx
34
- * import { useEditorSelector } from '@portabletext/editor'
35
- *
36
- * function MyComponent(editor) {
37
- * const schema = useEditorSelector(editor, (snapshot) => snapshot.context.schema)
38
- * }
39
- * ```
40
- * @group Hooks
41
- */
42
- export function useEditorSelector<TSelected>(
43
- editor: Editor,
44
- selector: EditorSelector<TSelected>,
45
- compare: (a: TSelected, b: TSelected) => boolean = defaultCompare,
46
- ) {
47
- return useSelector(
48
- (editor as InternalEditor)._internal.editorActor,
49
- (editorActorSnapshot) => {
50
- const snapshot = getEditorSnapshot({
51
- editorActorSnapshot,
52
- slateEditorInstance: (editor as InternalEditor)._internal.slateEditor
53
- .instance,
54
- })
55
-
56
- return selector(snapshot)
57
- },
58
- compare,
59
- )
60
- }
61
-
62
- export function getEditorSnapshot({
63
- editorActorSnapshot,
64
- slateEditorInstance,
65
- }: {
66
- editorActorSnapshot: ReturnType<EditorActor['getSnapshot']>
67
- slateEditorInstance: PortableTextSlateEditor
68
- }): EditorSnapshot {
69
- const selection = slateEditorInstance.selection
70
- ? slateRangeToSelection({
71
- schema: editorActorSnapshot.context.schema,
72
- editor: slateEditorInstance,
73
- range: slateEditorInstance.selection,
74
- })
75
- : null
76
-
77
- return {
78
- blockIndexMap: slateEditorInstance.blockIndexMap,
79
- context: {
80
- converters: [...editorActorSnapshot.context.converters],
81
- keyGenerator: editorActorSnapshot.context.keyGenerator,
82
- readOnly: editorActorSnapshot.matches({'edit mode': 'read only'}),
83
- schema: editorActorSnapshot.context.schema,
84
- selection,
85
- value: slateEditorInstance.value,
86
- },
87
- decoratorState: slateEditorInstance.decoratorState,
88
- }
89
- }
@@ -1,68 +0,0 @@
1
- import type {PortableTextBlock} from '@sanity/types'
2
- import type {Converter} from '../converters/converter.types'
3
- import {slateRangeToSelection} from '../internal-utils/slate-utils'
4
- import type {EditorSelection} from '../types/editor'
5
- import type {PortableTextSlateEditor} from '../types/slate-editor'
6
- import type {EditorSchema} from './editor-schema'
7
-
8
- /**
9
- * @public
10
- */
11
- export type EditorContext = {
12
- converters: Array<Converter>
13
- keyGenerator: () => string
14
- readOnly: boolean
15
- schema: EditorSchema
16
- selection: EditorSelection
17
- value: Array<PortableTextBlock>
18
- }
19
-
20
- /**
21
- * @public
22
- */
23
- export type EditorSnapshot = {
24
- context: EditorContext
25
- blockIndexMap: Map<string, number>
26
- /**
27
- * @beta
28
- * Subject to change
29
- */
30
- decoratorState: Record<string, boolean | undefined>
31
- }
32
-
33
- export function createEditorSnapshot({
34
- converters,
35
- editor,
36
- keyGenerator,
37
- readOnly,
38
- schema,
39
- }: {
40
- converters: Array<Converter>
41
- editor: PortableTextSlateEditor
42
- keyGenerator: () => string
43
- readOnly: boolean
44
- schema: EditorSchema
45
- }) {
46
- const selection = editor.selection
47
- ? slateRangeToSelection({
48
- schema,
49
- editor,
50
- range: editor.selection,
51
- })
52
- : null
53
-
54
- const context = {
55
- converters,
56
- keyGenerator,
57
- readOnly,
58
- schema,
59
- selection,
60
- value: editor.value,
61
- } satisfies EditorContext
62
-
63
- return {
64
- blockIndexMap: editor.blockIndexMap,
65
- context,
66
- decoratorState: editor.decoratorState,
67
- } satisfies EditorSnapshot
68
- }
@@ -1,49 +0,0 @@
1
- import type {EditorChange} from '../types/editor'
2
- import type {InternalEditorEmittedEvent} from './relay-machine'
3
-
4
- export function eventToChange(
5
- event: InternalEditorEmittedEvent,
6
- ): EditorChange | undefined {
7
- switch (event.type) {
8
- case 'blurred': {
9
- return {type: 'blur', event: event.event}
10
- }
11
- case 'patch':
12
- return event
13
- case 'loading': {
14
- return {type: 'loading', isLoading: true}
15
- }
16
- case 'done loading': {
17
- return {type: 'loading', isLoading: false}
18
- }
19
- case 'focused': {
20
- return {type: 'focus', event: event.event}
21
- }
22
- case 'value changed': {
23
- return {type: 'value', value: event.value}
24
- }
25
- case 'invalid value': {
26
- return {
27
- type: 'invalidValue',
28
- resolution: event.resolution,
29
- value: event.value,
30
- }
31
- }
32
- case 'mutation': {
33
- return {
34
- type: 'mutation',
35
- patches: event.patches,
36
- snapshot: event.value,
37
- }
38
- }
39
- case 'ready': {
40
- return event
41
- }
42
- case 'selection': {
43
- return event
44
- }
45
- case 'unset': {
46
- return event
47
- }
48
- }
49
- }
@@ -1,25 +0,0 @@
1
- import {createContext, useContext} from 'react'
2
- import type {PortableTextEditor} from '../PortableTextEditor'
3
-
4
- /**
5
- * A React context for sharing the editor object.
6
- */
7
- export const PortableTextEditorContext =
8
- createContext<PortableTextEditor | null>(null)
9
-
10
- /**
11
- * @deprecated Use `useEditor` to get the current editor instance.
12
- * @public
13
- * Get the current editor object from the React context.
14
- */
15
- export const usePortableTextEditor = (): PortableTextEditor => {
16
- const editor = useContext(PortableTextEditorContext)
17
-
18
- if (!editor) {
19
- throw new Error(
20
- `The \`usePortableTextEditor\` hook must be used inside the <PortableTextEditor> component's context.`,
21
- )
22
- }
23
-
24
- return editor
25
- }
@@ -1,28 +0,0 @@
1
- import {startTransition, useContext, useEffect, useState} from 'react'
2
- import type {EditorSelection} from '../../types/editor'
3
- import {EditorActorContext} from '../editor-actor-context'
4
-
5
- /**
6
- * @deprecated Use `useEditorSelector` to get the current editor selection.
7
- * @public
8
- * Get the current editor selection from the React context.
9
- */
10
- export const usePortableTextEditorSelection = (): EditorSelection => {
11
- const editorActor = useContext(EditorActorContext)
12
- const [selection, setSelection] = useState<EditorSelection>(null)
13
-
14
- useEffect(() => {
15
- const subscription = editorActor.on('selection', (event) => {
16
- // Set the selection state in a transition, we don't need the state immediately.
17
- startTransition(() => {
18
- setSelection(event.selection)
19
- })
20
- })
21
-
22
- return () => {
23
- subscription.unsubscribe()
24
- }
25
- }, [editorActor])
26
-
27
- return selection
28
- }
@@ -1,322 +0,0 @@
1
- import type {Patch} from '@portabletext/patches'
2
- import type {PortableTextBlock} from '@sanity/types'
3
- import {Editor} from 'slate'
4
- import type {ActorRefFrom} from 'xstate'
5
- import {
6
- and,
7
- assertEvent,
8
- assign,
9
- emit,
10
- enqueueActions,
11
- fromCallback,
12
- not,
13
- raise,
14
- setup,
15
- type AnyEventObject,
16
- } from 'xstate'
17
- import {debugWithName} from '../internal-utils/debug'
18
- import type {PortableTextSlateEditor} from '../types/slate-editor'
19
- import type {EditorSchema} from './editor-schema'
20
- import type {PatchEvent} from './relay-machine'
21
-
22
- const debug = debugWithName('mutation-machine')
23
-
24
- export type MutationActor = ActorRefFrom<typeof mutationMachine>
25
-
26
- /**
27
- * Makes sure editor mutation events are debounced
28
- */
29
- export const mutationMachine = setup({
30
- types: {
31
- context: {} as {
32
- pendingMutations: Array<{
33
- operationId?: string
34
- value: Array<PortableTextBlock> | undefined
35
- patches: Array<Patch>
36
- }>
37
- pendingPatchEvents: Array<PatchEvent>
38
- readOnly: boolean
39
- schema: EditorSchema
40
- slateEditor: PortableTextSlateEditor
41
- },
42
- events: {} as
43
- | {
44
- type: 'emit changes'
45
- }
46
- | {
47
- type: 'patch'
48
- patch: Patch
49
- operationId?: string
50
- value: Array<PortableTextBlock>
51
- }
52
- | {
53
- type: 'typing'
54
- }
55
- | {
56
- type: 'not typing'
57
- }
58
- | {
59
- type: 'update readOnly'
60
- readOnly: boolean
61
- },
62
- input: {} as {
63
- readOnly: boolean
64
- schema: EditorSchema
65
- slateEditor: PortableTextSlateEditor
66
- },
67
- emitted: {} as
68
- | {
69
- type: 'has pending mutations'
70
- }
71
- | {
72
- type: 'mutation'
73
- patches: Array<Patch>
74
- snapshot: Array<PortableTextBlock> | undefined
75
- }
76
- | PatchEvent,
77
- },
78
- actions: {
79
- 'assign readOnly': assign({
80
- readOnly: ({context, event}) =>
81
- event.type === 'update readOnly' ? event.readOnly : context.readOnly,
82
- }),
83
- 'emit patch': emit(({event}) => {
84
- assertEvent(event, 'patch')
85
- return {type: 'patch' as const, patch: event.patch}
86
- }),
87
- 'emit has pending mutations': emit({type: 'has pending mutations'}),
88
- 'emit mutations': enqueueActions(({context, enqueue}) => {
89
- for (const bulk of context.pendingMutations) {
90
- enqueue.emit({
91
- type: 'mutation',
92
- patches: bulk.patches,
93
- snapshot: bulk.value,
94
- })
95
- }
96
- }),
97
- 'clear pending mutations': assign({
98
- pendingMutations: [],
99
- }),
100
- 'defer mutation': assign({
101
- pendingMutations: ({context, event}) => {
102
- assertEvent(event, 'patch')
103
-
104
- if (context.pendingMutations.length === 0) {
105
- return [
106
- {
107
- operationId: event.operationId,
108
- value: event.value,
109
- patches: [event.patch],
110
- },
111
- ]
112
- }
113
-
114
- const lastBulk = context.pendingMutations.at(-1)
115
-
116
- if (lastBulk && lastBulk.operationId === event.operationId) {
117
- return context.pendingMutations.slice(0, -1).concat({
118
- value: event.value,
119
- operationId: lastBulk.operationId,
120
- patches: [...lastBulk.patches, event.patch],
121
- })
122
- }
123
-
124
- return context.pendingMutations.concat({
125
- value: event.value,
126
- operationId: event.operationId,
127
- patches: [event.patch],
128
- })
129
- },
130
- }),
131
- 'clear pending patch events': assign({
132
- pendingPatchEvents: [],
133
- }),
134
- 'defer patch': assign({
135
- pendingPatchEvents: ({context, event}) =>
136
- event.type === 'patch'
137
- ? [...context.pendingPatchEvents, event]
138
- : context.pendingPatchEvents,
139
- }),
140
- 'emit pending patch events': enqueueActions(({context, enqueue}) => {
141
- for (const event of context.pendingPatchEvents) {
142
- enqueue.emit(event)
143
- }
144
- }),
145
- },
146
- actors: {
147
- 'type listener': fromCallback<
148
- AnyEventObject,
149
- {slateEditor: PortableTextSlateEditor},
150
- {type: 'typing'} | {type: 'not typing'}
151
- >(({input, sendBack}) => {
152
- const originalApply = input.slateEditor.apply
153
-
154
- input.slateEditor.apply = (op) => {
155
- if (op.type === 'insert_text' || op.type === 'remove_text') {
156
- sendBack({type: 'typing'})
157
- } else {
158
- sendBack({type: 'not typing'})
159
- }
160
- originalApply(op)
161
- }
162
-
163
- return () => {
164
- input.slateEditor.apply = originalApply
165
- }
166
- }),
167
- 'mutation interval': fromCallback(({sendBack}) => {
168
- const interval = setInterval(
169
- () => {
170
- sendBack({type: 'emit changes'})
171
- },
172
- process.env.NODE_ENV === 'test' ? 250 : 1000,
173
- )
174
-
175
- return () => {
176
- clearInterval(interval)
177
- }
178
- }),
179
- },
180
- guards: {
181
- 'is read-only': ({context}) => context.readOnly,
182
- 'slate is normalizing': ({context}) =>
183
- Editor.isNormalizing(context.slateEditor),
184
- },
185
- delays: {
186
- 'type debounce': 250,
187
- },
188
- }).createMachine({
189
- id: 'mutation',
190
- context: ({input}) => ({
191
- pendingMutations: [],
192
- pendingPatchEvents: [],
193
- readOnly: input.readOnly,
194
- schema: input.schema,
195
- slateEditor: input.slateEditor,
196
- }),
197
- on: {
198
- 'update readOnly': {
199
- actions: ['assign readOnly'],
200
- },
201
- },
202
- type: 'parallel',
203
- states: {
204
- typing: {
205
- initial: 'idle',
206
- invoke: {
207
- src: 'type listener',
208
- input: ({context}) => ({slateEditor: context.slateEditor}),
209
- },
210
- states: {
211
- idle: {
212
- entry: [
213
- () => {
214
- debug('entry: typing->idle')
215
- },
216
- ],
217
- exit: [
218
- () => {
219
- debug('exit: typing->idle')
220
- debug('entry: typing->typing')
221
- },
222
- ],
223
- on: {
224
- typing: {
225
- target: 'typing',
226
- },
227
- },
228
- },
229
- typing: {
230
- after: {
231
- 'type debounce': {
232
- target: 'idle',
233
- actions: [
234
- raise({type: 'emit changes'}),
235
- () => {
236
- debug('exit: typing->typing')
237
- },
238
- ],
239
- },
240
- },
241
- on: {
242
- 'not typing': {
243
- target: 'idle',
244
- actions: [raise({type: 'emit changes'})],
245
- },
246
- 'typing': {
247
- target: 'typing',
248
- reenter: true,
249
- },
250
- },
251
- },
252
- },
253
- },
254
- mutations: {
255
- initial: 'idle',
256
- states: {
257
- 'idle': {
258
- entry: [
259
- () => {
260
- debug('entry: mutations->idle')
261
- },
262
- ],
263
- exit: [
264
- () => {
265
- debug('exit: mutations->idle')
266
- },
267
- ],
268
- on: {
269
- patch: [
270
- {
271
- guard: 'is read-only',
272
- actions: ['defer patch', 'defer mutation'],
273
- target: 'has pending mutations',
274
- },
275
- {
276
- actions: ['emit patch', 'defer mutation'],
277
- target: 'has pending mutations',
278
- },
279
- ],
280
- },
281
- },
282
- 'has pending mutations': {
283
- entry: [
284
- () => {
285
- debug('entry: mutations->has pending mutations')
286
- },
287
- 'emit has pending mutations',
288
- ],
289
- exit: [
290
- () => {
291
- debug('exit: mutations->has pending mutations')
292
- },
293
- ],
294
- invoke: {
295
- src: 'mutation interval',
296
- },
297
- on: {
298
- 'emit changes': {
299
- guard: and([not('is read-only'), 'slate is normalizing']),
300
- target: 'idle',
301
- actions: [
302
- 'emit pending patch events',
303
- 'clear pending patch events',
304
- 'emit mutations',
305
- 'clear pending mutations',
306
- ],
307
- },
308
- 'patch': [
309
- {
310
- guard: 'is read-only',
311
- actions: ['defer patch', 'defer mutation'],
312
- },
313
- {
314
- actions: ['emit patch', 'defer mutation'],
315
- },
316
- ],
317
- },
318
- },
319
- },
320
- },
321
- },
322
- })