@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,644 +0,0 @@
1
- import {
2
- applyAll,
3
- type DiffMatchPatch,
4
- type InsertPatch,
5
- type Patch,
6
- type SetPatch,
7
- type UnsetPatch,
8
- } from '@portabletext/patches'
9
- import {
10
- cleanupEfficiency,
11
- DIFF_DELETE,
12
- DIFF_EQUAL,
13
- DIFF_INSERT,
14
- applyPatches as diffMatchPatchApplyPatches,
15
- makeDiff,
16
- parsePatch,
17
- } from '@sanity/diff-match-patch'
18
- import type {Path, PortableTextBlock, PortableTextChild} from '@sanity/types'
19
- import {Editor, Element, Node, Text, Transforms, type Descendant} from 'slate'
20
- import type {EditorContext} from '../editor/editor-snapshot'
21
- import {KEY_TO_SLATE_ELEMENT} from '../editor/weakMaps'
22
- import type {PortableTextSlateEditor} from '../types/slate-editor'
23
- import {isKeyedSegment} from '../utils/util.is-keyed-segment'
24
- import {isEqualToEmptyEditor, toSlateBlock} from './values'
25
-
26
- /**
27
- * Creates a function that can apply a patch onto a PortableTextSlateEditor.
28
- */
29
- export function createApplyPatch(
30
- context: Pick<EditorContext, 'schema' | 'keyGenerator'> & {
31
- initialValue: Array<PortableTextBlock> | undefined
32
- },
33
- ): (editor: PortableTextSlateEditor, patch: Patch) => boolean {
34
- return (editor: PortableTextSlateEditor, patch: Patch): boolean => {
35
- let changed = false
36
-
37
- try {
38
- switch (patch.type) {
39
- case 'insert':
40
- changed = insertPatch(context, editor, patch)
41
- break
42
- case 'unset':
43
- changed = unsetPatch(editor, patch)
44
- break
45
- case 'set':
46
- changed = setPatch(editor, patch)
47
- break
48
- case 'diffMatchPatch':
49
- changed = diffMatchPatch(editor, patch)
50
- break
51
- }
52
- } catch (err) {
53
- console.error(err)
54
- }
55
-
56
- return changed
57
- }
58
- }
59
-
60
- function diffMatchPatch(
61
- editor: Pick<
62
- PortableTextSlateEditor,
63
- 'children' | 'isTextBlock' | 'apply' | 'selection' | 'onChange'
64
- >,
65
- patch: DiffMatchPatch,
66
- ): boolean {
67
- const block = findBlock(editor.children, patch.path)
68
-
69
- if (!block) {
70
- return false
71
- }
72
-
73
- const child = findBlockChild(block, patch.path)
74
-
75
- if (!child) {
76
- return false
77
- }
78
-
79
- const isSpanTextDiffMatchPatch =
80
- block &&
81
- editor.isTextBlock(block.node) &&
82
- patch.path.length === 4 &&
83
- patch.path[1] === 'children' &&
84
- patch.path[3] === 'text'
85
-
86
- if (!isSpanTextDiffMatchPatch || !Text.isText(child.node)) {
87
- return false
88
- }
89
-
90
- const patches = parsePatch(patch.value)
91
- const [newValue] = diffMatchPatchApplyPatches(patches, child.node.text, {
92
- allowExceedingIndices: true,
93
- })
94
- const diff = cleanupEfficiency(makeDiff(child.node.text, newValue), 5)
95
-
96
- let offset = 0
97
- for (const [op, text] of diff) {
98
- if (op === DIFF_INSERT) {
99
- editor.apply({
100
- type: 'insert_text',
101
- path: [block.index, child.index],
102
- offset,
103
- text,
104
- })
105
- offset += text.length
106
- } else if (op === DIFF_DELETE) {
107
- editor.apply({
108
- type: 'remove_text',
109
- path: [block.index, child.index],
110
- offset: offset,
111
- text,
112
- })
113
- } else if (op === DIFF_EQUAL) {
114
- offset += text.length
115
- }
116
- }
117
-
118
- return true
119
- }
120
-
121
- function insertPatch(
122
- context: Pick<EditorContext, 'schema' | 'keyGenerator'> & {
123
- initialValue: Array<PortableTextBlock> | undefined
124
- },
125
- editor: PortableTextSlateEditor,
126
- patch: InsertPatch,
127
- ) {
128
- const block = findBlock(editor.children, patch.path)
129
-
130
- if (!block) {
131
- if (patch.path.length === 1 && patch.path[0] === 0) {
132
- const blocksToInsert = patch.items.map((item) =>
133
- toSlateBlock(
134
- item as PortableTextBlock,
135
- {schemaTypes: context.schema},
136
- KEY_TO_SLATE_ELEMENT.get(editor),
137
- ),
138
- )
139
-
140
- Transforms.insertNodes(editor, blocksToInsert, {
141
- at: [0],
142
- })
143
-
144
- return true
145
- }
146
-
147
- return false
148
- }
149
-
150
- if (patch.path.length > 1 && patch.path[1] !== 'children') {
151
- return false
152
- }
153
-
154
- // Insert blocks
155
- if (patch.path.length === 1) {
156
- const {items, position} = patch
157
- const blocksToInsert = items.map((item) =>
158
- toSlateBlock(
159
- item as PortableTextBlock,
160
- {schemaTypes: context.schema},
161
- KEY_TO_SLATE_ELEMENT.get(editor),
162
- ),
163
- )
164
- const targetBlockIndex = block.index
165
- const normalizedIdx =
166
- position === 'after' ? targetBlockIndex + 1 : targetBlockIndex
167
-
168
- const editorWasEmptyBefore = isEqualToEmptyEditor(
169
- context.initialValue,
170
- editor.value,
171
- context.schema,
172
- )
173
-
174
- Transforms.insertNodes(editor, blocksToInsert, {at: [normalizedIdx]})
175
-
176
- if (
177
- editorWasEmptyBefore &&
178
- typeof patch.path[0] === 'number' &&
179
- patch.path[0] === 0
180
- ) {
181
- Transforms.removeNodes(editor, {
182
- at: [
183
- position === 'before'
184
- ? targetBlockIndex + blocksToInsert.length
185
- : targetBlockIndex,
186
- ],
187
- })
188
- }
189
-
190
- return true
191
- }
192
-
193
- // Insert children
194
- const {items, position} = patch
195
-
196
- const targetChild = findBlockChild(block, patch.path)
197
-
198
- if (!targetChild) {
199
- return false
200
- }
201
-
202
- const childrenToInsert = toSlateBlock(
203
- {...block.node, children: items as PortableTextChild[]},
204
- {schemaTypes: context.schema},
205
- KEY_TO_SLATE_ELEMENT.get(editor),
206
- )
207
- const normalizedIdx =
208
- position === 'after' ? targetChild.index + 1 : targetChild.index
209
- const childInsertPath = [block.index, normalizedIdx]
210
-
211
- if (childrenToInsert && Element.isElement(childrenToInsert)) {
212
- Transforms.insertNodes(editor, childrenToInsert.children, {
213
- at: childInsertPath,
214
- })
215
- }
216
-
217
- return true
218
- }
219
-
220
- function setPatch(editor: PortableTextSlateEditor, patch: SetPatch) {
221
- let value = patch.value
222
- if (typeof patch.path[3] === 'string') {
223
- value = {}
224
- value[patch.path[3]] = patch.value
225
- }
226
-
227
- const block = findBlock(editor.children, patch.path)
228
-
229
- if (!block) {
230
- return false
231
- }
232
-
233
- const isTextBlock = editor.isTextBlock(block.node)
234
-
235
- if (patch.path.length === 1) {
236
- const updatedBlock = applyAll(block.node, [
237
- {
238
- ...patch,
239
- path: patch.path.slice(1),
240
- },
241
- ])
242
-
243
- if (editor.isTextBlock(block.node) && Element.isElement(updatedBlock)) {
244
- Transforms.setNodes(editor, updatedBlock, {at: [block.index]})
245
-
246
- const previousSelection = editor.selection
247
-
248
- // Remove the previous children
249
- for (const [_, childPath] of Editor.nodes(editor, {
250
- at: [block.index],
251
- reverse: true,
252
- mode: 'lowest',
253
- })) {
254
- Transforms.removeNodes(editor, {at: childPath})
255
- }
256
-
257
- // Insert the new children
258
- Transforms.insertNodes(editor, updatedBlock.children, {
259
- at: [block.index, 0],
260
- })
261
-
262
- // Restore the selection
263
- if (previousSelection) {
264
- // Update the selection on the editor object
265
- Transforms.setSelection(editor, previousSelection)
266
- // Actively select the previous selection
267
- Transforms.select(editor, previousSelection)
268
- }
269
-
270
- return true
271
- } else {
272
- Transforms.setNodes(editor, updatedBlock as Partial<Node>, {
273
- at: [block.index],
274
- })
275
-
276
- return true
277
- }
278
- }
279
-
280
- if (isTextBlock && patch.path[1] !== 'children') {
281
- const updatedBlock = applyAll(block.node, [
282
- {
283
- ...patch,
284
- path: patch.path.slice(1),
285
- },
286
- ])
287
-
288
- Transforms.setNodes(editor, updatedBlock as Partial<Node>, {
289
- at: [block.index],
290
- })
291
-
292
- return true
293
- }
294
-
295
- const child = findBlockChild(block, patch.path)
296
-
297
- // If this is targeting a text block child
298
- if (isTextBlock && child) {
299
- if (Text.isText(child.node)) {
300
- if (Text.isText(value)) {
301
- const oldText = child.node.text
302
- const newText = value.text
303
- if (oldText !== newText) {
304
- editor.apply({
305
- type: 'remove_text',
306
- path: [block.index, child.index],
307
- offset: 0,
308
- text: oldText,
309
- })
310
- editor.apply({
311
- type: 'insert_text',
312
- path: [block.index, child.index],
313
- offset: 0,
314
- text: newText,
315
- })
316
- // call OnChange here to emit the new selection
317
- // the user's selection might be interfering with
318
- editor.onChange()
319
- }
320
- } else {
321
- // Setting non-text span property
322
-
323
- const propPath = patch.path.slice(3)
324
- const propEntry = propPath.at(0)
325
- const reservedProps = ['_key', '_type', 'text']
326
-
327
- if (propEntry === undefined) {
328
- return false
329
- }
330
-
331
- if (
332
- typeof propEntry === 'string' &&
333
- reservedProps.includes(propEntry)
334
- ) {
335
- return false
336
- }
337
-
338
- const newNode = applyAll(child.node, [
339
- {
340
- ...patch,
341
- path: propPath,
342
- },
343
- ])
344
-
345
- Transforms.setNodes(editor, newNode, {at: [block.index, child.index]})
346
- }
347
- } else {
348
- // Setting inline object property
349
-
350
- const propPath = patch.path.slice(3)
351
- const reservedProps = ['_key', '_type', 'children', '__inline']
352
- const propEntry = propPath.at(0)
353
-
354
- if (propEntry === undefined) {
355
- return false
356
- }
357
-
358
- if (typeof propEntry === 'string' && reservedProps.includes(propEntry)) {
359
- return false
360
- }
361
-
362
- // If the child is an inline object, we need to apply the patch to the
363
- // `value` property object.
364
- const value =
365
- 'value' in child.node && typeof child.node.value === 'object'
366
- ? child.node.value
367
- : {}
368
-
369
- const newValue = applyAll(value, [
370
- {
371
- ...patch,
372
- path: patch.path.slice(3),
373
- },
374
- ])
375
-
376
- Transforms.setNodes(
377
- editor,
378
- {...child.node, value: newValue},
379
- {at: [block.index, child.index]},
380
- )
381
- }
382
-
383
- return true
384
- } else if (Element.isElement(block.node) && patch.path.length === 1) {
385
- const {children, ...nextRest} = value as unknown as PortableTextBlock
386
- const {children: _prevChildren, ...prevRest} = block.node || {
387
- children: undefined,
388
- }
389
-
390
- // Set any block properties
391
- editor.apply({
392
- type: 'set_node',
393
- path: [block.index],
394
- properties: {...prevRest},
395
- newProperties: nextRest,
396
- })
397
-
398
- // Replace the children in the block
399
- // Note that children must be explicitly inserted, and can't be set with set_node
400
- const blockNode = block.node
401
-
402
- blockNode.children.forEach((child, childIndex) => {
403
- editor.apply({
404
- type: 'remove_node',
405
- path: [block.index, blockNode.children.length - 1 - childIndex],
406
- node: child,
407
- })
408
- })
409
-
410
- if (Array.isArray(children)) {
411
- children.forEach((child, childIndex) => {
412
- editor.apply({
413
- type: 'insert_node',
414
- path: [block.index, childIndex],
415
- node: child,
416
- })
417
- })
418
- }
419
- } else if (block && 'value' in block.node) {
420
- if (patch.path.length > 1 && patch.path[1] !== 'children') {
421
- const newVal = applyAll(block.node.value, [
422
- {
423
- ...patch,
424
- path: patch.path.slice(1),
425
- },
426
- ])
427
-
428
- Transforms.setNodes(
429
- editor,
430
- {...block.node, value: newVal},
431
- {at: [block.index]},
432
- )
433
- } else {
434
- return false
435
- }
436
- }
437
-
438
- return true
439
- }
440
-
441
- function unsetPatch(editor: PortableTextSlateEditor, patch: UnsetPatch) {
442
- // Value
443
- if (patch.path.length === 0) {
444
- Transforms.deselect(editor)
445
-
446
- const children = Node.children(editor, [], {
447
- reverse: true,
448
- })
449
-
450
- for (const [_, path] of children) {
451
- Transforms.removeNodes(editor, {at: path})
452
- }
453
-
454
- return true
455
- }
456
-
457
- const block = findBlock(editor.children, patch.path)
458
-
459
- if (!block) {
460
- return false
461
- }
462
-
463
- // Single blocks
464
- if (patch.path.length === 1) {
465
- Transforms.removeNodes(editor, {at: [block.index]})
466
-
467
- return true
468
- }
469
-
470
- const child = findBlockChild(block, patch.path)
471
-
472
- // Unset on text block children
473
- if (editor.isTextBlock(block.node) && child) {
474
- if (patch.path[1] === 'children' && patch.path.length === 3) {
475
- Transforms.removeNodes(editor, {at: [block.index, child.index]})
476
-
477
- return true
478
- }
479
- }
480
-
481
- if (child && !Text.isText(child.node)) {
482
- // Unsetting inline object property
483
-
484
- const propPath = patch.path.slice(3)
485
- const propEntry = propPath.at(0)
486
- const reservedProps = ['_key', '_type', 'children', '__inline']
487
-
488
- if (propEntry === undefined) {
489
- return false
490
- }
491
-
492
- if (typeof propEntry === 'string' && reservedProps.includes(propEntry)) {
493
- // All custom properties are stored on the `value` property object.
494
- // If you try to unset any of the other top-level properties it's a
495
- // no-op.
496
- return false
497
- }
498
-
499
- const value =
500
- 'value' in child.node && typeof child.node.value === 'object'
501
- ? child.node.value
502
- : {}
503
-
504
- const newValue = applyAll(value, [
505
- {
506
- ...patch,
507
- path: patch.path.slice(3),
508
- },
509
- ])
510
-
511
- Transforms.setNodes(
512
- editor,
513
- {...child.node, value: newValue},
514
- {at: [block.index, child.index]},
515
- )
516
-
517
- return true
518
- }
519
-
520
- if (child && Text.isText(child.node)) {
521
- const propPath = patch.path.slice(3)
522
- const propEntry = propPath.at(0)
523
- const reservedProps = ['_key', '_type']
524
-
525
- if (propEntry === undefined) {
526
- return false
527
- }
528
-
529
- if (typeof propEntry === 'string' && reservedProps.includes(propEntry)) {
530
- return false
531
- }
532
-
533
- if (typeof propEntry === 'string' && propEntry === 'text') {
534
- editor.apply({
535
- type: 'remove_text',
536
- path: [block.index, child.index],
537
- offset: 0,
538
- text: child.node.text,
539
- })
540
-
541
- return true
542
- }
543
-
544
- const newNode = applyAll(child.node, [
545
- {
546
- ...patch,
547
- path: propPath,
548
- },
549
- ])
550
- const newKeys = Object.keys(newNode)
551
-
552
- const removedProperties = Object.keys(child.node).filter(
553
- (property) => !newKeys.includes(property),
554
- )
555
-
556
- Transforms.unsetNodes(editor, removedProperties, {
557
- at: [block.index, child.index],
558
- })
559
-
560
- return true
561
- }
562
-
563
- if (!child) {
564
- if ('value' in block.node) {
565
- const newVal = applyAll(block.node.value, [
566
- {
567
- ...patch,
568
- path: patch.path.slice(1),
569
- },
570
- ])
571
-
572
- Transforms.setNodes(
573
- editor,
574
- {...block.node, value: newVal},
575
- {at: [block.index]},
576
- )
577
-
578
- return true
579
- }
580
-
581
- return false
582
- }
583
-
584
- return false
585
- }
586
-
587
- function findBlock(
588
- children: Descendant[],
589
- path: Path,
590
- ): {node: Descendant; index: number} | undefined {
591
- let blockIndex = -1
592
-
593
- const block = children.find((node: Descendant, index: number) => {
594
- const isMatch = isKeyedSegment(path[0])
595
- ? node._key === path[0]._key
596
- : index === path[0]
597
-
598
- if (isMatch) {
599
- blockIndex = index
600
- }
601
-
602
- return isMatch
603
- })
604
-
605
- if (!block) {
606
- return undefined
607
- }
608
-
609
- return {node: block, index: blockIndex}
610
- }
611
-
612
- function findBlockChild(
613
- block: {node: Descendant; index: number},
614
- path: Path,
615
- ): {node: Descendant; index: number} | undefined {
616
- const blockNode = block.node
617
-
618
- if (!Element.isElement(blockNode) || path[1] !== 'children') {
619
- return undefined
620
- }
621
-
622
- let childIndex = -1
623
-
624
- const child = blockNode.children.find((node, index: number) => {
625
- const isMatch = isKeyedSegment(path[2])
626
- ? node._key === path[2]._key
627
- : index === path[2]
628
-
629
- if (isMatch) {
630
- childIndex = index
631
- }
632
-
633
- return isMatch
634
- })
635
-
636
- if (!child) {
637
- return undefined
638
- }
639
-
640
- return {
641
- node: child,
642
- index: childIndex,
643
- }
644
- }
@@ -1,9 +0,0 @@
1
- import type {PortableTextBlock} from '@sanity/types'
2
-
3
- export function getBlockKeys(value: Array<PortableTextBlock> | undefined) {
4
- if (!value) {
5
- return []
6
- }
7
-
8
- return value.map((block) => block._key)
9
- }