@blocknote/core 0.55.0 → 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 (460) 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-DaEUT0O1.js → blocks-COYcDVg7.js} +1572 -1156
  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.js +12 -12
  19. package/dist/{en-BAn9gxr-.js → en-Ch-vvt5e.js} +36 -2
  20. package/dist/en-Ch-vvt5e.js.map +1 -0
  21. package/dist/en-N5lu0-j8.cjs +2 -0
  22. package/dist/en-N5lu0-j8.cjs.map +1 -0
  23. package/dist/extensions-BbM81hHa.js +3069 -0
  24. package/dist/extensions-BbM81hHa.js.map +1 -0
  25. package/dist/extensions-D_WkAfSt.cjs +58 -0
  26. package/dist/extensions-D_WkAfSt.cjs.map +1 -0
  27. package/dist/extensions.cjs +1 -1
  28. package/dist/extensions.js +4 -4
  29. package/dist/locales.cjs +1 -1
  30. package/dist/locales.cjs.map +1 -1
  31. package/dist/locales.js +771 -23
  32. package/dist/locales.js.map +1 -1
  33. package/dist/src-4OQT3z5S.cjs +23 -0
  34. package/dist/src-4OQT3z5S.cjs.map +1 -0
  35. package/dist/{src-D5w0XAHX.js → src-BmLsLZfE.js} +1390 -1278
  36. package/dist/src-BmLsLZfE.js.map +1 -0
  37. package/dist/style.css +1 -1
  38. package/dist/webpack-stats.json +1 -1
  39. package/dist/y.cjs +1 -1
  40. package/dist/y.cjs.map +1 -1
  41. package/dist/y.js +1284 -554
  42. package/dist/y.js.map +1 -1
  43. package/dist/yjs.cjs +1 -1
  44. package/dist/yjs.cjs.map +1 -1
  45. package/dist/yjs.js +189 -148
  46. package/dist/yjs.js.map +1 -1
  47. package/package.json +8 -8
  48. package/src/api/blockManipulation/commands/insertBlocks/insertBlocks.ts +45 -6
  49. package/src/api/blockManipulation/commands/insertBlocks/insertPlacement.test.ts +217 -0
  50. package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.test.ts +113 -9
  51. package/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.ts +151 -163
  52. package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.test.ts +47 -31
  53. package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.ts +122 -181
  54. package/src/api/blockManipulation/commands/nestBlock/nestBlock.test.ts +256 -0
  55. package/src/api/blockManipulation/commands/nestBlock/nestBlock.ts +80 -40
  56. package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.test.ts +71 -0
  57. package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.ts +39 -31
  58. package/src/api/blockManipulation/commands/splitBlock/splitBlock.test.ts +221 -8
  59. package/src/api/blockManipulation/commands/splitBlock/splitBlock.ts +40 -8
  60. package/src/api/blockManipulation/commands/updateBlock/updateBlock.test.ts +272 -40
  61. package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +138 -117
  62. package/src/api/blockManipulation/getBlock/getBlock.ts +4 -11
  63. package/src/api/blockManipulation/layoutBlocks/fixLayoutBlock.ts +173 -0
  64. package/src/api/blockManipulation/layoutBlocks/layoutBlocks.browser.test.ts +293 -0
  65. package/src/api/blockManipulation/layoutBlocks/layoutBlocks.fixture.ts +145 -0
  66. package/src/api/blockManipulation/layoutBlocks/layoutBlocks.test.ts +783 -0
  67. package/src/api/blockManipulation/layoutBlocks/plainBlocks.test.ts +180 -0
  68. package/src/api/blockManipulation/layoutBlocks/titledBlocks.test.ts +365 -0
  69. package/src/api/blockManipulation/selections/selection.ts +20 -53
  70. package/src/api/blockManipulation/selections/textCursorPosition.ts +17 -72
  71. package/src/api/blockManipulation/setupTestEnv.ts +38 -6
  72. package/src/api/clipboard/fromClipboard/handleFileInsertion.ts +4 -4
  73. package/src/api/clipboard/toClipboard/copyExtension.ts +3 -3
  74. package/src/api/combineTransactionSteps.ts +22 -0
  75. package/src/api/exporters/html/externalHTMLExporter.ts +1 -1
  76. package/src/api/exporters/html/internalHTMLSerializer.ts +4 -4
  77. package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +111 -36
  78. package/src/api/exporters/html/util/serializeBlocksInternalHTML.ts +51 -21
  79. package/src/api/exporters/markdown/htmlToMarkdown.ts +12 -1
  80. package/src/api/exporters/markdown/markdownExporter.ts +1 -1
  81. package/src/api/getBlockInfoFromPos.test.ts +428 -3
  82. package/src/api/getBlockInfoFromPos.ts +531 -129
  83. package/src/api/getBlocksChangedByTransaction.test.ts +7 -7
  84. package/src/api/getBlocksChangedByTransaction.ts +3 -3
  85. package/src/api/{getChangedRange.ts → getChangedRangeWithAttrs.ts} +2 -2
  86. package/src/api/getChangedRanges.test.ts +391 -0
  87. package/src/api/getChangedRanges.ts +368 -0
  88. package/src/api/nodeConversions/blockToNode.ts +44 -16
  89. package/src/api/nodeConversions/fragmentToBlocks.ts +29 -44
  90. package/src/api/nodeConversions/nodeToBlock.ts +120 -68
  91. package/src/api/nodeUtil.ts +1 -1
  92. package/src/api/parsers/markdown/autolink.ts +23 -0
  93. package/src/api/parsers/markdown/markdownToHtml.ts +115 -11
  94. package/src/blocks/Code/block.test.ts +4 -4
  95. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +6 -1
  96. package/src/blocks/Heading/block.ts +54 -15
  97. package/src/blocks/ListItem/BulletListItem/block.ts +6 -4
  98. package/src/blocks/ListItem/CheckListItem/block.test.ts +2 -2
  99. package/src/blocks/ListItem/CheckListItem/block.ts +6 -4
  100. package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +14 -17
  101. package/src/blocks/ListItem/NumberedListItem/block.ts +6 -4
  102. package/src/blocks/ListItem/ToggleListItem/block.ts +23 -12
  103. package/src/blocks/Table/block.ts +2 -10
  104. package/src/blocks/ToggleWrapper/createToggleFrame.ts +126 -0
  105. package/src/blocks/ToggleWrapper/toggleBlocks.browser.test.ts +1056 -0
  106. package/src/blocks/defaultBlockTypeItems.test.ts +70 -0
  107. package/src/blocks/defaultBlockTypeItems.ts +99 -0
  108. package/src/blocks/defaultBlocks.test.ts +67 -0
  109. package/src/blocks/defaultBlocks.ts +7 -1
  110. package/src/blocks/index.ts +2 -1
  111. package/src/editor/Block.css +286 -154
  112. package/src/editor/BlockNoteEditor.node.test.ts +35 -0
  113. package/src/editor/BlockNoteEditor.test.ts +2 -2
  114. package/src/editor/BlockNoteEditor.ts +71 -9
  115. package/src/editor/BlockNoteExtension.ts +5 -5
  116. package/src/editor/blockColors.browser.test.ts +127 -0
  117. package/src/editor/editor.css +60 -0
  118. package/src/editor/managers/BlockManager.ts +4 -3
  119. package/src/editor/managers/ExtensionManager/ExtensionManager.browser.test.ts +45 -0
  120. package/src/editor/managers/ExtensionManager/ExtensionManager.node.test.ts +209 -0
  121. package/src/editor/managers/ExtensionManager/extensions.ts +10 -1
  122. package/src/editor/managers/ExtensionManager/index.ts +135 -68
  123. package/src/editor/managers/StateManager.ts +14 -4
  124. package/src/editor/transformPasted.ts +14 -13
  125. package/src/exporter/Exporter.test.ts +70 -1
  126. package/src/exporter/Exporter.ts +34 -4
  127. package/src/exporter/mapping.ts +41 -10
  128. package/src/extensions/DropCursor/DropCursor.ts +119 -6
  129. package/src/extensions/DropCursor/dropIntoChildren.ts +80 -0
  130. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +7 -1
  131. package/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboard.test.ts +190 -0
  132. package/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboard.ts +41 -4
  133. package/src/extensions/Placeholder/Placeholder.test.ts +23 -0
  134. package/src/extensions/Placeholder/Placeholder.ts +8 -7
  135. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +36 -0
  136. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +8 -4
  137. package/src/extensions/ReadOnly/ReadOnly.test.ts +181 -0
  138. package/src/extensions/ReadOnly/ReadOnly.ts +73 -0
  139. package/src/extensions/SideMenu/SideMenu.ts +401 -764
  140. package/src/extensions/SideMenu/blockDropHandling.ts +372 -0
  141. package/src/extensions/SideMenu/blockGeometry.browser.test.ts +148 -0
  142. package/src/extensions/SideMenu/blockGeometry.test.ts +46 -0
  143. package/src/extensions/SideMenu/blockGeometry.ts +53 -0
  144. package/src/extensions/SideMenu/dragging.ts +19 -32
  145. package/src/extensions/SideMenu/sideMenuHover.test.ts +36 -0
  146. package/src/extensions/SideMenu/sideMenuHover.ts +270 -0
  147. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +5 -4
  148. package/src/extensions/SuggestionMenu/SuggestionMenu.ts +10 -1
  149. package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +36 -136
  150. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +14 -4
  151. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +1 -1
  152. package/src/extensions/SyntaxHighlighting/shiki.ts +1 -1
  153. package/src/extensions/TableHandles/TableHandles.ts +4 -6
  154. package/src/extensions/Versioning/Versioning.browser.test.ts +151 -0
  155. package/src/extensions/Versioning/Versioning.node.test.ts +177 -0
  156. package/src/extensions/Versioning/Versioning.ts +145 -612
  157. package/src/extensions/Versioning/__test__/result.ts +12 -0
  158. package/src/extensions/Versioning/comparison.test.ts +40 -0
  159. package/src/extensions/Versioning/createVersioning.test.ts +867 -0
  160. package/src/extensions/Versioning/createVersioning.ts +499 -0
  161. package/src/extensions/Versioning/formatVersionDate.ts +24 -0
  162. package/src/extensions/Versioning/inMemoryVersioning.test.ts +533 -430
  163. package/src/extensions/Versioning/inMemoryVersioning.ts +246 -251
  164. package/src/extensions/Versioning/index.ts +4 -0
  165. package/src/extensions/Versioning/scrollScheduling.test.ts +51 -0
  166. package/src/extensions/Versioning/scrollToFirstChange.test.ts +320 -0
  167. package/src/extensions/Versioning/scrollToFirstChange.ts +139 -0
  168. package/src/extensions/Versioning/types.ts +219 -0
  169. package/src/extensions/Versioning/versioningState.ts +154 -0
  170. package/src/extensions/blockDOM.browser.test.ts +102 -0
  171. package/src/extensions/blockDOM.test.ts +178 -0
  172. package/src/extensions/blockDOM.ts +135 -0
  173. package/src/extensions/index.ts +2 -0
  174. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +1719 -53
  175. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +645 -531
  176. package/src/extensions/tiptap-extensions/KeyboardShortcuts/blockIdentity.browser.test.ts +152 -0
  177. package/src/extensions/tiptap-extensions/Link/helpers/autolink.ts +4 -9
  178. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.test.ts +10 -24
  179. package/src/extensions/tiptap-extensions/UniqueID/UniqueID.ts +22 -19
  180. package/src/fonts/inter.browser.test.ts +57 -0
  181. package/src/fonts/inter.css +111 -0
  182. package/src/i18n/locales/ar.ts +32 -0
  183. package/src/i18n/locales/de.ts +32 -0
  184. package/src/i18n/locales/en.ts +32 -0
  185. package/src/i18n/locales/es.ts +32 -0
  186. package/src/i18n/locales/fa.ts +32 -0
  187. package/src/i18n/locales/fr.ts +32 -0
  188. package/src/i18n/locales/he.ts +32 -0
  189. package/src/i18n/locales/hr.ts +32 -0
  190. package/src/i18n/locales/is.ts +32 -0
  191. package/src/i18n/locales/it.ts +32 -0
  192. package/src/i18n/locales/ja.ts +32 -0
  193. package/src/i18n/locales/ko.ts +32 -0
  194. package/src/i18n/locales/nl.ts +32 -0
  195. package/src/i18n/locales/no.ts +32 -0
  196. package/src/i18n/locales/pl.ts +32 -0
  197. package/src/i18n/locales/pt.ts +32 -0
  198. package/src/i18n/locales/ru.ts +32 -0
  199. package/src/i18n/locales/sk.ts +32 -0
  200. package/src/i18n/locales/uk.ts +32 -0
  201. package/src/i18n/locales/uz.ts +32 -0
  202. package/src/i18n/locales/vi.ts +32 -0
  203. package/src/i18n/locales/zh-tw.ts +32 -0
  204. package/src/i18n/locales/zh.ts +32 -0
  205. package/src/index.ts +13 -3
  206. package/src/pm-nodes/BlockContainer.test.ts +101 -0
  207. package/src/pm-nodes/BlockContainer.ts +110 -1
  208. package/src/pm-nodes/BlockGroup.ts +2 -1
  209. package/src/pm-nodes/README.md +2 -0
  210. package/src/schema/blocks/children.test.ts +288 -0
  211. package/src/schema/blocks/children.ts +136 -0
  212. package/src/schema/blocks/createSpec.browser.test.ts +176 -0
  213. package/src/schema/blocks/createSpec.test.ts +231 -0
  214. package/src/schema/blocks/createSpec.ts +493 -267
  215. package/src/schema/blocks/frame.test.ts +112 -0
  216. package/src/schema/blocks/frame.ts +337 -0
  217. package/src/schema/blocks/internal.ts +50 -17
  218. package/src/schema/blocks/keyboard.ts +154 -0
  219. package/src/schema/blocks/layoutBlockAttributes.ts +29 -0
  220. package/src/schema/blocks/meta.test.ts +53 -0
  221. package/src/schema/blocks/meta.ts +35 -0
  222. package/src/schema/blocks/renderFrame.test.ts +691 -0
  223. package/src/schema/blocks/types.ts +285 -21
  224. package/src/schema/blocks/validateChildren.ts +156 -0
  225. package/src/schema/dataAttributes.test.ts +72 -0
  226. package/src/schema/dataAttributes.ts +69 -0
  227. package/src/schema/index.ts +3 -0
  228. package/src/schema/inlineContent/internal.ts +7 -12
  229. package/src/schema/markGroups.ts +34 -2
  230. package/src/schema/schema.ts +8 -1
  231. package/src/user/userColors.test.ts +108 -0
  232. package/src/user/userColors.ts +42 -13
  233. package/src/y/comments/RESTYjsThreadStore.ts +1 -1
  234. package/src/y/comments/YjsThreadStore.test.ts +1 -1
  235. package/src/y/comments/YjsThreadStore.ts +27 -27
  236. package/src/y/comments/YjsThreadStoreBase.ts +2 -2
  237. package/src/y/comments/yjsHelpers.ts +11 -11
  238. package/src/y/extensions/AttributionExtension.test.ts +271 -2
  239. package/src/y/extensions/AttributionExtension.ts +146 -123
  240. package/src/y/extensions/DiffVersioningExtension.test.ts +67 -18
  241. package/src/y/extensions/DiffVersioningExtension.ts +95 -123
  242. package/src/y/extensions/EmptyDocBinding.test.ts +282 -0
  243. package/src/y/extensions/ForkYDoc.node.test.ts +77 -0
  244. package/src/y/extensions/ForkYDoc.test.ts +93 -2
  245. package/src/y/extensions/ForkYDoc.ts +22 -13
  246. package/src/y/extensions/History.test.ts +125 -0
  247. package/src/y/extensions/RelativePositionMapping.test.ts +35 -0
  248. package/src/y/extensions/RelativePositionMapping.ts +7 -11
  249. package/src/y/extensions/Versioning.node.test.ts +126 -0
  250. package/src/y/extensions/Versioning.test.ts +110 -401
  251. package/src/y/extensions/Versioning.ts +66 -107
  252. package/src/y/extensions/YAttributionMarks.ts +111 -9
  253. package/src/y/extensions/YCursorPlugin.ts +3 -0
  254. package/src/y/extensions/YSync.node.test.ts +75 -0
  255. package/src/y/extensions/YSync.ts +76 -4
  256. package/src/y/extensions/YSyncRemount.test.ts +99 -0
  257. package/src/y/extensions/blockMatchNodes.ts +20 -29
  258. package/src/y/extensions/index.ts +10 -26
  259. package/src/y/extensions/legacyYjsDocBinding.test.ts +652 -0
  260. package/src/y/extensions/nestingChanges.test.ts +326 -0
  261. package/src/y/extensions/persistentBlockGroups.test.ts +1709 -0
  262. package/src/y/extensions/persistentBlockGroups.ts +953 -0
  263. package/src/y/extensions/snapshotCodec.test.ts +105 -0
  264. package/src/y/extensions/snapshotCodec.ts +39 -0
  265. package/src/y/extensions/snapshotPreview.test.ts +201 -0
  266. package/src/y/extensions/snapshotPreview.ts +292 -0
  267. package/src/y/extensions/tableReshape.test.ts +121 -0
  268. package/src/y/extensions/v1BindingSnapshotDiff.test.ts +121 -0
  269. package/src/y/extensions/versionDiffAttribution.test.ts +944 -0
  270. package/src/y/extensions/versionDiffPerformance.test.ts +101 -0
  271. package/src/y/index.ts +1 -0
  272. package/src/y/utils.test.ts +289 -22
  273. package/src/y/utils.ts +167 -43
  274. package/src/y/versioning/__test__/fixtures/activity-all-after.json +5 -5
  275. package/src/y/versioning/__test__/fixtures/activity-all.json +3 -3
  276. package/src/y/versioning/__test__/fixtures/activity-filtered-after.json +2 -2
  277. package/src/y/versioning/__test__/yhub.test.ts +902 -721
  278. package/src/y/versioning/index.ts +6 -1
  279. package/src/y/versioning/yhub.ts +316 -603
  280. package/src/y/versioning/yhubClient.ts +345 -0
  281. package/src/yjs/comments/RESTYjsThreadStore.test.ts +10 -0
  282. package/src/yjs/extensions/FixUpSchema.ts +11 -0
  283. package/src/yjs/extensions/ForkYDoc.browser.test.ts +84 -0
  284. package/src/yjs/extensions/ForkYDoc.node.test.ts +231 -0
  285. package/src/yjs/extensions/ForkYDoc.test.ts +68 -1
  286. package/src/yjs/extensions/ForkYDoc.ts +57 -39
  287. package/src/yjs/extensions/Versioning.test.ts +345 -530
  288. package/src/yjs/extensions/Versioning.ts +111 -68
  289. package/src/yjs/extensions/index.ts +1 -1
  290. package/src/yjs/index.ts +4 -0
  291. package/types/src/api/blockManipulation/commands/insertBlocks/insertBlocks.d.ts +2 -1
  292. package/types/src/api/blockManipulation/commands/mergeBlocks/mergeBlocks.d.ts +24 -24
  293. package/types/src/api/blockManipulation/commands/nestBlock/nestBlock.d.ts +3 -2
  294. package/types/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.d.ts +1 -1
  295. package/types/src/api/blockManipulation/commands/splitBlock/splitBlock.d.ts +2 -2
  296. package/types/src/api/blockManipulation/layoutBlocks/fixLayoutBlock.d.ts +34 -0
  297. package/types/src/api/blockManipulation/layoutBlocks/layoutBlocks.browser.test.d.ts +1 -0
  298. package/types/src/api/blockManipulation/layoutBlocks/layoutBlocks.fixture.d.ts +460 -0
  299. package/types/src/api/blockManipulation/layoutBlocks/layoutBlocks.test.d.ts +1 -0
  300. package/types/src/api/blockManipulation/layoutBlocks/plainBlocks.test.d.ts +1 -0
  301. package/types/src/api/blockManipulation/layoutBlocks/titledBlocks.test.d.ts +1 -0
  302. package/types/src/api/blockManipulation/setupTestEnv.d.ts +15 -69
  303. package/types/src/api/combineTransactionSteps.d.ts +12 -0
  304. package/types/src/api/getBlockInfoFromPos.d.ts +218 -44
  305. package/types/src/api/{getChangedRange.d.ts → getChangedRangeWithAttrs.d.ts} +1 -1
  306. package/types/src/api/getChangedRanges.d.ts +23 -0
  307. package/types/src/api/getChangedRanges.test.d.ts +1 -0
  308. package/types/src/api/nodeConversions/fragmentToBlocks.d.ts +0 -3
  309. package/types/src/api/parsers/markdown/autolink.d.ts +5 -0
  310. package/types/src/blocks/ToggleWrapper/createToggleFrame.d.ts +29 -0
  311. package/types/src/blocks/ToggleWrapper/toggleBlocks.browser.test.d.ts +1 -0
  312. package/types/src/blocks/defaultBlockTypeItems.d.ts +37 -0
  313. package/types/src/blocks/defaultBlockTypeItems.test.d.ts +1 -0
  314. package/types/src/blocks/defaultBlocks.test.d.ts +1 -0
  315. package/types/src/blocks/index.d.ts +2 -1
  316. package/types/src/editor/BlockNoteEditor.d.ts +36 -8
  317. package/types/src/editor/BlockNoteEditor.node.test.d.ts +1 -0
  318. package/types/src/editor/blockColors.browser.test.d.ts +1 -0
  319. package/types/src/editor/managers/BlockManager.d.ts +4 -3
  320. package/types/src/editor/managers/ExtensionManager/ExtensionManager.browser.test.d.ts +1 -0
  321. package/types/src/editor/managers/ExtensionManager/ExtensionManager.node.test.d.ts +1 -0
  322. package/types/src/editor/managers/ExtensionManager/index.d.ts +15 -3
  323. package/types/src/editor/managers/StateManager.d.ts +1 -1
  324. package/types/src/editor/transformPasted.d.ts +1 -1
  325. package/types/src/exporter/Exporter.d.ts +11 -4
  326. package/types/src/exporter/mapping.d.ts +13 -1
  327. package/types/src/extensions/DropCursor/DropCursor.d.ts +1 -1
  328. package/types/src/extensions/DropCursor/dropIntoChildren.d.ts +38 -0
  329. package/types/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboard.d.ts +1 -1
  330. package/types/src/extensions/NodeSelectionKeyboard/NodeSelectionKeyboard.test.d.ts +1 -0
  331. package/types/src/extensions/Placeholder/Placeholder.test.d.ts +1 -0
  332. package/types/src/extensions/ReadOnly/ReadOnly.d.ts +21 -0
  333. package/types/src/extensions/ReadOnly/ReadOnly.test.d.ts +1 -0
  334. package/types/src/extensions/SideMenu/SideMenu.d.ts +49 -88
  335. package/types/src/extensions/SideMenu/blockDropHandling.d.ts +11 -0
  336. package/types/src/extensions/SideMenu/blockGeometry.browser.test.d.ts +1 -0
  337. package/types/src/extensions/SideMenu/blockGeometry.d.ts +22 -0
  338. package/types/src/extensions/SideMenu/blockGeometry.test.d.ts +1 -0
  339. package/types/src/extensions/SideMenu/dragging.d.ts +0 -4
  340. package/types/src/extensions/SideMenu/sideMenuHover.d.ts +58 -0
  341. package/types/src/extensions/SideMenu/sideMenuHover.test.d.ts +1 -0
  342. package/types/src/extensions/Versioning/Versioning.browser.test.d.ts +1 -0
  343. package/types/src/extensions/Versioning/Versioning.d.ts +34 -311
  344. package/types/src/extensions/Versioning/Versioning.node.test.d.ts +1 -0
  345. package/types/src/extensions/Versioning/__test__/result.d.ts +3 -0
  346. package/types/src/extensions/Versioning/comparison.test.d.ts +1 -0
  347. package/types/src/extensions/Versioning/createVersioning.d.ts +32 -0
  348. package/types/src/extensions/Versioning/createVersioning.test.d.ts +1 -0
  349. package/types/src/extensions/Versioning/formatVersionDate.d.ts +7 -0
  350. package/types/src/extensions/Versioning/inMemoryVersioning.d.ts +64 -36
  351. package/types/src/extensions/Versioning/index.d.ts +4 -0
  352. package/types/src/extensions/Versioning/scrollScheduling.test.d.ts +1 -0
  353. package/types/src/extensions/Versioning/scrollToFirstChange.d.ts +18 -0
  354. package/types/src/extensions/Versioning/scrollToFirstChange.test.d.ts +1 -0
  355. package/types/src/extensions/Versioning/types.d.ts +239 -0
  356. package/types/src/extensions/Versioning/versioningState.d.ts +43 -0
  357. package/types/src/extensions/blockDOM.browser.test.d.ts +1 -0
  358. package/types/src/extensions/blockDOM.d.ts +37 -0
  359. package/types/src/extensions/blockDOM.test.d.ts +1 -0
  360. package/types/src/extensions/index.d.ts +2 -0
  361. package/types/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.d.ts +1 -0
  362. package/types/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.d.ts +3 -0
  363. package/types/src/extensions/tiptap-extensions/KeyboardShortcuts/blockIdentity.browser.test.d.ts +1 -0
  364. package/types/src/fonts/inter.browser.test.d.ts +2 -0
  365. package/types/src/i18n/locales/en.d.ts +32 -0
  366. package/types/src/i18n/locales/fa.d.ts +32 -0
  367. package/types/src/i18n/locales/sk.d.ts +32 -0
  368. package/types/src/index.d.ts +6 -3
  369. package/types/src/pm-nodes/BlockContainer.d.ts +6 -0
  370. package/types/src/pm-nodes/BlockContainer.test.d.ts +1 -0
  371. package/types/src/schema/blocks/children.d.ts +39 -0
  372. package/types/src/schema/blocks/children.test.d.ts +1 -0
  373. package/types/src/schema/blocks/createSpec.browser.test.d.ts +1 -0
  374. package/types/src/schema/blocks/createSpec.d.ts +13 -0
  375. package/types/src/schema/blocks/frame.d.ts +109 -0
  376. package/types/src/schema/blocks/frame.test.d.ts +1 -0
  377. package/types/src/schema/blocks/internal.d.ts +6 -0
  378. package/types/src/schema/blocks/keyboard.d.ts +111 -0
  379. package/types/src/schema/blocks/layoutBlockAttributes.d.ts +9 -0
  380. package/types/src/schema/blocks/meta.d.ts +14 -0
  381. package/types/src/schema/blocks/meta.test.d.ts +1 -0
  382. package/types/src/schema/blocks/renderFrame.test.d.ts +1 -0
  383. package/types/src/schema/blocks/types.d.ts +228 -15
  384. package/types/src/schema/blocks/validateChildren.d.ts +5 -0
  385. package/types/src/schema/dataAttributes.d.ts +19 -0
  386. package/types/src/schema/dataAttributes.test.d.ts +1 -0
  387. package/types/src/schema/index.d.ts +3 -0
  388. package/types/src/schema/markGroups.d.ts +8 -0
  389. package/types/src/user/userColors.d.ts +24 -3
  390. package/types/src/user/userColors.test.d.ts +1 -0
  391. package/types/src/y/comments/RESTYjsThreadStore.d.ts +1 -1
  392. package/types/src/y/comments/YjsThreadStore.d.ts +1 -1
  393. package/types/src/y/comments/YjsThreadStoreBase.d.ts +2 -2
  394. package/types/src/y/comments/yjsHelpers.d.ts +5 -5
  395. package/types/src/y/extensions/AttributionExtension.d.ts +19 -21
  396. package/types/src/y/extensions/DiffVersioningExtension.d.ts +9 -4
  397. package/types/src/y/extensions/EmptyDocBinding.test.d.ts +1 -0
  398. package/types/src/y/extensions/ForkYDoc.d.ts +3 -1
  399. package/types/src/y/extensions/ForkYDoc.node.test.d.ts +1 -0
  400. package/types/src/y/extensions/History.test.d.ts +1 -0
  401. package/types/src/y/extensions/Versioning.d.ts +4 -15
  402. package/types/src/y/extensions/Versioning.node.test.d.ts +1 -0
  403. package/types/src/y/extensions/YAttributionMarks.d.ts +13 -2
  404. package/types/src/y/extensions/YCursorPlugin.d.ts +1 -0
  405. package/types/src/y/extensions/YSync.d.ts +4 -18
  406. package/types/src/y/extensions/YSync.node.test.d.ts +1 -0
  407. package/types/src/y/extensions/YSyncRemount.test.d.ts +1 -0
  408. package/types/src/y/extensions/blockMatchNodes.d.ts +0 -22
  409. package/types/src/y/extensions/index.d.ts +27 -44
  410. package/types/src/y/extensions/legacyYjsDocBinding.test.d.ts +1 -0
  411. package/types/src/y/extensions/nestingChanges.test.d.ts +1 -0
  412. package/types/src/y/extensions/persistentBlockGroups.d.ts +189 -0
  413. package/types/src/y/extensions/persistentBlockGroups.test.d.ts +1 -0
  414. package/types/src/y/extensions/snapshotCodec.d.ts +13 -0
  415. package/types/src/y/extensions/snapshotCodec.test.d.ts +1 -0
  416. package/types/src/y/extensions/snapshotPreview.d.ts +21 -0
  417. package/types/src/y/extensions/snapshotPreview.test.d.ts +1 -0
  418. package/types/src/y/extensions/tableReshape.test.d.ts +1 -0
  419. package/types/src/y/extensions/v1BindingSnapshotDiff.test.d.ts +1 -0
  420. package/types/src/y/extensions/versionDiffAttribution.test.d.ts +1 -0
  421. package/types/src/y/extensions/versionDiffPerformance.test.d.ts +1 -0
  422. package/types/src/y/index.d.ts +1 -0
  423. package/types/src/y/utils.d.ts +39 -19
  424. package/types/src/y/versioning/index.d.ts +1 -1
  425. package/types/src/y/versioning/yhub.d.ts +69 -86
  426. package/types/src/y/versioning/yhubClient.d.ts +67 -0
  427. package/types/src/yjs/extensions/ForkYDoc.browser.test.d.ts +1 -0
  428. package/types/src/yjs/extensions/ForkYDoc.d.ts +2 -0
  429. package/types/src/yjs/extensions/ForkYDoc.node.test.d.ts +1 -0
  430. package/types/src/yjs/extensions/Versioning.d.ts +36 -22
  431. package/types/src/yjs/extensions/index.d.ts +2 -1
  432. package/types/src/yjs/index.d.ts +1 -0
  433. package/dist/BlockNoteExtension-BJCLnwrE.cjs +0 -2
  434. package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +0 -1
  435. package/dist/BlockNoteExtension-CbvDA0F7.js.map +0 -1
  436. package/dist/blocks-DLgTreVu.cjs +0 -12
  437. package/dist/blocks-DLgTreVu.cjs.map +0 -1
  438. package/dist/blocks-DaEUT0O1.js.map +0 -1
  439. package/dist/en-BAn9gxr-.js.map +0 -1
  440. package/dist/en-CHnrV5l8.cjs +0 -2
  441. package/dist/en-CHnrV5l8.cjs.map +0 -1
  442. package/dist/extensions-B1JQURVF.js +0 -2138
  443. package/dist/extensions-B1JQURVF.js.map +0 -1
  444. package/dist/extensions-CZkqKzJO.cjs +0 -58
  445. package/dist/extensions-CZkqKzJO.cjs.map +0 -1
  446. package/dist/src-D5w0XAHX.js.map +0 -1
  447. package/dist/src-th5uZEvx.cjs +0 -23
  448. package/dist/src-th5uZEvx.cjs.map +0 -1
  449. package/src/api/blockManipulation/commands/replaceBlocks/util/fixColumnList.ts +0 -173
  450. package/src/blocks/ListItem/ListItemKeyboardShortcuts.ts +0 -63
  451. package/src/blocks/ToggleWrapper/createToggleWrapper.ts +0 -197
  452. package/src/blocks/utils/listItemEnterHandler.ts +0 -43
  453. package/src/extensions/Versioning/Versioning.test.ts +0 -435
  454. package/src/extensions/getDraggableBlockFromElement.ts +0 -19
  455. package/types/src/api/blockManipulation/commands/replaceBlocks/util/fixColumnList.d.ts +0 -32
  456. package/types/src/blocks/ListItem/ListItemKeyboardShortcuts.d.ts +0 -2
  457. package/types/src/blocks/ToggleWrapper/createToggleWrapper.d.ts +0 -15
  458. package/types/src/blocks/utils/listItemEnterHandler.d.ts +0 -2
  459. package/types/src/extensions/getDraggableBlockFromElement.d.ts +0 -5
  460. /package/types/src/{extensions/Versioning/Versioning.test.d.ts → api/blockManipulation/commands/insertBlocks/insertPlacement.test.d.ts} +0 -0
@@ -1,18 +1,10 @@
1
- import { DOMParser, Slice } from "@tiptap/pm/model";
2
- import {
3
- EditorState,
4
- Plugin,
5
- PluginKey,
6
- PluginView,
7
- TextSelection,
8
- } from "@tiptap/pm/state";
9
- import { EditorView } from "@tiptap/pm/view";
1
+ import { Plugin, PluginKey } from "@tiptap/pm/state";
10
2
 
11
3
  import { Block } from "../../blocks/defaultBlocks.js";
12
- import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
13
4
  import {
14
5
  createExtension,
15
6
  createStore,
7
+ ExtensionOptions,
16
8
  } from "../../editor/BlockNoteExtension.js";
17
9
  import { UiElementPosition } from "../../extensions-shared/UiElementPosition.js";
18
10
  import {
@@ -20,814 +12,459 @@ import {
20
12
  InlineContentSchema,
21
13
  StyleSchema,
22
14
  } from "../../schema/index.js";
23
- import { getDraggableBlockFromElement } from "../getDraggableBlockFromElement.js";
15
+ import { registerBlockDropHandlers } from "./blockDropHandling.js";
24
16
  import { dragStart, unsetDragImage } from "./dragging.js";
17
+ import {
18
+ HoveredBlock,
19
+ isHeadingToMenu,
20
+ Point,
21
+ resolveHoveredBlock,
22
+ } from "./sideMenuHover.js";
25
23
 
26
24
  export type SideMenuState<
27
25
  BSchema extends BlockSchema,
28
26
  I extends InlineContentSchema,
29
27
  S extends StyleSchema,
30
28
  > = UiElementPosition & {
31
- // The block that the side menu is attached to.
29
+ /**
30
+ * The block that the side menu is attached to. While the menu is hidden
31
+ * (`show` is `false`), the block it was last attached to, so it can animate
32
+ * out next to it. The store's state is `undefined` until the menu first
33
+ * shows.
34
+ */
32
35
  block: Block<BSchema, I, S>;
33
36
  };
34
37
 
35
- const DISTANCE_TO_CONSIDER_EDITOR_BOUNDS = 250;
36
-
37
- function getBlockFromCoords(
38
- view: EditorView,
39
- coords: { left: number; top: number },
40
- adjustForColumns = true,
41
- ) {
42
- const elements = view.root.elementsFromPoint(coords.left, coords.top);
43
-
44
- for (const element of elements) {
45
- if (!view.dom.contains(element)) {
46
- // probably a ui overlay like formatting toolbar etc
47
- continue;
48
- }
49
- if (adjustForColumns) {
50
- const column = element.closest("[data-node-type=columnList]");
51
- if (column) {
52
- return getBlockFromCoords(
53
- view,
54
- {
55
- // TODO can we do better than this?
56
- left: coords.left + 50, // bit hacky, but if we're inside a column, offset x position to right to account for the width of sidemenu itself
57
- top: coords.top,
58
- },
59
- false,
60
- );
61
- }
62
- }
63
- return getDraggableBlockFromElement(element, view);
64
- }
65
- return undefined;
66
- }
67
-
68
- function getBlockFromMousePos(
69
- mousePos: {
70
- x: number;
71
- y: number;
72
- },
73
- view: EditorView,
74
- ): { node: HTMLElement; id: string } | undefined {
75
- // Editor itself may have padding or other styling which affects
76
- // size/position, so we get the boundingRect of the first child (i.e. the
77
- // blockGroup that wraps all blocks in the editor) for more accurate side
78
- // menu placement.
79
- if (!view.dom.firstChild) {
80
- return;
81
- }
82
-
83
- const editorBoundingBox = (
84
- view.dom.firstChild as HTMLElement
85
- ).getBoundingClientRect();
86
-
87
- // Gets block at mouse cursor's position.
88
- const coords = {
89
- // Clamps the x position to the editor's bounding box.
90
- left: Math.min(
91
- Math.max(editorBoundingBox.left + 10, mousePos.x),
92
- editorBoundingBox.right - 10,
93
- ),
94
- top: mousePos.y,
95
- };
96
-
97
- const referenceBlock = getBlockFromCoords(view, coords);
98
-
99
- if (!referenceBlock) {
100
- // could not find the reference block
101
- return undefined;
102
- }
103
-
38
+ /**
39
+ * How far the areas around the editor where the side menu responds reach, in
40
+ * px from the editor element's box. The editor element itself (including its
41
+ * padding, where the menu sits) always counts.
42
+ */
43
+ export type SideMenuOptions = {
104
44
  /**
105
- * Because blocks may be nested, we need to check the right edge of the parent block:
106
- * ```
107
- * | BlockA |
108
- * x | BlockB y|
109
- * ```
110
- * Hovering at position x (left edge of BlockB) would return BlockA.
111
- * Instead, we check at position y (right edge of BlockA) to correctly identify BlockB.
45
+ * How far left or right of the editor element the pointer still shows the
46
+ * side menu, for the block at the pointer's height. Above or below the
47
+ * editor, no block is at that height.
48
+ * @default 0
112
49
  */
113
- const referenceBlocksBoundingBox =
114
- referenceBlock.node.getBoundingClientRect();
115
- return getBlockFromCoords(
116
- view,
117
- {
118
- left: referenceBlocksBoundingBox.right - 10,
119
- top: mousePos.y,
120
- },
121
- false,
122
- );
123
- }
50
+ hoverMargin?: number;
51
+ /**
52
+ * How far outside the editor element, on any side, a dragged block can still
53
+ * be dropped into it (the drop goes to the nearest position in the editor).
54
+ * @default 250
55
+ */
56
+ dropMargin?: number;
57
+ };
124
58
 
125
59
  /**
126
- * With the sidemenu plugin we can position a menu next to a hovered block.
60
+ * How long the pointer may rest on its way to the menu (inside the triangle
61
+ * from where it left the block to the menu) before the menu follows it to the
62
+ * block it is over.
127
63
  */
128
- export class SideMenuView<
129
- BSchema extends BlockSchema,
130
- I extends InlineContentSchema,
131
- S extends StyleSchema,
132
- > implements PluginView {
133
- public state?: SideMenuState<BSchema, I, S>;
134
- public readonly emitUpdate: (state: SideMenuState<BSchema, I, S>) => void;
135
-
136
- private mousePos: { x: number; y: number } | undefined;
137
-
138
- private hoveredBlock: HTMLElement | undefined;
139
-
140
- public menuFrozen = false;
141
-
142
- public isDragOrigin = false;
143
-
144
- constructor(
145
- private readonly editor: BlockNoteEditor<BSchema, I, S>,
146
- private readonly pmView: EditorView,
147
- emitUpdate: (state: SideMenuState<BSchema, I, S>) => void,
148
- ) {
149
- this.emitUpdate = () => {
150
- if (!this.state) {
151
- throw new Error("Attempting to update uninitialized side menu");
152
- }
153
-
154
- emitUpdate(this.state);
155
- };
64
+ const INTENT_TIMEOUT = 350;
156
65
 
157
- this.pmView.root.addEventListener(
158
- "dragstart",
159
- this.onDragStart as EventListener,
160
- );
161
- this.pmView.root.addEventListener(
162
- "dragover",
163
- this.onDragOver as EventListener,
164
- );
165
- this.pmView.root.addEventListener(
166
- "drop",
167
- this.onDrop as EventListener,
168
- true,
169
- );
170
- this.pmView.root.addEventListener(
171
- "dragend",
172
- this.onDragEnd as EventListener,
173
- true,
174
- );
66
+ /** The event's target as an element (text nodes resolve to their parent). */
67
+ function targetElement(target: EventTarget | null): Element | null {
68
+ if (!target || !("nodeType" in target)) {
69
+ return null;
70
+ }
71
+ const node = target as Node;
72
+ return node.nodeType === Node.ELEMENT_NODE
73
+ ? (node as Element)
74
+ : node.parentElement;
75
+ }
175
76
 
176
- // Shows or updates menu position whenever the cursor moves, if the menu isn't frozen.
177
- this.pmView.root.addEventListener(
178
- "mousemove",
179
- this.onMouseMove as EventListener,
180
- true,
181
- );
77
+ export const sideMenuPluginKey = new PluginKey("SideMenuPlugin");
182
78
 
183
- // Hides and unfreezes the menu whenever the user presses a key.
184
- this.pmView.root.addEventListener(
185
- "keydown",
186
- this.onKeyDown as EventListener,
187
- true,
79
+ /**
80
+ * Shows a menu next to the block the pointer is about. This
81
+ * extension owns that decision: it listens to the pointer in `mount` and
82
+ * writes the store; the UI only reads the store, positions the menu, and
83
+ * registers the menu's element with `setMenuElement`.
84
+ *
85
+ * The block comes from the topmost element under the pointer (so an editor
86
+ * covered by another element or editor gets no menu), falling back to probing
87
+ * at the pointer's height when that element is padding or the gutter beside
88
+ * the blocks. When the pointer leaves the block towards the menu, the menu
89
+ * stays with the block while the pointer is inside the triangle from its exit
90
+ * point to the menu, until it rests there for {@link INTENT_TIMEOUT}. Once on
91
+ * the menu, leaving it anywhere but onto its block follows the pointer at
92
+ * once. The menu hides when the pointer leaves the window.
93
+ */
94
+ export const SideMenuExtension = createExtension(
95
+ ({
96
+ editor,
97
+ options,
98
+ }: ExtensionOptions<{ sideMenu?: SideMenuOptions } | undefined>) => {
99
+ const store = createStore<SideMenuState<any, any, any> | undefined>(
100
+ undefined,
188
101
  );
189
- }
190
102
 
191
- updateState = (state: SideMenuState<BSchema, I, S>) => {
192
- this.state = state;
193
- this.emitUpdate(this.state);
194
- };
195
-
196
- updateStateFromMousePos = () => {
197
- if (this.menuFrozen || !this.mousePos) {
198
- return;
103
+ const hoverMargin = options?.sideMenu?.hoverMargin ?? 0;
104
+
105
+ let menuFrozen = false;
106
+ let isDragOrigin = false;
107
+ // The side menu's DOM element, registered by the UI that renders it.
108
+ let menuElement: HTMLElement | undefined;
109
+ // The last pointer position over this document.
110
+ let pointer: Point | undefined;
111
+ // The last pointer position over the active block (or the menu).
112
+ let lastOnActive: Point | undefined;
113
+ // Set while the pointer is heading from the active block to the menu.
114
+ let intent:
115
+ | { exit: Point; timer: ReturnType<typeof setTimeout> }
116
+ | undefined;
117
+
118
+ function isDraggable(type: string) {
119
+ return editor.schema.blockSpecs[type].implementation.meta.draggable;
199
120
  }
200
121
 
201
- const closestEditor = this.findClosestEditorElement({
202
- clientX: this.mousePos.x,
203
- clientY: this.mousePos.y,
204
- });
205
-
206
- if (
207
- closestEditor?.element !== this.pmView.dom ||
208
- closestEditor.distance > DISTANCE_TO_CONSIDER_EDITOR_BOUNDS
209
- ) {
210
- if (this.state?.show) {
211
- this.state.show = false;
212
- this.updateState(this.state);
122
+ function clearIntent() {
123
+ if (intent) {
124
+ clearTimeout(intent.timer);
125
+ intent = undefined;
213
126
  }
214
- return;
215
127
  }
216
128
 
217
- const block = getBlockFromMousePos(this.mousePos, this.pmView);
218
-
219
- // Closes the menu if the mouse cursor is beyond the editor vertically.
220
- if (!block || !this.editor.isEditable) {
221
- if (this.state?.show) {
222
- this.state.show = false;
223
- this.updateState(this.state);
129
+ function hideMenu() {
130
+ const state = store.state;
131
+ if (state?.show) {
132
+ store.setState({ ...state, show: false });
224
133
  }
225
-
226
- return;
227
- }
228
-
229
- // Doesn't update if the menu is already open and the mouse cursor is still hovering the same block.
230
- if (
231
- this.state?.show &&
232
- this.hoveredBlock?.hasAttribute("data-id") &&
233
- this.hoveredBlock?.getAttribute("data-id") === block.id
234
- ) {
235
- return;
236
134
  }
237
135
 
238
- this.hoveredBlock = block.node;
239
-
240
- // Shows or updates elements.
241
- if (this.editor.isEditable) {
242
- const blockContentBoundingBox = block.node.getBoundingClientRect();
243
- const column = block.node.closest("[data-node-type=column]");
244
- const sideMenuBlock = this.editor.getBlock(
245
- this.hoveredBlock!.getAttribute("data-id")!,
246
- );
247
- if (!sideMenuBlock) {
248
- if (this.state?.show) {
249
- this.state.show = false;
250
- this.hoveredBlock = undefined;
251
- this.emitUpdate(this.state);
252
- }
136
+ function showMenu(hovered: HoveredBlock) {
137
+ const block = editor.getBlock(hovered.id);
138
+ if (!block) {
139
+ hideMenu();
253
140
  return;
254
141
  }
255
- this.state = {
142
+ store.setState({
256
143
  show: true,
257
- referencePos: new DOMRect(
258
- column
259
- ? // We take the first child as column elements have some default
260
- // padding. This is a little weird since this child element will
261
- // be the first block, but since it's always non-nested and we
262
- // only take the x coordinate, it's ok.
263
- column.firstElementChild!.getBoundingClientRect().x
264
- : (
265
- this.pmView.dom.firstChild as HTMLElement
266
- ).getBoundingClientRect().x,
267
- blockContentBoundingBox.y,
268
- blockContentBoundingBox.width,
269
- blockContentBoundingBox.height,
270
- ),
271
- block: sideMenuBlock,
272
- };
273
- this.updateState(this.state);
144
+ // The hovered block's box: the menu goes next to the block itself, as
145
+ // the React UI anchors it to the block's element.
146
+ referencePos: hovered.node.getBoundingClientRect(),
147
+ block,
148
+ });
274
149
  }
275
- };
276
150
 
277
- /**
278
- * If a block is being dragged, ProseMirror usually gets the context of what's
279
- * being dragged from `view.dragging`, which is automatically set when a
280
- * `dragstart` event fires in the editor. However, if the user tries to drag
281
- * and drop blocks between multiple editors, only the one in which the drag
282
- * began has that context, so we need to set it on the others manually. This
283
- * ensures that PM always drops the blocks in between other blocks, and not
284
- * inside them.
285
- *
286
- * After the `dragstart` event fires on the drag handle, it sets
287
- * `blocknote/html` data on the clipboard. This handler fires right after,
288
- * parsing the `blocknote/html` data into nodes and setting them on
289
- * `view.dragging`.
290
- *
291
- * Note: Setting `view.dragging` on `dragover` would be better as the user
292
- * could then drag between editors in different windows, but you can only
293
- * access `dataTransfer` contents on `dragstart` and `drop` events.
294
- */
295
- onDragStart = (event: DragEvent) => {
296
- const html = event.dataTransfer?.getData("blocknote/html");
297
- if (!html) {
298
- return;
299
- }
300
-
301
- if (this.pmView.dragging) {
302
- // already dragging, so no-op
303
- return;
304
- }
305
-
306
- const element = document.createElement("div");
307
- element.innerHTML = html;
308
-
309
- const parser = DOMParser.fromSchema(this.pmView.state.schema);
310
- const node = parser.parse(element, {
311
- topNode: this.pmView.state.schema.nodes["blockGroup"].create(),
312
- });
313
-
314
- this.pmView.dragging = {
315
- slice: new Slice(node.content, 0, 0),
316
- move: true,
317
- };
318
- };
151
+ /**
152
+ * Updates the menu for the pointer at `point` over `target`. With
153
+ * `allowIntent`, a pointer heading to the menu keeps the active block.
154
+ */
155
+ function update(
156
+ point: Point,
157
+ target: Element | null,
158
+ allowIntent: boolean,
159
+ ) {
160
+ // While a block is dragged by its handle, the menu must stay as it is:
161
+ // the handle is the drag source, and if the menu hid or moved, React would
162
+ // remove it from the DOM, so its `dragend` (and `blockDragEnd`, which
163
+ // removes the drag image) would never fire. The drop's document change
164
+ // otherwise re-resolves the block at the pointer's pre-drag position.
165
+ if (menuFrozen || isDragOrigin) {
166
+ // Frozen or dragging: keep the menu as it is.
167
+ return;
168
+ }
169
+ if (target && menuElement?.contains(target)) {
170
+ // On the menu: the pointer got where it was heading. Recording the
171
+ // point here makes it the exit point if the pointer leaves the menu,
172
+ // and as nothing is closer to the menu than the menu, leaving it
173
+ // anywhere but onto its block follows the pointer at once.
174
+ clearIntent();
175
+ lastOnActive = point;
176
+ // On the menu: keep the menu as it is.
177
+ return;
178
+ }
319
179
 
320
- /**
321
- * Finds the closest editor visually to the given coordinates
322
- */
323
- private findClosestEditorElement = (coords: {
324
- clientX: number;
325
- clientY: number;
326
- }) => {
327
- // Get all editor elements in the document
328
- const editors = Array.from(this.pmView.root.querySelectorAll(".bn-editor"));
329
-
330
- if (editors.length === 0) {
331
- return null;
332
- }
180
+ if (!editor.isEditable) {
181
+ clearIntent();
182
+ hideMenu();
183
+ // Read-only editor: hide the menu.
184
+ return;
185
+ }
333
186
 
334
- // Find the editor with the smallest distance to the coordinates
335
- let closestEditor = editors[0];
336
- let minDistance = Number.MAX_VALUE;
337
-
338
- editors.forEach((editor) => {
339
- const rect = editor
340
- .querySelector(".bn-block-group")!
341
- .getBoundingClientRect();
342
-
343
- const distanceX =
344
- coords.clientX < rect.left
345
- ? rect.left - coords.clientX
346
- : coords.clientX > rect.right
347
- ? coords.clientX - rect.right
348
- : 0;
349
-
350
- const distanceY =
351
- coords.clientY < rect.top
352
- ? rect.top - coords.clientY
353
- : coords.clientY > rect.bottom
354
- ? coords.clientY - rect.bottom
355
- : 0;
356
-
357
- const distance = Math.sqrt(
358
- Math.pow(distanceX, 2) + Math.pow(distanceY, 2),
187
+ const resolution = resolveHoveredBlock(
188
+ editor.prosemirrorView,
189
+ target,
190
+ point,
191
+ isDraggable,
192
+ editor.isWithinEditor,
193
+ hoverMargin,
359
194
  );
360
-
361
- if (distance < minDistance) {
362
- minDistance = distance;
363
- closestEditor = editor;
195
+ if (resolution.kind === "editorUI") {
196
+ // On other UI of the editor (e.g. a toolbar): keep the menu as it is.
197
+ return;
364
198
  }
365
- });
366
-
367
- return {
368
- element: closestEditor,
369
- distance: minDistance,
370
- };
371
- };
372
-
373
- /**
374
- * This dragover event handler listens at the document level,
375
- * and is trying to handle dragover events for all editors.
376
- *
377
- * It specifically is trying to handle the following cases:
378
- * - If the dragover event is within the bounds of any editor, then it does nothing
379
- * - If the dragover event is outside the bounds of any editor, but close enough (within DISTANCE_TO_CONSIDER_EDITOR_BOUNDS) to the closest editor,
380
- * then it dispatches a synthetic dragover event to the closest editor (which will trigger the drop-cursor to be shown on that editor)
381
- * - If the dragover event is outside the bounds of the current editor, then it will dispatch a synthetic dragleave event to the current editor
382
- * (which will trigger the drop-cursor to be removed from the current editor)
383
- *
384
- * The synthetic event is a necessary evil because we do not control prosemirror-dropcursor to be able to show the drop-cursor within the range we want
385
- */
386
- onDragOver = (event: DragEvent) => {
387
- if ((event as any).synthetic) {
388
- return;
389
- }
390
-
391
- // Relevance gate: Only handle drags that belong to BlockNote
392
- // This prevents interference with external drag-and-drop libraries
393
- // by avoiding calls to closeDropCursor() for non-BlockNote drags
394
- const isBlockNoteDrag =
395
- this.pmView.dragging !== null ||
396
- this.isDragOrigin ||
397
- event.dataTransfer?.types.includes("blocknote/html") ||
398
- (event.target instanceof Node && this.pmView.dom.contains(event.target));
399
-
400
- if (!isBlockNoteDrag) {
401
- // Not a BlockNote-related drag, return early without any processing
402
- return;
403
- }
404
-
405
- const dragEventContext = this.getDragEventContext(event);
406
-
407
- if (!dragEventContext || !dragEventContext.isDropPoint) {
408
- // This is not a drag event that we are interested in
409
- // so, we close the drop-cursor
410
- this.closeDropCursor();
411
- return;
412
- }
413
-
414
- if (
415
- dragEventContext.isDropPoint &&
416
- !dragEventContext.isDropWithinEditorBounds
417
- ) {
418
- // we are the drop point, but the drag over event is not within the bounds of this editor instance
419
- // so, we need to dispatch an event that is in the bounds of this editor instance
420
- this.dispatchSyntheticEvent(event);
421
- }
422
- };
423
-
424
- /**
425
- * Closes the drop-cursor for the current editor
426
- */
427
- private closeDropCursor = () => {
428
- const evt = new Event("dragleave", { bubbles: false });
429
- // It needs to be synthetic, so we don't accidentally think it is a real dragend event
430
- (evt as any).synthetic = true;
431
- // We dispatch the event to the current editor, so that the drop-cursor is removed for it
432
- this.pmView.dom.dispatchEvent(evt);
433
- };
434
-
435
- /**
436
- * It is surprisingly difficult to determine the information we need to know about a drag event
437
- *
438
- * This function is trying to determine the following:
439
- * - Whether the current editor instance is the drop point
440
- * - Whether the current editor instance is the drag origin
441
- * - Whether the drop event is within the bounds of the current editor instance
442
- */
443
- getDragEventContext = (event: DragEvent) => {
444
- // Relevance gate: Only handle drags that belong to BlockNote
445
- // Check if at least one of the following is true:
446
- // 1. ProseMirror drag started in an editor
447
- // 2. Side menu drag
448
- // 3. BlockNote-specific data type in the drag
449
- // 4. (optional stricter mode) Event target is inside this editor
450
- const isBlockNoteDrag =
451
- this.pmView.dragging !== null ||
452
- this.isDragOrigin ||
453
- event.dataTransfer?.types.includes("blocknote/html") ||
454
- (event.target instanceof Node && this.pmView.dom.contains(event.target));
455
-
456
- if (!isBlockNoteDrag) {
457
- // Not a BlockNote-related drag, return early
458
- return undefined;
459
- }
460
-
461
- // We need to check if there is text content that is being dragged (select some text & just drag it)
462
- const textContentIsBeingDragged =
463
- !event.dataTransfer?.types.includes("blocknote/html") &&
464
- !!this.pmView.dragging;
465
- // This is the side menu drag from this plugin
466
- const sideMenuIsBeingDragged = !!this.isDragOrigin;
467
- // Tells us that the current editor instance has a drag ongoing (either text or side menu)
468
- const isDragOrigin = textContentIsBeingDragged || sideMenuIsBeingDragged;
469
-
470
- // Tells us which editor instance is the closest to the drag event (whether or not it is actually reasonably close)
471
- const closestEditor = this.findClosestEditorElement(event);
472
-
473
- // We arbitrarily decide how far is "too far" from the closest editor to be considered a drop point
474
- if (
475
- !closestEditor ||
476
- closestEditor.distance > DISTANCE_TO_CONSIDER_EDITOR_BOUNDS
477
- ) {
478
- // we are too far from the closest editor, or no editor was found
479
- return undefined;
480
- }
481
-
482
- // We check if the closest editor is the same as the current editor instance (which is the drop point)
483
- const isDropPoint = closestEditor.element === this.pmView.dom;
484
- // We check if the current editor instance is the same as the editor instance that the drag event is happening within
485
- const isDropWithinEditorBounds =
486
- isDropPoint && closestEditor.distance === 0;
487
-
488
- // We never want to handle drop events that are not related to us
489
- if (!isDropPoint && !isDragOrigin) {
490
- // we are not the drop point or drag origin, so not relevant to us
491
- return undefined;
492
- }
493
-
494
- return {
495
- isDropPoint,
496
- isDropWithinEditorBounds,
497
- isDragOrigin,
498
- };
499
- };
500
-
501
- /**
502
- * The drop event handler listens at the document level,
503
- * and handles drop events for all editors.
504
- *
505
- * It specifically handles the following cases:
506
- * - If we are both the drag origin and drop point:
507
- * - Let normal drop handling take over
508
- * - If we are the drop point but not the drag origin:
509
- * - Collapse selection to prevent PM from deleting unrelated content
510
- * - If drop event is outside our editor bounds, dispatch synthetic drop event to our editor
511
- * - If we are the drag origin but not the drop point:
512
- * - Delete the dragged content from our editor after a delay
513
- */
514
- onDrop = (event: DragEvent) => {
515
- if ((event as any).synthetic) {
516
- return;
517
- }
518
-
519
- // Relevance gate: Only handle drags that belong to BlockNote
520
- // This prevents interference with external drag-and-drop libraries
521
- const isBlockNoteDrag =
522
- this.pmView.dragging !== null ||
523
- this.isDragOrigin ||
524
- event.dataTransfer?.types.includes("blocknote/html") ||
525
- (event.target instanceof Node && this.pmView.dom.contains(event.target));
526
-
527
- if (!isBlockNoteDrag) {
528
- // Not a BlockNote-related drag, return early without any processing
529
- return;
530
- }
531
-
532
- const context = this.getDragEventContext(event);
533
- if (!context) {
534
- this.closeDropCursor();
535
- // This is not a drag event that we are interested in
536
- return;
537
- }
538
- const { isDropPoint, isDropWithinEditorBounds, isDragOrigin } = context;
539
-
540
- if (!isDropWithinEditorBounds && isDropPoint) {
541
- // Any time that the drop event is outside of the editor bounds (but still close to an editor instance)
542
- // We dispatch a synthetic event that is in the bounds of the editor instance, to have the correct drop point
543
- this.dispatchSyntheticEvent(event);
544
- }
545
199
 
546
- if (isDropPoint) {
547
- // The current instance is the drop point
548
-
549
- if (this.pmView.dragging) {
550
- // Do not collapse selection when text content is being dragged
200
+ const state = store.state;
201
+ const active = state?.show ? state.block.id : undefined;
202
+ if (resolution.kind === "block" && resolution.block.id === active) {
203
+ clearIntent();
204
+ lastOnActive = point;
205
+ // On the block of the menu: keep the menu as it is.
551
206
  return;
552
207
  }
553
- // Because the editor selection is unrelated to the dragged content, we
554
- // don't want PM to delete its content. Therefore, we collapse the
555
- // selection.
556
- this.pmView.dispatch(
557
- this.pmView.state.tr.setSelection(
558
- TextSelection.create(
559
- this.pmView.state.tr.doc,
560
- this.pmView.state.tr.selection.anchor,
561
- ),
562
- ),
563
- );
564
- return;
565
- } else if (isDragOrigin) {
566
- // The current instance is the drag origin, but not the drop point
567
- // our content got dropped somewhere else
568
-
569
- // Because the editor from which the block originates doesn't get a drop
570
- // event on it, PM doesn't delete its selected content. Therefore, we
571
- // need to do so manually.
572
- //
573
- // Note: Deleting the selected content from the editor from which the
574
- // block originates, may change its height. This can cause the position of
575
- // the editor in which the block is being dropping to shift, before it
576
- // can handle the drop event. That in turn can cause the drop to happen
577
- // somewhere other than the user intended. To get around this, we delay
578
- // deleting the selected content until all editors have had the chance to
579
- // handle the event.
580
- setTimeout(
581
- () => this.pmView.dispatch(this.pmView.state.tr.deleteSelection()),
582
- 0,
583
- );
584
- return;
585
- }
586
- };
587
-
588
- onDragEnd = (event: DragEvent) => {
589
- if ((event as any).synthetic) {
590
- return;
591
- }
592
- // When the user starts dragging a block, `view.dragging` is set on all
593
- // BlockNote editors. However, when the drag ends, only the editor that the
594
- // drag originated in automatically clears `view.dragging`. Therefore, we
595
- // have to manually clear it on all editors.
596
- this.pmView.dragging = null;
597
- };
598
-
599
- onKeyDown = (_event: KeyboardEvent) => {
600
- if (this.state?.show && this.editor.isFocused()) {
601
- // Typing in editor should hide side menu
602
- this.state.show = false;
603
- this.emitUpdate(this.state);
604
- }
605
- };
606
-
607
- onMouseMove = (event: MouseEvent) => {
608
- if (this.menuFrozen) {
609
- return;
610
- }
611
208
 
612
- // Synthetic mousemove events created via `new Event("mousemove")` (e.g.
613
- // dispatched by browser extensions) have no `clientX`/`clientY`, which
614
- // would make `elementsFromPoint` throw on the resulting non-finite
615
- // coordinates.
616
- if (!Number.isFinite(event.clientX) || !Number.isFinite(event.clientY)) {
617
- return;
618
- }
619
-
620
- // Leaves the menu as it is while the pointer is over this editor's own UI
621
- // (a toolbar, a menu, the side menu itself) rather than its content: the
622
- // block under that UI is not what the pointer is about. It also keeps taps
623
- // on the UI working on iOS Safari, which delivers a tap as a hover first
624
- // and drops the click when that hover changes the page (WebKit's
625
- // ContentChangeObserver); with the mobile toolbar far below the blocks,
626
- // the hover would hide a shown side menu and every button needed two taps.
627
- if (
628
- event.target instanceof Node &&
629
- !this.pmView.dom.contains(event.target) &&
630
- this.editor.isWithinEditor(event.target as Element)
631
- ) {
632
- return;
633
- }
634
-
635
- this.mousePos = { x: event.clientX, y: event.clientY };
636
-
637
- // We want the full area of the editor to check if the cursor is hovering
638
- // above it though.
639
- const editorOuterBoundingBox = this.pmView.dom.getBoundingClientRect();
640
- const cursorWithinEditor =
641
- this.mousePos.x > editorOuterBoundingBox.left &&
642
- this.mousePos.x < editorOuterBoundingBox.right &&
643
- this.mousePos.y > editorOuterBoundingBox.top &&
644
- this.mousePos.y < editorOuterBoundingBox.bottom;
645
-
646
- // Doesn't update if the mouse hovers an element that's over the editor but
647
- // isn't a part of it or the side menu.
648
- if (
649
- // Cursor is within the editor area
650
- cursorWithinEditor &&
651
- // An element is hovered
652
- event &&
653
- event.target &&
654
- // Element is outside this editor and its portaled UI
655
- !this.editor.isWithinEditor(event.target as HTMLElement)
656
- ) {
657
- if (this.state?.show) {
658
- this.state.show = false;
659
- this.emitUpdate(this.state);
209
+ if (allowIntent && active !== undefined && menuElement?.isConnected) {
210
+ const exit = intent?.exit ?? lastOnActive ?? point;
211
+ if (isHeadingToMenu(point, exit, menuElement.getBoundingClientRect())) {
212
+ // Restarts the timeout, so it measures how long the pointer rests.
213
+ clearIntent();
214
+ intent = { exit, timer: setTimeout(releaseIntent, INTENT_TIMEOUT) };
215
+ // On the way to the menu: keep the menu on its block for now.
216
+ return;
217
+ }
660
218
  }
661
219
 
662
- return;
220
+ // Not on the way to the menu: show the menu for the block under the
221
+ // pointer, or hide it if there is no block.
222
+ clearIntent();
223
+ if (resolution.kind === "block") {
224
+ lastOnActive = point;
225
+ showMenu(resolution.block);
226
+ } else {
227
+ hideMenu();
228
+ }
663
229
  }
664
230
 
665
- this.updateStateFromMousePos();
666
- };
667
-
668
- private dispatchSyntheticEvent(event: DragEvent) {
669
- const evt = new Event(event.type as "dragover", event) as any;
670
- const dropPointBoundingBox = (
671
- this.pmView.dom.firstChild as HTMLElement
672
- ).getBoundingClientRect();
673
- evt.clientX = event.clientX;
674
- evt.clientY = event.clientY;
675
-
676
- evt.clientX = Math.min(
677
- Math.max(event.clientX, dropPointBoundingBox.left),
678
- dropPointBoundingBox.left + dropPointBoundingBox.width,
679
- );
680
- evt.clientY = Math.min(
681
- Math.max(event.clientY, dropPointBoundingBox.top),
682
- dropPointBoundingBox.top + dropPointBoundingBox.height,
683
- );
684
-
685
- evt.dataTransfer = event.dataTransfer;
686
- evt.preventDefault = () => event.preventDefault();
687
- evt.synthetic = true; // prevent recursion
688
- this.pmView.dom.dispatchEvent(evt);
689
- }
690
-
691
- // Needed in cases where the editor state updates without the mouse cursor
692
- // moving, as some state updates can require a side menu update. For example,
693
- // adding a button to the side menu which removes the block can cause the
694
- // block below to jump up into the place of the removed block when clicked,
695
- // allowing the user to click the button again without moving the cursor. This
696
- // would otherwise not update the side menu, and so clicking the button again
697
- // would attempt to remove the same block again, causing an error.
698
- update(_view: EditorView, prevState: EditorState) {
699
- const docChanged = !prevState.doc.eq(this.pmView.state.doc);
700
- if (docChanged && this.state?.show) {
701
- this.updateStateFromMousePos();
231
+ function endBlockDrag() {
232
+ unsetDragImage(editor.prosemirrorView.root);
233
+ isDragOrigin = false;
702
234
  }
703
- }
704
235
 
705
- destroy() {
706
- if (this.state?.show) {
707
- this.state.show = false;
708
- this.emitUpdate(this.state);
236
+ /** The pointer rested on its way to the menu: follow it after all. */
237
+ function releaseIntent() {
238
+ intent = undefined;
239
+ refresh();
709
240
  }
710
- this.pmView.root.removeEventListener(
711
- "mousemove",
712
- this.onMouseMove as EventListener,
713
- true,
714
- );
715
- this.pmView.root.removeEventListener(
716
- "dragstart",
717
- this.onDragStart as EventListener,
718
- );
719
- this.pmView.root.removeEventListener(
720
- "dragover",
721
- this.onDragOver as EventListener,
722
- );
723
- this.pmView.root.removeEventListener(
724
- "drop",
725
- this.onDrop as EventListener,
726
- true,
727
- );
728
- this.pmView.root.removeEventListener(
729
- "dragend",
730
- this.onDragEnd as EventListener,
731
- true,
732
- );
733
- this.pmView.root.removeEventListener(
734
- "keydown",
735
- this.onKeyDown as EventListener,
736
- true,
737
- );
738
- }
739
- }
740
-
741
- export const sideMenuPluginKey = new PluginKey("SideMenuPlugin");
742
-
743
- export const SideMenuExtension = createExtension(({ editor }) => {
744
- let view: SideMenuView<any, any, any> | undefined;
745
- const store = createStore<SideMenuState<any, any, any> | undefined>(
746
- undefined,
747
- );
748
-
749
- return {
750
- key: "sideMenu",
751
- store,
752
- prosemirrorPlugins: [
753
- new Plugin({
754
- key: sideMenuPluginKey,
755
- view: (editorView) => {
756
- view = new SideMenuView(editor, editorView, (state) => {
757
- // TODO: Without spreading the state, in some cases like toggling
758
- // `show`, this doesn't trigger an update.
759
- store.setState({ ...state });
760
- });
761
- return view;
762
- },
763
- }),
764
- ],
765
241
 
766
242
  /**
767
- * Handles drag & drop events for blocks.
243
+ * Re-resolves the block for the last pointer position, without intent,
244
+ * e.g. after a document change. If the pointer is on the menu (e.g. it
245
+ * clicked a menu button), this resolves just right of the menu instead:
246
+ * the menu is next to its block, so that point is on the line of the block,
247
+ * in its column. The pointer itself can be over another block, e.g. the
248
+ * "+" of a block in a column is over the column to its left. If the block
249
+ * was removed, the block that moved up into its place takes over.
768
250
  */
769
- blockDragStart(
770
- event: { dataTransfer: DataTransfer | null; clientY: number },
771
- block: Block<any, any, any>,
772
- ) {
773
- if (view) {
774
- view.isDragOrigin = true;
775
- }
776
- dragStart(event, block, editor);
777
- },
778
-
779
- /**
780
- * Handles drag & drop events for blocks.
781
- */
782
- blockDragEnd() {
783
- unsetDragImage(editor.prosemirrorView.root);
784
- if (view) {
785
- view.isDragOrigin = false;
251
+ function refresh() {
252
+ if (!pointer) {
253
+ return;
786
254
  }
255
+ const view = editor.prosemirrorView;
256
+ const menu = menuElement?.isConnected
257
+ ? menuElement.getBoundingClientRect()
258
+ : undefined;
259
+ const onMenu =
260
+ menu !== undefined &&
261
+ pointer.x >= menu.left &&
262
+ pointer.x <= menu.right &&
263
+ pointer.y >= menu.top &&
264
+ pointer.y <= menu.bottom;
265
+ const point = onMenu ? { x: menu.right + 1, y: pointer.y } : pointer;
266
+ // Looks past other UI of the editor at what is underneath.
267
+ const target =
268
+ view.root
269
+ .elementsFromPoint(point.x, point.y)
270
+ .find(
271
+ (element) =>
272
+ view.dom.contains(element) || !editor.isWithinEditor(element),
273
+ ) ?? null;
274
+ update(point, target, false);
275
+ }
787
276
 
788
- editor.blur();
789
- },
277
+ return {
278
+ key: "sideMenu",
279
+ store,
280
+ prosemirrorPlugins: [
281
+ new Plugin({
282
+ key: sideMenuPluginKey,
283
+ appendTransaction: (transactions, _oldState, newState) => {
284
+ if (transactions.some((tr) => tr.getMeta("uiEvent") === "drop")) {
285
+ // Forces a `scrollIntoView` immediately after a drop. Fixes WebKit
286
+ // specific behavior where focusing scrolls the selection into
287
+ // view. This happens on drop before ProseMirror updates the
288
+ // document/selection, which is incorrect.
289
+ return newState.tr.scrollIntoView();
290
+ }
291
+ return null;
292
+ },
293
+ }),
294
+ ],
295
+ mount({
296
+ root,
297
+ signal,
298
+ }: {
299
+ root: Document | ShadowRoot;
300
+ signal: AbortSignal;
301
+ }) {
302
+ registerBlockDropHandlers(
303
+ editor.prosemirrorView,
304
+ () => isDragOrigin,
305
+ options?.sideMenu?.dropMargin ?? 250,
306
+ signal,
307
+ );
790
308
 
791
- /**
792
- * Whether the side menu is currently frozen (e.g. because the drag handle
793
- * menu is open).
794
- */
795
- get menuFrozen() {
796
- return view!.menuFrozen;
797
- },
309
+ // Shows or updates the menu whenever the pointer moves, unless frozen.
310
+ root.addEventListener(
311
+ // `mousemove`, not `pointermove`: iOS Safari delivers a tap as a hover
312
+ // (one compatibility `mousemove` to the tap point, then the click),
313
+ // which is how the side menu shows on phones.
314
+ "mousemove",
315
+ (event) => {
316
+ // Synthetic events created via `new Event("mousemove")` (e.g.
317
+ // dispatched by browser extensions) have no `clientX`/`clientY`,
318
+ // which would make `elementsFromPoint` throw on the resulting
319
+ // non-finite coordinates.
320
+ const { clientX, clientY, buttons } = event as Partial<MouseEvent>;
321
+ if (
322
+ menuFrozen ||
323
+ typeof clientX !== "number" ||
324
+ typeof clientY !== "number" ||
325
+ !Number.isFinite(clientX) ||
326
+ !Number.isFinite(clientY)
327
+ ) {
328
+ return;
329
+ }
330
+ if (isDragOrigin && buttons === 0) {
331
+ // The drag ended without `dragend` reaching the handle (e.g. the
332
+ // menu was unmounted by its controller): clean up now.
333
+ endBlockDrag();
334
+ }
335
+ const point = { x: clientX, y: clientY };
336
+ pointer = point;
337
+ update(point, targetElement(event.target), true);
338
+ },
339
+ { signal, capture: true },
340
+ );
798
341
 
799
- /**
800
- * Freezes the side menu. When frozen, the side menu will stay
801
- * attached to the same block regardless of which block is hovered by the
802
- * mouse cursor.
803
- */
804
- freezeMenu() {
805
- view!.menuFrozen = true;
806
- view!.state!.show = true;
807
- view!.emitUpdate(view!.state!);
808
- },
342
+ // The pointer left the window: no block is hovered any more. Only
343
+ // `mouseout` reports this (with no `relatedTarget`), as `mouseleave`
344
+ // doesn't reach a document or shadow root.
345
+ root.addEventListener(
346
+ "mouseout",
347
+ (event) => {
348
+ if (
349
+ (event as MouseEvent).relatedTarget === null &&
350
+ !menuFrozen &&
351
+ !isDragOrigin
352
+ ) {
353
+ pointer = undefined;
354
+ clearIntent();
355
+ hideMenu();
356
+ }
357
+ },
358
+ { signal, capture: true },
359
+ );
809
360
 
810
- /**
811
- * Unfreezes the side menu. When frozen, the side menu will stay
812
- * attached to the same block regardless of which block is hovered by the
813
- * mouse cursor.
814
- */
815
- unfreezeMenu() {
816
- view!.menuFrozen = false;
817
- view!.state!.show = false;
818
- view!.emitUpdate(view!.state!);
819
- },
361
+ // Hides the menu whenever the user types.
362
+ root.addEventListener(
363
+ "keydown",
364
+ () => {
365
+ if (store.state?.show && editor.isFocused()) {
366
+ clearIntent();
367
+ hideMenu();
368
+ }
369
+ },
370
+ { signal, capture: true },
371
+ );
820
372
 
821
- /**
822
- * Hides the side menu unless it is currently frozen (e.g. the drag
823
- * handle menu is open). Used to dismiss the menu on scroll without
824
- * interfering with open submenus.
825
- */
826
- hideMenuIfNotFrozen() {
827
- if (!view!.menuFrozen && view!.state?.show) {
828
- view!.state.show = false;
829
- view!.emitUpdate(view!.state!);
830
- }
831
- },
832
- } as const;
833
- });
373
+ // Needed in cases where the document changes without the pointer
374
+ // moving: blocks move under it when blocks above it change. For
375
+ // example, a side menu button which removes the block can cause the
376
+ // block below to jump up into the place of the removed block when
377
+ // clicked, allowing the user to click the button again without moving
378
+ // the pointer. This would otherwise not update the side menu, and so
379
+ // clicking the button again would attempt to remove the same block
380
+ // again, causing an error.
381
+ const unsubscribeOnChange = editor.onChange(() => {
382
+ if (store.state?.show) {
383
+ clearIntent();
384
+ refresh();
385
+ }
386
+ });
387
+
388
+ signal.addEventListener("abort", () => {
389
+ unsubscribeOnChange();
390
+ clearIntent();
391
+ hideMenu();
392
+ });
393
+ },
394
+
395
+ /**
396
+ * Registers the element that renders the side menu. Moving the pointer
397
+ * from a block towards this element keeps the menu on that block, and
398
+ * hovering it never changes the block.
399
+ */
400
+ setMenuElement(element: HTMLElement | undefined) {
401
+ menuElement = element;
402
+ },
403
+
404
+ /**
405
+ * Handles drag & drop events for blocks.
406
+ */
407
+ blockDragStart(
408
+ event: { dataTransfer: DataTransfer | null; clientY: number },
409
+ block: Block<any, any, any>,
410
+ ) {
411
+ isDragOrigin = true;
412
+ dragStart(event, block, editor);
413
+ },
414
+
415
+ /**
416
+ * Handles drag & drop events for blocks.
417
+ */
418
+ blockDragEnd() {
419
+ endBlockDrag();
420
+ editor.blur();
421
+ },
422
+
423
+ /**
424
+ * Whether the side menu is currently frozen (e.g. because the drag handle
425
+ * menu is open).
426
+ */
427
+ get menuFrozen() {
428
+ return menuFrozen;
429
+ },
430
+
431
+ /**
432
+ * Freezes the side menu. When frozen, the side menu will stay
433
+ * attached to the same block regardless of which block is hovered by the
434
+ * mouse cursor.
435
+ */
436
+ freezeMenu() {
437
+ menuFrozen = true;
438
+ clearIntent();
439
+ const state = store.state;
440
+ if (state) {
441
+ store.setState({ ...state, show: true });
442
+ }
443
+ },
444
+
445
+ /**
446
+ * Unfreezes the side menu. When frozen, the side menu will stay
447
+ * attached to the same block regardless of which block is hovered by the
448
+ * mouse cursor.
449
+ */
450
+ unfreezeMenu() {
451
+ menuFrozen = false;
452
+ hideMenu();
453
+ },
454
+
455
+ /**
456
+ * Hides the side menu unless it is currently frozen (e.g. the drag
457
+ * handle menu is open). Used to dismiss the menu on scroll without
458
+ * interfering with open submenus.
459
+ */
460
+ hideMenuIfNotFrozen() {
461
+ // A drag can scroll the page, but hiding the menu would remove the drag
462
+ // source (see `update`).
463
+ if (!menuFrozen && !isDragOrigin) {
464
+ clearIntent();
465
+ hideMenu();
466
+ }
467
+ },
468
+ } as const;
469
+ },
470
+ );