@@ -26,46 +26,26 @@ export type SideMenuState<
2626 block : Block < BSchema , I , S > ;
2727} ;
2828
29- export function getDraggableBlockFromCoords (
30- coords : { left : number ; top : number } ,
29+ export function getDraggableBlockFromElement (
30+ element : Element ,
3131 view : EditorView
3232) {
33- if ( ! view . dom . isConnected ) {
34- // view is not connected to the DOM, this can cause posAtCoords to fail
35- // (Cannot read properties of null (reading 'nearestDesc'), https://github.com/TypeCellOS/BlockNote/issues/123)
36- return undefined ;
37- }
38-
39- const pos = view . posAtCoords ( coords ) ;
40- if ( ! pos ) {
41- return undefined ;
42- }
43- let node = view . domAtPos ( pos . pos ) . node as HTMLElement ;
44-
45- if ( node === view . dom ) {
46- // mouse over root
47- return undefined ;
48- }
49-
5033 while (
51- node &&
52- node . parentNode &&
53- node . parentNode !== view . dom &&
54- ! node . hasAttribute ?.( "data-id" )
34+ element &&
35+ element . parentElement &&
36+ element . parentElement !== view . dom &&
37+ ! element . hasAttribute ?.( "data-id" )
5538 ) {
56- node = node . parentNode as HTMLElement ;
39+ element = element . parentElement ;
5740 }
58- if ( ! node ) {
41+ if ( ! element . hasAttribute ( "data-id" ) ) {
5942 return undefined ;
6043 }
61- return { node, id : node . getAttribute ( "data-id" ) ! } ;
44+ return { node : element as HTMLElement , id : element . getAttribute ( "data-id" ) ! } ;
6245}
6346
64- function blockPositionFromCoords (
65- coords : { left : number ; top : number } ,
66- view : EditorView
67- ) {
68- const block = getDraggableBlockFromCoords ( coords , view ) ;
47+ function blockPositionFromElement ( element : Element , view : EditorView ) {
48+ const block = getDraggableBlockFromElement ( element , view ) ;
6949
7050 if ( block && block . node . nodeType === 1 ) {
7151 // TODO: this uses undocumented PM APIs? do we need this / let's add docs?
@@ -197,7 +177,21 @@ function dragStart<
197177 top : e . clientY ,
198178 } ;
199179
200- const pos = blockPositionFromCoords ( coords , view ) ;
180+ const elements = document . elementsFromPoint ( coords . left , coords . top ) ;
181+ let blockEl = undefined ;
182+
183+ for ( const element of elements ) {
184+ if ( view . dom . contains ( element ) ) {
185+ blockEl = getDraggableBlockFromElement ( element , view ) ;
186+ break ;
187+ }
188+ }
189+
190+ if ( ! blockEl ) {
191+ return ;
192+ }
193+
194+ const pos = blockPositionFromElement ( blockEl . node , view ) ;
201195 if ( pos != null ) {
202196 const selection = view . state . selection ;
203197 const doc = view . state . doc ;
@@ -327,7 +321,16 @@ export class SideMenuView<
327321 left : editorBoundingBox . left + editorBoundingBox . width / 2 , // take middle of editor
328322 top : this . mousePos . y ,
329323 } ;
330- const block = getDraggableBlockFromCoords ( coords , this . pmView ) ;
324+
325+ const elements = document . elementsFromPoint ( coords . left , coords . top ) ;
326+ let block = undefined ;
327+
328+ for ( const element of elements ) {
329+ if ( this . pmView . dom . contains ( element ) ) {
330+ block = getDraggableBlockFromElement ( element , this . pmView ) ;
331+ break ;
332+ }
333+ }
331334
332335 // Closes the menu if the mouse cursor is beyond the editor vertically.
333336 if ( ! block || ! this . editor . isEditable ) {
0 commit comments