77 isLocationChildrenDetail ,
88} from '@alilc/lowcode-utils' ;
99import {
10- DragObject ,
10+ IPublicModelDragObject ,
1111 IPublicModelScrollable ,
1212 ISensor ,
1313 IPublicTypeLocationChildrenDetail ,
@@ -50,7 +50,6 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
5050 setup ( ) ;
5151 }
5252
53-
5453 /** -------------------- ISensor begin -------------------- */
5554
5655 private indentTrack = new IndentTrack ( ) ;
@@ -107,12 +106,12 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
107106 const document = project . getCurrentDocument ( ) ;
108107 const pos = getPosFromEvent ( e , this . _shell ) ;
109108 const irect = this . getInsertionRect ( ) ;
110- const originLoc = document . dropLocation ;
109+ const originLoc = document ? .dropLocation ;
111110
112- const componentMeta = e . dragObject . nodes ? e . dragObject . nodes [ 0 ] . componentMeta : null ;
113- if ( e . dragObject . type === 'node' && componentMeta && componentMeta . isModal ) {
111+ const componentMeta = e . dragObject ? .nodes ? e . dragObject . nodes [ 0 ] . componentMeta : null ;
112+ if ( e . dragObject ? .type === 'node' && componentMeta && componentMeta . isModal && document ?. focusNode ) {
114113 return canvas . createLocation ( {
115- target : document . focusNode ,
114+ target : document ? .focusNode ,
116115 detail : {
117116 type : IPublicTypeLocationDetailType . Children ,
118117 index : 0 ,
@@ -123,7 +122,9 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
123122 } ) ;
124123 }
125124
126- if ( originLoc && ( ( pos && pos === 'unchanged' ) || ( irect && globalY >= irect . top && globalY <= irect . bottom ) ) ) {
125+ if ( originLoc
126+ && ( ( pos && pos === 'unchanged' ) || ( irect && globalY >= irect . top && globalY <= irect . bottom ) )
127+ && dragObject ) {
127128 const loc = originLoc . clone ( e ) ;
128129 const indented = this . indentTrack . getIndentParent ( originLoc , loc ) ;
129130 if ( indented ) {
@@ -138,7 +139,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
138139 detail : {
139140 type : IPublicTypeLocationDetailType . Children ,
140141 index,
141- valid : document . checkNesting ( parent , e . dragObject as any ) ,
142+ valid : document ? .checkNesting ( parent , e . dragObject as any ) ,
142143 } ,
143144 } ) ;
144145 }
@@ -177,7 +178,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
177178 }
178179 }
179180 if ( p !== node ) {
180- node = p || document . focusNode ;
181+ node = p || document ? .focusNode ;
181182 treeNode = tree . getTreeNode ( node ) ;
182183 focusSlots = false ;
183184 }
@@ -258,7 +259,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
258259 cancelIdleCallback ( this . tryScrollAgain ) ;
259260 this . tryScrollAgain = null ;
260261 }
261- if ( this . sensing || ! this . bounds || ! this . scroller || ! this . scrollTarget ) {
262+ if ( ! this . bounds || ! this . scroller || ! this . scrollTarget ) {
262263 // is a active sensor
263264 return ;
264265 }
@@ -305,7 +306,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
305306 focus = { type : 'slots' } ;
306307 } else {
307308 index = 0 ;
308- valid = document . checkNesting ( target , event . dragObject as any ) ;
309+ valid = ! ! document ? .checkNesting ( target , event . dragObject as any ) ;
309310 }
310311 canvas . createLocation ( {
311312 target,
@@ -320,23 +321,28 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
320321 } ) ;
321322 } ) ;
322323
323- private getNear ( treeNode : TreeNode , e : IPublicModelLocateEvent , index ?: number , rect ?: DOMRect ) {
324+ private getNear ( treeNode : TreeNode , e : IPublicModelLocateEvent , originalIndex ?: number , originalRect ?: DOMRect ) {
324325 const { canvas, project } = this . pluginContext ;
325326 const document = project . getCurrentDocument ( ) ;
326327 const { globalY, dragObject } = e ;
328+ if ( ! dragObject ) {
329+ return null ;
330+ }
327331 // TODO: check dragObject is anyData
328332 const { node, expanded } = treeNode ;
333+ let rect = originalRect ;
329334 if ( ! rect ) {
330335 rect = this . getTreeNodeRect ( treeNode ) ;
331336 if ( ! rect ) {
332337 return null ;
333338 }
334339 }
340+ let index = originalIndex ;
335341 if ( index == null ) {
336342 index = node . index ;
337343 }
338344
339- if ( node . isSlot ) {
345+ if ( node . isSlotNode ) {
340346 // 是个插槽根节点
341347 if ( ! treeNode . isContainer ( ) && ! treeNode . hasSlots ( ) ) {
342348 return canvas . createLocation ( {
@@ -385,7 +391,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
385391 detail : {
386392 type : IPublicTypeLocationDetailType . Children ,
387393 index,
388- valid : document . checkNesting ( node . parent ! , dragObject as any ) ,
394+ valid : document ? .checkNesting ( node . parent ! , dragObject as any ) ,
389395 near : { node, pos : 'before' } ,
390396 focus : checkRecursion ( focusNode , dragObject ) ? { type : 'node' , node : focusNode } : undefined ,
391397 } ,
@@ -412,7 +418,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
412418 detail : {
413419 type : IPublicTypeLocationDetailType . Children ,
414420 index : index + 1 ,
415- valid : document . checkNesting ( node . parent ! , dragObject as any ) ,
421+ valid : document ? .checkNesting ( node . parent ! , dragObject as any ) ,
416422 near : { node, pos : 'after' } ,
417423 focus : checkRecursion ( focusNode , dragObject ) ? { type : 'node' , node : focusNode } : undefined ,
418424 } ,
@@ -423,6 +429,9 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
423429 const { canvas, project } = this . pluginContext ;
424430 const document = project . getCurrentDocument ( ) ;
425431 const { dragObject, globalY } = e ;
432+ if ( ! dragObject ) {
433+ return null ;
434+ }
426435
427436 if ( ! checkRecursion ( treeNode . node , dragObject ) ) {
428437 return null ;
@@ -454,7 +463,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
454463 detail . valid = false ;
455464 } else {
456465 detail . index = 0 ;
457- detail . valid = document . checkNesting ( container , dragObject ) ;
466+ detail . valid = document ? .checkNesting ( container , dragObject ) ;
458467 }
459468 }
460469
@@ -526,7 +535,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
526535 } else {
527536 detail . index = l ;
528537 }
529- detail . valid = document . checkNesting ( container , dragObject ) ;
538+ detail . valid = document ? .checkNesting ( container , dragObject ) ;
530539 }
531540
532541 return canvas . createLocation ( locationData ) ;
@@ -572,9 +581,26 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
572581 return ;
573582 }
574583 this . _shell = shell ;
584+ const { canvas, project } = this . pluginContext ;
575585 if ( shell ) {
576- this . _scrollTarget = this . pluginContext . canvas . createScrollTarget ( shell ) ;
586+ this . _scrollTarget = canvas . createScrollTarget ( shell ) ;
577587 this . _sensorAvailable = true ;
588+
589+ // check if there is current selection and scroll to it
590+ const selection = project . currentDocument ?. selection ;
591+ const topNodes = selection ?. getTopNodes ( true ) ;
592+ const tree = this . treeMaster ?. currentTree ;
593+ if ( topNodes && topNodes [ 0 ] && tree ) {
594+ const treeNode = tree . getTreeNodeById ( topNodes [ 0 ] . id ) ;
595+ if ( treeNode ) {
596+ // at this moment, it is possible that pane is not ready yet, so
597+ // put ui related operations to the next loop
598+ setTimeout ( ( ) => {
599+ tree . setNodeSelected ( treeNode . id ) ;
600+ this . scrollToNode ( treeNode , null , 4 ) ;
601+ } , 0 ) ;
602+ }
603+ }
578604 } else {
579605 this . _scrollTarget = undefined ;
580606 this . _sensorAvailable = false ;
@@ -610,7 +636,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
610636 }
611637}
612638
613- function checkRecursion ( parent : IPublicModelNode | undefined | null , dragObject : DragObject ) : boolean {
639+ function checkRecursion ( parent : IPublicModelNode | undefined | null , dragObject : IPublicModelDragObject ) : boolean {
614640 if ( ! parent ) {
615641 return false ;
616642 }
@@ -633,14 +659,14 @@ function getPosFromEvent(
633659 if ( target . matches ( '.insertion' ) ) {
634660 return 'unchanged' ;
635661 }
636- target = target . closest ( '[data-id]' ) ;
637- if ( ! target || ! stop . contains ( target ) ) {
662+ const closest = target . closest ( '[data-id]' ) ;
663+ if ( ! closest || ! stop . contains ( closest ) ) {
638664 return null ;
639665 }
640666
641- const nodeId = ( target as HTMLDivElement ) . dataset . id ! ;
667+ const nodeId = ( closest as HTMLDivElement ) . dataset . id ! ;
642668 return {
643- focusSlots : target . matches ( '.tree-node-slots' ) ,
669+ focusSlots : closest . matches ( '.tree-node-slots' ) ,
644670 nodeId,
645671 } ;
646672}
0 commit comments