11import { EditorUiContext } from "../ui/framework/core" ;
22import {
3- $createParagraphNode , $getRoot ,
4- $getSelection ,
3+ $createParagraphNode , $getNearestNodeFromDOMNode , $getRoot ,
54 $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 ,
116 LexicalNode
127} from "lexical" ;
138import { $isImageNode } from "@lexical/rich-text/LexicalImageNode" ;
149import { $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" ;
2010import { $isDiagramNode } from "../utils/diagrams" ;
2111import { $isTableNode } from "@lexical/table" ;
12+ import { $isDetailsNode } from "@lexical/rich-text/LexicalDetailsNode" ;
2213
2314function 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 ( ) ;
2534}
2635
2736function insertBelowLastNode ( context : EditorUiContext , event : MouseEvent ) : boolean {
28- const lastNode = $getRoot ( ) . getLastChild ( ) ;
37+ const contextNode = $getContextNode ( event ) ;
38+ const lastNode = contextNode . getLastChild ( ) ;
2939 if ( ! lastNode || ! isHardToEscapeNode ( lastNode ) ) {
3040 return false ;
3141 }
@@ -40,7 +50,7 @@ function insertBelowLastNode(context: EditorUiContext, event: MouseEvent): boole
4050 if ( isClickBelow ) {
4151 context . editor . update ( ( ) => {
4252 const newNode = $createParagraphNode ( ) ;
43- $getRoot ( ) . append ( newNode ) ;
53+ contextNode . append ( newNode ) ;
4454 newNode . select ( ) ;
4555 } ) ;
4656 return true ;
@@ -49,7 +59,6 @@ function insertBelowLastNode(context: EditorUiContext, event: MouseEvent): boole
4959 return false ;
5060}
5161
52-
5362export function registerMouseHandling ( context : EditorUiContext ) : ( ) => void {
5463 const unregisterClick = context . editor . registerCommand ( CLICK_COMMAND , ( event ) : boolean => {
5564 insertBelowLastNode ( context , event ) ;
0 commit comments