@@ -38,72 +38,64 @@ export class Dnd {
3838 }
3939 }
4040
41+ isInsideCanvas ( e : PointerEvent ) : boolean {
42+ const overlay = this . lf . graphModel . rootEl . querySelector (
43+ '[name="canvas-overlay"]' ,
44+ ) as HTMLElement | null
45+ const topEl = window . document . elementFromPoint (
46+ e . clientX ,
47+ e . clientY ,
48+ ) as HTMLElement | null
49+ return (
50+ topEl === overlay ||
51+ ( topEl !== null && ! ! overlay && overlay . contains ( topEl ) )
52+ )
53+ }
4154 startDrag ( nodeConfig : OnDragNodeConfig ) {
4255 const { editConfigModel } = this . lf . graphModel
43- if ( ! editConfigModel ?. isSilentMode ) {
44- this . nodeConfig = nodeConfig
45- // 指针移动:根据命中结果判断是否在画布覆盖层上,驱动假节点创建/移动或清理
46- this . docPointerMove = ( e : PointerEvent ) => {
47- if ( ! this . nodeConfig ) return
48- // 获取画布覆盖层元素(仅在其自身或后代命中时视为“在画布内”)
49- const overlay = this . lf . graphModel . rootEl . querySelector (
50- '[name="canvas-overlay"]' ,
51- ) as HTMLElement | null
52- // 获取当前指针位置下最上层的DOM元素,判断当前指针是否“在画布上”
53- const topEl = window . document . elementFromPoint (
54- e . clientX ,
55- e . clientY ,
56- ) as HTMLElement | null
57- const inside = topEl === overlay || ( topEl && overlay ?. contains ( topEl ) )
58- // 离开画布:清理吸附线与假节点
59- if ( ! inside ) {
60- this . onDragLeave ( )
61- return
62- }
63- // 首次进入画布:创建假节点并初始化位置
64- if ( ! this . fakeNode ) {
65- this . dragEnter ( e )
66- return
67- }
68- // 在画布内移动:更新假节点位置与吸附线
69- this . onDragOver ( e )
56+ if ( editConfigModel ?. isSilentMode ) return
57+ this . nodeConfig = nodeConfig
58+ // 指针移动:根据命中结果判断是否在画布覆盖层上,驱动假节点创建/移动或清理
59+ this . docPointerMove = ( e : PointerEvent ) => {
60+ if ( ! this . nodeConfig ) return
61+ // 离开画布:清理吸附线与假节点
62+ if ( ! this . isInsideCanvas ( e ) ) {
63+ this . onDragLeave ( )
64+ return
7065 }
71- // 指针抬起:在画布内落点生成节点,否则清理假节点
72- this . docPointerUp = ( e : PointerEvent ) => {
73- if ( ! this . nodeConfig ) return
74- const overlay = this . lf . graphModel . rootEl . querySelector (
75- '[name="canvas-overlay"]' ,
76- ) as HTMLElement | null
77- const topEl = window . document . elementFromPoint (
78- e . clientX ,
79- e . clientY ,
80- ) as HTMLElement | null
81- const inside = topEl === overlay || ( topEl && overlay ?. contains ( topEl ) )
82- if ( inside ) {
83- this . onDrop ( e )
84- } else {
85- this . onDragLeave ( )
86- }
87- // 阻止默认行为与冒泡,避免滚动/点击穿透
88- e . preventDefault ( )
89- e . stopPropagation ( )
90- // 结束拖拽并移除监听
91- this . stopDrag ( )
66+ // 首次进入画布:创建假节点并初始化位置
67+ if ( ! this . fakeNode ) {
68+ this . dragEnter ( e )
69+ return
9270 }
93- window . document . addEventListener ( 'pointermove' , this . docPointerMove )
94- window . document . addEventListener ( 'pointerup' , this . docPointerUp )
71+ // 在画布内移动:更新假节点位置与吸附线
72+ this . onDragOver ( e )
9573 }
74+ // 指针抬起:在画布内落点生成节点,否则清理假节点
75+ this . docPointerUp = ( e : PointerEvent ) => {
76+ if ( ! this . nodeConfig ) return
77+ if ( this . isInsideCanvas ( e ) ) {
78+ this . onDrop ( e )
79+ } else {
80+ this . onDragLeave ( )
81+ }
82+ // 阻止默认行为与冒泡,避免滚动/点击穿透
83+ e . preventDefault ( )
84+ e . stopPropagation ( )
85+ // 结束拖拽并移除监听
86+ this . stopDrag ( )
87+ }
88+ window . document . addEventListener ( 'pointermove' , this . docPointerMove )
89+ window . document . addEventListener ( 'pointerup' , this . docPointerUp )
9690 }
9791
9892 stopDrag = ( ) => {
9993 this . nodeConfig = null
10094 if ( this . docPointerMove ) {
10195 window . document . removeEventListener ( 'pointermove' , this . docPointerMove )
102- this . docPointerMove = undefined
10396 }
10497 if ( this . docPointerUp ) {
10598 window . document . removeEventListener ( 'pointerup' , this . docPointerUp )
106- this . docPointerUp = undefined
10799 }
108100 }
109101 dragEnter = ( e : PointerEvent ) => {
@@ -165,17 +157,6 @@ export class Dnd {
165157 this . lf . graphModel . removeFakeNode ( )
166158 this . fakeNode = null
167159 }
168-
169- // eventMap() {
170- // return {
171- // // onPointerEnter: this.dragEnter,
172- // // onPointerOver: this.dragEnter, // IE11
173- // // onMouseMove: this.onDragOver,
174- // // onPointerLeave: this.onDragLeave,
175- // // onMouseOut: this.onDragLeave, // IE11
176- // // onMouseUp: this.onDrop,
177- // }
178- // }
179160}
180161
181162export default Dnd
0 commit comments