@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,282 +0,0 @@
1
- import {compileSchema, defineSchema} from '@portabletext/schema'
2
- import {createTestKeyGenerator} from '@portabletext/test'
3
- import {describe, expect, test} from 'vitest'
4
- import {toSlateBlock, VOID_CHILD_KEY} from './values'
5
-
6
- describe(toSlateBlock.name, () => {
7
- describe('text block', () => {
8
- describe('with span', () => {
9
- test('with _type', () => {
10
- const keyGenerator = createTestKeyGenerator()
11
- const blockKey = keyGenerator()
12
- const spanKey = keyGenerator()
13
-
14
- expect(
15
- toSlateBlock(
16
- {
17
- _type: 'block',
18
- _key: blockKey,
19
- children: [{_key: spanKey, _type: 'span', text: 'foo'}],
20
- },
21
- {
22
- schemaTypes: compileSchema(defineSchema({})),
23
- },
24
- ),
25
- ).toEqual({
26
- _type: 'block',
27
- _key: blockKey,
28
- children: [
29
- {
30
- _key: spanKey,
31
- _type: 'span',
32
- text: 'foo',
33
- },
34
- ],
35
- })
36
- })
37
-
38
- test('without _type', () => {
39
- const keyGenerator = createTestKeyGenerator()
40
- const blockKey = keyGenerator()
41
- const spanKey = keyGenerator()
42
-
43
- expect(
44
- toSlateBlock(
45
- {
46
- _type: 'block',
47
- _key: blockKey,
48
- children: [{_key: spanKey, text: 'foo'}],
49
- },
50
- {
51
- schemaTypes: compileSchema(defineSchema({})),
52
- },
53
- ),
54
- ).toEqual({
55
- _type: 'block',
56
- _key: blockKey,
57
- children: [
58
- {
59
- _key: spanKey,
60
- _type: 'span',
61
- text: 'foo',
62
- },
63
- ],
64
- })
65
- })
66
-
67
- test('wrong _type', () => {
68
- const keyGenerator = createTestKeyGenerator()
69
- const blockKey = keyGenerator()
70
- const spanKey = keyGenerator()
71
-
72
- expect(
73
- toSlateBlock(
74
- {
75
- _type: 'block',
76
- _key: blockKey,
77
- children: [{_key: spanKey, _type: 'stock-ticker', text: 'foo'}],
78
- },
79
- {
80
- schemaTypes: compileSchema(defineSchema({})),
81
- },
82
- ),
83
- ).toEqual({
84
- _type: 'block',
85
- _key: blockKey,
86
- children: [
87
- {
88
- _key: spanKey,
89
- _type: 'stock-ticker',
90
- children: [
91
- {
92
- _key: VOID_CHILD_KEY,
93
- _type: 'span',
94
- text: '',
95
- marks: [],
96
- },
97
- ],
98
- value: {text: 'foo'},
99
- __inline: true,
100
- },
101
- ],
102
- })
103
- })
104
- })
105
-
106
- describe('with inline object', () => {
107
- test('known inline object _type', () => {
108
- const keyGenerator = createTestKeyGenerator()
109
- const blockKey = keyGenerator()
110
- const stockTickerKey = keyGenerator()
111
-
112
- expect(
113
- toSlateBlock(
114
- {
115
- _type: 'block',
116
- _key: blockKey,
117
- children: [
118
- {_type: 'stock-ticker', _key: stockTickerKey, symbol: 'AAPL'},
119
- ],
120
- },
121
- {
122
- schemaTypes: compileSchema(
123
- defineSchema({inlineObjects: [{name: 'stock-ticker'}]}),
124
- ),
125
- },
126
- ),
127
- ).toEqual({
128
- _type: 'block',
129
- _key: blockKey,
130
- children: [
131
- {
132
- __inline: true,
133
- _key: stockTickerKey,
134
- _type: 'stock-ticker',
135
- children: [
136
- {
137
- _key: VOID_CHILD_KEY,
138
- _type: 'span',
139
- text: '',
140
- marks: [],
141
- },
142
- ],
143
- value: {
144
- symbol: 'AAPL',
145
- },
146
- },
147
- ],
148
- })
149
- })
150
-
151
- test('unknown inline object _type', () => {
152
- const keyGenerator = createTestKeyGenerator()
153
- const blockKey = keyGenerator()
154
- const stockTickerKey = keyGenerator()
155
-
156
- expect(
157
- toSlateBlock(
158
- {
159
- _type: 'block',
160
- _key: blockKey,
161
- children: [
162
- {_type: 'stock-ticker', _key: stockTickerKey, symbol: 'AAPL'},
163
- ],
164
- },
165
- {
166
- schemaTypes: compileSchema(defineSchema({})),
167
- },
168
- ),
169
- ).toEqual({
170
- _type: 'block',
171
- _key: blockKey,
172
- children: [
173
- {
174
- __inline: true,
175
- _key: stockTickerKey,
176
- _type: 'stock-ticker',
177
- children: [
178
- {
179
- _key: VOID_CHILD_KEY,
180
- _type: 'span',
181
- text: '',
182
- marks: [],
183
- },
184
- ],
185
- value: {
186
- symbol: 'AAPL',
187
- },
188
- },
189
- ],
190
- })
191
- })
192
- })
193
-
194
- describe('with inline object with text prop', () => {
195
- test('known inline object _type', () => {
196
- const keyGenerator = createTestKeyGenerator()
197
- const blockKey = keyGenerator()
198
- const stockTickerKey = keyGenerator()
199
-
200
- expect(
201
- toSlateBlock(
202
- {
203
- _type: 'block',
204
- _key: blockKey,
205
- children: [
206
- {_type: 'stock-ticker', _key: stockTickerKey, text: 'foo'},
207
- ],
208
- },
209
- {
210
- schemaTypes: compileSchema(
211
- defineSchema({inlineObjects: [{name: 'stock-ticker'}]}),
212
- ),
213
- },
214
- ),
215
- ).toEqual({
216
- _type: 'block',
217
- _key: blockKey,
218
- children: [
219
- {
220
- __inline: true,
221
- _key: stockTickerKey,
222
- _type: 'stock-ticker',
223
- children: [
224
- {
225
- _key: VOID_CHILD_KEY,
226
- _type: 'span',
227
- text: '',
228
- marks: [],
229
- },
230
- ],
231
- value: {
232
- text: 'foo',
233
- },
234
- },
235
- ],
236
- })
237
- })
238
-
239
- test('unknown inline object _type', () => {
240
- const keyGenerator = createTestKeyGenerator()
241
- const blockKey = keyGenerator()
242
- const stockTickerKey = keyGenerator()
243
-
244
- expect(
245
- toSlateBlock(
246
- {
247
- _type: 'block',
248
- _key: blockKey,
249
- children: [
250
- {_type: 'stock-ticker', _key: stockTickerKey, text: 'foo'},
251
- ],
252
- },
253
- {
254
- schemaTypes: compileSchema(defineSchema({})),
255
- },
256
- ),
257
- ).toEqual({
258
- _type: 'block',
259
- _key: blockKey,
260
- children: [
261
- {
262
- __inline: true,
263
- _key: stockTickerKey,
264
- _type: 'stock-ticker',
265
- children: [
266
- {
267
- _key: VOID_CHILD_KEY,
268
- _type: 'span',
269
- text: '',
270
- marks: [],
271
- },
272
- ],
273
- value: {
274
- text: 'foo',
275
- },
276
- },
277
- ],
278
- })
279
- })
280
- })
281
- })
282
- })
@@ -1,266 +0,0 @@
1
- import type {
2
- PortableTextBlock,
3
- PortableTextChild,
4
- PortableTextObject,
5
- PortableTextTextBlock,
6
- } from '@sanity/types'
7
- import {isEqual} from 'lodash'
8
- import {Element, Text, type Descendant} from 'slate'
9
- import type {EditorSchema} from '../editor/editor-schema'
10
-
11
- export const EMPTY_MARKDEFS: PortableTextObject[] = []
12
-
13
- export const VOID_CHILD_KEY = 'void-child'
14
-
15
- function keepObjectEquality(
16
- object: PortableTextBlock | PortableTextChild,
17
- keyMap: Record<string, PortableTextBlock | PortableTextChild>,
18
- ) {
19
- const value = keyMap[object._key]
20
- if (value && isEqual(object, value)) {
21
- return value
22
- }
23
- keyMap[object._key] = object
24
- return object
25
- }
26
-
27
- export function toSlateBlock(
28
- block: PortableTextBlock,
29
- {schemaTypes}: {schemaTypes: EditorSchema},
30
- keyMap: Record<string, any> = {},
31
- ): Descendant {
32
- const {_type, _key, ...rest} = block
33
- const isPortableText = block && block._type === schemaTypes.block.name
34
-
35
- if (isPortableText) {
36
- const textBlock = block as PortableTextTextBlock
37
- let hasInlines = false
38
- const hasMissingMarkDefs = typeof textBlock.markDefs === 'undefined'
39
- const hasMissingChildren = typeof textBlock.children === 'undefined'
40
-
41
- const children = (textBlock.children || []).map((child) => {
42
- const {_type: childType, _key: childKey, ...childProps} = child
43
- const propKeys = Object.keys(childProps)
44
-
45
- if (childType === undefined) {
46
- if (propKeys.length === 1 && propKeys.at(0) === 'text') {
47
- return {
48
- _key: childKey,
49
- _type: schemaTypes.span.name,
50
- text: childProps.text,
51
- }
52
- }
53
- }
54
-
55
- if (childType !== schemaTypes.span.name) {
56
- // Return 'slate' version of inline object where the actual
57
- // value is stored in the `value` property.
58
- // In slate, inline objects are represented as regular
59
- // children with actual text node in order to be able to
60
- // be selected the same way as the rest of the (text) content.
61
- hasInlines = true
62
-
63
- return keepObjectEquality(
64
- {
65
- _type: childType,
66
- _key: childKey,
67
- children: [
68
- {
69
- _key: VOID_CHILD_KEY,
70
- _type: schemaTypes.span.name,
71
- text: '',
72
- marks: [],
73
- },
74
- ],
75
- value: childProps,
76
- __inline: true,
77
- },
78
- keyMap,
79
- )
80
- }
81
-
82
- // Original child object (span)
83
- return child
84
- })
85
-
86
- // Return original block
87
- if (
88
- !hasMissingMarkDefs &&
89
- !hasMissingChildren &&
90
- !hasInlines &&
91
- Element.isElement(block)
92
- ) {
93
- // Original object
94
- return block
95
- }
96
-
97
- return keepObjectEquality(
98
- {_type, _key, ...rest, children},
99
- keyMap,
100
- ) as Descendant
101
- }
102
-
103
- return keepObjectEquality(
104
- {
105
- _type,
106
- _key,
107
- children: [
108
- {
109
- _key: VOID_CHILD_KEY,
110
- _type: 'span',
111
- text: '',
112
- marks: [],
113
- },
114
- ],
115
- value: rest,
116
- },
117
- keyMap,
118
- ) as Descendant
119
- }
120
-
121
- export function fromSlateBlock(
122
- block: Descendant,
123
- textBlockType: string,
124
- keyMap: Record<string, PortableTextBlock | PortableTextChild> = {},
125
- ) {
126
- const {_key, _type} = block
127
- if (!_key || !_type) {
128
- throw new Error('Not a valid block')
129
- }
130
- if (
131
- _type === textBlockType &&
132
- 'children' in block &&
133
- Array.isArray(block.children) &&
134
- _key
135
- ) {
136
- let hasInlines = false
137
- const children = block.children.map((child) => {
138
- const {_type: _cType} = child
139
- if ('value' in child && _cType !== 'span') {
140
- hasInlines = true
141
- const {
142
- value: v,
143
- _key: k,
144
- _type: t,
145
- __inline: _i,
146
- children: _c,
147
- ...rest
148
- } = child
149
- return keepObjectEquality(
150
- {...rest, ...v, _key: k as string, _type: t as string},
151
- keyMap,
152
- )
153
- }
154
- return child
155
- })
156
- if (!hasInlines) {
157
- return block as PortableTextBlock // Original object
158
- }
159
- return keepObjectEquality(
160
- {...block, children, _key, _type},
161
- keyMap,
162
- ) as PortableTextBlock
163
- }
164
- const blockValue = 'value' in block && block.value
165
- return keepObjectEquality(
166
- {_key, _type, ...(typeof blockValue === 'object' ? blockValue : {})},
167
- keyMap,
168
- ) as PortableTextBlock
169
- }
170
-
171
- export function isEqualToEmptyEditor(
172
- initialValue: Array<PortableTextBlock> | undefined,
173
- blocks: Array<Descendant> | Array<PortableTextBlock>,
174
- schemaTypes: EditorSchema,
175
- ): boolean {
176
- if (!blocks) {
177
- return false
178
- }
179
-
180
- // Must have exactly one block
181
- if (blocks.length !== 1) {
182
- return false
183
- }
184
-
185
- const firstBlock = blocks.at(0)
186
-
187
- if (!firstBlock) {
188
- return true
189
- }
190
-
191
- if (!Element.isElement(firstBlock)) {
192
- return false
193
- }
194
-
195
- // Must be a text block
196
- if (firstBlock._type !== schemaTypes.block.name) {
197
- return false
198
- }
199
-
200
- // Must not be a list item
201
- if ('listItem' in firstBlock) {
202
- return false
203
- }
204
-
205
- // Style must exist and be the default style
206
- if (
207
- !('style' in firstBlock) ||
208
- firstBlock.style !== schemaTypes.styles.at(0)?.name
209
- ) {
210
- return false
211
- }
212
-
213
- // Must have children array
214
- if (!Array.isArray(firstBlock.children)) {
215
- return false
216
- }
217
-
218
- // Must have exactly one child
219
- if (firstBlock.children.length !== 1) {
220
- return false
221
- }
222
-
223
- const firstChild = firstBlock.children.at(0)
224
-
225
- if (!firstChild) {
226
- return false
227
- }
228
-
229
- if (!Text.isText(firstChild)) {
230
- return false
231
- }
232
-
233
- // Must be a span type
234
- if (!('_type' in firstChild) || firstChild._type !== schemaTypes.span.name) {
235
- return false
236
- }
237
-
238
- // Must have empty text
239
- if (firstChild.text !== '') {
240
- return false
241
- }
242
-
243
- // Must have no marks (marks can be undefined or empty array)
244
- if (firstChild.marks?.join('')) {
245
- return false
246
- }
247
-
248
- if (
249
- Object.keys(firstBlock).some(
250
- (key) =>
251
- key !== '_type' &&
252
- key !== '_key' &&
253
- key !== 'children' &&
254
- key !== 'markDefs' &&
255
- key !== 'style',
256
- )
257
- ) {
258
- return false
259
- }
260
-
261
- if (isEqual(initialValue, [firstBlock])) {
262
- return false
263
- }
264
-
265
- return true
266
- }
@@ -1,146 +0,0 @@
1
- import {
2
- bold,
3
- code,
4
- createKeyboardShortcut,
5
- italic,
6
- redo,
7
- underline,
8
- undo,
9
- } from '@portabletext/keyboard-shortcuts'
10
-
11
- export const defaultKeyboardShortcuts = {
12
- arrowDown: createKeyboardShortcut({
13
- default: [
14
- {
15
- key: 'ArrowDown',
16
- alt: false,
17
- ctrl: false,
18
- meta: false,
19
- shift: false,
20
- },
21
- ],
22
- }),
23
- arrowUp: createKeyboardShortcut({
24
- default: [
25
- {
26
- key: 'ArrowUp',
27
- alt: false,
28
- ctrl: false,
29
- meta: false,
30
- shift: false,
31
- },
32
- ],
33
- }),
34
- backspace: createKeyboardShortcut({
35
- default: [
36
- {
37
- key: 'Backspace',
38
- alt: false,
39
- ctrl: false,
40
- meta: false,
41
- shift: false,
42
- },
43
- ],
44
- }),
45
- break: createKeyboardShortcut({
46
- default: [
47
- {
48
- key: 'Enter',
49
- shift: false,
50
- },
51
- ],
52
- }),
53
- lineBreak: createKeyboardShortcut({
54
- default: [
55
- {
56
- key: 'Enter',
57
- shift: true,
58
- },
59
- ],
60
- }),
61
- decorators: {
62
- strong: bold,
63
- em: italic,
64
- underline: underline,
65
- code: code,
66
- },
67
- delete: createKeyboardShortcut({
68
- default: [
69
- {
70
- key: 'Delete',
71
- alt: false,
72
- ctrl: false,
73
- meta: false,
74
- shift: false,
75
- },
76
- ],
77
- }),
78
- deleteWord: {
79
- backward: createKeyboardShortcut({
80
- default: [
81
- {
82
- key: 'Backspace',
83
- alt: false,
84
- ctrl: true,
85
- meta: false,
86
- // shift is optional
87
- },
88
- ],
89
- apple: [
90
- {
91
- key: 'Backspace',
92
- alt: true,
93
- ctrl: false,
94
- meta: false,
95
- // shift is optional
96
- },
97
- ],
98
- }),
99
- forward: createKeyboardShortcut({
100
- default: [
101
- {
102
- key: 'Delete',
103
- alt: false,
104
- ctrl: true,
105
- meta: false,
106
- // shift is optional
107
- },
108
- ],
109
- apple: [
110
- {
111
- key: 'Delete',
112
- alt: true,
113
- ctrl: false,
114
- meta: false,
115
- // shift is optional
116
- },
117
- ],
118
- }),
119
- },
120
- history: {
121
- undo,
122
- redo,
123
- },
124
- tab: createKeyboardShortcut({
125
- default: [
126
- {
127
- key: 'Tab',
128
- alt: false,
129
- ctrl: false,
130
- meta: false,
131
- shift: false,
132
- },
133
- ],
134
- }),
135
- shiftTab: createKeyboardShortcut({
136
- default: [
137
- {
138
- key: 'Tab',
139
- alt: false,
140
- ctrl: false,
141
- meta: false,
142
- shift: true,
143
- },
144
- ],
145
- }),
146
- }