@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,495 +0,0 @@
1
- import {isSpan} from '@portabletext/schema'
2
- import {isEqual} from 'lodash'
3
- import {
4
- Editor,
5
- Element,
6
- Path,
7
- Point,
8
- Range,
9
- Transforms,
10
- type Descendant,
11
- } from 'slate'
12
- import {DOMEditor} from 'slate-dom'
13
- import {createPlaceholderBlock} from '../internal-utils/create-placeholder-block'
14
- import {getFocusBlock, getFocusChild} from '../internal-utils/slate-utils'
15
- import {toSlateRange} from '../internal-utils/to-slate-range'
16
- import {toSlateBlock} from '../internal-utils/values'
17
- import type {EditorSelection} from '../types/editor'
18
- import type {PortableTextSlateEditor} from '../types/slate-editor'
19
- import {parseBlock} from '../utils/parse-blocks'
20
- import {isEmptyTextBlock} from '../utils/util.is-empty-text-block'
21
- import type {
22
- BehaviorOperationImplementation,
23
- BehaviorOperationImplementationContext,
24
- } from './behavior.operations'
25
-
26
- export const insertBlockOperationImplementation: BehaviorOperationImplementation<
27
- 'insert.block'
28
- > = ({context, operation}) => {
29
- const parsedBlock = parseBlock({
30
- block: operation.block,
31
- context,
32
- options: {
33
- normalize: true,
34
- removeUnusedMarkDefs: true,
35
- validateFields: true,
36
- },
37
- })
38
-
39
- if (!parsedBlock) {
40
- throw new Error(`Failed to parse block ${JSON.stringify(operation.block)}`)
41
- }
42
-
43
- const block = toSlateBlock(parsedBlock, {schemaTypes: context.schema})
44
-
45
- insertBlock({
46
- context,
47
- block,
48
- placement: operation.placement,
49
- select: operation.select ?? 'start',
50
- at: operation.at,
51
- editor: operation.editor,
52
- })
53
- }
54
-
55
- export function insertBlock(options: {
56
- context: BehaviorOperationImplementationContext
57
- block: Descendant
58
- placement: 'auto' | 'after' | 'before'
59
- select: 'start' | 'end' | 'none'
60
- at?: NonNullable<EditorSelection>
61
- editor: PortableTextSlateEditor
62
- }) {
63
- const {context, block, placement, select, editor} = options
64
- const at = options.at
65
- ? toSlateRange({
66
- context: {
67
- schema: context.schema,
68
- value: editor.value,
69
- selection: options.at,
70
- },
71
- blockIndexMap: editor.blockIndexMap,
72
- })
73
- : editor.selection
74
-
75
- if (editor.children.length === 0) {
76
- Transforms.insertNodes(editor, createPlaceholderBlock(context), {at: [0]})
77
- }
78
-
79
- // Fall back to the start and end of the editor if neither an editor
80
- // selection nor an `at` range is provided
81
- const start = at ? Range.start(at) : Editor.start(editor, [])
82
- const end = at ? Range.end(at) : Editor.end(editor, [])
83
-
84
- const [startBlock, startBlockPath] = Array.from(
85
- Editor.nodes(editor, {
86
- at: start,
87
- mode: 'lowest',
88
- match: (node, path) =>
89
- Element.isElement(node) && path.length <= start.path.length,
90
- }),
91
- ).at(0) ?? [undefined, undefined]
92
- const [endBlock, endBlockPath] = Array.from(
93
- Editor.nodes(editor, {
94
- at: end,
95
- mode: 'lowest',
96
- match: (node, path) =>
97
- Element.isElement(node) && path.length <= end.path.length,
98
- }),
99
- ).at(0) ?? [undefined, undefined]
100
-
101
- if (!startBlock || !startBlockPath || !endBlock || !endBlockPath) {
102
- throw new Error('Unable to insert block without a start and end block')
103
- }
104
-
105
- if (!editor.selection && select !== 'none') {
106
- DOMEditor.focus(editor)
107
- }
108
-
109
- if (!at) {
110
- if (placement === 'before') {
111
- Transforms.insertNodes(editor, [block], {at: [0]})
112
-
113
- if (select === 'start') {
114
- Transforms.select(editor, Editor.start(editor, [0]))
115
- } else if (select === 'end') {
116
- Transforms.select(editor, Editor.end(editor, [0]))
117
- }
118
- } else if (placement === 'after') {
119
- const nextPath = Path.next(endBlockPath)
120
- Transforms.insertNodes(editor, [block], {at: nextPath})
121
-
122
- if (select === 'start') {
123
- Transforms.select(editor, Editor.start(editor, nextPath))
124
- } else if (select === 'end') {
125
- Transforms.select(editor, Editor.end(editor, nextPath))
126
- }
127
- } else {
128
- // placement === 'auto'
129
-
130
- if (isEmptyTextBlock(context, endBlock)) {
131
- Transforms.insertNodes(editor, [block], {
132
- at: endBlockPath,
133
- select: false,
134
- })
135
-
136
- // And if the last block was an empty text block, let's remove
137
- // that too
138
- Transforms.removeNodes(editor, {at: Path.next(endBlockPath)})
139
-
140
- Transforms.deselect(editor)
141
-
142
- if (select === 'start') {
143
- Transforms.select(editor, Editor.start(editor, endBlockPath))
144
- } else if (select === 'end') {
145
- Transforms.select(editor, Editor.end(editor, endBlockPath))
146
- }
147
-
148
- return
149
- }
150
-
151
- if (
152
- editor.isTextBlock(block) &&
153
- endBlock &&
154
- editor.isTextBlock(endBlock)
155
- ) {
156
- const selectionBefore = Editor.end(editor, endBlockPath)
157
-
158
- Transforms.insertFragment(editor, [block], {
159
- at: Editor.end(editor, endBlockPath),
160
- })
161
-
162
- if (select === 'start') {
163
- Transforms.select(editor, selectionBefore)
164
- } else if (select === 'none') {
165
- Transforms.deselect(editor)
166
- }
167
-
168
- return
169
- }
170
-
171
- const nextPath = Path.next(endBlockPath)
172
-
173
- Transforms.insertNodes(editor, [block], {at: nextPath, select: false})
174
-
175
- if (select === 'start') {
176
- Transforms.select(editor, Editor.start(editor, nextPath))
177
- } else if (select === 'end') {
178
- Transforms.select(editor, Editor.end(editor, nextPath))
179
- }
180
- }
181
-
182
- return
183
- }
184
-
185
- if (!at) {
186
- throw new Error('Unable to insert block without a selection')
187
- }
188
-
189
- if (placement === 'before') {
190
- Transforms.insertNodes(editor, [block], {
191
- at: startBlockPath,
192
- select: false,
193
- })
194
-
195
- if (select === 'start') {
196
- Transforms.select(editor, Editor.start(editor, startBlockPath))
197
- } else if (select === 'end') {
198
- Transforms.select(editor, Editor.end(editor, startBlockPath))
199
- }
200
- } else if (placement === 'after') {
201
- const nextPath = Path.next(endBlockPath)
202
-
203
- Transforms.insertNodes(editor, [block], {
204
- at: nextPath,
205
- select: false,
206
- })
207
-
208
- if (select === 'start') {
209
- Transforms.select(editor, Editor.start(editor, nextPath))
210
- } else if (select === 'end') {
211
- Transforms.select(editor, Editor.end(editor, nextPath))
212
- }
213
- } else {
214
- // placement === 'auto'
215
-
216
- const endBlockEndPoint = Editor.start(editor, endBlockPath)
217
-
218
- if (Range.isExpanded(at) && !editor.isTextBlock(block)) {
219
- const atBeforeDelete = Editor.rangeRef(editor, at, {affinity: 'inward'})
220
-
221
- Transforms.delete(editor, {at})
222
-
223
- const [focusBlock, focusBlockPath] = getFocusBlock({editor})
224
-
225
- const atAfterDelete = atBeforeDelete.unref() ?? editor.selection
226
-
227
- const atBeforeInsert = atAfterDelete
228
- ? Editor.rangeRef(editor, atAfterDelete, {affinity: 'inward'})
229
- : undefined
230
-
231
- Transforms.insertNodes(editor, [block], {
232
- voids: true,
233
- at: atAfterDelete ?? undefined,
234
- select: select !== 'none',
235
- })
236
-
237
- const atAfterInsert = atBeforeInsert?.unref() ?? editor.selection
238
-
239
- if (select === 'none' && atAfterInsert) {
240
- Transforms.select(editor, atAfterInsert)
241
- }
242
-
243
- if (isEmptyTextBlock(context, focusBlock)) {
244
- Transforms.removeNodes(editor, {at: focusBlockPath})
245
- }
246
-
247
- return
248
- }
249
-
250
- if (editor.isTextBlock(endBlock) && editor.isTextBlock(block)) {
251
- const selectionStartPoint = Range.start(at)
252
-
253
- if (isEmptyTextBlock(context, endBlock)) {
254
- Transforms.insertNodes(editor, [block], {
255
- at: endBlockPath,
256
- select: false,
257
- })
258
- Transforms.removeNodes(editor, {at: Path.next(endBlockPath)})
259
-
260
- if (select === 'start') {
261
- Transforms.select(editor, selectionStartPoint)
262
- } else if (select === 'end') {
263
- Transforms.select(editor, Editor.end(editor, endBlockPath))
264
- } else {
265
- Transforms.select(editor, at)
266
- }
267
-
268
- return
269
- }
270
-
271
- const endBlockChildKeys = endBlock.children.map((child) => child._key)
272
- const endBlockMarkDefsKeys =
273
- endBlock.markDefs?.map((markDef) => markDef._key) ?? []
274
-
275
- // Assign new keys to markDefs with duplicate keys and keep track of
276
- // the mapping between the old and new keys
277
- const markDefKeyMap = new Map<string, string>()
278
- const adjustedMarkDefs = block.markDefs?.map((markDef) => {
279
- if (endBlockMarkDefsKeys.includes(markDef._key)) {
280
- const newKey = context.keyGenerator()
281
- markDefKeyMap.set(markDef._key, newKey)
282
- return {
283
- ...markDef,
284
- _key: newKey,
285
- }
286
- }
287
-
288
- return markDef
289
- })
290
-
291
- // Assign new keys to spans with duplicate keys and update any markDef
292
- // key if needed
293
- const adjustedChildren = block.children.map((child) => {
294
- if (isSpan(context, child)) {
295
- const marks =
296
- child.marks?.map((mark) => {
297
- const markDefKey = markDefKeyMap.get(mark)
298
-
299
- if (markDefKey) {
300
- return markDefKey
301
- }
302
-
303
- return mark
304
- }) ?? []
305
-
306
- if (!isEqual(child.marks, marks)) {
307
- return {
308
- ...child,
309
- _key: endBlockChildKeys.includes(child._key)
310
- ? context.keyGenerator()
311
- : child._key,
312
- marks,
313
- }
314
- }
315
- }
316
-
317
- if (endBlockChildKeys.includes(child._key)) {
318
- return {
319
- ...child,
320
- _key: context.keyGenerator(),
321
- }
322
- }
323
-
324
- return child
325
- })
326
-
327
- // Carry over the markDefs from the incoming block to the end block
328
- Transforms.setNodes(
329
- editor,
330
- {
331
- markDefs: [...(endBlock.markDefs ?? []), ...(adjustedMarkDefs ?? [])],
332
- },
333
- {
334
- at: endBlockPath,
335
- },
336
- )
337
-
338
- // If the children have changed, we need to create a new block with
339
- // the adjusted children
340
- const adjustedBlock = !isEqual(block.children, adjustedChildren)
341
- ? {
342
- ...block,
343
- children: adjustedChildren as Descendant[],
344
- }
345
- : block
346
-
347
- if (select === 'end') {
348
- Transforms.insertFragment(editor, [adjustedBlock], {
349
- voids: true,
350
- })
351
-
352
- return
353
- }
354
-
355
- Transforms.insertFragment(editor, [adjustedBlock], {
356
- at,
357
- voids: true,
358
- })
359
-
360
- if (select === 'start') {
361
- Transforms.select(editor, selectionStartPoint)
362
- } else {
363
- if (!Point.equals(selectionStartPoint, endBlockEndPoint)) {
364
- Transforms.select(editor, selectionStartPoint)
365
- }
366
- }
367
- } else {
368
- if (!editor.isTextBlock(endBlock)) {
369
- Transforms.insertNodes(editor, [block], {select: false})
370
-
371
- const nextPath = [endBlockPath[0] + 1]
372
-
373
- if (select === 'start') {
374
- Transforms.select(editor, Editor.start(editor, nextPath))
375
- } else if (select === 'end') {
376
- Transforms.select(editor, Editor.end(editor, nextPath))
377
- }
378
- } else {
379
- const endBlockStartPoint = Editor.start(editor, endBlockPath)
380
- const endBlockEndPoint = Editor.end(editor, endBlockPath)
381
- const selectionStartPoint = Range.start(at)
382
- const selectionEndPoint = Range.end(at)
383
-
384
- if (
385
- Range.isCollapsed(at) &&
386
- Point.equals(selectionStartPoint, endBlockStartPoint)
387
- ) {
388
- Transforms.insertNodes(editor, [block], {
389
- at: endBlockPath,
390
- select: false,
391
- })
392
-
393
- if (select === 'start' || select === 'end') {
394
- Transforms.select(editor, Editor.start(editor, endBlockPath))
395
- }
396
-
397
- if (isEmptyTextBlock(context, endBlock)) {
398
- Transforms.removeNodes(editor, {at: Path.next(endBlockPath)})
399
- }
400
- } else if (
401
- Range.isCollapsed(at) &&
402
- Point.equals(selectionEndPoint, endBlockEndPoint)
403
- ) {
404
- const nextPath = [endBlockPath[0] + 1]
405
-
406
- Transforms.insertNodes(editor, [block], {
407
- at: nextPath,
408
- select: false,
409
- })
410
-
411
- if (select === 'start' || select === 'end') {
412
- Transforms.select(editor, Editor.start(editor, nextPath))
413
- }
414
- } else if (
415
- Range.isExpanded(at) &&
416
- Point.equals(selectionStartPoint, endBlockStartPoint) &&
417
- Point.equals(selectionEndPoint, endBlockEndPoint)
418
- ) {
419
- Transforms.insertFragment(editor, [block], {
420
- at,
421
- })
422
-
423
- if (select === 'start') {
424
- Transforms.select(editor, Editor.start(editor, endBlockPath))
425
- } else if (select === 'end') {
426
- Transforms.select(editor, Editor.end(editor, endBlockPath))
427
- }
428
- } else if (
429
- Range.isExpanded(at) &&
430
- Point.equals(selectionStartPoint, endBlockStartPoint)
431
- ) {
432
- Transforms.insertFragment(editor, [block], {
433
- at,
434
- })
435
-
436
- if (select === 'start') {
437
- Transforms.select(editor, Editor.start(editor, endBlockPath))
438
- } else if (select === 'end') {
439
- Transforms.select(editor, Editor.end(editor, endBlockPath))
440
- }
441
- } else if (
442
- Range.isExpanded(at) &&
443
- Point.equals(selectionEndPoint, endBlockEndPoint)
444
- ) {
445
- Transforms.insertFragment(editor, [block], {
446
- at,
447
- })
448
-
449
- if (select === 'start') {
450
- Transforms.select(
451
- editor,
452
- Editor.start(editor, Path.next(endBlockPath)),
453
- )
454
- } else if (select === 'end') {
455
- Transforms.select(
456
- editor,
457
- Editor.end(editor, Path.next(endBlockPath)),
458
- )
459
- }
460
- } else {
461
- const [focusChild] = getFocusChild({editor})
462
-
463
- if (focusChild && editor.isTextSpan(focusChild)) {
464
- Transforms.splitNodes(editor, {
465
- at,
466
- })
467
-
468
- Transforms.insertFragment(editor, [block], {
469
- at,
470
- })
471
-
472
- if (select === 'start' || select === 'end') {
473
- Transforms.select(editor, [endBlockPath[0] + 1])
474
- } else {
475
- Transforms.select(editor, at)
476
- }
477
- } else {
478
- const nextPath = [endBlockPath[0] + 1]
479
- Transforms.insertNodes(editor, [block], {
480
- at: nextPath,
481
- select: false,
482
- })
483
- Transforms.select(editor, at)
484
-
485
- if (select === 'start') {
486
- Transforms.select(editor, Editor.start(editor, nextPath))
487
- } else if (select === 'end') {
488
- Transforms.select(editor, Editor.end(editor, nextPath))
489
- }
490
- }
491
- }
492
- }
493
- }
494
- }
495
- }
@@ -1,129 +0,0 @@
1
- import {isTextBlock} from '@portabletext/schema'
2
- import {Transforms} from 'slate'
3
- import {EDITOR_TO_PENDING_SELECTION} from 'slate-dom'
4
- import {getFocusBlock, getFocusSpan} from '../internal-utils/slate-utils'
5
- import {VOID_CHILD_KEY} from '../internal-utils/values'
6
- import {parseInlineObject, parseSpan} from '../utils/parse-blocks'
7
- import type {BehaviorOperationImplementation} from './behavior.operations'
8
-
9
- export const insertChildOperationImplementation: BehaviorOperationImplementation<
10
- 'insert.child'
11
- > = ({context, operation}) => {
12
- const focus = operation.editor.selection?.focus
13
- const focusBlockIndex = focus?.path.at(0)
14
- const focusChildIndex = focus?.path.at(1)
15
-
16
- if (focusBlockIndex === undefined || focusChildIndex === undefined) {
17
- throw new Error('Unable to insert child without a focus')
18
- }
19
-
20
- const [focusBlock, focusBlockPath] = getFocusBlock({editor: operation.editor})
21
-
22
- if (!focus || !focusBlock || !focusBlockPath) {
23
- throw new Error('Unable to insert child without a focus block')
24
- }
25
-
26
- if (!isTextBlock(context, focusBlock)) {
27
- throw new Error('Unable to insert child into a non-text block')
28
- }
29
-
30
- const markDefs = focusBlock.markDefs ?? []
31
- const markDefKeyMap = new Map<string, string>()
32
- for (const markDef of markDefs) {
33
- markDefKeyMap.set(markDef._key, markDef._key)
34
- }
35
-
36
- const span = parseSpan({
37
- span: operation.child,
38
- context,
39
- markDefKeyMap,
40
- options: {validateFields: true},
41
- })
42
-
43
- if (span) {
44
- const [focusSpan] = getFocusSpan({editor: operation.editor})
45
-
46
- if (focusSpan) {
47
- Transforms.insertNodes(operation.editor, span, {
48
- at: focus,
49
- select: true,
50
- })
51
- } else {
52
- Transforms.insertNodes(operation.editor, span, {
53
- at: [focusBlockIndex, focusChildIndex + 1],
54
- select: true,
55
- })
56
- }
57
-
58
- // This makes sure the selection is set correctly when event handling is run
59
- // through Slate's Android input handling
60
- EDITOR_TO_PENDING_SELECTION.set(
61
- operation.editor,
62
- operation.editor.selection,
63
- )
64
-
65
- return
66
- }
67
-
68
- const inlineObject = parseInlineObject({
69
- inlineObject: operation.child,
70
- context,
71
- options: {validateFields: true},
72
- })
73
-
74
- if (inlineObject) {
75
- const {_key, _type, ...rest} = inlineObject
76
-
77
- const [focusSpan] = getFocusSpan({editor: operation.editor})
78
-
79
- if (focusSpan) {
80
- Transforms.insertNodes(
81
- operation.editor,
82
- {
83
- _key,
84
- _type,
85
- children: [
86
- {
87
- _key: VOID_CHILD_KEY,
88
- _type: 'span',
89
- text: '',
90
- marks: [],
91
- },
92
- ],
93
- value: rest,
94
- __inline: true,
95
- },
96
- {
97
- at: focus,
98
- select: true,
99
- },
100
- )
101
- } else {
102
- Transforms.insertNodes(
103
- operation.editor,
104
- {
105
- _key,
106
- _type,
107
- children: [
108
- {
109
- _key: VOID_CHILD_KEY,
110
- _type: 'span',
111
- text: '',
112
- marks: [],
113
- },
114
- ],
115
- value: rest,
116
- __inline: true,
117
- },
118
- {
119
- at: [focusBlockIndex, focusChildIndex + 1],
120
- select: true,
121
- },
122
- )
123
- }
124
-
125
- return
126
- }
127
-
128
- throw new Error('Unable to parse child')
129
- }
@@ -1,8 +0,0 @@
1
- import {Transforms} from 'slate'
2
- import type {BehaviorOperationImplementation} from './behavior.operations'
3
-
4
- export const insertTextOperationImplementation: BehaviorOperationImplementation<
5
- 'insert.text'
6
- > = ({operation}) => {
7
- Transforms.insertText(operation.editor, operation.text)
8
- }
@@ -1,12 +0,0 @@
1
- import {Transforms} from 'slate'
2
- import type {BehaviorOperationImplementation} from './behavior.operations'
3
-
4
- export const moveBackwardOperationImplementation: BehaviorOperationImplementation<
5
- 'move.backward'
6
- > = ({operation}) => {
7
- Transforms.move(operation.editor, {
8
- unit: 'character',
9
- distance: operation.distance,
10
- reverse: true,
11
- })
12
- }
@@ -1,44 +0,0 @@
1
- import {Transforms} from 'slate'
2
- import {getBlockKeyFromSelectionPoint} from '../utils/util.selection-point'
3
- import type {BehaviorOperationImplementation} from './behavior.operations'
4
-
5
- export const moveBlockOperationImplementation: BehaviorOperationImplementation<
6
- 'move.block'
7
- > = ({operation}) => {
8
- const originKey = getBlockKeyFromSelectionPoint({
9
- path: operation.at,
10
- offset: 0,
11
- })
12
-
13
- if (!originKey) {
14
- throw new Error('Failed to get block key from selection point')
15
- }
16
-
17
- const originBlockIndex = operation.editor.blockIndexMap.get(originKey)
18
-
19
- if (originBlockIndex === undefined) {
20
- throw new Error('Failed to get block index from block key')
21
- }
22
-
23
- const destinationKey = getBlockKeyFromSelectionPoint({
24
- path: operation.to,
25
- offset: 0,
26
- })
27
-
28
- if (!destinationKey) {
29
- throw new Error('Failed to get block key from selection point')
30
- }
31
-
32
- const destinationBlockIndex =
33
- operation.editor.blockIndexMap.get(destinationKey)
34
-
35
- if (destinationBlockIndex === undefined) {
36
- throw new Error('Failed to get block index from block key')
37
- }
38
-
39
- Transforms.moveNodes(operation.editor, {
40
- at: [originBlockIndex],
41
- to: [destinationBlockIndex],
42
- mode: 'highest',
43
- })
44
- }
@@ -1,11 +0,0 @@
1
- import {Transforms} from 'slate'
2
- import type {BehaviorOperationImplementation} from './behavior.operations'
3
-
4
- export const moveForwardOperationImplementation: BehaviorOperationImplementation<
5
- 'move.forward'
6
- > = ({operation}) => {
7
- Transforms.move(operation.editor, {
8
- unit: 'character',
9
- distance: operation.distance,
10
- })
11
- }