1
1
import { EditorUiContext } from "../ui/framework/core" ;
2
2
import {
3
- $createParagraphNode , $getRoot ,
4
- $getSelection ,
3
+ $createParagraphNode , $getNearestNodeFromDOMNode , $getRoot ,
5
4
$isDecoratorNode , CLICK_COMMAND ,
6
- COMMAND_PRIORITY_LOW , KEY_ARROW_DOWN_COMMAND , KEY_ARROW_UP_COMMAND ,
7
- KEY_BACKSPACE_COMMAND ,
8
- KEY_DELETE_COMMAND ,
9
- KEY_ENTER_COMMAND , KEY_TAB_COMMAND ,
10
- LexicalEditor ,
5
+ COMMAND_PRIORITY_LOW , ElementNode ,
11
6
LexicalNode
12
7
} from "lexical" ;
13
8
import { $isImageNode } from "@lexical/rich-text/LexicalImageNode" ;
14
9
import { $isMediaNode } from "@lexical/rich-text/LexicalMediaNode" ;
15
- import { getLastSelection } from "../utils/selection" ;
16
- import { $getNearestNodeBlockParent , $getParentOfType , $selectOrCreateAdjacent } from "../utils/nodes" ;
17
- import { $setInsetForSelection } from "../utils/lists" ;
18
- import { $isListItemNode } from "@lexical/list" ;
19
- import { $isDetailsNode , DetailsNode } from "@lexical/rich-text/LexicalDetailsNode" ;
20
10
import { $isDiagramNode } from "../utils/diagrams" ;
21
11
import { $isTableNode } from "@lexical/table" ;
12
+ import { $isDetailsNode } from "@lexical/rich-text/LexicalDetailsNode" ;
22
13
23
14
function isHardToEscapeNode ( node : LexicalNode ) : boolean {
24
- return $isDecoratorNode ( node ) || $isImageNode ( node ) || $isMediaNode ( node ) || $isDiagramNode ( node ) || $isTableNode ( node ) ;
15
+ return $isDecoratorNode ( node )
16
+ || $isImageNode ( node )
17
+ || $isMediaNode ( node )
18
+ || $isDiagramNode ( node )
19
+ || $isTableNode ( node )
20
+ || $isDetailsNode ( node ) ;
21
+ }
22
+
23
+ function $getContextNode ( event : MouseEvent ) : ElementNode {
24
+ if ( event . target instanceof HTMLElement ) {
25
+ const nearestDetails = event . target . closest ( 'details' ) ;
26
+ if ( nearestDetails ) {
27
+ const detailsNode = $getNearestNodeFromDOMNode ( nearestDetails ) ;
28
+ if ( $isDetailsNode ( detailsNode ) ) {
29
+ return detailsNode ;
30
+ }
31
+ }
32
+ }
33
+ return $getRoot ( ) ;
25
34
}
26
35
27
36
function insertBelowLastNode ( context : EditorUiContext , event : MouseEvent ) : boolean {
28
- const lastNode = $getRoot ( ) . getLastChild ( ) ;
37
+ const contextNode = $getContextNode ( event ) ;
38
+ const lastNode = contextNode . getLastChild ( ) ;
29
39
if ( ! lastNode || ! isHardToEscapeNode ( lastNode ) ) {
30
40
return false ;
31
41
}
@@ -40,7 +50,7 @@ function insertBelowLastNode(context: EditorUiContext, event: MouseEvent): boole
40
50
if ( isClickBelow ) {
41
51
context . editor . update ( ( ) => {
42
52
const newNode = $createParagraphNode ( ) ;
43
- $getRoot ( ) . append ( newNode ) ;
53
+ contextNode . append ( newNode ) ;
44
54
newNode . select ( ) ;
45
55
} ) ;
46
56
return true ;
@@ -49,7 +59,6 @@ function insertBelowLastNode(context: EditorUiContext, event: MouseEvent): boole
49
59
return false ;
50
60
}
51
61
52
-
53
62
export function registerMouseHandling ( context : EditorUiContext ) : ( ) => void {
54
63
const unregisterClick = context . editor . registerCommand ( CLICK_COMMAND , ( event ) : boolean => {
55
64
insertBelowLastNode ( context , event ) ;
0 commit comments