@portabletext/editor 3.3.3 → 3.3.5

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 (309) hide show
  1. package/lib/_chunks-dts/index.d.ts +21 -1667
  2. package/lib/index.js +511 -9010
  3. package/lib/index.js.map +1 -1
  4. package/package.json +14 -15
  5. package/src/behaviors/_exports/index.ts +0 -1
  6. package/src/behaviors/behavior.abstract.annotation.ts +0 -77
  7. package/src/behaviors/behavior.abstract.decorator.ts +0 -39
  8. package/src/behaviors/behavior.abstract.delete.ts +0 -273
  9. package/src/behaviors/behavior.abstract.deserialize.ts +0 -232
  10. package/src/behaviors/behavior.abstract.insert.ts +0 -525
  11. package/src/behaviors/behavior.abstract.keyboard.ts +0 -189
  12. package/src/behaviors/behavior.abstract.list-item.ts +0 -70
  13. package/src/behaviors/behavior.abstract.move.ts +0 -79
  14. package/src/behaviors/behavior.abstract.select.ts +0 -118
  15. package/src/behaviors/behavior.abstract.serialize.ts +0 -96
  16. package/src/behaviors/behavior.abstract.split.ts +0 -170
  17. package/src/behaviors/behavior.abstract.style.ts +0 -55
  18. package/src/behaviors/behavior.abstract.ts +0 -139
  19. package/src/behaviors/behavior.config.ts +0 -7
  20. package/src/behaviors/behavior.core.annotations.ts +0 -62
  21. package/src/behaviors/behavior.core.block-element.ts +0 -116
  22. package/src/behaviors/behavior.core.block-objects.ts +0 -285
  23. package/src/behaviors/behavior.core.decorators.ts +0 -44
  24. package/src/behaviors/behavior.core.dnd.ts +0 -356
  25. package/src/behaviors/behavior.core.insert-break.ts +0 -266
  26. package/src/behaviors/behavior.core.insert.ts +0 -52
  27. package/src/behaviors/behavior.core.lists.ts +0 -691
  28. package/src/behaviors/behavior.core.ts +0 -44
  29. package/src/behaviors/behavior.perform-event.ts +0 -354
  30. package/src/behaviors/behavior.types.action.ts +0 -102
  31. package/src/behaviors/behavior.types.behavior.ts +0 -83
  32. package/src/behaviors/behavior.types.event.ts +0 -667
  33. package/src/behaviors/behavior.types.guard.ts +0 -11
  34. package/src/behaviors/index.ts +0 -17
  35. package/src/converters/converter.json.ts +0 -53
  36. package/src/converters/converter.portable-text.deserialize.test.ts +0 -680
  37. package/src/converters/converter.portable-text.ts +0 -75
  38. package/src/converters/converter.text-html.deserialize.test.ts +0 -406
  39. package/src/converters/converter.text-html.serialize.test.ts +0 -246
  40. package/src/converters/converter.text-html.ts +0 -87
  41. package/src/converters/converter.text-markdown.ts +0 -67
  42. package/src/converters/converter.text-plain.test.ts +0 -245
  43. package/src/converters/converter.text-plain.ts +0 -126
  44. package/src/converters/converter.types.ts +0 -72
  45. package/src/converters/converters.core.ts +0 -18
  46. package/src/editor/Editable.tsx +0 -1012
  47. package/src/editor/PortableTextEditor.tsx +0 -791
  48. package/src/editor/components/drop-indicator.tsx +0 -17
  49. package/src/editor/components/render-block-object.tsx +0 -121
  50. package/src/editor/components/render-default-object.tsx +0 -21
  51. package/src/editor/components/render-element.tsx +0 -88
  52. package/src/editor/components/render-inline-object.tsx +0 -129
  53. package/src/editor/components/render-leaf.tsx +0 -64
  54. package/src/editor/components/render-span.tsx +0 -303
  55. package/src/editor/components/render-text-block.tsx +0 -265
  56. package/src/editor/components/render-text.tsx +0 -18
  57. package/src/editor/components/use-core-block-element-behaviors.ts +0 -39
  58. package/src/editor/create-editor.ts +0 -323
  59. package/src/editor/create-slate-editor.tsx +0 -64
  60. package/src/editor/editor-actor-context.ts +0 -4
  61. package/src/editor/editor-context.tsx +0 -7
  62. package/src/editor/editor-dom.ts +0 -220
  63. package/src/editor/editor-machine.ts +0 -751
  64. package/src/editor/editor-provider.tsx +0 -111
  65. package/src/editor/editor-schema.ts +0 -6
  66. package/src/editor/editor-selector.ts +0 -89
  67. package/src/editor/editor-snapshot.ts +0 -68
  68. package/src/editor/event-to-change.tsx +0 -49
  69. package/src/editor/hooks/usePortableTextEditor.ts +0 -25
  70. package/src/editor/hooks/usePortableTextEditorSelection.tsx +0 -28
  71. package/src/editor/mutation-machine.ts +0 -322
  72. package/src/editor/plugins/create-with-event-listeners.ts +0 -271
  73. package/src/editor/plugins/createWithEditableAPI.ts +0 -529
  74. package/src/editor/plugins/createWithHotKeys.ts +0 -68
  75. package/src/editor/plugins/createWithObjectKeys.ts +0 -289
  76. package/src/editor/plugins/createWithPatches.ts +0 -272
  77. package/src/editor/plugins/createWithPortableTextMarkModel.ts +0 -559
  78. package/src/editor/plugins/createWithSchemaTypes.ts +0 -121
  79. package/src/editor/plugins/slate-plugin.update-selection.ts +0 -51
  80. package/src/editor/plugins/slate-plugin.update-value.ts +0 -46
  81. package/src/editor/plugins/with-plugins.ts +0 -69
  82. package/src/editor/range-decorations-machine.ts +0 -421
  83. package/src/editor/relay-actor-context.ts +0 -4
  84. package/src/editor/relay-machine.ts +0 -152
  85. package/src/editor/sync-machine.ts +0 -961
  86. package/src/editor/use-editor.ts +0 -27
  87. package/src/editor/validate-selection-machine.test.ts +0 -47
  88. package/src/editor/validate-selection-machine.ts +0 -149
  89. package/src/editor/weakMaps.ts +0 -15
  90. package/src/editor/with-normalizing-node.ts +0 -14
  91. package/src/editor/with-performing-behavior-operation.ts +0 -21
  92. package/src/editor/withChanges.ts +0 -13
  93. package/src/editor/without-normalizing-conditional.ts +0 -13
  94. package/src/editor/withoutPatching.ts +0 -14
  95. package/src/editor.ts +0 -59
  96. package/src/history/behavior.operation.history.redo.ts +0 -67
  97. package/src/history/behavior.operation.history.undo.ts +0 -71
  98. package/src/history/event.history.undo.test.tsx +0 -672
  99. package/src/history/history.preserving-keys.test.tsx +0 -112
  100. package/src/history/remote-patches.ts +0 -20
  101. package/src/history/slate-plugin.history.ts +0 -142
  102. package/src/history/slate-plugin.redoing.ts +0 -21
  103. package/src/history/slate-plugin.undoing.ts +0 -21
  104. package/src/history/slate-plugin.without-history.ts +0 -23
  105. package/src/history/transform-operation.ts +0 -245
  106. package/src/history/undo-redo-collaboration.test.tsx +0 -541
  107. package/src/history/undo-redo.feature +0 -125
  108. package/src/history/undo-redo.test.tsx +0 -195
  109. package/src/history/undo-step.ts +0 -148
  110. package/src/index.ts +0 -107
  111. package/src/internal-utils/__tests__/ranges.test.ts +0 -23
  112. package/src/internal-utils/__tests__/values.test.ts +0 -110
  113. package/src/internal-utils/apply-operation-to-portable-text.test.ts +0 -1861
  114. package/src/internal-utils/apply-operation-to-portable-text.ts +0 -615
  115. package/src/internal-utils/applyPatch.ts +0 -644
  116. package/src/internal-utils/block-keys.ts +0 -9
  117. package/src/internal-utils/build-index-maps.test.ts +0 -464
  118. package/src/internal-utils/build-index-maps.ts +0 -131
  119. package/src/internal-utils/collapse-selection.ts +0 -36
  120. package/src/internal-utils/compound-client-rect.ts +0 -28
  121. package/src/internal-utils/create-placeholder-block.ts +0 -21
  122. package/src/internal-utils/create-test-snapshot.ts +0 -28
  123. package/src/internal-utils/debug.ts +0 -12
  124. package/src/internal-utils/editor-selection.test.ts +0 -44
  125. package/src/internal-utils/editor-selection.ts +0 -56
  126. package/src/internal-utils/event-position.ts +0 -318
  127. package/src/internal-utils/global-scope.ts +0 -27
  128. package/src/internal-utils/globally-scoped-context.ts +0 -39
  129. package/src/internal-utils/is-hotkey.test.ts +0 -114
  130. package/src/internal-utils/is-hotkey.ts +0 -209
  131. package/src/internal-utils/mime-type.ts +0 -1
  132. package/src/internal-utils/move-range-by-operation.ts +0 -19
  133. package/src/internal-utils/operation-to-patches.test.ts +0 -522
  134. package/src/internal-utils/operation-to-patches.ts +0 -571
  135. package/src/internal-utils/portable-text-node.ts +0 -209
  136. package/src/internal-utils/schema.ts +0 -8
  137. package/src/internal-utils/selection-block-keys.ts +0 -20
  138. package/src/internal-utils/selection-focus-text.ts +0 -40
  139. package/src/internal-utils/selection-text.test.ts +0 -32
  140. package/src/internal-utils/selection-text.ts +0 -21
  141. package/src/internal-utils/selection.ts +0 -77
  142. package/src/internal-utils/sibling-utils.ts +0 -55
  143. package/src/internal-utils/slate-utils.test.tsx +0 -121
  144. package/src/internal-utils/slate-utils.ts +0 -417
  145. package/src/internal-utils/split-string.ts +0 -12
  146. package/src/internal-utils/stop-actor.ts +0 -43
  147. package/src/internal-utils/string-overlap.test.ts +0 -14
  148. package/src/internal-utils/string-overlap.ts +0 -28
  149. package/src/internal-utils/string-utils.ts +0 -7
  150. package/src/internal-utils/text-block-key.test.ts +0 -41
  151. package/src/internal-utils/text-block-key.ts +0 -26
  152. package/src/internal-utils/text-marks.test.ts +0 -41
  153. package/src/internal-utils/text-marks.ts +0 -22
  154. package/src/internal-utils/text-selection.test.ts +0 -211
  155. package/src/internal-utils/text-selection.ts +0 -121
  156. package/src/internal-utils/to-slate-range.test.ts +0 -278
  157. package/src/internal-utils/to-slate-range.ts +0 -171
  158. package/src/internal-utils/validateValue.ts +0 -443
  159. package/src/internal-utils/value-annotations.ts +0 -33
  160. package/src/internal-utils/values.test.ts +0 -282
  161. package/src/internal-utils/values.ts +0 -266
  162. package/src/keyboard-shortcuts/default-keyboard-shortcuts.ts +0 -146
  163. package/src/operations/behavior.operation.annotation.add.ts +0 -99
  164. package/src/operations/behavior.operation.annotation.remove.ts +0 -150
  165. package/src/operations/behavior.operation.block.set.ts +0 -104
  166. package/src/operations/behavior.operation.block.unset.ts +0 -54
  167. package/src/operations/behavior.operation.child.set.ts +0 -107
  168. package/src/operations/behavior.operation.child.unset.ts +0 -116
  169. package/src/operations/behavior.operation.decorator.add.ts +0 -131
  170. package/src/operations/behavior.operation.delete.ts +0 -294
  171. package/src/operations/behavior.operation.insert.block.ts +0 -495
  172. package/src/operations/behavior.operation.insert.child.ts +0 -129
  173. package/src/operations/behavior.operation.insert.text.ts +0 -8
  174. package/src/operations/behavior.operation.move.backward.ts +0 -12
  175. package/src/operations/behavior.operation.move.block.ts +0 -44
  176. package/src/operations/behavior.operation.move.forward.ts +0 -11
  177. package/src/operations/behavior.operation.select.ts +0 -27
  178. package/src/operations/behavior.operations.ts +0 -221
  179. package/src/plugins/_exports/index.ts +0 -1
  180. package/src/plugins/index.ts +0 -3
  181. package/src/plugins/plugin.behavior.tsx +0 -24
  182. package/src/plugins/plugin.editor-ref.tsx +0 -17
  183. package/src/plugins/plugin.event-listener.tsx +0 -68
  184. package/src/plugins/plugin.internal.auto-close-brackets.test.tsx +0 -71
  185. package/src/plugins/plugin.internal.auto-close-brackets.ts +0 -62
  186. package/src/plugins/plugin.internal.change-ref.tsx +0 -19
  187. package/src/plugins/plugin.internal.portable-text-editor-ref.tsx +0 -16
  188. package/src/plugins/plugin.internal.slate-editor-ref.tsx +0 -15
  189. package/src/priority/priority.core.ts +0 -3
  190. package/src/priority/priority.sort.test.ts +0 -319
  191. package/src/priority/priority.sort.ts +0 -123
  192. package/src/priority/priority.types.ts +0 -24
  193. package/src/selectors/_exports/index.ts +0 -1
  194. package/src/selectors/drag-selection.test.ts +0 -578
  195. package/src/selectors/drag-selection.ts +0 -118
  196. package/src/selectors/index.ts +0 -54
  197. package/src/selectors/selector.get-active-annotation-marks.ts +0 -12
  198. package/src/selectors/selector.get-active-annotations.ts +0 -36
  199. package/src/selectors/selector.get-active-decorators.ts +0 -29
  200. package/src/selectors/selector.get-active-list-item.ts +0 -38
  201. package/src/selectors/selector.get-active-style.ts +0 -38
  202. package/src/selectors/selector.get-anchor-block.ts +0 -22
  203. package/src/selectors/selector.get-anchor-child.ts +0 -36
  204. package/src/selectors/selector.get-anchor-span.ts +0 -17
  205. package/src/selectors/selector.get-anchor-text-block.ts +0 -18
  206. package/src/selectors/selector.get-block-offsets.ts +0 -34
  207. package/src/selectors/selector.get-caret-word-selection.test.ts +0 -284
  208. package/src/selectors/selector.get-caret-word-selection.ts +0 -134
  209. package/src/selectors/selector.get-first-block.ts +0 -14
  210. package/src/selectors/selector.get-focus-block-object.ts +0 -18
  211. package/src/selectors/selector.get-focus-block.ts +0 -23
  212. package/src/selectors/selector.get-focus-child.ts +0 -36
  213. package/src/selectors/selector.get-focus-inline-object.ts +0 -17
  214. package/src/selectors/selector.get-focus-list-block.ts +0 -18
  215. package/src/selectors/selector.get-focus-span.ts +0 -18
  216. package/src/selectors/selector.get-focus-text-block.ts +0 -18
  217. package/src/selectors/selector.get-last-block.ts +0 -16
  218. package/src/selectors/selector.get-mark-state.test.ts +0 -325
  219. package/src/selectors/selector.get-mark-state.ts +0 -263
  220. package/src/selectors/selector.get-next-block.ts +0 -29
  221. package/src/selectors/selector.get-next-inline-object.ts +0 -53
  222. package/src/selectors/selector.get-next-inline-objects.ts +0 -50
  223. package/src/selectors/selector.get-next-span.ts +0 -56
  224. package/src/selectors/selector.get-previous-block.ts +0 -29
  225. package/src/selectors/selector.get-previous-inline-object.ts +0 -50
  226. package/src/selectors/selector.get-previous-inline-objects.ts +0 -47
  227. package/src/selectors/selector.get-previous-span.ts +0 -53
  228. package/src/selectors/selector.get-selected-blocks.ts +0 -61
  229. package/src/selectors/selector.get-selected-spans.test.ts +0 -347
  230. package/src/selectors/selector.get-selected-spans.ts +0 -155
  231. package/src/selectors/selector.get-selected-text-blocks.ts +0 -73
  232. package/src/selectors/selector.get-selected-value.test.ts +0 -834
  233. package/src/selectors/selector.get-selected-value.ts +0 -66
  234. package/src/selectors/selector.get-selection-end-block.ts +0 -33
  235. package/src/selectors/selector.get-selection-end-child.ts +0 -33
  236. package/src/selectors/selector.get-selection-end-point.ts +0 -17
  237. package/src/selectors/selector.get-selection-start-block.ts +0 -33
  238. package/src/selectors/selector.get-selection-start-child.ts +0 -33
  239. package/src/selectors/selector.get-selection-start-point.ts +0 -17
  240. package/src/selectors/selector.get-selection-text.test.ts +0 -421
  241. package/src/selectors/selector.get-selection-text.ts +0 -27
  242. package/src/selectors/selector.get-selection.ts +0 -9
  243. package/src/selectors/selector.get-text-after.ts +0 -46
  244. package/src/selectors/selector.get-text-before.ts +0 -46
  245. package/src/selectors/selector.get-value.ts +0 -11
  246. package/src/selectors/selector.is-active-annotation.test.ts +0 -320
  247. package/src/selectors/selector.is-active-annotation.ts +0 -52
  248. package/src/selectors/selector.is-active-decorator.test.ts +0 -136
  249. package/src/selectors/selector.is-active-decorator.ts +0 -24
  250. package/src/selectors/selector.is-active-list-item.ts +0 -13
  251. package/src/selectors/selector.is-active-style.ts +0 -13
  252. package/src/selectors/selector.is-at-the-end-of-block.ts +0 -30
  253. package/src/selectors/selector.is-at-the-start-of-block.ts +0 -30
  254. package/src/selectors/selector.is-overlapping-selection.test.ts +0 -304
  255. package/src/selectors/selector.is-overlapping-selection.ts +0 -181
  256. package/src/selectors/selector.is-point-after-selection.ts +0 -97
  257. package/src/selectors/selector.is-point-before-selection.ts +0 -97
  258. package/src/selectors/selector.is-selecting-entire-blocks.ts +0 -43
  259. package/src/selectors/selector.is-selection-collapsed.ts +0 -17
  260. package/src/selectors/selector.is-selection-expanded.test.ts +0 -63
  261. package/src/selectors/selector.is-selection-expanded.ts +0 -9
  262. package/src/test/_exports/index.ts +0 -1
  263. package/src/test/gherkin-parameter-types.ts +0 -112
  264. package/src/test/index.ts +0 -1
  265. package/src/test/vitest/_exports/index.ts +0 -1
  266. package/src/test/vitest/index.ts +0 -3
  267. package/src/test/vitest/step-context.ts +0 -13
  268. package/src/test/vitest/step-definitions.tsx +0 -960
  269. package/src/test/vitest/test-editor.tsx +0 -198
  270. package/src/type-utils.ts +0 -29
  271. package/src/types/block-offset.ts +0 -9
  272. package/src/types/block-with-optional-key.ts +0 -25
  273. package/src/types/editor.ts +0 -509
  274. package/src/types/options.ts +0 -13
  275. package/src/types/paths.ts +0 -35
  276. package/src/types/slate-editor.ts +0 -50
  277. package/src/types/slate.ts +0 -27
  278. package/src/utils/_exports/index.ts +0 -1
  279. package/src/utils/asserters.ts +0 -9
  280. package/src/utils/index.ts +0 -24
  281. package/src/utils/key-generator.ts +0 -33
  282. package/src/utils/parse-blocks.test.ts +0 -836
  283. package/src/utils/parse-blocks.ts +0 -504
  284. package/src/utils/util.at-the-beginning-of-block.ts +0 -32
  285. package/src/utils/util.block-offset-to-block-selection-point.ts +0 -28
  286. package/src/utils/util.block-offset-to-selection-point.ts +0 -33
  287. package/src/utils/util.block-offset.test.ts +0 -375
  288. package/src/utils/util.block-offset.ts +0 -136
  289. package/src/utils/util.block-offsets-to-selection.ts +0 -38
  290. package/src/utils/util.child-selection-point-to-block-offset.ts +0 -51
  291. package/src/utils/util.get-block-end-point.ts +0 -35
  292. package/src/utils/util.get-block-start-point.ts +0 -31
  293. package/src/utils/util.get-selection-end-point.ts +0 -20
  294. package/src/utils/util.get-selection-start-point.ts +0 -20
  295. package/src/utils/util.get-text-block-text.ts +0 -8
  296. package/src/utils/util.is-empty-text-block.ts +0 -21
  297. package/src/utils/util.is-equal-selection-points.ts +0 -13
  298. package/src/utils/util.is-equal-selections.ts +0 -20
  299. package/src/utils/util.is-keyed-segment.ts +0 -8
  300. package/src/utils/util.is-selection-collapsed.ts +0 -16
  301. package/src/utils/util.is-selection-expanded.ts +0 -13
  302. package/src/utils/util.merge-text-blocks.ts +0 -40
  303. package/src/utils/util.reverse-selection.ts +0 -26
  304. package/src/utils/util.selection-point-to-block-offset.ts +0 -30
  305. package/src/utils/util.selection-point.ts +0 -22
  306. package/src/utils/util.slice-blocks.ts +0 -221
  307. package/src/utils/util.slice-text-block.test.ts +0 -190
  308. package/src/utils/util.slice-text-block.ts +0 -89
  309. package/src/utils/util.split-text-block.ts +0 -54
@@ -1,751 +0,0 @@
1
- import type {Patch} from '@portabletext/patches'
2
- import type {PortableTextBlock} from '@sanity/types'
3
- import {Transforms} from 'slate'
4
- import {ReactEditor} from 'slate-react'
5
- import {
6
- assertEvent,
7
- assign,
8
- emit,
9
- enqueueActions,
10
- setup,
11
- type ActorRefFrom,
12
- } from 'xstate'
13
- import type {BehaviorConfig} from '../behaviors/behavior.config'
14
- import {coreBehaviorsConfig} from '../behaviors/behavior.core'
15
- import {performEvent} from '../behaviors/behavior.perform-event'
16
- import type {
17
- BehaviorEvent,
18
- ExternalBehaviorEvent,
19
- } from '../behaviors/behavior.types.event'
20
- import type {Converter} from '../converters/converter.types'
21
- import {debugWithName} from '../internal-utils/debug'
22
- import type {EventPosition} from '../internal-utils/event-position'
23
- import {sortByPriority} from '../priority/priority.sort'
24
- import type {NamespaceEvent, OmitFromUnion} from '../type-utils'
25
- import type {
26
- EditorSelection,
27
- PortableTextMemberSchemaTypes,
28
- } from '../types/editor'
29
- import type {PortableTextSlateEditor} from '../types/slate-editor'
30
- import type {EditorSchema} from './editor-schema'
31
- import {createEditorSnapshot} from './editor-snapshot'
32
- import type {
33
- EditorEmittedEvent,
34
- MutationEvent,
35
- PatchEvent,
36
- } from './relay-machine'
37
- import {isNormalizingNode} from './with-normalizing-node'
38
-
39
- export * from 'xstate/guards'
40
-
41
- const debug = debugWithName('editor machine')
42
-
43
- /**
44
- * @public
45
- */
46
- export type PatchesEvent = {
47
- type: 'patches'
48
- patches: Array<Patch>
49
- snapshot: Array<PortableTextBlock> | undefined
50
- }
51
-
52
- /**
53
- * @public
54
- */
55
- export type ExternalEditorEvent =
56
- | {
57
- type: 'update readOnly'
58
- readOnly: boolean
59
- }
60
- | PatchesEvent
61
-
62
- type InternalPatchEvent = NamespaceEvent<PatchEvent, 'internal'> & {
63
- operationId?: string
64
- value: Array<PortableTextBlock>
65
- }
66
-
67
- /**
68
- * @internal
69
- */
70
- export type EditorActor = ActorRefFrom<typeof editorMachine>
71
-
72
- /**
73
- * @internal
74
- */
75
- export type InternalEditorEvent =
76
- | ExternalEditorEvent
77
- | {
78
- type: 'add behavior'
79
- behaviorConfig: BehaviorConfig
80
- }
81
- | {
82
- type: 'remove behavior'
83
- behaviorConfig: BehaviorConfig
84
- }
85
- | {
86
- type: 'blur'
87
- editor: PortableTextSlateEditor
88
- }
89
- | {
90
- type: 'focus'
91
- editor: PortableTextSlateEditor
92
- }
93
- | {
94
- type: 'update selection'
95
- selection: EditorSelection
96
- }
97
- | {
98
- type: 'done syncing value'
99
- }
100
- | {
101
- type: 'syncing value'
102
- }
103
- | {
104
- type: 'behavior event'
105
- behaviorEvent: BehaviorEvent
106
- editor: PortableTextSlateEditor
107
- nativeEvent?: {preventDefault: () => void}
108
- }
109
- | MutationEvent
110
- | InternalPatchEvent
111
- | {
112
- type: 'set drag ghost'
113
- ghost: HTMLElement
114
- }
115
- | {
116
- type: 'dragstart'
117
- ghost?: HTMLElement
118
- origin: Pick<EventPosition, 'selection'>
119
- }
120
- | {type: 'dragend'}
121
- | {type: 'drop'}
122
- | {type: 'add slate editor'; editor: PortableTextSlateEditor}
123
-
124
- /**
125
- * @internal
126
- */
127
- export type InternalEditorEmittedEvent =
128
- | OmitFromUnion<EditorEmittedEvent, 'type', 'patch'>
129
- | InternalPatchEvent
130
- | PatchesEvent
131
-
132
- export function rerouteExternalBehaviorEvent({
133
- event,
134
- slateEditor,
135
- }: {
136
- event: ExternalBehaviorEvent
137
- slateEditor: PortableTextSlateEditor
138
- }): InternalEditorEvent {
139
- switch (event.type) {
140
- case 'blur':
141
- return {
142
- type: 'blur',
143
- editor: slateEditor,
144
- }
145
-
146
- case 'focus':
147
- return {
148
- type: 'focus',
149
- editor: slateEditor,
150
- }
151
-
152
- case 'insert.block object':
153
- return {
154
- type: 'behavior event',
155
- behaviorEvent: {
156
- type: 'insert.block',
157
- block: {
158
- _type: event.blockObject.name,
159
- ...(event.blockObject.value ?? {}),
160
- },
161
- placement: event.placement,
162
- },
163
- editor: slateEditor,
164
- }
165
-
166
- default:
167
- return {
168
- type: 'behavior event',
169
- behaviorEvent: event,
170
- editor: slateEditor,
171
- }
172
- }
173
- }
174
-
175
- /**
176
- * @internal
177
- */
178
- export const editorMachine = setup({
179
- types: {
180
- context: {} as {
181
- behaviors: Set<BehaviorConfig>
182
- behaviorsSorted: boolean
183
- converters: Set<Converter>
184
- getLegacySchema: () => PortableTextMemberSchemaTypes
185
- keyGenerator: () => string
186
- pendingEvents: Array<InternalPatchEvent | MutationEvent>
187
- pendingIncomingPatchesEvents: Array<PatchesEvent>
188
- schema: EditorSchema
189
- initialReadOnly: boolean
190
- selection: EditorSelection
191
- initialValue: Array<PortableTextBlock> | undefined
192
- internalDrag?: {
193
- origin: Pick<EventPosition, 'selection'>
194
- }
195
- dragGhost?: HTMLElement
196
- slateEditor?: PortableTextSlateEditor
197
- },
198
- events: {} as InternalEditorEvent,
199
- emitted: {} as InternalEditorEmittedEvent,
200
- input: {} as {
201
- converters?: Array<Converter>
202
- getLegacySchema: () => PortableTextMemberSchemaTypes
203
- keyGenerator: () => string
204
- readOnly?: boolean
205
- schema: EditorSchema
206
- initialValue?: Array<PortableTextBlock>
207
- },
208
- tags: {} as 'dragging internally',
209
- },
210
- actions: {
211
- 'add behavior to context': assign({
212
- behaviors: ({context, event}) => {
213
- assertEvent(event, 'add behavior')
214
-
215
- return new Set([...context.behaviors, event.behaviorConfig])
216
- },
217
- behaviorsSorted: false,
218
- }),
219
- 'remove behavior from context': assign({
220
- behaviors: ({context, event}) => {
221
- assertEvent(event, 'remove behavior')
222
-
223
- context.behaviors.delete(event.behaviorConfig)
224
-
225
- return new Set([...context.behaviors])
226
- },
227
- }),
228
- 'add slate editor to context': assign({
229
- slateEditor: ({context, event}) => {
230
- return event.type === 'add slate editor'
231
- ? event.editor
232
- : context.slateEditor
233
- },
234
- }),
235
- 'emit patch event': emit(({event}) => {
236
- assertEvent(event, 'internal.patch')
237
- return event
238
- }),
239
- 'emit mutation event': emit(({event}) => {
240
- assertEvent(event, 'mutation')
241
- return event
242
- }),
243
- 'emit read only': emit({type: 'read only'}),
244
- 'emit editable': emit({type: 'editable'}),
245
- 'defer event': assign({
246
- pendingEvents: ({context, event}) => {
247
- assertEvent(event, ['internal.patch', 'mutation'])
248
- return [...context.pendingEvents, event]
249
- },
250
- }),
251
- 'emit pending events': enqueueActions(({context, enqueue}) => {
252
- for (const event of context.pendingEvents) {
253
- enqueue.emit(event)
254
- }
255
- }),
256
- 'emit ready': emit({type: 'ready'}),
257
- 'clear pending events': assign({
258
- pendingEvents: [],
259
- }),
260
- 'defer incoming patches': assign({
261
- pendingIncomingPatchesEvents: ({context, event}) => {
262
- return event.type === 'patches'
263
- ? [...context.pendingIncomingPatchesEvents, event]
264
- : context.pendingIncomingPatchesEvents
265
- },
266
- }),
267
- 'emit pending incoming patches': enqueueActions(({context, enqueue}) => {
268
- for (const event of context.pendingIncomingPatchesEvents) {
269
- enqueue.emit(event)
270
- }
271
- }),
272
- 'clear pending incoming patches': assign({
273
- pendingIncomingPatchesEvents: [],
274
- }),
275
- 'handle blur': ({event}) => {
276
- assertEvent(event, 'blur')
277
-
278
- try {
279
- ReactEditor.blur(event.editor)
280
- } catch (error) {
281
- console.error(new Error(`Failed to blur editor: ${error.message}`))
282
- }
283
- },
284
- 'handle focus': ({context}) => {
285
- if (!context.slateEditor) {
286
- console.error('No Slate editor found to focus')
287
- return
288
- }
289
-
290
- try {
291
- const currentSelection = context.slateEditor.selection
292
- ReactEditor.focus(context.slateEditor)
293
- if (currentSelection) {
294
- Transforms.select(context.slateEditor, currentSelection)
295
- }
296
- } catch (error) {
297
- console.error(new Error(`Failed to focus editor: ${error.message}`))
298
- }
299
- },
300
- 'handle behavior event': ({context, event, self}) => {
301
- assertEvent(event, ['behavior event'])
302
-
303
- try {
304
- const behaviors = [...context.behaviors.values()].map(
305
- (config) => config.behavior,
306
- )
307
-
308
- performEvent({
309
- mode: 'send',
310
- behaviors,
311
- remainingEventBehaviors: behaviors,
312
- event: event.behaviorEvent,
313
- editor: event.editor,
314
- keyGenerator: context.keyGenerator,
315
- schema: context.schema,
316
- getSnapshot: () =>
317
- createEditorSnapshot({
318
- converters: [...context.converters],
319
- editor: event.editor,
320
- keyGenerator: context.keyGenerator,
321
- readOnly: self.getSnapshot().matches({'edit mode': 'read only'}),
322
- schema: context.schema,
323
- }),
324
- nativeEvent: event.nativeEvent,
325
- sendBack: (eventSentBack) => {
326
- if (eventSentBack.type === 'set drag ghost') {
327
- self.send(eventSentBack)
328
- return
329
- }
330
-
331
- self.send(
332
- rerouteExternalBehaviorEvent({
333
- event: eventSentBack,
334
- slateEditor: event.editor,
335
- }),
336
- )
337
- },
338
- })
339
- } catch (error) {
340
- console.error(
341
- new Error(
342
- `Raising "${event.behaviorEvent.type}" failed due to: ${error.message}`,
343
- ),
344
- )
345
- }
346
- },
347
- 'sort behaviors': assign({
348
- behaviors: ({context}) =>
349
- !context.behaviorsSorted
350
- ? new Set(sortByPriority([...context.behaviors.values()]))
351
- : context.behaviors,
352
- behaviorsSorted: true,
353
- }),
354
- },
355
- guards: {
356
- 'slate is busy': ({context}) => {
357
- if (!context.slateEditor) {
358
- return false
359
- }
360
-
361
- return context.slateEditor.operations.length > 0
362
- },
363
- 'slate is normalizing node': ({context}) => {
364
- if (!context.slateEditor) {
365
- return false
366
- }
367
-
368
- return isNormalizingNode(context.slateEditor)
369
- },
370
- },
371
- }).createMachine({
372
- id: 'editor',
373
- context: ({input}) => ({
374
- behaviors: new Set(coreBehaviorsConfig),
375
- behaviorsSorted: false,
376
- converters: new Set(input.converters ?? []),
377
- getLegacySchema: input.getLegacySchema,
378
- keyGenerator: input.keyGenerator,
379
- pendingEvents: [],
380
- pendingIncomingPatchesEvents: [],
381
- schema: input.schema,
382
- selection: null,
383
- initialReadOnly: input.readOnly ?? false,
384
- initialValue: input.initialValue,
385
- }),
386
- on: {
387
- 'add behavior': {actions: 'add behavior to context'},
388
- 'remove behavior': {actions: 'remove behavior from context'},
389
- 'add slate editor': {actions: 'add slate editor to context'},
390
- 'update selection': {
391
- actions: [
392
- assign({selection: ({event}) => event.selection}),
393
- emit(({event}) => ({...event, type: 'selection'})),
394
- ],
395
- },
396
- 'set drag ghost': {
397
- actions: assign({dragGhost: ({event}) => event.ghost}),
398
- },
399
- },
400
- type: 'parallel',
401
- states: {
402
- 'edit mode': {
403
- initial: 'read only',
404
- states: {
405
- 'read only': {
406
- initial: 'determine initial edit mode',
407
- on: {
408
- 'behavior event': {
409
- actions: ['sort behaviors', 'handle behavior event'],
410
- guard: ({event}) =>
411
- event.behaviorEvent.type === 'clipboard.copy' ||
412
- event.behaviorEvent.type === 'mouse.click' ||
413
- event.behaviorEvent.type === 'serialize' ||
414
- event.behaviorEvent.type === 'serialization.failure' ||
415
- event.behaviorEvent.type === 'serialization.success' ||
416
- event.behaviorEvent.type === 'select',
417
- },
418
- },
419
- states: {
420
- 'determine initial edit mode': {
421
- entry: [
422
- () => {
423
- debug(
424
- 'entry: edit mode->read only->determine initial edit mode',
425
- )
426
- },
427
- ],
428
- exit: [
429
- () => {
430
- debug(
431
- 'exit: edit mode->read only->determine initial edit mode',
432
- )
433
- },
434
- ],
435
- on: {
436
- 'done syncing value': [
437
- {
438
- target: '#editor.edit mode.read only.read only',
439
- guard: ({context}) => context.initialReadOnly,
440
- },
441
- {
442
- target: '#editor.edit mode.editable',
443
- },
444
- ],
445
- },
446
- },
447
- 'read only': {
448
- entry: [
449
- () => {
450
- debug('entry: edit mode->read only->read only')
451
- },
452
- ],
453
- exit: [
454
- () => {
455
- debug('exit: edit mode->read only->read only')
456
- },
457
- ],
458
- on: {
459
- 'update readOnly': {
460
- guard: ({event}) => !event.readOnly,
461
- target: '#editor.edit mode.editable',
462
- actions: ['emit editable'],
463
- },
464
- },
465
- },
466
- },
467
- },
468
- 'editable': {
469
- on: {
470
- 'update readOnly': {
471
- guard: ({event}) => event.readOnly,
472
- target: '#editor.edit mode.read only.read only',
473
- actions: ['emit read only'],
474
- },
475
- 'behavior event': {
476
- actions: ['sort behaviors', 'handle behavior event'],
477
- },
478
- 'blur': {
479
- actions: 'handle blur',
480
- },
481
- 'focus': {
482
- target: '.focusing',
483
- actions: [assign({slateEditor: ({event}) => event.editor})],
484
- },
485
- },
486
- initial: 'idle',
487
- states: {
488
- 'idle': {
489
- entry: [
490
- () => {
491
- debug('entry: edit mode->editable->idle')
492
- },
493
- ],
494
- exit: [
495
- () => {
496
- debug('exit: edit mode->editable-idle')
497
- },
498
- ],
499
- on: {
500
- dragstart: {
501
- actions: [
502
- assign({
503
- internalDrag: ({event}) => ({
504
- origin: event.origin,
505
- }),
506
- }),
507
- ],
508
- target: 'dragging internally',
509
- },
510
- },
511
- },
512
- 'focusing': {
513
- initial: 'checking if busy',
514
- states: {
515
- 'checking if busy': {
516
- entry: [
517
- () => {
518
- debug(
519
- 'entry: edit mode->editable->focusing->checking if busy',
520
- )
521
- },
522
- ],
523
- exit: [
524
- () => {
525
- debug(
526
- 'exit: edit mode->editable->focusing->checking if busy',
527
- )
528
- },
529
- ],
530
- always: [
531
- {
532
- guard: 'slate is busy',
533
- target: 'busy',
534
- },
535
- {
536
- target: '#editor.edit mode.editable.idle',
537
- actions: ['handle focus'],
538
- },
539
- ],
540
- },
541
- 'busy': {
542
- entry: [
543
- () => {
544
- debug('entry: edit mode->editable->focusing-busy')
545
- },
546
- ],
547
- exit: [
548
- () => {
549
- debug('exit: edit mode->editable->focusing->busy')
550
- },
551
- ],
552
- after: {
553
- 10: {
554
- target: 'checking if busy',
555
- },
556
- },
557
- },
558
- },
559
- },
560
- 'dragging internally': {
561
- entry: [
562
- () => {
563
- debug('entry: edit mode->editable->dragging internally')
564
- },
565
- ],
566
- exit: [
567
- () => {
568
- debug('exit: edit mode->editable->dragging internally')
569
- },
570
- ({context}) => {
571
- if (context.dragGhost) {
572
- try {
573
- context.dragGhost.parentNode?.removeChild(
574
- context.dragGhost,
575
- )
576
- } catch (error) {
577
- console.error(
578
- new Error(
579
- `Removing the drag ghost failed due to: ${error.message}`,
580
- ),
581
- )
582
- }
583
- }
584
- },
585
- assign({dragGhost: undefined}),
586
- assign({internalDrag: undefined}),
587
- ],
588
- tags: ['dragging internally'],
589
- on: {
590
- dragend: {target: 'idle'},
591
- drop: {target: 'idle'},
592
- },
593
- },
594
- },
595
- },
596
- },
597
- },
598
- 'setup': {
599
- initial: 'setting up',
600
- states: {
601
- 'setting up': {
602
- entry: [
603
- () => {
604
- debug('entry: setup->setting up')
605
- },
606
- ],
607
- exit: [
608
- () => {
609
- debug('exit: setup->setting up')
610
- },
611
- 'emit ready',
612
- 'emit pending incoming patches',
613
- 'clear pending incoming patches',
614
- ],
615
- on: {
616
- 'internal.patch': {
617
- actions: 'defer event',
618
- },
619
- 'mutation': {
620
- actions: 'defer event',
621
- },
622
- 'done syncing value': {
623
- target: 'set up',
624
- },
625
- 'patches': {
626
- actions: ['defer incoming patches'],
627
- },
628
- },
629
- },
630
- 'set up': {
631
- type: 'parallel',
632
- states: {
633
- 'value sync': {
634
- initial: 'idle',
635
- states: {
636
- 'idle': {
637
- entry: [
638
- () => {
639
- debug('entry: setup->set up->value sync->idle')
640
- },
641
- ],
642
- exit: [
643
- () => {
644
- debug('exit: setup->set up->value sync->idle')
645
- },
646
- ],
647
- on: {
648
- 'patches': {
649
- actions: [emit(({event}) => event)],
650
- },
651
- 'syncing value': {
652
- target: 'syncing value',
653
- },
654
- },
655
- },
656
- 'syncing value': {
657
- entry: [
658
- () => {
659
- debug('entry: setup->set up->value sync->syncing value')
660
- },
661
- ],
662
- exit: [
663
- () => {
664
- debug('exit: setup->set up->value sync->syncing value')
665
- },
666
- 'emit pending incoming patches',
667
- 'clear pending incoming patches',
668
- ],
669
- on: {
670
- 'patches': {
671
- actions: ['defer incoming patches'],
672
- },
673
- 'done syncing value': {
674
- target: 'idle',
675
- },
676
- },
677
- },
678
- },
679
- },
680
- 'writing': {
681
- initial: 'pristine',
682
- states: {
683
- pristine: {
684
- initial: 'idle',
685
- states: {
686
- idle: {
687
- entry: [
688
- () => {
689
- debug('entry: setup->set up->writing->pristine->idle')
690
- },
691
- ],
692
- exit: [
693
- () => {
694
- debug('exit: setup->set up->writing->pristine->idle')
695
- },
696
- ],
697
- on: {
698
- 'internal.patch': [
699
- {
700
- guard: 'slate is normalizing node',
701
- actions: 'defer event',
702
- },
703
- {
704
- actions: 'defer event',
705
- target: '#editor.setup.set up.writing.dirty',
706
- },
707
- ],
708
- 'mutation': [
709
- {
710
- guard: 'slate is normalizing node',
711
- actions: 'defer event',
712
- },
713
- {
714
- actions: 'defer event',
715
- target: '#editor.setup.set up.writing.dirty',
716
- },
717
- ],
718
- },
719
- },
720
- },
721
- },
722
- dirty: {
723
- entry: [
724
- () => {
725
- debug('entry: setup->set up->writing->dirty')
726
- },
727
- 'emit pending events',
728
- 'clear pending events',
729
- ],
730
- exit: [
731
- () => {
732
- debug('exit: setup->set up->writing->dirty')
733
- },
734
- ],
735
- on: {
736
- 'internal.patch': {
737
- actions: 'emit patch event',
738
- },
739
- 'mutation': {
740
- actions: 'emit mutation event',
741
- },
742
- },
743
- },
744
- },
745
- },
746
- },
747
- },
748
- },
749
- },
750
- },
751
- })