@blocknote/core 0.54.2 → 0.56.0

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 (491) hide show
  1. package/dist/BlockNoteExtension-DjEkXkR5.cjs +2 -0
  2. package/dist/BlockNoteExtension-DjEkXkR5.cjs.map +1 -0
  3. package/dist/{BlockNoteExtension-CbvDA0F7.js → BlockNoteExtension-TsTGtIeX.js} +34 -27
  4. package/dist/BlockNoteExtension-TsTGtIeX.js.map +1 -0
  5. package/dist/{UserStore-mj3fXW4D.cjs → UserStore-CTVzVGh-.cjs} +2 -2
  6. package/dist/{UserStore-mj3fXW4D.cjs.map → UserStore-CTVzVGh-.cjs.map} +1 -1
  7. package/dist/{UserStore-C4TzkGEn.js → UserStore-OZJP3K8T.js} +2 -2
  8. package/dist/{UserStore-C4TzkGEn.js.map → UserStore-OZJP3K8T.js.map} +1 -1
  9. package/dist/blocknote.cjs +1 -1
  10. package/dist/blocknote.js +6 -6
  11. package/dist/blocks-COVwe5Cf.cjs +12 -0
  12. package/dist/blocks-COVwe5Cf.cjs.map +1 -0
  13. package/dist/{blocks-Dol85hpH.js → blocks-COYcDVg7.js} +1577 -1155
  14. package/dist/blocks-COYcDVg7.js.map +1 -0
  15. package/dist/blocks.cjs +1 -1
  16. package/dist/blocks.js +2 -2
  17. package/dist/comments.cjs +1 -1
  18. package/dist/comments.cjs.map +1 -1
  19. package/dist/comments.js +69 -43
  20. package/dist/comments.js.map +1 -1
  21. package/dist/{en-uzHzj6eH.js → en-Ch-vvt5e.js} +40 -3
  22. package/dist/en-Ch-vvt5e.js.map +1 -0
  23. package/dist/en-N5lu0-j8.cjs +2 -0
  24. package/dist/en-N5lu0-j8.cjs.map +1 -0
  25. package/dist/extensions-BbM81hHa.js +3069 -0
  26. package/dist/extensions-BbM81hHa.js.map +1 -0
  27. package/dist/extensions-D_WkAfSt.cjs +58 -0
  28. package/dist/extensions-D_WkAfSt.cjs.map +1 -0
  29. package/dist/extensions.cjs +1 -1
  30. package/dist/extensions.js +4 -4
  31. package/dist/locales.cjs +1 -1
  32. package/dist/locales.cjs.map +1 -1
  33. package/dist/locales.js +859 -45
  34. package/dist/locales.js.map +1 -1
  35. package/dist/src-4OQT3z5S.cjs +23 -0
  36. package/dist/src-4OQT3z5S.cjs.map +1 -0
  37. package/dist/src-BmLsLZfE.js +3491 -0
  38. package/dist/src-BmLsLZfE.js.map +1 -0
  39. package/dist/style.css +1 -1
  40. package/dist/webpack-stats.json +1 -1
  41. package/dist/y.cjs +1 -1
  42. package/dist/y.cjs.map +1 -1
  43. package/dist/y.js +1284 -554
  44. package/dist/y.js.map +1 -1
  45. package/dist/yjs.cjs +1 -1
  46. package/dist/yjs.cjs.map +1 -1
  47. package/dist/yjs.js +189 -148
  48. package/dist/yjs.js.map +1 -1
  49. package/package.json +18 -18
  50. package/src/api/blockManipulation/commands/insertBlocks/insertBlocks.ts +45 -6
  51. package/src/api/blockManipulation/commands/insertBlocks/insertPlacement.test.ts +217 -0
  52. package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts +113 -9
  53. package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts +151 -163
  54. package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.test.ts +47 -31
  55. package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.ts +122 -181
  56. package/src/api/blockManipulation/commands/nestBlock/nestBlock.test.ts +256 -0
  57. package/src/api/blockManipulation/commands/nestBlock/nestBlock.ts +80 -40
  58. package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.test.ts +71 -0
  59. package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.ts +39 -31
  60. package/src/api/blockManipulation/commands/splitBlock/splitBlock.test.ts +221 -8
  61. package/src/api/blockManipulation/commands/splitBlock/splitBlock.ts +40 -8
  62. package/src/api/blockManipulation/commands/updateBlock/updateBlock.test.ts +272 -40
  63. package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +138 -117
  64. package/src/api/blockManipulation/getBlock/getBlock.ts +4 -11
  65. package/src/api/blockManipulation/layoutBlocks/fixLayoutBlock.ts +173 -0
  66. package/src/api/blockManipulation/layoutBlocks/layoutBlocks.browser.test.ts +293 -0
  67. package/src/api/blockManipulation/layoutBlocks/layoutBlocks.fixture.ts +145 -0
  68. package/src/api/blockManipulation/layoutBlocks/layoutBlocks.test.ts +783 -0
  69. package/src/api/blockManipulation/layoutBlocks/plainBlocks.test.ts +180 -0
  70. package/src/api/blockManipulation/layoutBlocks/titledBlocks.test.ts +365 -0
  71. package/src/api/blockManipulation/selections/selection.ts +20 -53
  72. package/src/api/blockManipulation/selections/textCursorPosition.ts +17 -72
  73. package/src/api/blockManipulation/setupTestEnv.ts +38 -6
  74. package/src/api/clipboard/fromClipboard/handleFileInsertion.ts +4 -4
  75. package/src/api/clipboard/toClipboard/copyExtension.ts +3 -3
  76. package/src/api/combineTransactionSteps.ts +22 -0
  77. package/src/api/exporters/html/externalHTMLExporter.ts +1 -1
  78. package/src/api/exporters/html/internalHTMLSerializer.ts +4 -4
  79. package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +111 -36
  80. package/src/api/exporters/html/util/serializeBlocksInternalHTML.ts +51 -21
  81. package/src/api/exporters/markdown/htmlToMarkdown.ts +12 -1
  82. package/src/api/exporters/markdown/markdownExporter.ts +1 -1
  83. package/src/api/getBlockInfoFromPos.test.ts +428 -3
  84. package/src/api/getBlockInfoFromPos.ts +531 -129
  85. package/src/api/getBlocksChangedByTransaction.test.ts +7 -7
  86. package/src/api/getBlocksChangedByTransaction.ts +3 -3
  87. package/src/api/{getChangedRange.ts → getChangedRangeWithAttrs.ts} +2 -2
  88. package/src/api/getChangedRanges.test.ts +391 -0
  89. package/src/api/getChangedRanges.ts +368 -0
  90. package/src/api/nodeConversions/blockToNode.ts +44 -16
  91. package/src/api/nodeConversions/fragmentToBlocks.ts +29 -44
  92. package/src/api/nodeConversions/nodeToBlock.ts +120 -68
  93. package/src/api/nodeUtil.ts +1 -1
  94. package/src/api/parsers/markdown/autolink.ts +23 -0
  95. package/src/api/parsers/markdown/markdownToHtml.ts +115 -11
  96. package/src/blocks/Code/block.test.ts +4 -4
  97. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +6 -1
  98. package/src/blocks/Heading/block.ts +54 -15
  99. package/src/blocks/ListItem/BulletListItem/block.ts +6 -4
  100. package/src/blocks/ListItem/CheckListItem/block.test.ts +2 -2
  101. package/src/blocks/ListItem/CheckListItem/block.ts +6 -4
  102. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +14 -17
  103. package/src/blocks/ListItem/NumberedListItem/block.ts +6 -4
  104. package/src/blocks/ListItem/ToggleListItem/block.ts +23 -12
  105. package/src/blocks/Table/block.ts +2 -10
  106. package/src/blocks/ToggleWrapper/createToggleFrame.ts +126 -0
  107. package/src/blocks/ToggleWrapper/toggleBlocks.browser.test.ts +1056 -0
  108. package/src/blocks/defaultBlockTypeItems.test.ts +70 -0
  109. package/src/blocks/defaultBlockTypeItems.ts +99 -0
  110. package/src/blocks/defaultBlocks.test.ts +67 -0
  111. package/src/blocks/defaultBlocks.ts +7 -1
  112. package/src/blocks/index.ts +2 -1
  113. package/src/comments/CommentEditorSubmitExtension.test.ts +141 -0
  114. package/src/comments/CommentEditorSubmitExtension.ts +56 -0
  115. package/src/comments/extension.ts +8 -0
  116. package/src/comments/index.ts +1 -0
  117. package/src/editor/Block.css +286 -154
  118. package/src/editor/BlockNoteEditor.node.test.ts +35 -0
  119. package/src/editor/BlockNoteEditor.test.ts +2 -2
  120. package/src/editor/BlockNoteEditor.ts +165 -50
  121. package/src/editor/BlockNoteExtension.ts +11 -6
  122. package/src/editor/blockColors.browser.test.ts +127 -0
  123. package/src/editor/editor.css +60 -0
  124. package/src/editor/managers/BlockManager.ts +4 -3
  125. package/src/editor/managers/EventManager.browser.test.ts +334 -0
  126. package/src/editor/managers/EventManager.ts +128 -31
  127. package/src/editor/managers/ExtensionManager/ExtensionManager.browser.test.ts +45 -0
  128. package/src/editor/managers/ExtensionManager/ExtensionManager.node.test.ts +209 -0
  129. package/src/editor/managers/ExtensionManager/ExtensionManager.test.ts +88 -1
  130. package/src/editor/managers/ExtensionManager/extensions.ts +10 -1
  131. package/src/editor/managers/ExtensionManager/index.ts +165 -75
  132. package/src/editor/managers/StateManager.ts +14 -4
  133. package/src/editor/managers/StyleManager.browser.test.ts +122 -0
  134. package/src/editor/managers/StyleManager.test.ts +98 -0
  135. package/src/editor/managers/StyleManager.ts +31 -15
  136. package/src/editor/managers/index.ts +1 -0
  137. package/src/editor/transformPasted.ts +14 -13
  138. package/src/exporter/Exporter.test.ts +70 -1
  139. package/src/exporter/Exporter.ts +34 -4
  140. package/src/exporter/mapping.ts +41 -10
  141. package/src/extensions/DropCursor/DropCursor.ts +119 -6
  142. package/src/extensions/DropCursor/dropIntoChildren.ts +80 -0
  143. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +29 -0
  144. package/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboard.test.ts +190 -0
  145. package/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboard.ts +41 -4
  146. package/src/extensions/Placeholder/Placeholder.test.ts +23 -0
  147. package/src/extensions/Placeholder/Placeholder.ts +8 -7
  148. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +36 -0
  149. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +8 -4
  150. package/src/extensions/ReadOnly/ReadOnly.test.ts +181 -0
  151. package/src/extensions/ReadOnly/ReadOnly.ts +73 -0
  152. package/src/extensions/SideMenu/SideMenu.ts +401 -749
  153. package/src/extensions/SideMenu/blockDropHandling.ts +372 -0
  154. package/src/extensions/SideMenu/blockGeometry.browser.test.ts +148 -0
  155. package/src/extensions/SideMenu/blockGeometry.test.ts +46 -0
  156. package/src/extensions/SideMenu/blockGeometry.ts +53 -0
  157. package/src/extensions/SideMenu/dragging.ts +19 -32
  158. package/src/extensions/SideMenu/sideMenuHover.test.ts +36 -0
  159. package/src/extensions/SideMenu/sideMenuHover.ts +270 -0
  160. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +5 -4
  161. package/src/extensions/SuggestionMenu/SuggestionMenu.ts +10 -1
  162. package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +36 -136
  163. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +14 -4
  164. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +1 -1
  165. package/src/extensions/SyntaxHighlighting/shiki.ts +1 -1
  166. package/src/extensions/TableHandles/TableHandles.browser.test.ts +3 -1
  167. package/src/extensions/TableHandles/TableHandles.ts +4 -6
  168. package/src/extensions/Versioning/Versioning.browser.test.ts +151 -0
  169. package/src/extensions/Versioning/Versioning.node.test.ts +177 -0
  170. package/src/extensions/Versioning/Versioning.ts +145 -612
  171. package/src/extensions/Versioning/__test__/result.ts +12 -0
  172. package/src/extensions/Versioning/comparison.test.ts +40 -0
  173. package/src/extensions/Versioning/createVersioning.test.ts +867 -0
  174. package/src/extensions/Versioning/createVersioning.ts +499 -0
  175. package/src/extensions/Versioning/formatVersionDate.ts +24 -0
  176. package/src/extensions/Versioning/inMemoryVersioning.test.ts +533 -430
  177. package/src/extensions/Versioning/inMemoryVersioning.ts +246 -251
  178. package/src/extensions/Versioning/index.ts +4 -0
  179. package/src/extensions/Versioning/scrollScheduling.test.ts +51 -0
  180. package/src/extensions/Versioning/scrollToFirstChange.test.ts +320 -0
  181. package/src/extensions/Versioning/scrollToFirstChange.ts +139 -0
  182. package/src/extensions/Versioning/types.ts +219 -0
  183. package/src/extensions/Versioning/versioningState.ts +154 -0
  184. package/src/extensions/blockDOM.browser.test.ts +102 -0
  185. package/src/extensions/blockDOM.test.ts +178 -0
  186. package/src/extensions/blockDOM.ts +135 -0
  187. package/src/extensions/index.ts +2 -0
  188. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +1719 -53
  189. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +645 -531
  190. package/src/extensions/tiptap-extensions/KeyboardShortcuts/blockIdentity.browser.test.ts +152 -0
  191. package/src/extensions/tiptap-extensions/Link/helpers/autolink.ts +4 -9
  192. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.test.ts +10 -24
  193. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +22 -19
  194. package/src/fonts/inter.browser.test.ts +57 -0
  195. package/src/fonts/inter.css +111 -0
  196. package/src/i18n/locales/ar.ts +33 -0
  197. package/src/i18n/locales/de.ts +33 -0
  198. package/src/i18n/locales/en.ts +33 -0
  199. package/src/i18n/locales/es.ts +33 -0
  200. package/src/i18n/locales/fa.ts +33 -0
  201. package/src/i18n/locales/fr.ts +33 -0
  202. package/src/i18n/locales/he.ts +33 -0
  203. package/src/i18n/locales/hr.ts +33 -0
  204. package/src/i18n/locales/is.ts +33 -0
  205. package/src/i18n/locales/it.ts +33 -0
  206. package/src/i18n/locales/ja.ts +33 -0
  207. package/src/i18n/locales/ko.ts +33 -0
  208. package/src/i18n/locales/nl.ts +33 -0
  209. package/src/i18n/locales/no.ts +33 -0
  210. package/src/i18n/locales/pl.ts +33 -0
  211. package/src/i18n/locales/pt.ts +33 -0
  212. package/src/i18n/locales/ru.ts +33 -0
  213. package/src/i18n/locales/sk.ts +33 -0
  214. package/src/i18n/locales/uk.ts +33 -0
  215. package/src/i18n/locales/uz.ts +33 -0
  216. package/src/i18n/locales/vi.ts +33 -0
  217. package/src/i18n/locales/zh-tw.ts +33 -0
  218. package/src/i18n/locales/zh.ts +33 -0
  219. package/src/index.ts +14 -3
  220. package/src/pm-nodes/BlockContainer.test.ts +101 -0
  221. package/src/pm-nodes/BlockContainer.ts +110 -1
  222. package/src/pm-nodes/BlockGroup.ts +2 -1
  223. package/src/pm-nodes/README.md +2 -0
  224. package/src/schema/blocks/children.test.ts +288 -0
  225. package/src/schema/blocks/children.ts +136 -0
  226. package/src/schema/blocks/createSpec.browser.test.ts +176 -0
  227. package/src/schema/blocks/createSpec.test.ts +231 -0
  228. package/src/schema/blocks/createSpec.ts +493 -267
  229. package/src/schema/blocks/frame.test.ts +112 -0
  230. package/src/schema/blocks/frame.ts +337 -0
  231. package/src/schema/blocks/internal.ts +50 -17
  232. package/src/schema/blocks/keyboard.ts +154 -0
  233. package/src/schema/blocks/layoutBlockAttributes.ts +29 -0
  234. package/src/schema/blocks/meta.test.ts +53 -0
  235. package/src/schema/blocks/meta.ts +35 -0
  236. package/src/schema/blocks/renderFrame.test.ts +691 -0
  237. package/src/schema/blocks/types.ts +285 -21
  238. package/src/schema/blocks/validateChildren.ts +156 -0
  239. package/src/schema/dataAttributes.test.ts +72 -0
  240. package/src/schema/dataAttributes.ts +69 -0
  241. package/src/schema/index.ts +3 -0
  242. package/src/schema/inlineContent/internal.ts +7 -12
  243. package/src/schema/markGroups.ts +34 -2
  244. package/src/schema/schema.ts +8 -1
  245. package/src/user/userColors.test.ts +108 -0
  246. package/src/user/userColors.ts +42 -13
  247. package/src/util/browser.ts +24 -0
  248. package/src/y/comments/RESTYjsThreadStore.ts +1 -1
  249. package/src/y/comments/YjsThreadStore.test.ts +1 -1
  250. package/src/y/comments/YjsThreadStore.ts +27 -27
  251. package/src/y/comments/YjsThreadStoreBase.ts +2 -2
  252. package/src/y/comments/yjsHelpers.ts +11 -11
  253. package/src/y/extensions/AttributionExtension.test.ts +271 -2
  254. package/src/y/extensions/AttributionExtension.ts +146 -123
  255. package/src/y/extensions/DiffVersioningExtension.test.ts +67 -18
  256. package/src/y/extensions/DiffVersioningExtension.ts +95 -123
  257. package/src/y/extensions/EmptyDocBinding.test.ts +282 -0
  258. package/src/y/extensions/ForkYDoc.node.test.ts +77 -0
  259. package/src/y/extensions/ForkYDoc.test.ts +93 -2
  260. package/src/y/extensions/ForkYDoc.ts +22 -13
  261. package/src/y/extensions/History.test.ts +125 -0
  262. package/src/y/extensions/RelativePositionMapping.test.ts +35 -0
  263. package/src/y/extensions/RelativePositionMapping.ts +7 -11
  264. package/src/y/extensions/Versioning.node.test.ts +126 -0
  265. package/src/y/extensions/Versioning.test.ts +110 -401
  266. package/src/y/extensions/Versioning.ts +66 -107
  267. package/src/y/extensions/YAttributionMarks.ts +111 -9
  268. package/src/y/extensions/YCursorPlugin.ts +3 -0
  269. package/src/y/extensions/YSync.node.test.ts +75 -0
  270. package/src/y/extensions/YSync.ts +76 -4
  271. package/src/y/extensions/YSyncRemount.test.ts +99 -0
  272. package/src/y/extensions/blockMatchNodes.ts +20 -29
  273. package/src/y/extensions/index.ts +10 -26
  274. package/src/y/extensions/legacyYjsDocBinding.test.ts +652 -0
  275. package/src/y/extensions/nestingChanges.test.ts +326 -0
  276. package/src/y/extensions/persistentBlockGroups.test.ts +1709 -0
  277. package/src/y/extensions/persistentBlockGroups.ts +953 -0
  278. package/src/y/extensions/snapshotCodec.test.ts +105 -0
  279. package/src/y/extensions/snapshotCodec.ts +39 -0
  280. package/src/y/extensions/snapshotPreview.test.ts +201 -0
  281. package/src/y/extensions/snapshotPreview.ts +292 -0
  282. package/src/y/extensions/tableReshape.test.ts +121 -0
  283. package/src/y/extensions/v1BindingSnapshotDiff.test.ts +121 -0
  284. package/src/y/extensions/versionDiffAttribution.test.ts +944 -0
  285. package/src/y/extensions/versionDiffPerformance.test.ts +101 -0
  286. package/src/y/index.ts +1 -0
  287. package/src/y/utils.test.ts +289 -22
  288. package/src/y/utils.ts +167 -43
  289. package/src/y/versioning/__test__/fixtures/activity-all-after.json +5 -5
  290. package/src/y/versioning/__test__/fixtures/activity-all.json +3 -3
  291. package/src/y/versioning/__test__/fixtures/activity-filtered-after.json +2 -2
  292. package/src/y/versioning/__test__/yhub.test.ts +902 -721
  293. package/src/y/versioning/index.ts +6 -1
  294. package/src/y/versioning/yhub.ts +316 -603
  295. package/src/y/versioning/yhubClient.ts +345 -0
  296. package/src/yjs/comments/RESTYjsThreadStore.test.ts +10 -0
  297. package/src/yjs/extensions/FixUpSchema.ts +11 -0
  298. package/src/yjs/extensions/ForkYDoc.browser.test.ts +84 -0
  299. package/src/yjs/extensions/ForkYDoc.node.test.ts +231 -0
  300. package/src/yjs/extensions/ForkYDoc.test.ts +68 -1
  301. package/src/yjs/extensions/ForkYDoc.ts +57 -39
  302. package/src/yjs/extensions/Versioning.test.ts +345 -530
  303. package/src/yjs/extensions/Versioning.ts +111 -68
  304. package/src/yjs/extensions/index.ts +1 -1
  305. package/src/yjs/index.ts +4 -0
  306. package/types/src/api/blockManipulation/commands/insertBlocks/insertBlocks.d.ts +2 -1
  307. package/types/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.d.ts +24 -24
  308. package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.d.ts +3 -2
  309. package/types/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.d.ts +1 -1
  310. package/types/src/api/blockManipulation/commands/splitBlock/splitBlock.d.ts +2 -2
  311. package/types/src/api/blockManipulation/layoutBlocks/fixLayoutBlock.d.ts +34 -0
  312. package/types/src/api/blockManipulation/layoutBlocks/layoutBlocks.browser.test.d.ts +1 -0
  313. package/types/src/api/blockManipulation/layoutBlocks/layoutBlocks.fixture.d.ts +460 -0
  314. package/types/src/api/blockManipulation/layoutBlocks/layoutBlocks.test.d.ts +1 -0
  315. package/types/src/api/blockManipulation/layoutBlocks/plainBlocks.test.d.ts +1 -0
  316. package/types/src/api/blockManipulation/layoutBlocks/titledBlocks.test.d.ts +1 -0
  317. package/types/src/api/blockManipulation/setupTestEnv.d.ts +15 -69
  318. package/types/src/api/combineTransactionSteps.d.ts +12 -0
  319. package/types/src/api/getBlockInfoFromPos.d.ts +218 -44
  320. package/types/src/api/{getChangedRange.d.ts → getChangedRangeWithAttrs.d.ts} +1 -1
  321. package/types/src/api/getChangedRanges.d.ts +23 -0
  322. package/types/src/api/getChangedRanges.test.d.ts +1 -0
  323. package/types/src/api/nodeConversions/fragmentToBlocks.d.ts +0 -3
  324. package/types/src/api/parsers/markdown/autolink.d.ts +5 -0
  325. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +4 -0
  326. package/types/src/blocks/ToggleWrapper/createToggleFrame.d.ts +29 -0
  327. package/types/src/blocks/ToggleWrapper/toggleBlocks.browser.test.d.ts +1 -0
  328. package/types/src/blocks/defaultBlockTypeItems.d.ts +37 -0
  329. package/types/src/blocks/defaultBlockTypeItems.test.d.ts +1 -0
  330. package/types/src/blocks/defaultBlocks.test.d.ts +1 -0
  331. package/types/src/blocks/index.d.ts +2 -1
  332. package/types/src/comments/CommentEditorSubmitExtension.d.ts +19 -0
  333. package/types/src/comments/CommentEditorSubmitExtension.test.d.ts +1 -0
  334. package/types/src/comments/extension.d.ts +7 -0
  335. package/types/src/comments/index.d.ts +1 -0
  336. package/types/src/editor/BlockNoteEditor.d.ts +88 -27
  337. package/types/src/editor/BlockNoteEditor.node.test.d.ts +1 -0
  338. package/types/src/editor/BlockNoteExtension.d.ts +3 -0
  339. package/types/src/editor/blockColors.browser.test.d.ts +1 -0
  340. package/types/src/editor/managers/BlockManager.d.ts +4 -3
  341. package/types/src/editor/managers/EventManager.browser.test.d.ts +1 -0
  342. package/types/src/editor/managers/EventManager.d.ts +55 -0
  343. package/types/src/editor/managers/ExtensionManager/ExtensionManager.browser.test.d.ts +1 -0
  344. package/types/src/editor/managers/ExtensionManager/ExtensionManager.node.test.d.ts +1 -0
  345. package/types/src/editor/managers/ExtensionManager/index.d.ts +15 -3
  346. package/types/src/editor/managers/StateManager.d.ts +1 -1
  347. package/types/src/editor/managers/StyleManager.browser.test.d.ts +1 -0
  348. package/types/src/editor/managers/StyleManager.d.ts +4 -2
  349. package/types/src/editor/managers/StyleManager.test.d.ts +1 -0
  350. package/types/src/editor/managers/index.d.ts +1 -0
  351. package/types/src/editor/transformPasted.d.ts +1 -1
  352. package/types/src/exporter/Exporter.d.ts +11 -4
  353. package/types/src/exporter/mapping.d.ts +13 -1
  354. package/types/src/extensions/DropCursor/DropCursor.d.ts +1 -1
  355. package/types/src/extensions/DropCursor/dropIntoChildren.d.ts +38 -0
  356. package/types/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboard.d.ts +1 -1
  357. package/types/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboard.test.d.ts +1 -0
  358. package/types/src/extensions/Placeholder/Placeholder.test.d.ts +1 -0
  359. package/types/src/extensions/ReadOnly/ReadOnly.d.ts +21 -0
  360. package/types/src/extensions/ReadOnly/ReadOnly.test.d.ts +1 -0
  361. package/types/src/extensions/SideMenu/SideMenu.d.ts +49 -88
  362. package/types/src/extensions/SideMenu/blockDropHandling.d.ts +11 -0
  363. package/types/src/extensions/SideMenu/blockGeometry.browser.test.d.ts +1 -0
  364. package/types/src/extensions/SideMenu/blockGeometry.d.ts +22 -0
  365. package/types/src/extensions/SideMenu/blockGeometry.test.d.ts +1 -0
  366. package/types/src/extensions/SideMenu/dragging.d.ts +0 -4
  367. package/types/src/extensions/SideMenu/sideMenuHover.d.ts +58 -0
  368. package/types/src/extensions/SideMenu/sideMenuHover.test.d.ts +1 -0
  369. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +3 -0
  370. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +1 -0
  371. package/types/src/extensions/Versioning/Versioning.browser.test.d.ts +1 -0
  372. package/types/src/extensions/Versioning/Versioning.d.ts +34 -311
  373. package/types/src/extensions/Versioning/Versioning.node.test.d.ts +1 -0
  374. package/types/src/extensions/Versioning/__test__/result.d.ts +3 -0
  375. package/types/src/extensions/Versioning/comparison.test.d.ts +1 -0
  376. package/types/src/extensions/Versioning/createVersioning.d.ts +32 -0
  377. package/types/src/extensions/Versioning/createVersioning.test.d.ts +1 -0
  378. package/types/src/extensions/Versioning/formatVersionDate.d.ts +7 -0
  379. package/types/src/extensions/Versioning/inMemoryVersioning.d.ts +64 -36
  380. package/types/src/extensions/Versioning/index.d.ts +4 -0
  381. package/types/src/extensions/Versioning/scrollScheduling.test.d.ts +1 -0
  382. package/types/src/extensions/Versioning/scrollToFirstChange.d.ts +18 -0
  383. package/types/src/extensions/Versioning/scrollToFirstChange.test.d.ts +1 -0
  384. package/types/src/extensions/Versioning/types.d.ts +239 -0
  385. package/types/src/extensions/Versioning/versioningState.d.ts +43 -0
  386. package/types/src/extensions/blockDOM.browser.test.d.ts +1 -0
  387. package/types/src/extensions/blockDOM.d.ts +37 -0
  388. package/types/src/extensions/blockDOM.test.d.ts +1 -0
  389. package/types/src/extensions/index.d.ts +2 -0
  390. package/types/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.d.ts +1 -0
  391. package/types/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.d.ts +3 -0
  392. package/types/src/extensions/tiptap-extensions/KeyboardShortcuts/blockIdentity.browser.test.d.ts +1 -0
  393. package/types/src/fonts/inter.browser.test.d.ts +2 -0
  394. package/types/src/i18n/locales/en.d.ts +33 -0
  395. package/types/src/i18n/locales/fa.d.ts +33 -0
  396. package/types/src/i18n/locales/sk.d.ts +33 -0
  397. package/types/src/index.d.ts +7 -3
  398. package/types/src/pm-nodes/BlockContainer.d.ts +6 -0
  399. package/types/src/pm-nodes/BlockContainer.test.d.ts +1 -0
  400. package/types/src/schema/blocks/children.d.ts +39 -0
  401. package/types/src/schema/blocks/children.test.d.ts +1 -0
  402. package/types/src/schema/blocks/createSpec.browser.test.d.ts +1 -0
  403. package/types/src/schema/blocks/createSpec.d.ts +13 -0
  404. package/types/src/schema/blocks/frame.d.ts +109 -0
  405. package/types/src/schema/blocks/frame.test.d.ts +1 -0
  406. package/types/src/schema/blocks/internal.d.ts +6 -0
  407. package/types/src/schema/blocks/keyboard.d.ts +111 -0
  408. package/types/src/schema/blocks/layoutBlockAttributes.d.ts +9 -0
  409. package/types/src/schema/blocks/meta.d.ts +14 -0
  410. package/types/src/schema/blocks/meta.test.d.ts +1 -0
  411. package/types/src/schema/blocks/renderFrame.test.d.ts +1 -0
  412. package/types/src/schema/blocks/types.d.ts +228 -15
  413. package/types/src/schema/blocks/validateChildren.d.ts +5 -0
  414. package/types/src/schema/dataAttributes.d.ts +19 -0
  415. package/types/src/schema/dataAttributes.test.d.ts +1 -0
  416. package/types/src/schema/index.d.ts +3 -0
  417. package/types/src/schema/markGroups.d.ts +8 -0
  418. package/types/src/user/userColors.d.ts +24 -3
  419. package/types/src/user/userColors.test.d.ts +1 -0
  420. package/types/src/util/browser.d.ts +2 -0
  421. package/types/src/y/comments/RESTYjsThreadStore.d.ts +1 -1
  422. package/types/src/y/comments/YjsThreadStore.d.ts +1 -1
  423. package/types/src/y/comments/YjsThreadStoreBase.d.ts +2 -2
  424. package/types/src/y/comments/yjsHelpers.d.ts +5 -5
  425. package/types/src/y/extensions/AttributionExtension.d.ts +19 -21
  426. package/types/src/y/extensions/DiffVersioningExtension.d.ts +9 -4
  427. package/types/src/y/extensions/EmptyDocBinding.test.d.ts +1 -0
  428. package/types/src/y/extensions/ForkYDoc.d.ts +3 -1
  429. package/types/src/y/extensions/ForkYDoc.node.test.d.ts +1 -0
  430. package/types/src/y/extensions/History.test.d.ts +1 -0
  431. package/types/src/y/extensions/Versioning.d.ts +4 -15
  432. package/types/src/y/extensions/Versioning.node.test.d.ts +1 -0
  433. package/types/src/y/extensions/YAttributionMarks.d.ts +13 -2
  434. package/types/src/y/extensions/YCursorPlugin.d.ts +1 -0
  435. package/types/src/y/extensions/YSync.d.ts +4 -18
  436. package/types/src/y/extensions/YSync.node.test.d.ts +1 -0
  437. package/types/src/y/extensions/YSyncRemount.test.d.ts +1 -0
  438. package/types/src/y/extensions/blockMatchNodes.d.ts +0 -22
  439. package/types/src/y/extensions/index.d.ts +27 -44
  440. package/types/src/y/extensions/legacyYjsDocBinding.test.d.ts +1 -0
  441. package/types/src/y/extensions/nestingChanges.test.d.ts +1 -0
  442. package/types/src/y/extensions/persistentBlockGroups.d.ts +189 -0
  443. package/types/src/y/extensions/persistentBlockGroups.test.d.ts +1 -0
  444. package/types/src/y/extensions/snapshotCodec.d.ts +13 -0
  445. package/types/src/y/extensions/snapshotCodec.test.d.ts +1 -0
  446. package/types/src/y/extensions/snapshotPreview.d.ts +21 -0
  447. package/types/src/y/extensions/snapshotPreview.test.d.ts +1 -0
  448. package/types/src/y/extensions/tableReshape.test.d.ts +1 -0
  449. package/types/src/y/extensions/v1BindingSnapshotDiff.test.d.ts +1 -0
  450. package/types/src/y/extensions/versionDiffAttribution.test.d.ts +1 -0
  451. package/types/src/y/extensions/versionDiffPerformance.test.d.ts +1 -0
  452. package/types/src/y/index.d.ts +1 -0
  453. package/types/src/y/utils.d.ts +39 -19
  454. package/types/src/y/versioning/index.d.ts +1 -1
  455. package/types/src/y/versioning/yhub.d.ts +69 -86
  456. package/types/src/y/versioning/yhubClient.d.ts +67 -0
  457. package/types/src/yjs/extensions/ForkYDoc.browser.test.d.ts +1 -0
  458. package/types/src/yjs/extensions/ForkYDoc.d.ts +2 -0
  459. package/types/src/yjs/extensions/ForkYDoc.node.test.d.ts +1 -0
  460. package/types/src/yjs/extensions/Versioning.d.ts +36 -22
  461. package/types/src/yjs/extensions/index.d.ts +2 -1
  462. package/types/src/yjs/index.d.ts +1 -0
  463. package/dist/BlockNoteExtension-BJCLnwrE.cjs +0 -2
  464. package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +0 -1
  465. package/dist/BlockNoteExtension-CbvDA0F7.js.map +0 -1
  466. package/dist/blocks-CTB_5Vyj.cjs +0 -12
  467. package/dist/blocks-CTB_5Vyj.cjs.map +0 -1
  468. package/dist/blocks-Dol85hpH.js.map +0 -1
  469. package/dist/en-BuzSuuTq.cjs +0 -2
  470. package/dist/en-BuzSuuTq.cjs.map +0 -1
  471. package/dist/en-uzHzj6eH.js.map +0 -1
  472. package/dist/extensions-B7Rsp_Ft.cjs +0 -58
  473. package/dist/extensions-B7Rsp_Ft.cjs.map +0 -1
  474. package/dist/extensions-D6JcJCwd.js +0 -2138
  475. package/dist/extensions-D6JcJCwd.js.map +0 -1
  476. package/dist/src-Buuo5l7X.js +0 -3317
  477. package/dist/src-Buuo5l7X.js.map +0 -1
  478. package/dist/src-C54Ok9E0.cjs +0 -23
  479. package/dist/src-C54Ok9E0.cjs.map +0 -1
  480. package/src/api/blockManipulation/commands/replaceBlocks/util/fixColumnList.ts +0 -173
  481. package/src/blocks/ListItem/ListItemKeyboardShortcuts.ts +0 -63
  482. package/src/blocks/ToggleWrapper/createToggleWrapper.ts +0 -197
  483. package/src/blocks/utils/listItemEnterHandler.ts +0 -43
  484. package/src/extensions/Versioning/Versioning.test.ts +0 -435
  485. package/src/extensions/getDraggableBlockFromElement.ts +0 -19
  486. package/types/src/api/blockManipulation/commands/replaceBlocks/util/fixColumnList.d.ts +0 -32
  487. package/types/src/blocks/ListItem/ListItemKeyboardShortcuts.d.ts +0 -2
  488. package/types/src/blocks/ToggleWrapper/createToggleWrapper.d.ts +0 -15
  489. package/types/src/blocks/utils/listItemEnterHandler.d.ts +0 -2
  490. package/types/src/extensions/getDraggableBlockFromElement.d.ts +0 -5
  491. /package/types/src/{extensions/Versioning/Versioning.test.d.ts → api/blockManipulation/commands/insertBlocks/insertPlacement.test.d.ts} +0 -0
@@ -1,20 +1,30 @@
1
- import { describe, expect, it } from "vite-plus/test";
2
-
3
- import { BlockNoteSchema } from "../../../blocks/BlockNoteSchema.js";
4
- import { defaultBlockSpecs } from "../../../blocks/defaultBlocks.js";
5
- import { BlockNoteEditor } from "../../../editor/BlockNoteEditor.js";
6
- import { createBlockSpec } from "../../../schema/index.js";
7
-
8
1
  /**
9
2
  * @vitest-environment jsdom
10
3
  */
11
4
 
12
- // The `hardBreakShortcut` setting lives on the block spec's implementation
13
- // (`schema.blockSpecs[type].implementation.meta`), not on the block config in
14
- // `schema.blockSchema`. These blocks verify that the Enter / Shift-Enter
15
- // handlers read it from the right place — a previous regression read it from
16
- // `blockSchema`, which never contains `meta`, so custom settings were silently
17
- // ignored and every block behaved as "shift+enter".
5
+ import { closeHistory } from "@tiptap/pm/history";
6
+ import { NodeSelection, Selection, TextSelection } from "prosemirror-state";
7
+ import { afterEach, describe, expect, it } from "vite-plus/test";
8
+
9
+ import { mergeBlocksCommand } from "../../../api/blockManipulation/commands/mergeBlocks/mergeBlocks.js";
10
+ import {
11
+ getBlockInfoAt,
12
+ getBlockInfoFromSelection,
13
+ } from "../../../api/getBlockInfoFromPos.js";
14
+ import { getNodeById } from "../../../api/nodeUtil.js";
15
+ import { BlockNoteSchema } from "../../../blocks/BlockNoteSchema.js";
16
+ import {
17
+ defaultBlockSpecs,
18
+ type PartialBlock,
19
+ } from "../../../blocks/defaultBlocks.js";
20
+ import {
21
+ BlockNoteEditor,
22
+ type BlockNoteEditorOptions,
23
+ } from "../../../editor/BlockNoteEditor.js";
24
+ import { createBlockSpec } from "../../../schema/index.js";
25
+
26
+ // Blocks configured with the deprecated `meta.hardBreakShortcut`, which still
27
+ // sets the default of `keyboard.enter` and `keyboard.shiftEnter`.
18
28
  const createHardBreakTestBlockSpec = <
19
29
  const T extends string,
20
30
  const S extends "shift+enter" | "enter" | "none",
@@ -44,9 +54,73 @@ const createHardBreakTestBlockSpec = <
44
54
  },
45
55
  )();
46
56
 
57
+ // The same blocks, configured with the `experimental_keyboard` settings that
58
+ // replace the
59
+ // deprecated `meta.hardBreakShortcut`.
60
+ const createKeyboardTestBlockSpec = <
61
+ const T extends string,
62
+ const C extends "inline" | "plain",
63
+ >(
64
+ type: T,
65
+ keyboard: {
66
+ enter?: "split" | "into-children" | "line-break";
67
+ shiftEnter?: "line-break" | "same-as-enter";
68
+ },
69
+ content: C = "inline" as C,
70
+ ) =>
71
+ createBlockSpec(
72
+ {
73
+ type,
74
+ propSchema: {},
75
+ content,
76
+ },
77
+ {
78
+ experimental_keyboard: keyboard,
79
+ render: () => {
80
+ const dom = document.createElement("p");
81
+ return {
82
+ dom,
83
+ contentDOM: dom,
84
+ };
85
+ },
86
+ },
87
+ )();
88
+
89
+ // A block whose keyboard function leaves settings `undefined` when a prop is
90
+ // off. Those settings must keep their defaults.
91
+ const conditionalKeyboardBlock = createBlockSpec(
92
+ {
93
+ type: "conditionalKeyboard",
94
+ propSchema: { on: { default: false } },
95
+ content: "inline",
96
+ },
97
+ {
98
+ experimental_keyboard: (block) => ({
99
+ enter: block.props.on ? "into-children" : undefined,
100
+ resetsTo: block.props.on ? { type: "heading" } : undefined,
101
+ }),
102
+ render: () => {
103
+ const dom = document.createElement("p");
104
+ return { dom, contentDOM: dom };
105
+ },
106
+ },
107
+ )();
108
+
47
109
  const schema = BlockNoteSchema.create({
48
110
  blockSpecs: {
49
111
  ...defaultBlockSpecs,
112
+ conditionalKeyboard: conditionalKeyboardBlock,
113
+ keyboardEnter: createKeyboardTestBlockSpec("keyboardEnter", {
114
+ enter: "line-break",
115
+ }),
116
+ keyboardNone: createKeyboardTestBlockSpec("keyboardNone", {
117
+ shiftEnter: "same-as-enter",
118
+ }),
119
+ keyboardEnterPlain: createKeyboardTestBlockSpec(
120
+ "keyboardEnterPlain",
121
+ { enter: "line-break" },
122
+ "plain",
123
+ ),
50
124
  hardBreakEnter: createHardBreakTestBlockSpec("hardBreakEnter", "enter"),
51
125
  hardBreakNone: createHardBreakTestBlockSpec("hardBreakNone", "none"),
52
126
  // "plain" content (`text*`) can't hold a `hardBreak` node, so these blocks
@@ -64,7 +138,10 @@ function createEditor(
64
138
  | "paragraph"
65
139
  | "hardBreakEnter"
66
140
  | "hardBreakNone"
67
- | "hardBreakEnterPlain",
141
+ | "hardBreakEnterPlain"
142
+ | "keyboardEnter"
143
+ | "keyboardNone"
144
+ | "keyboardEnterPlain",
68
145
  ) {
69
146
  const editor = BlockNoteEditor.create({
70
147
  schema,
@@ -90,6 +167,22 @@ function pressKeys(editor: BlockNoteEditor<any, any, any>, keys: string) {
90
167
  editor._tiptapEditor.commands.keyboardShortcut(keys);
91
168
  }
92
169
 
170
+ /**
171
+ * Dispatches a keydown event straight to the view's handlers. Unlike
172
+ * `pressKeys`, whose command loses the selection and stored marks the handler
173
+ * sets, this leaves the state as a real key press does. Use it for tests that
174
+ * check the caret or the styles of what is typed next.
175
+ */
176
+ function keyDown(
177
+ editor: BlockNoteEditor<any, any, any>,
178
+ init: KeyboardEventInit,
179
+ ) {
180
+ const view = editor._tiptapEditor.view;
181
+ view.someProp("handleKeyDown", (handler) =>
182
+ handler(view, new KeyboardEvent("keydown", init)),
183
+ );
184
+ }
185
+
93
186
  function countHardBreaks(editor: BlockNoteEditor<any, any, any>) {
94
187
  let count = 0;
95
188
  editor._tiptapEditor.state.doc.descendants((node) => {
@@ -110,42 +203,730 @@ function getTextContent(editor: BlockNoteEditor<any, any, any>) {
110
203
  return text;
111
204
  }
112
205
 
113
- describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
114
- it("inserts a hard break on Shift-Enter by default", () => {
115
- const editor = createEditor("paragraph");
206
+ describe("KeyboardShortcutsExtension Mod-a (select all)", () => {
207
+ // BlockNote disables TipTap's core extensions, so it has no default `Mod-a`
208
+ // binding and select-all used to rely on the browser's native behaviour. That
209
+ // native select-all collapses to a cursor when the editor's first element is
210
+ // non-editable - e.g. the checkbox `<div>` of a check list item as the first
211
+ // block - so `Mod-a` is now handled explicitly. These tests exercise the
212
+ // keymap path (not native selection) and would collapse before the fix.
213
+ function createSelectAllEditor(
214
+ blocks: PartialBlock<any, any, any>[],
215
+ selectAllBehavior?: BlockNoteEditorOptions<
216
+ typeof schema.blockSchema,
217
+ typeof schema.inlineContentSchema,
218
+ typeof schema.styleSchema
219
+ >["selectAllBehavior"],
220
+ ) {
221
+ const editor = BlockNoteEditor.create({
222
+ schema,
223
+ selectAllBehavior,
224
+ initialContent: blocks.map((block, index) => ({
225
+ id: `block-${index}`,
226
+ ...block,
227
+ })),
228
+ });
229
+ editor.mount(document.createElement("div"));
230
+ return editor;
231
+ }
116
232
 
117
- pressKeys(editor, "Shift-Enter");
233
+ // Dispatches a real `Mod-a` keydown through ProseMirror's `handleKeyDown`, the
234
+ // path browsers use to invoke the keymap. TipTap's `keyboardShortcut` command
235
+ // doesn't reliably simulate modifier combos in jsdom, and prosemirror-keymap
236
+ // resolves `Mod` to `Ctrl` outside of a Mac environment (jsdom reports none).
237
+ function pressSelectAll(editor: BlockNoteEditor<any, any, any>) {
238
+ const view = editor._tiptapEditor.view;
239
+ const event = new KeyboardEvent("keydown", {
240
+ key: "a",
241
+ code: "KeyA",
242
+ ctrlKey: true,
243
+ });
244
+ view.someProp("handleKeyDown", (handler) => handler(view, event));
245
+ }
118
246
 
119
- expect(countHardBreaks(editor)).toBe(1);
120
- expect(editor.document.length).toBe(1);
247
+ function pressBackspace(editor: BlockNoteEditor<any, any, any>) {
248
+ const view = editor._tiptapEditor.view;
249
+ const event = new KeyboardEvent("keydown", {
250
+ key: "Backspace",
251
+ code: "Backspace",
252
+ });
253
+ view.someProp("handleKeyDown", (handler) => handler(view, event));
254
+ }
255
+
256
+ function expectWholeDocSelected(editor: BlockNoteEditor<any, any, any>) {
257
+ const { selection, doc } = editor._tiptapEditor.state;
258
+ // Select-all spans all content as a `TextSelection` (from the first
259
+ // selectable position to the last), not an `AllSelection`.
260
+ expect(selection).toBeInstanceOf(TextSelection);
261
+ expect(selection.from).toBe(Selection.atStart(doc).from);
262
+ expect(selection.to).toBe(Selection.atEnd(doc).to);
263
+ }
264
+
265
+ function expectBlockContentSelected(
266
+ editor: BlockNoteEditor<any, any, any>,
267
+ blockId: string,
268
+ ) {
269
+ const { selection, doc } = editor._tiptapEditor.state;
270
+ const blockInfo = getBlockInfoAt(
271
+ doc,
272
+ getNodeById(blockId, doc)!.posBeforeNode,
273
+ );
274
+ if (!blockInfo.hasContent) {
275
+ throw new Error(`Block ${blockId} is not a block container`);
276
+ }
277
+ // The current block's content is selected as a `TextSelection` spanning its
278
+ // full content, without reaching into neighbouring blocks.
279
+ expect(selection).toBeInstanceOf(TextSelection);
280
+ expect(selection.from).toBe(blockInfo.contentStart);
281
+ expect(selection.to).toBe(blockInfo.contentEnd);
282
+ }
283
+
284
+ // Like `expectBlockContentSelected`, but for blocks whose content isn't
285
+ // directly inline (e.g. tables), where the selection snaps inward to the
286
+ // nearest inline positions rather than sitting exactly on the content bounds.
287
+ function expectSelectionWithinBlock(
288
+ editor: BlockNoteEditor<any, any, any>,
289
+ blockId: string,
290
+ ) {
291
+ const { selection, doc } = editor._tiptapEditor.state;
292
+ const blockInfo = getBlockInfoAt(
293
+ doc,
294
+ getNodeById(blockId, doc)!.posBeforeNode,
295
+ );
296
+ if (!blockInfo.hasContent) {
297
+ throw new Error(`Block ${blockId} is not a block container`);
298
+ }
299
+ expect(selection).toBeInstanceOf(TextSelection);
300
+ expect(selection.empty).toBe(false);
301
+ // The selection stays inside the block's content, without reaching into
302
+ // neighbouring blocks.
303
+ expect(selection.from).toBeGreaterThanOrEqual(blockInfo.contentStart);
304
+ expect(selection.to).toBeLessThanOrEqual(blockInfo.contentEnd);
305
+ }
306
+
307
+ it("selects the block first when explicitly configured", () => {
308
+ const editor = createSelectAllEditor(
309
+ [
310
+ { type: "paragraph", content: "First" },
311
+ { type: "paragraph", content: "Second" },
312
+ ],
313
+ "block-first",
314
+ );
315
+ editor.setTextCursorPosition("block-0", "end");
316
+
317
+ pressSelectAll(editor);
318
+ expectBlockContentSelected(editor, "block-0");
319
+
320
+ pressSelectAll(editor);
321
+ expectWholeDocSelected(editor);
121
322
 
122
323
  editor._tiptapEditor.destroy();
123
324
  });
124
325
 
125
- it("splits the block on Enter by default", () => {
126
- const editor = createEditor("paragraph");
326
+ it("selects the whole document immediately when configured", () => {
327
+ const editor = createSelectAllEditor(
328
+ [
329
+ { type: "checkListItem", content: "First" },
330
+ { type: "paragraph", content: "Second" },
331
+ ],
332
+ "document",
333
+ );
334
+ editor.setTextCursorPosition("block-1", "end");
127
335
 
128
- pressKeys(editor, "Enter");
336
+ pressSelectAll(editor);
337
+ expectWholeDocSelected(editor);
129
338
 
130
- expect(countHardBreaks(editor)).toBe(0);
131
- expect(editor.document.length).toBe(2);
339
+ pressSelectAll(editor);
340
+ expectWholeDocSelected(editor);
341
+
342
+ pressBackspace(editor);
343
+ expect(editor.document).toEqual([
344
+ expect.objectContaining({ type: "paragraph", content: [] }),
345
+ ]);
346
+
347
+ editor._tiptapEditor.destroy();
348
+ });
349
+
350
+ // Each test walks the full Notion-style flow: the first `Mod-a` selects the
351
+ // current block, the second expands to the whole document, and Backspace
352
+ // clears it (issue #2973 - the bug was specific to documents starting with a
353
+ // check list item).
354
+ it("escalates the selection and clears a paragraph-first document", () => {
355
+ const editor = createSelectAllEditor([
356
+ { type: "paragraph", content: "First" },
357
+ { type: "paragraph", content: "Second" },
358
+ ]);
359
+ editor.setTextCursorPosition("block-0", "end");
360
+
361
+ pressSelectAll(editor);
362
+ expectBlockContentSelected(editor, "block-0");
363
+
364
+ pressSelectAll(editor);
365
+ expectWholeDocSelected(editor);
366
+
367
+ pressBackspace(editor);
368
+ expect(editor.document).toEqual([
369
+ expect.objectContaining({ type: "paragraph", content: [] }),
370
+ ]);
371
+
372
+ editor._tiptapEditor.destroy();
373
+ });
374
+
375
+ it("escalates the selection and clears a check-list-first document", () => {
376
+ const editor = createSelectAllEditor([
377
+ { type: "checkListItem", content: "First" },
378
+ { type: "paragraph", content: "Second" },
379
+ ]);
380
+ // Cursor starts in a later block to check select-all still spans the whole
381
+ // document, not just the current block.
382
+ editor.setTextCursorPosition("block-1", "end");
383
+
384
+ pressSelectAll(editor);
385
+ expectBlockContentSelected(editor, "block-1");
386
+
387
+ pressSelectAll(editor);
388
+ expectWholeDocSelected(editor);
389
+
390
+ pressBackspace(editor);
391
+ expect(editor.document).toEqual([
392
+ expect.objectContaining({ type: "paragraph", content: [] }),
393
+ ]);
394
+
395
+ editor._tiptapEditor.destroy();
396
+ });
397
+
398
+ it("escalates the selection and clears an all-check-list document", () => {
399
+ const editor = createSelectAllEditor([
400
+ { type: "checkListItem", content: "First" },
401
+ { type: "checkListItem", content: "Second" },
402
+ ]);
403
+ editor.setTextCursorPosition("block-0", "end");
404
+
405
+ pressSelectAll(editor);
406
+ expectBlockContentSelected(editor, "block-0");
407
+
408
+ pressSelectAll(editor);
409
+ expectWholeDocSelected(editor);
410
+
411
+ pressBackspace(editor);
412
+ expect(editor.document).toEqual([
413
+ expect.objectContaining({ type: "paragraph", content: [] }),
414
+ ]);
415
+
416
+ editor._tiptapEditor.destroy();
417
+ });
418
+
419
+ it("escalates the selection and clears a document ending in a check list item", () => {
420
+ const editor = createSelectAllEditor([
421
+ { type: "paragraph", content: "First" },
422
+ { type: "checkListItem", content: "Second" },
423
+ ]);
424
+ editor.setTextCursorPosition("block-0", "end");
425
+
426
+ pressSelectAll(editor);
427
+ expectBlockContentSelected(editor, "block-0");
428
+
429
+ pressSelectAll(editor);
430
+ expectWholeDocSelected(editor);
431
+
432
+ pressBackspace(editor);
433
+ expect(editor.document).toEqual([
434
+ expect.objectContaining({ type: "paragraph", content: [] }),
435
+ ]);
436
+
437
+ editor._tiptapEditor.destroy();
438
+ });
439
+
440
+ it("keeps the block type when clearing a single-block document", () => {
441
+ const editor = createSelectAllEditor([
442
+ { type: "checkListItem", content: "Only" },
443
+ ]);
444
+ editor.setTextCursorPosition("block-0", "end");
445
+
446
+ pressSelectAll(editor);
447
+ expectBlockContentSelected(editor, "block-0");
448
+
449
+ pressSelectAll(editor);
450
+ expectWholeDocSelected(editor);
451
+
452
+ // A single block can only ever have its content selected, so Backspace
453
+ // clears the content but (correctly) leaves the block type unchanged.
454
+ pressBackspace(editor);
455
+ expect(editor.document).toEqual([
456
+ expect.objectContaining({ type: "checkListItem", content: [] }),
457
+ ]);
458
+
459
+ editor._tiptapEditor.destroy();
460
+ });
461
+
462
+ it("escalates the selection and clears a document containing a table", () => {
463
+ const editor = createSelectAllEditor([
464
+ { type: "paragraph", content: "Before" },
465
+ {
466
+ type: "table",
467
+ content: {
468
+ type: "tableContent",
469
+ rows: [{ cells: ["A1", "B1"] }, { cells: ["A2", "B2"] }],
470
+ },
471
+ },
472
+ { type: "paragraph", content: "After" },
473
+ ]);
474
+ editor.setTextCursorPosition("block-1", "start");
475
+
476
+ // A table's content node isn't directly inline, so the first `Mod-a` selects
477
+ // across its cells rather than producing an invalid whole-block selection.
478
+ pressSelectAll(editor);
479
+ expectSelectionWithinBlock(editor, "block-1");
480
+
481
+ pressSelectAll(editor);
482
+ expectWholeDocSelected(editor);
483
+
484
+ pressBackspace(editor);
485
+ expect(editor.document).toEqual([
486
+ expect.objectContaining({ type: "paragraph", content: [] }),
487
+ ]);
488
+
489
+ editor._tiptapEditor.destroy();
490
+ });
491
+ });
492
+
493
+ /**
494
+ * Characterization tests for the Backspace/Delete/Enter/Tab handlers: they pin
495
+ * the current document transformations so the BlockInfo migration inside the
496
+ * handlers is provably behavior-preserving.
497
+ */
498
+ function createEditorWithBlocks(
499
+ initialContent: any[],
500
+ cursor: { id: string; placement: "start" | "end" },
501
+ ) {
502
+ const editor = BlockNoteEditor.create({ schema, initialContent });
503
+ editor.mount(document.createElement("div"));
504
+ editor.setTextCursorPosition(cursor.id, cursor.placement);
505
+ return editor;
506
+ }
507
+
508
+ /** Compact structural view of the document for snapshotting. */
509
+ function outline(blocks: any[]): any[] {
510
+ return blocks.map((b) => ({
511
+ type: b.type,
512
+ text: Array.isArray(b.content)
513
+ ? b.content.map((c: any) => c.text ?? "").join("")
514
+ : undefined,
515
+ ...(b.children.length > 0 ? { children: outline(b.children) } : {}),
516
+ }));
517
+ }
518
+
519
+ describe("KeyboardShortcutsExtension Backspace", () => {
520
+ it("merges a block into the previous one at block start", () => {
521
+ const editor = createEditorWithBlocks(
522
+ [
523
+ { id: "a", type: "paragraph", content: "Hello" },
524
+ { id: "b", type: "paragraph", content: "World" },
525
+ ],
526
+ { id: "b", placement: "start" },
527
+ );
528
+
529
+ pressKeys(editor, "Backspace");
530
+
531
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
532
+ [
533
+ {
534
+ "text": "HelloWorld",
535
+ "type": "paragraph",
536
+ },
537
+ ]
538
+ `);
539
+ editor._tiptapEditor.destroy();
540
+ });
541
+
542
+ it("merges into the previous block's deepest descendant", () => {
543
+ const editor = createEditorWithBlocks(
544
+ [
545
+ {
546
+ id: "a",
547
+ type: "paragraph",
548
+ content: "Parent",
549
+ children: [{ id: "a1", type: "paragraph", content: "Nested" }],
550
+ },
551
+ { id: "b", type: "paragraph", content: "World" },
552
+ ],
553
+ { id: "b", placement: "start" },
554
+ );
555
+
556
+ pressKeys(editor, "Backspace");
557
+
558
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
559
+ [
560
+ {
561
+ "children": [
562
+ {
563
+ "text": "NestedWorld",
564
+ "type": "paragraph",
565
+ },
566
+ ],
567
+ "text": "Parent",
568
+ "type": "paragraph",
569
+ },
570
+ ]
571
+ `);
572
+ editor._tiptapEditor.destroy();
573
+ });
574
+
575
+ it("merges a nested first child into its parent at block start", () => {
576
+ const editor = createEditorWithBlocks(
577
+ [
578
+ {
579
+ id: "a",
580
+ type: "paragraph",
581
+ content: "Parent",
582
+ children: [{ id: "a1", type: "paragraph", content: "Nested" }],
583
+ },
584
+ ],
585
+ { id: "a1", placement: "start" },
586
+ );
587
+
588
+ pressKeys(editor, "Backspace");
589
+
590
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
591
+ [
592
+ {
593
+ "text": "ParentNested",
594
+ "type": "paragraph",
595
+ },
596
+ ]
597
+ `);
598
+ editor._tiptapEditor.destroy();
599
+ });
600
+
601
+ it("deletes an empty block, moving its children out", () => {
602
+ const editor = createEditorWithBlocks(
603
+ [
604
+ { id: "a", type: "paragraph", content: "Before" },
605
+ {
606
+ id: "b",
607
+ type: "paragraph",
608
+ content: "",
609
+ children: [{ id: "b1", type: "paragraph", content: "Child" }],
610
+ },
611
+ ],
612
+ { id: "b", placement: "start" },
613
+ );
614
+
615
+ pressKeys(editor, "Backspace");
616
+
617
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
618
+ [
619
+ {
620
+ "text": "Before",
621
+ "type": "paragraph",
622
+ },
623
+ {
624
+ "text": "Child",
625
+ "type": "paragraph",
626
+ },
627
+ ]
628
+ `);
629
+ editor._tiptapEditor.destroy();
630
+ });
631
+ // The block above isn't rich text, but its last child is: the text joins
632
+ // that child, as after any block with children.
633
+ it("merges into the last child of a block above whose own content isn't inline", () => {
634
+ const editor = createEditorWithBlocks(
635
+ [
636
+ {
637
+ id: "code",
638
+ type: "codeBlock",
639
+ content: "x",
640
+ children: [{ id: "child", type: "paragraph", content: "Child" }],
641
+ },
642
+ { id: "after", type: "paragraph", content: "After" },
643
+ ],
644
+ { id: "after", placement: "start" },
645
+ );
646
+
647
+ pressKeys(editor, "Backspace");
648
+
649
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
650
+ [
651
+ {
652
+ "children": [
653
+ {
654
+ "text": "ChildAfter",
655
+ "type": "paragraph",
656
+ },
657
+ ],
658
+ "text": "x",
659
+ "type": "codeBlock",
660
+ },
661
+ ]
662
+ `);
663
+ editor._tiptapEditor.destroy();
664
+ });
665
+
666
+ it("does not merge rich text into a code block above", () => {
667
+ const editor = createEditorWithBlocks(
668
+ [
669
+ { id: "code", type: "codeBlock", content: "x" },
670
+ { id: "after", type: "paragraph", content: "After" },
671
+ ],
672
+ { id: "after", placement: "start" },
673
+ );
674
+
675
+ pressKeys(editor, "Backspace");
676
+
677
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
678
+ [
679
+ {
680
+ "text": "x",
681
+ "type": "codeBlock",
682
+ },
683
+ {
684
+ "text": "After",
685
+ "type": "paragraph",
686
+ },
687
+ ]
688
+ `);
689
+ editor._tiptapEditor.destroy();
690
+ });
691
+
692
+ // #2566: the caret used to jump to the end of the top-level block above,
693
+ // instead of the last block nested under it.
694
+ it("in an empty block moves the caret to the end of the deepest last block above", () => {
695
+ const editor = createEditorWithBlocks(
696
+ [
697
+ {
698
+ id: "list",
699
+ type: "bulletListItem",
700
+ content: "One",
701
+ children: [
702
+ { id: "nested", type: "bulletListItem", content: "Two" },
703
+ { id: "last", type: "bulletListItem" },
704
+ ],
705
+ },
706
+ { id: "empty", type: "paragraph" },
707
+ ],
708
+ { id: "empty", placement: "start" },
709
+ );
710
+
711
+ keyDown(editor, { key: "Backspace", keyCode: 8 });
712
+
713
+ expect(editor.getBlock("empty")).toBeUndefined();
714
+ expect(editor.getTextCursorPosition().block.id).toBe("last");
715
+ editor._tiptapEditor.destroy();
716
+ });
717
+
718
+ // #605: Backspace in an empty block below an image used to delete the image
719
+ // too.
720
+ it("in an empty block below a block without content deletes only the empty block", () => {
721
+ const editor = createEditorWithBlocks(
722
+ [
723
+ { id: "image", type: "image" },
724
+ { id: "empty", type: "paragraph" },
725
+ ],
726
+ { id: "empty", placement: "start" },
727
+ );
728
+
729
+ pressKeys(editor, "Backspace");
730
+
731
+ expect(editor.document.map((block) => block.id)).toEqual(["image"]);
732
+ editor._tiptapEditor.destroy();
733
+ });
734
+ it("keeps the default of a keyboard setting given as undefined", () => {
735
+ const editor = createEditorWithBlocks(
736
+ [{ id: "a", type: "conditionalKeyboard", content: "Text" }],
737
+ { id: "a", placement: "start" },
738
+ );
132
739
 
740
+ // `resetsTo` is undefined for this block, so the default applies.
741
+ pressKeys(editor, "Backspace");
742
+
743
+ expect(editor.getBlock("a")!.type).toBe("paragraph");
744
+ editor._tiptapEditor.destroy();
745
+ });
746
+ });
747
+
748
+ describe("KeyboardShortcutsExtension Delete", () => {
749
+ it("merges the next block in at block end", () => {
750
+ const editor = createEditorWithBlocks(
751
+ [
752
+ { id: "a", type: "paragraph", content: "Hello" },
753
+ { id: "b", type: "paragraph", content: "World" },
754
+ ],
755
+ { id: "a", placement: "end" },
756
+ );
757
+
758
+ pressKeys(editor, "Delete");
759
+
760
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
761
+ [
762
+ {
763
+ "text": "HelloWorld",
764
+ "type": "paragraph",
765
+ },
766
+ ]
767
+ `);
768
+ editor._tiptapEditor.destroy();
769
+ });
770
+
771
+ it("merges a next block that has children, un-nesting them", () => {
772
+ const editor = createEditorWithBlocks(
773
+ [
774
+ { id: "a", type: "paragraph", content: "Hello" },
775
+ {
776
+ id: "b",
777
+ type: "paragraph",
778
+ content: "World",
779
+ children: [
780
+ { id: "b1", type: "paragraph", content: "Child 1" },
781
+ { id: "b2", type: "paragraph", content: "Child 2" },
782
+ ],
783
+ },
784
+ ],
785
+ { id: "a", placement: "end" },
786
+ );
787
+
788
+ pressKeys(editor, "Delete");
789
+
790
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
791
+ [
792
+ {
793
+ "text": "HelloWorld",
794
+ "type": "paragraph",
795
+ },
796
+ {
797
+ "text": "Child 1",
798
+ "type": "paragraph",
799
+ },
800
+ {
801
+ "text": "Child 2",
802
+ "type": "paragraph",
803
+ },
804
+ ]
805
+ `);
806
+ editor._tiptapEditor.destroy();
807
+ });
808
+
809
+ it("removes an empty next block, adopting its children", () => {
810
+ const editor = createEditorWithBlocks(
811
+ [
812
+ { id: "a", type: "paragraph", content: "Hello" },
813
+ {
814
+ id: "b",
815
+ type: "paragraph",
816
+ content: "",
817
+ children: [{ id: "b1", type: "paragraph", content: "Child" }],
818
+ },
819
+ ],
820
+ { id: "a", placement: "end" },
821
+ );
822
+
823
+ pressKeys(editor, "Delete");
824
+
825
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
826
+ [
827
+ {
828
+ "text": "Hello",
829
+ "type": "paragraph",
830
+ },
831
+ {
832
+ "text": "Child",
833
+ "type": "paragraph",
834
+ },
835
+ ]
836
+ `);
837
+ editor._tiptapEditor.destroy();
838
+ });
839
+
840
+ it("removes an empty current block on Delete", () => {
841
+ const editor = createEditorWithBlocks(
842
+ [
843
+ { id: "a", type: "paragraph", content: "" },
844
+ { id: "b", type: "paragraph", content: "After" },
845
+ ],
846
+ { id: "a", placement: "start" },
847
+ );
848
+
849
+ pressKeys(editor, "Delete");
850
+
851
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
852
+ [
853
+ {
854
+ "text": "After",
855
+ "type": "paragraph",
856
+ },
857
+ ]
858
+ `);
133
859
  editor._tiptapEditor.destroy();
134
860
  });
861
+ });
135
862
 
136
- it('inserts a hard break on Enter when hardBreakShortcut is "enter"', () => {
137
- const editor = createEditor("hardBreakEnter");
863
+ describe("KeyboardShortcutsExtension Enter", () => {
864
+ it("lifts an empty nested block on Enter", () => {
865
+ const editor = createEditorWithBlocks(
866
+ [
867
+ {
868
+ id: "a",
869
+ type: "paragraph",
870
+ content: "Parent",
871
+ children: [{ id: "a1", type: "paragraph", content: "" }],
872
+ },
873
+ ],
874
+ { id: "a1", placement: "start" },
875
+ );
138
876
 
139
877
  pressKeys(editor, "Enter");
140
878
 
141
- expect(countHardBreaks(editor)).toBe(1);
142
- expect(editor.document.length).toBe(1);
879
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
880
+ [
881
+ {
882
+ "text": "Parent",
883
+ "type": "paragraph",
884
+ },
885
+ {
886
+ "text": "",
887
+ "type": "paragraph",
888
+ },
889
+ ]
890
+ `);
891
+ editor._tiptapEditor.destroy();
892
+ });
893
+ });
894
+
895
+ describe("KeyboardShortcutsExtension Shift-Tab", () => {
896
+ it("un-nests a nested block", () => {
897
+ const editor = createEditorWithBlocks(
898
+ [
899
+ {
900
+ id: "a",
901
+ type: "paragraph",
902
+ content: "Parent",
903
+ children: [{ id: "a1", type: "paragraph", content: "Nested" }],
904
+ },
905
+ ],
906
+ { id: "a1", placement: "start" },
907
+ );
908
+
909
+ pressKeys(editor, "Shift-Tab");
143
910
 
911
+ expect(outline(editor.document)).toMatchInlineSnapshot(`
912
+ [
913
+ {
914
+ "text": "Parent",
915
+ "type": "paragraph",
916
+ },
917
+ {
918
+ "text": "Nested",
919
+ "type": "paragraph",
920
+ },
921
+ ]
922
+ `);
144
923
  editor._tiptapEditor.destroy();
145
924
  });
925
+ });
146
926
 
147
- it('inserts a hard break on Shift-Enter when hardBreakShortcut is "enter"', () => {
148
- const editor = createEditor("hardBreakEnter");
927
+ describe("KeyboardShortcutsExtension line breaks", () => {
928
+ it("inserts a hard break on Shift-Enter by default", () => {
929
+ const editor = createEditor("paragraph");
149
930
 
150
931
  pressKeys(editor, "Shift-Enter");
151
932
 
@@ -155,18 +936,35 @@ describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
155
936
  editor._tiptapEditor.destroy();
156
937
  });
157
938
 
158
- it('does not insert a hard break on Shift-Enter when hardBreakShortcut is "none"', () => {
159
- const editor = createEditor("hardBreakNone");
160
-
161
- pressKeys(editor, "Shift-Enter");
939
+ // #1672: text typed after the line break used to lose the styles of the
940
+ // text before it.
941
+ it("keeps the styles of the text before the line break", () => {
942
+ const editor = createEditorWithBlocks(
943
+ [
944
+ {
945
+ id: "p",
946
+ type: "paragraph",
947
+ content: [
948
+ { type: "text", text: "Red", styles: { textColor: "red" } },
949
+ ],
950
+ },
951
+ ],
952
+ { id: "p", placement: "end" },
953
+ );
162
954
 
163
- expect(countHardBreaks(editor)).toBe(0);
955
+ keyDown(editor, { key: "Enter", keyCode: 13, shiftKey: true });
956
+ // Typing: `insertText` takes the stored marks, as the browser input does.
957
+ const view = editor._tiptapEditor.view;
958
+ view.dispatch(view.state.tr.insertText("x"));
164
959
 
960
+ expect(editor.getBlock("p")!.content).toEqual([
961
+ { type: "text", text: "Red\nx", styles: { textColor: "red" } },
962
+ ]);
165
963
  editor._tiptapEditor.destroy();
166
964
  });
167
965
 
168
- it('splits the block on Enter when hardBreakShortcut is "none"', () => {
169
- const editor = createEditor("hardBreakNone");
966
+ it("splits the block on Enter by default", () => {
967
+ const editor = createEditor("paragraph");
170
968
 
171
969
  pressKeys(editor, "Enter");
172
970
 
@@ -175,30 +973,898 @@ describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
175
973
 
176
974
  editor._tiptapEditor.destroy();
177
975
  });
976
+ });
178
977
 
179
- it('inserts a newline character on Enter when content is "plain"', () => {
180
- const editor = createEditor("hardBreakEnterPlain");
978
+ describe.each([
979
+ {
980
+ setting: "meta.hardBreakShortcut (deprecated)",
981
+ enter: "hardBreakEnter",
982
+ none: "hardBreakNone",
983
+ plain: "hardBreakEnterPlain",
984
+ },
985
+ {
986
+ setting: "keyboard",
987
+ enter: "keyboardEnter",
988
+ none: "keyboardNone",
989
+ plain: "keyboardEnterPlain",
990
+ },
991
+ ] as const)(
992
+ "hard breaks configured with $setting",
993
+ ({ enter, none, plain }) => {
994
+ it("inserts a hard break on Enter when Enter makes line breaks", () => {
995
+ const editor = createEditor(enter);
181
996
 
182
- pressKeys(editor, "Enter");
997
+ pressKeys(editor, "Enter");
183
998
 
184
- // A "plain" block can't hold a `hardBreak` node, so no node is inserted and
185
- // the block is not split - a literal newline is added to its text instead.
186
- expect(countHardBreaks(editor)).toBe(0);
187
- expect(editor.document.length).toBe(1);
188
- expect(getTextContent(editor)).toBe("Hello world\n");
999
+ expect(countHardBreaks(editor)).toBe(1);
1000
+ expect(editor.document.length).toBe(1);
1001
+
1002
+ editor._tiptapEditor.destroy();
1003
+ });
1004
+
1005
+ it("inserts a hard break on Shift-Enter when Enter makes line breaks", () => {
1006
+ const editor = createEditor(enter);
1007
+
1008
+ pressKeys(editor, "Shift-Enter");
1009
+
1010
+ expect(countHardBreaks(editor)).toBe(1);
1011
+ expect(editor.document.length).toBe(1);
1012
+
1013
+ editor._tiptapEditor.destroy();
1014
+ });
1015
+
1016
+ it("does not insert a hard break on Shift-Enter when it acts as Enter", () => {
1017
+ const editor = createEditor(none);
1018
+
1019
+ pressKeys(editor, "Shift-Enter");
1020
+
1021
+ expect(countHardBreaks(editor)).toBe(0);
1022
+
1023
+ editor._tiptapEditor.destroy();
1024
+ });
1025
+
1026
+ it("splits the block on Enter when Shift-Enter acts as Enter", () => {
1027
+ const editor = createEditor(none);
1028
+
1029
+ pressKeys(editor, "Enter");
1030
+
1031
+ expect(countHardBreaks(editor)).toBe(0);
1032
+ expect(editor.document.length).toBe(2);
1033
+
1034
+ editor._tiptapEditor.destroy();
1035
+ });
1036
+
1037
+ it('inserts a newline character on Enter when content is "plain"', () => {
1038
+ const editor = createEditor(plain);
1039
+
1040
+ pressKeys(editor, "Enter");
1041
+
1042
+ // A "plain" block can't hold a `hardBreak` node, so no node is inserted and
1043
+ // the block is not split - a literal newline is added to its text instead.
1044
+ expect(countHardBreaks(editor)).toBe(0);
1045
+ expect(editor.document.length).toBe(1);
1046
+ expect(getTextContent(editor)).toBe("Hello world\n");
189
1047
 
1048
+ editor._tiptapEditor.destroy();
1049
+ });
1050
+
1051
+ it('inserts a newline character on Shift-Enter when content is "plain"', () => {
1052
+ const editor = createEditor(plain);
1053
+
1054
+ pressKeys(editor, "Shift-Enter");
1055
+
1056
+ expect(countHardBreaks(editor)).toBe(0);
1057
+ expect(editor.document.length).toBe(1);
1058
+ expect(getTextContent(editor)).toBe("Hello world\n");
1059
+
1060
+ editor._tiptapEditor.destroy();
1061
+ });
1062
+ },
1063
+ );
1064
+
1065
+ describe("Delete preserves the caret before appended text", () => {
1066
+ function paragraph(id: string, children: PartialBlock[] = []): PartialBlock {
1067
+ return { id, type: "paragraph", content: id, children };
1068
+ }
1069
+
1070
+ it.each([
1071
+ {
1072
+ name: "sole child",
1073
+ initialContent: [paragraph("selected", [paragraph("removed")])],
1074
+ },
1075
+ {
1076
+ name: "following shallower block",
1077
+ initialContent: [
1078
+ paragraph("parent", [paragraph("selected")]),
1079
+ paragraph("removed"),
1080
+ ],
1081
+ },
1082
+ ])("$name", ({ initialContent }) => {
1083
+ const editor = BlockNoteEditor.create({ initialContent });
1084
+ editor.mount(document.createElement("div"));
1085
+ editor.setTextCursorPosition("selected", "end");
1086
+
1087
+ const view = editor.prosemirrorView;
1088
+ const event = new KeyboardEvent("keydown", {
1089
+ key: "Delete",
1090
+ code: "Delete",
1091
+ keyCode: 46,
1092
+ });
1093
+ view.someProp("handleKeyDown", (handler) => handler(view, event));
1094
+
1095
+ expect(editor.getBlock("removed")).toBeUndefined();
1096
+ expect(editor.getTextCursorPosition().block.id).toBe("selected");
1097
+ expect(editor.prosemirrorState.selection.$from.parentOffset).toBe(
1098
+ "selected".length,
1099
+ );
190
1100
  editor._tiptapEditor.destroy();
191
1101
  });
1102
+ });
192
1103
 
193
- it('inserts a newline character on Shift-Enter when content is "plain"', () => {
194
- const editor = createEditor("hardBreakEnterPlain");
1104
+ describe("KeyboardShortcutsExtension Backspace", () => {
1105
+ let editor: BlockNoteEditor | undefined;
195
1106
 
196
- pressKeys(editor, "Shift-Enter");
1107
+ afterEach(() => {
1108
+ editor?._tiptapEditor.destroy();
1109
+ editor = undefined;
1110
+ });
197
1111
 
198
- expect(countHardBreaks(editor)).toBe(0);
199
- expect(editor.document.length).toBe(1);
200
- expect(getTextContent(editor)).toBe("Hello world\n");
1112
+ function createEditor(
1113
+ content: string,
1114
+ type:
1115
+ | "paragraph"
1116
+ | "bulletListItem"
1117
+ | "numberedListItem"
1118
+ | "checkListItem" = "paragraph",
1119
+ ) {
1120
+ const instance = BlockNoteEditor.create({
1121
+ trailingBlock: false,
1122
+ initialContent: [
1123
+ {
1124
+ id: "parent",
1125
+ content: "Parent",
1126
+ children: [
1127
+ { id: "before", content: "Before" },
1128
+ { id: "current", type, content },
1129
+ { id: "after", content: "After" },
1130
+ ],
1131
+ },
1132
+ ],
1133
+ });
1134
+ editor = instance;
1135
+ instance.mount(document.createElement("div"));
1136
+ instance.setTextCursorPosition("current", "start");
1137
+ return instance;
1138
+ }
201
1139
 
202
- editor._tiptapEditor.destroy();
1140
+ function pressKey(
1141
+ instance: BlockNoteEditor,
1142
+ key: "Backspace" | "Shift-Tab" | "Delete",
1143
+ ) {
1144
+ const event = new KeyboardEvent("keydown", {
1145
+ key: key === "Shift-Tab" ? "Tab" : key,
1146
+ shiftKey: key === "Shift-Tab",
1147
+ bubbles: true,
1148
+ cancelable: true,
1149
+ });
1150
+ const view = instance.prosemirrorView;
1151
+ view.someProp("handleKeyDown", (handler) => handler(view, event));
1152
+ }
1153
+
1154
+ function expectUnindented(instance: BlockNoteEditor) {
1155
+ expect(instance.document.map((block) => block.id)).toEqual([
1156
+ "parent",
1157
+ "current",
1158
+ ]);
1159
+ expect(
1160
+ instance.getBlock("parent")?.children.map((block) => block.id),
1161
+ ).toEqual(["before"]);
1162
+ expect(
1163
+ instance.getBlock("current")?.children.map((block) => block.id),
1164
+ ).toEqual(["after"]);
1165
+ }
1166
+
1167
+ describe.each(["", "Current"])("Backspace with content %j", (content) => {
1168
+ it("merges without changing following siblings' nesting", () => {
1169
+ const instance = createEditor(content);
1170
+ pressKey(instance, "Backspace");
1171
+
1172
+ expect(instance.document.map((block) => block.id)).toEqual(["parent"]);
1173
+ expect(
1174
+ instance.getBlock("parent")?.children.map((block) => block.id),
1175
+ ).toEqual(["before", "after"]);
1176
+ expect(instance.getBlock("current")).toBeUndefined();
1177
+ expect(instance.getBlock("before")?.content).toEqual([
1178
+ { type: "text", text: `Before${content}`, styles: {} },
1179
+ ]);
1180
+ expect(instance.getTextCursorPosition().block.id).toBe("before");
1181
+ });
1182
+ });
1183
+
1184
+ it("still unindents with Shift-Tab", () => {
1185
+ const instance = createEditor("Current");
1186
+ pressKey(instance, "Shift-Tab");
1187
+ expectUnindented(instance);
1188
+ });
1189
+
1190
+ it.each(["bulletListItem", "numberedListItem", "checkListItem"] as const)(
1191
+ "converts %s to a paragraph before merging",
1192
+ (type) => {
1193
+ const instance = createEditor("Current", type);
1194
+ pressKey(instance, "Backspace");
1195
+ expect(instance.getBlock("current")?.type).toBe("paragraph");
1196
+ expect(
1197
+ instance.getBlock("parent")?.children.map((block) => block.id),
1198
+ ).toEqual(["before", "current", "after"]);
1199
+ pressKey(instance, "Backspace");
1200
+ expect(instance.getBlock("current")).toBeUndefined();
1201
+ expect(instance.getBlock("before")?.content).toEqual([
1202
+ { type: "text", text: "BeforeCurrent", styles: {} },
1203
+ ]);
1204
+ },
1205
+ );
1206
+
1207
+ it("does not merge when the cursor is inside the block", () => {
1208
+ const instance = createEditor("Current");
1209
+ instance.setTextCursorPosition("current", "end");
1210
+ // The shortcut must leave normal character deletion to the browser.
1211
+ pressKey(instance, "Backspace");
1212
+ expect(
1213
+ instance.getBlock("parent")?.children.map((block) => block.id),
1214
+ ).toEqual(["before", "current", "after"]);
1215
+ expect(instance.getBlock("current")?.content).toEqual([
1216
+ { type: "text", text: "Current", styles: {} },
1217
+ ]);
1218
+ });
1219
+
1220
+ function createScenario(initialContent: PartialBlock[]) {
1221
+ const instance = createEditor("");
1222
+ instance.replaceBlocks(instance.document, initialContent);
1223
+ instance.setTextCursorPosition("current", "start");
1224
+ return instance;
1225
+ }
1226
+
1227
+ describe.each(["paragraph", "image", "table"] as const)(
1228
+ "first child of %s",
1229
+ (type) => {
1230
+ it.each([false, true])(
1231
+ "deletes an empty first child (only child: %s)",
1232
+ (onlyChild) => {
1233
+ const instance = createScenario([
1234
+ {
1235
+ id: "parent",
1236
+ ...parentSpec(type),
1237
+ children: [
1238
+ { id: "current", content: "" },
1239
+ ...(onlyChild ? [] : [{ id: "after", content: "After" }]),
1240
+ ],
1241
+ },
1242
+ ]);
1243
+ pressKey(instance, "Backspace");
1244
+ expect(instance.getBlock("current")).toBeUndefined();
1245
+ expect(
1246
+ instance.getBlock("parent")?.children.map((block) => block.id),
1247
+ ).toEqual(onlyChild ? [] : ["after"]);
1248
+ expect(instance.getBlock("parent")?.type).toBe(type);
1249
+ expectSelectionAtEnd(instance, "parent", type);
1250
+ instance.prosemirrorView.state.doc.check();
1251
+ },
1252
+ );
1253
+
1254
+ it("preserves descendants when removing an empty first child", () => {
1255
+ const instance = createScenario([
1256
+ {
1257
+ id: "parent",
1258
+ ...parentSpec(type),
1259
+ children: [
1260
+ {
1261
+ id: "current",
1262
+ children: [{ id: "grandchild", content: "Keep me" }],
1263
+ },
1264
+ { id: "after", content: "After" },
1265
+ ],
1266
+ },
1267
+ ]);
1268
+ pressKey(instance, "Backspace");
1269
+ expect(instance.getBlock("current")).toBeUndefined();
1270
+ expect(
1271
+ instance.getBlock("parent")?.children.map((block) => block.id),
1272
+ ).toEqual(["grandchild", "after"]);
1273
+ expect(instance.getBlock("grandchild")?.content).toEqual([
1274
+ { type: "text", text: "Keep me", styles: {} },
1275
+ ]);
1276
+ instance.prosemirrorView.state.doc.check();
1277
+ expectSelectionAtEnd(instance, "parent", type);
1278
+ });
1279
+ },
1280
+ );
1281
+
1282
+ it("merges into a paragraph nested under a non-text previous sibling", () => {
1283
+ const instance = createScenario([
1284
+ {
1285
+ id: "parent",
1286
+ content: "Parent",
1287
+ children: [
1288
+ {
1289
+ id: "image",
1290
+ type: "image",
1291
+ children: [{ id: "target", content: "Target" }],
1292
+ },
1293
+ { id: "current", content: "Current" },
1294
+ { id: "after", content: "After" },
1295
+ ],
1296
+ },
1297
+ ]);
1298
+ pressKey(instance, "Backspace");
1299
+ expect(instance.getBlock("current")).toBeUndefined();
1300
+ expect(instance.getBlock("target")?.content).toEqual([
1301
+ { type: "text", text: "TargetCurrent", styles: {} },
1302
+ ]);
1303
+ expect(
1304
+ instance.getBlock("parent")?.children.map((block) => block.id),
1305
+ ).toEqual(["image", "after"]);
1306
+ });
1307
+
1308
+ it("keeps the only root block", () => {
1309
+ const instance = createScenario([{ id: "current", content: "" }]);
1310
+ pressKey(instance, "Backspace");
1311
+ expect(instance.document.map((block) => block.id)).toEqual(["current"]);
1312
+ });
1313
+
1314
+ it.each(["", "Parent"])(
1315
+ "merges the first child's text into parent %j",
1316
+ (content) => {
1317
+ const instance = createScenario([
1318
+ {
1319
+ id: "parent",
1320
+ content,
1321
+ children: [
1322
+ {
1323
+ id: "current",
1324
+ content: "Current",
1325
+ children: [{ id: "grandchild", content: "Keep" }],
1326
+ },
1327
+ { id: "after", content: "After" },
1328
+ ],
1329
+ },
1330
+ ]);
1331
+ pressKey(instance, "Backspace");
1332
+ expect(instance.getBlock("current")).toBeUndefined();
1333
+ expect(instance.getBlock("parent")?.content).toEqual([
1334
+ { type: "text", text: `${content}Current`, styles: {} },
1335
+ ]);
1336
+ expect(
1337
+ instance.getBlock("parent")?.children.map((block) => block.id),
1338
+ ).toEqual(["grandchild", "after"]);
1339
+ expect(instance.getTextCursorPosition().block.id).toBe("parent");
1340
+ expect(instance.prosemirrorView.state.selection.$from.parentOffset).toBe(
1341
+ content.length,
1342
+ );
1343
+ },
1344
+ );
1345
+
1346
+ it("unindents nonempty first children of non-text parents without losing text", () => {
1347
+ const instance = createScenario([
1348
+ {
1349
+ id: "parent",
1350
+ type: "image",
1351
+ children: [{ id: "current", content: "Keep" }],
1352
+ },
1353
+ ]);
1354
+ pressKey(instance, "Backspace");
1355
+ expect(instance.document.map((block) => block.id)).toEqual([
1356
+ "parent",
1357
+ "current",
1358
+ ]);
1359
+ expect(instance.getBlock("current")?.content).toEqual([
1360
+ { type: "text", text: "Keep", styles: {} },
1361
+ ]);
1362
+ });
1363
+
1364
+ it.each(["", "Current"])(
1365
+ "handles an empty previous sibling with current text %j",
1366
+ (content) => {
1367
+ const instance = createScenario([
1368
+ {
1369
+ id: "parent",
1370
+ content: "Parent",
1371
+ children: [
1372
+ { id: "before", content: "", props: { textAlignment: "right" } },
1373
+ { id: "current", content },
1374
+ { id: "after", content: "After" },
1375
+ ],
1376
+ },
1377
+ ]);
1378
+ pressKey(instance, "Backspace");
1379
+ expect(
1380
+ instance.getBlock("parent")?.children.map((block) => block.id),
1381
+ ).toEqual(["before", "after"]);
1382
+ instance.prosemirrorView.state.doc.check();
1383
+ expect(instance.getTextCursorPosition().block.id).toBe("before");
1384
+ expect(instance.getBlock("before")?.props).toMatchObject({
1385
+ textAlignment: "right",
1386
+ });
1387
+ expect(instance.getBlock("before")?.content).toEqual(
1388
+ content ? [{ type: "text", text: content, styles: {} }] : [],
1389
+ );
1390
+ expect(instance.prosemirrorView.state.selection.$from.parentOffset).toBe(
1391
+ 0,
1392
+ );
1393
+ },
1394
+ );
1395
+
1396
+ it("unindents nonempty blocks after a table when merging is impossible", () => {
1397
+ const instance = createScenario([
1398
+ {
1399
+ id: "parent",
1400
+ content: "Parent",
1401
+ children: [
1402
+ {
1403
+ id: "table",
1404
+ type: "table",
1405
+ content: { type: "tableContent", rows: [{ cells: ["Cell"] }] },
1406
+ },
1407
+ { id: "current", content: "Keep" },
1408
+ ],
1409
+ },
1410
+ ]);
1411
+ pressKey(instance, "Backspace");
1412
+ expect(instance.document.map((block) => block.id)).toEqual([
1413
+ "parent",
1414
+ "current",
1415
+ ]);
1416
+ expect(instance.getBlock("table")?.type).toBe("table");
1417
+ expect(instance.getBlock("current")?.content).toEqual([
1418
+ { type: "text", text: "Keep", styles: {} },
1419
+ ]);
1420
+ });
1421
+
1422
+ it.each(["image", "table"] as const)(
1423
+ "deletes empty blocks after %s while preserving descendants",
1424
+ (type) => {
1425
+ const previous: PartialBlock =
1426
+ type === "table"
1427
+ ? {
1428
+ id: "previous",
1429
+ type,
1430
+ content: { type: "tableContent", rows: [{ cells: ["Cell"] }] },
1431
+ }
1432
+ : { id: "previous", type };
1433
+ const instance = createScenario([
1434
+ {
1435
+ id: "parent",
1436
+ content: "Parent",
1437
+ children: [
1438
+ previous,
1439
+ {
1440
+ id: "current",
1441
+ children: [{ id: "grandchild", content: "Keep" }],
1442
+ },
1443
+ { id: "after", content: "After" },
1444
+ ],
1445
+ },
1446
+ ]);
1447
+ pressKey(instance, "Backspace");
1448
+ expect(instance.getBlock("current")).toBeUndefined();
1449
+ expect(
1450
+ instance.getBlock("parent")?.children.map((block) => block.id),
1451
+ ).toEqual(["previous", "grandchild", "after"]);
1452
+ expectSelectionAtEnd(instance, "previous", type);
1453
+ expect(instance.getBlock("grandchild")?.content).toEqual([
1454
+ { type: "text", text: "Keep", styles: {} },
1455
+ ]);
1456
+ instance.prosemirrorView.state.doc.check();
1457
+ },
1458
+ );
1459
+
1460
+ it("merges a deeply nested only child and can undo the operation", () => {
1461
+ const instance = createScenario([
1462
+ {
1463
+ id: "root",
1464
+ content: "Root",
1465
+ children: [
1466
+ {
1467
+ id: "parent",
1468
+ content: "Parent",
1469
+ children: [{ id: "current", content: "Current" }],
1470
+ },
1471
+ ],
1472
+ },
1473
+ ]);
1474
+ const before = instance.document;
1475
+ instance.prosemirrorView.dispatch(
1476
+ closeHistory(instance.prosemirrorView.state.tr),
1477
+ );
1478
+ pressKey(instance, "Backspace");
1479
+ expect(instance.getBlock("current")).toBeUndefined();
1480
+ expect(instance.getBlock("parent")?.children).toEqual([]);
1481
+ expect(instance.getBlock("parent")?.content).toEqual([
1482
+ { type: "text", text: "ParentCurrent", styles: {} },
1483
+ ]);
1484
+ expect(instance.prosemirrorView.state.selection.$from.parentOffset).toBe(6);
1485
+ instance.undo();
1486
+ expect(instance.document).toEqual(before);
1487
+ });
1488
+
1489
+ it.each(["", "Current"])(
1490
+ "the merge command handles a first child with text %j directly",
1491
+ (content) => {
1492
+ const instance = createScenario([
1493
+ {
1494
+ id: "parent",
1495
+ content: "Parent",
1496
+ children: [
1497
+ { id: "current", content },
1498
+ { id: "after", content: "After" },
1499
+ ],
1500
+ },
1501
+ ]);
1502
+ const pos = getBlockInfoFromSelection(instance.prosemirrorView.state)
1503
+ .block.beforePos;
1504
+ const command = mergeBlocksCommand(pos);
1505
+ const before = instance.document;
1506
+ expect(instance._tiptapEditor.can().command(command)).toBe(true);
1507
+ expect(instance.document).toEqual(before);
1508
+ expect(instance._tiptapEditor.commands.command(command)).toBe(true);
1509
+ expect(instance.getBlock("current")).toBeUndefined();
1510
+ expect(instance.getBlock("parent")?.content).toEqual([
1511
+ { type: "text", text: `Parent${content}`, styles: {} },
1512
+ ]);
1513
+ expect(
1514
+ instance.getBlock("parent")?.children.map((block) => block.id),
1515
+ ).toEqual(["after"]);
1516
+ },
1517
+ );
1518
+
1519
+ it("the merge command rejects incompatible parents without mutating", () => {
1520
+ const instance = createScenario([
1521
+ {
1522
+ id: "parent",
1523
+ type: "image",
1524
+ children: [{ id: "current", content: "Keep" }],
1525
+ },
1526
+ ]);
1527
+ const pos = getBlockInfoFromSelection(instance.prosemirrorView.state).block
1528
+ .beforePos;
1529
+ const before = instance.document;
1530
+ expect(
1531
+ instance._tiptapEditor.commands.command(mergeBlocksCommand(pos)),
1532
+ ).toBe(false);
1533
+ expect(instance.document).toEqual(before);
1534
+ });
1535
+
1536
+ it.each(["image", "table"] as const)(
1537
+ "the merge command rejects %s on either side and as a parent",
1538
+ (type) => {
1539
+ const nonText: PartialBlock =
1540
+ type === "image"
1541
+ ? { id: "non-text", type }
1542
+ : {
1543
+ id: "non-text",
1544
+ type,
1545
+ content: { type: "tableContent", rows: [{ cells: ["Cell"] }] },
1546
+ };
1547
+ const instance = createScenario([{ id: "current", content: "" }]);
1548
+ for (const content of ["", "Text"]) {
1549
+ for (const blocks of [
1550
+ [nonText, { id: "current", content }],
1551
+ [
1552
+ { id: "text", content },
1553
+ { ...nonText, id: "current" },
1554
+ ],
1555
+ [{ ...nonText, children: [{ id: "current", content }] }],
1556
+ ]) {
1557
+ instance.replaceBlocks(instance.document, blocks);
1558
+ const pos = instance.prosemirrorView.state.doc;
1559
+ let beforePos: number | undefined;
1560
+ pos.descendants((node, position) => {
1561
+ if (node.attrs.id === "current") {
1562
+ beforePos = position;
1563
+ }
1564
+ });
1565
+ if (beforePos === undefined) {
1566
+ throw new Error("Missing test block");
1567
+ }
1568
+ const before = instance.document;
1569
+ const command = mergeBlocksCommand(beforePos);
1570
+ expect(instance._tiptapEditor.can().command(command)).toBe(false);
1571
+ expect(instance._tiptapEditor.commands.command(command)).toBe(false);
1572
+ expect(instance.document).toEqual(before);
1573
+ }
1574
+ }
1575
+ },
1576
+ );
1577
+
1578
+ function expectSelectionAtEnd(
1579
+ instance: BlockNoteEditor,
1580
+ id: string,
1581
+ type: "paragraph" | "image" | "table",
1582
+ ) {
1583
+ expect(instance.getTextCursorPosition().block.id).toBe(id);
1584
+ const selection = instance.prosemirrorView.state.selection;
1585
+ if (type === "image") {
1586
+ expect(selection).toBeInstanceOf(NodeSelection);
1587
+ } else {
1588
+ expect(selection).toBeInstanceOf(TextSelection);
1589
+ expect(selection.empty).toBe(true);
1590
+ expect(selection.$from.parentOffset).toBe(
1591
+ selection.$from.parent.content.size,
1592
+ );
1593
+ if (type === "table") {
1594
+ expect(["Cell", "Last"]).toContain(selection.$from.parent.textContent);
1595
+ expect(
1596
+ selection.$from.node(selection.$from.depth - 1).type.name,
1597
+ ).toMatch(/table.*Cell/i);
1598
+ }
1599
+ }
1600
+ }
1601
+
1602
+ it("replaces a preceding image with a nonempty paragraph and keeps its descendants", () => {
1603
+ const instance = createScenario([
1604
+ {
1605
+ id: "parent",
1606
+ content: "Parent",
1607
+ children: [
1608
+ { id: "image", type: "image" },
1609
+ {
1610
+ id: "current",
1611
+ content: "Keep",
1612
+ children: [{ id: "child", content: "Child" }],
1613
+ },
1614
+ { id: "after", content: "After" },
1615
+ ],
1616
+ },
1617
+ ]);
1618
+ const current = instance.getBlock("current");
1619
+ pressKey(instance, "Backspace");
1620
+ expect(instance.getBlock("image")).toBeUndefined();
1621
+ expect(instance.getBlock("current")).toEqual(current);
1622
+ expect(
1623
+ instance.getBlock("parent")?.children.map((block) => block.id),
1624
+ ).toEqual(["current", "after"]);
1625
+ expect(instance.getTextCursorPosition().block.id).toBe("current");
1626
+ expect(instance.prosemirrorView.state.selection.$from.parentOffset).toBe(0);
1627
+ });
1628
+
1629
+ it("deletes selected text before attempting to merge or unindent", () => {
1630
+ const instance = createEditor("Current");
1631
+ const start = instance.prosemirrorView.state.selection.from;
1632
+ instance.prosemirrorView.dispatch(
1633
+ instance.prosemirrorView.state.tr.setSelection(
1634
+ TextSelection.create(
1635
+ instance.prosemirrorView.state.doc,
1636
+ start,
1637
+ start + 3,
1638
+ ),
1639
+ ),
1640
+ );
1641
+ pressKey(instance, "Backspace");
1642
+ expect(instance.getBlock("current")?.content).toEqual([
1643
+ { type: "text", text: "rent", styles: {} },
1644
+ ]);
1645
+ expect(
1646
+ instance.getBlock("parent")?.children.map((block) => block.id),
1647
+ ).toEqual(["before", "current", "after"]);
1648
+ });
1649
+
1650
+ it("Delete still merges forward", () => {
1651
+ const instance = createEditor("Current");
1652
+ instance.setTextCursorPosition("before", "end");
1653
+ pressKey(instance, "Delete");
1654
+ expect(instance.getBlock("current")).toBeUndefined();
1655
+ expect(instance.getBlock("before")?.content).toEqual([
1656
+ { type: "text", text: "BeforeCurrent", styles: {} },
1657
+ ]);
1658
+ expect(
1659
+ instance.getBlock("parent")?.children.map((block) => block.id),
1660
+ ).toEqual(["before", "after"]);
1661
+ });
1662
+
1663
+ it.each(["image", "table"] as const)(
1664
+ "promotes descendants of an empty only child under %s and supports undo",
1665
+ (type) => {
1666
+ const instance = createScenario([
1667
+ {
1668
+ id: "parent",
1669
+ ...parentSpec(type),
1670
+ children: [
1671
+ {
1672
+ id: "current",
1673
+ children: [
1674
+ {
1675
+ id: "child",
1676
+ content: "Keep",
1677
+ children: [{ id: "grandchild", content: "Deep" }],
1678
+ },
1679
+ ],
1680
+ },
1681
+ ],
1682
+ },
1683
+ ]);
1684
+ const before = instance.document;
1685
+ const child = instance.getBlock("child");
1686
+ instance.prosemirrorView.dispatch(
1687
+ closeHistory(instance.prosemirrorView.state.tr),
1688
+ );
1689
+ pressKey(instance, "Backspace");
1690
+ expect(
1691
+ instance.getBlock("parent")?.children.map((block) => block.id),
1692
+ ).toEqual(["child"]);
1693
+ expect(instance.getBlock("child")).toEqual(child);
1694
+ expectSelectionAtEnd(instance, "parent", type);
1695
+ instance.prosemirrorView.state.doc.check();
1696
+ instance.undo();
1697
+ expect(instance.document).toEqual(before);
1698
+ },
1699
+ );
1700
+
1701
+ function parentSpec(type: "paragraph" | "image" | "table"): PartialBlock {
1702
+ if (type === "table") {
1703
+ return {
1704
+ type,
1705
+ content: { type: "tableContent", rows: [{ cells: ["First", "Last"] }] },
1706
+ };
1707
+ }
1708
+ return { type };
1709
+ }
1710
+
1711
+ it("selects the last cell of a deeply nested preceding table", () => {
1712
+ const instance = createScenario([
1713
+ {
1714
+ id: "parent",
1715
+ content: "Parent",
1716
+ children: [
1717
+ {
1718
+ id: "before",
1719
+ content: "Before",
1720
+ children: [
1721
+ {
1722
+ id: "table",
1723
+ type: "table",
1724
+ content: {
1725
+ type: "tableContent",
1726
+ rows: [{ cells: ["A", "B"] }, { cells: ["C", "Last"] }],
1727
+ },
1728
+ },
1729
+ ],
1730
+ },
1731
+ { id: "current" },
1732
+ { id: "after", content: "After" },
1733
+ ],
1734
+ },
1735
+ ]);
1736
+ pressKey(instance, "Backspace");
1737
+ expect(instance.getBlock("current")).toBeUndefined();
1738
+ expect(
1739
+ instance.getBlock("parent")?.children.map((block) => block.id),
1740
+ ).toEqual(["before", "after"]);
1741
+ expectSelectionAtEnd(instance, "table", "table");
1742
+ });
1743
+
1744
+ it("preserves inline styles and target properties when merging into the parent", () => {
1745
+ const instance = createScenario([
1746
+ {
1747
+ id: "parent",
1748
+ props: { textAlignment: "right" },
1749
+ content: [{ type: "text", text: "Parent", styles: { bold: true } }],
1750
+ children: [
1751
+ {
1752
+ id: "current",
1753
+ props: { textAlignment: "center" },
1754
+ content: [
1755
+ { type: "text", text: "Child", styles: { italic: true } },
1756
+ ],
1757
+ },
1758
+ ],
1759
+ },
1760
+ ]);
1761
+ pressKey(instance, "Backspace");
1762
+ expect(instance.getBlock("parent")?.props).toMatchObject({
1763
+ textAlignment: "right",
1764
+ });
1765
+ expect(instance.getBlock("parent")?.content).toEqual([
1766
+ { type: "text", text: "Parent", styles: { bold: true } },
1767
+ { type: "text", text: "Child", styles: { italic: true } },
1768
+ ]);
1769
+ expect(instance.getBlock("current")).toBeUndefined();
1770
+ });
1771
+
1772
+ it.each([false, true])(
1773
+ "matches PR examples 2 and 4 (previous block has children: %s)",
1774
+ (nestedTarget) => {
1775
+ const instance = createScenario([
1776
+ {
1777
+ id: "parent",
1778
+ content: "Parent",
1779
+ children: nestedTarget ? [{ id: "target", content: "Target" }] : [],
1780
+ },
1781
+ {
1782
+ id: "current",
1783
+ content: "Current",
1784
+ children: [{ id: "child", content: "Child" }],
1785
+ },
1786
+ ]);
1787
+ pressKey(instance, "Backspace");
1788
+ expect(instance.document.map((block) => block.id)).toEqual([
1789
+ "parent",
1790
+ "child",
1791
+ ]);
1792
+ const target = nestedTarget ? "target" : "parent";
1793
+ expect(instance.getBlock(target)?.content).toEqual([
1794
+ {
1795
+ type: "text",
1796
+ text: `${nestedTarget ? "Target" : "Parent"}Current`,
1797
+ styles: {},
1798
+ },
1799
+ ]);
1800
+ expect(instance.getTextCursorPosition().block.id).toBe(target);
1801
+ expect(instance.prosemirrorView.state.selection.$from.parentOffset).toBe(
1802
+ 6,
1803
+ );
1804
+ expect(instance.getBlock("child")?.content).toEqual([
1805
+ { type: "text", text: "Child", styles: {} },
1806
+ ]);
1807
+ },
1808
+ );
1809
+
1810
+ it("deletes a selected nested image", () => {
1811
+ const instance = createScenario([
1812
+ {
1813
+ id: "parent",
1814
+ content: "Parent",
1815
+ children: [
1816
+ { id: "current", type: "image" },
1817
+ { id: "after", content: "After" },
1818
+ ],
1819
+ },
1820
+ ]);
1821
+ const info = getBlockInfoFromSelection(instance.prosemirrorView.state);
1822
+ if (!info.hasContent) {
1823
+ throw new Error("Expected image block");
1824
+ }
1825
+ instance.prosemirrorView.dispatch(
1826
+ instance.prosemirrorView.state.tr.setSelection(
1827
+ NodeSelection.create(
1828
+ instance.prosemirrorView.state.doc,
1829
+ info.content.beforePos,
1830
+ ),
1831
+ ),
1832
+ );
1833
+ pressKey(instance, "Backspace");
1834
+ expect(instance.getBlock("current")).toBeUndefined();
1835
+ expect(
1836
+ instance.getBlock("parent")?.children.map((block) => block.id),
1837
+ ).toEqual(["after"]);
1838
+ });
1839
+
1840
+ it("does nothing at the start of a nested table cell", () => {
1841
+ const instance = createScenario([
1842
+ {
1843
+ id: "parent",
1844
+ content: "Parent",
1845
+ children: [
1846
+ { id: "current", ...parentSpec("table") },
1847
+ { id: "after", content: "After" },
1848
+ ],
1849
+ },
1850
+ ]);
1851
+ let pos: number | undefined;
1852
+ instance.prosemirrorView.state.doc.descendants((node, nodePos) => {
1853
+ if (node.isText && node.text === "First") {
1854
+ pos = nodePos;
1855
+ }
1856
+ });
1857
+ if (pos === undefined) {
1858
+ throw new Error("Expected table cell text");
1859
+ }
1860
+ instance.prosemirrorView.dispatch(
1861
+ instance.prosemirrorView.state.tr.setSelection(
1862
+ TextSelection.create(instance.prosemirrorView.state.doc, pos),
1863
+ ),
1864
+ );
1865
+ const before = instance.document;
1866
+ pressKey(instance, "Backspace");
1867
+ expect(instance.document).toEqual(before);
1868
+ expect(instance.prosemirrorView.state.selection.from).toBe(pos);
203
1869
  });
204
1870
  });