@@ -18,6 +18,7 @@ import { useInteractions, useFloating, useClick } from '@floating-ui/react';
18
18
import { useEditor } from 'novel' ;
19
19
import { useState } from 'react' ;
20
20
21
+ import type { EditorInstance } from 'novel' ;
21
22
import type { ReactNode } from 'react' ;
22
23
23
24
function Item ( { children, onClick } : { children : ReactNode ; onClick : ( ) => void } ) {
@@ -41,15 +42,25 @@ function DragHandle() {
41
42
const click = useClick ( context ) ;
42
43
const { getReferenceProps, getFloatingProps } = useInteractions ( [ click ] ) ;
43
44
45
+ const getDragHandlePos = ( editor : EditorInstance ) => {
46
+ const { left, top } = refs . reference . current ! . getBoundingClientRect ( ) ;
47
+ const posAtCoords = editor . view . posAtCoords ( { left : left + 50 , top : top + 1 } ) ;
48
+
49
+ return posAtCoords ;
50
+ } ;
51
+
44
52
const insertNewBlockBelow = ( ) => {
45
53
if ( ! editor ) return ;
46
54
47
55
return editor
48
56
. chain ( )
49
57
. focus ( )
50
58
. command ( ( { tr } ) => {
51
- const { $from } = tr . selection ;
52
- const insertPos = $from . after ( $from . depth ) ;
59
+ const posAtCoords = getDragHandlePos ( editor ) ;
60
+ if ( ! posAtCoords ) return false ;
61
+
62
+ const $pos = editor . state . doc . resolve ( posAtCoords . pos ) ;
63
+ const insertPos = $pos . after ( $pos . depth ) ;
53
64
tr . insert ( insertPos , editor . schema . nodes . paragraph . create ( ) ) ;
54
65
55
66
setIsOpen ( false ) ;
@@ -64,8 +75,13 @@ function DragHandle() {
64
75
. chain ( )
65
76
. focus ( )
66
77
. command ( ( { tr } ) => {
67
- const { $from } = tr . selection ;
68
- tr . delete ( $from . before ( $from . depth ) , $from . after ( $from . depth ) ) ;
78
+ const posAtCoords = getDragHandlePos ( editor ) ;
79
+ if ( ! posAtCoords ) return false ;
80
+
81
+ const $pos = editor . state . doc . resolve ( posAtCoords . pos ) ;
82
+ const from = $pos . before ( $pos . depth ) ;
83
+ const to = $pos . after ( $pos . depth ) ;
84
+ tr . delete ( from , to ) ;
69
85
70
86
setIsOpen ( false ) ;
71
87
return true ;
0 commit comments