@@ -26,46 +26,26 @@ export type SideMenuState<
26
26
block : Block < BSchema , I , S > ;
27
27
} ;
28
28
29
- export function getDraggableBlockFromCoords (
30
- coords : { left : number ; top : number } ,
29
+ export function getDraggableBlockFromElement (
30
+ element : Element ,
31
31
view : EditorView
32
32
) {
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
-
50
33
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" )
55
38
) {
56
- node = node . parentNode as HTMLElement ;
39
+ element = element . parentElement ;
57
40
}
58
- if ( ! node ) {
41
+ if ( ! element . hasAttribute ( "data-id" ) ) {
59
42
return undefined ;
60
43
}
61
- return { node, id : node . getAttribute ( "data-id" ) ! } ;
44
+ return { node : element as HTMLElement , id : element . getAttribute ( "data-id" ) ! } ;
62
45
}
63
46
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 ) ;
69
49
70
50
if ( block && block . node . nodeType === 1 ) {
71
51
// TODO: this uses undocumented PM APIs? do we need this / let's add docs?
@@ -197,7 +177,21 @@ function dragStart<
197
177
top : e . clientY ,
198
178
} ;
199
179
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 ) ;
201
195
if ( pos != null ) {
202
196
const selection = view . state . selection ;
203
197
const doc = view . state . doc ;
@@ -327,7 +321,16 @@ export class SideMenuView<
327
321
left : editorBoundingBox . left + editorBoundingBox . width / 2 , // take middle of editor
328
322
top : this . mousePos . y ,
329
323
} ;
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
+ }
331
334
332
335
// Closes the menu if the mouse cursor is beyond the editor vertically.
333
336
if ( ! block || ! this . editor . isEditable ) {
0 commit comments