@@ -67,7 +67,7 @@ import { BuiltinSimulatorRenderer } from './renderer';
6767import { clipboard } from '../designer/clipboard' ;
6868import { LiveEditing } from './live-editing/live-editing' ;
6969import { Project } from '../project' ;
70- import { Scroller } from '../designer/scroller' ;
70+ import { IScroller } from '../designer/scroller' ;
7171import { isElementNode , isDOMNodeVisible } from '../utils/misc' ;
7272import { debounce } from 'lodash' ;
7373
@@ -170,7 +170,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
170170
171171 readonly viewport = new Viewport ( ) ;
172172
173- readonly scroller : Scroller ;
173+ readonly scroller : IScroller ;
174174
175175 readonly emitter : IEventBus = createModuleEventBus ( 'BuiltinSimulatorHost' ) ;
176176
@@ -381,7 +381,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
381381 // todo
382382 }
383383
384- mountViewport ( viewport : Element | null ) {
384+ mountViewport ( viewport : HTMLElement | null ) {
385385 this . viewport . mount ( viewport ) ;
386386 }
387387
@@ -510,7 +510,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
510510 // TODO: dispose the bindings
511511 }
512512
513- async setupComponents ( library ) {
513+ async setupComponents ( library : LibraryItem [ ] ) {
514514 const libraryAsset : AssetList = this . buildLibrary ( library ) ;
515515 await this . renderer ?. load ( libraryAsset ) ;
516516 if ( Object . keys ( this . asyncLibraryMap ) . length > 0 ) {
@@ -576,7 +576,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
576576 const isRGLNode = rglNode ?. isRGLContainer ;
577577 if ( isRGLNode ) {
578578 // 如果拖拽的是磁铁块的右下角 handle,则直接跳过
579- if ( downEvent . target . classList . contains ( 'react-resizable-handle' ) ) return ;
579+ if ( downEvent . target ? .classList . contains ( 'react-resizable-handle' ) ) return ;
580580 // 禁止多选
581581 isMulti = false ;
582582 designer . dragon . emitter . emit ( 'rgl.switch' , {
@@ -605,15 +605,17 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
605605 if ( ! isShaken ( downEvent , e ) || isRGLNode ) {
606606 let { id } = node ;
607607 designer . activeTracker . track ( { node, instance : nodeInst ?. instance } ) ;
608- if ( isMulti && ! node . contains ( focusNode ) && selection . has ( id ) ) {
608+ if ( isMulti && focusNode && ! node . contains ( focusNode ) && selection . has ( id ) ) {
609609 selection . remove ( id ) ;
610610 } else {
611611 // TODO: 避免选中 Page 组件,默认选中第一个子节点;新增规则 或 判断 Live 模式
612612 if ( node . isPage ( ) && node . getChildren ( ) ?. notEmpty ( ) && this . designMode === 'live' ) {
613613 const firstChildId = node . getChildren ( ) ?. get ( 0 ) ?. getId ( ) ;
614614 if ( firstChildId ) id = firstChildId ;
615615 }
616- selection . select ( node . contains ( focusNode ) ? focusNode . id : id ) ;
616+ if ( focusNode ) {
617+ selection . select ( node . contains ( focusNode ) ? focusNode . id : id ) ;
618+ }
617619
618620 // dirty code should refector
619621 const editor = this . designer ?. editor ;
@@ -629,8 +631,8 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
629631 }
630632 } ;
631633
632- if ( isLeftButton && ! node . contains ( focusNode ) ) {
633- let nodes : Node [ ] = [ node ] ;
634+ if ( isLeftButton && focusNode && ! node . contains ( focusNode ) ) {
635+ let nodes : INode [ ] = [ node ] ;
634636 let ignoreUpSelected = false ;
635637 if ( isMulti ) {
636638 // multi select mode, directily add
@@ -639,7 +641,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
639641 selection . add ( node . id ) ;
640642 ignoreUpSelected = true ;
641643 }
642- selection . remove ( focusNode . id ) ;
644+ focusNode ?. id && selection . remove ( focusNode . id ) ;
643645 // 获得顶层 nodes
644646 nodes = selection . getTopNodes ( ) ;
645647 } else if ( selection . containsNode ( node , true ) ) {
@@ -727,7 +729,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
727729 if ( nodeInst ?. node ) {
728730 let { node } = nodeInst ;
729731 const focusNode = node . document ?. focusNode ;
730- if ( node . contains ( focusNode ) ) {
732+ if ( focusNode && node . contains ( focusNode ) ) {
731733 node = focusNode ;
732734 }
733735 detecting . capture ( node ) ;
@@ -738,7 +740,9 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
738740 e . stopPropagation ( ) ;
739741 }
740742 } ;
741- const leave = ( ) => detecting . leave ( this . project . currentDocument ) ;
743+ const leave = ( ) => {
744+ this . project . currentDocument && detecting . leave ( this . project . currentDocument )
745+ } ;
742746
743747 doc . addEventListener ( 'mouseover' , hover , true ) ;
744748 doc . addEventListener ( 'mouseleave' , leave , false ) ;
@@ -912,8 +916,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
912916 /**
913917 * @see ISimulator
914918 */
915- getComponentInstances ( node : Node , context ?: IPublicTypeNodeInstance ) : IPublicTypeComponentInstance [ ] | null {
916- const docId = node . document . id ;
919+ getComponentInstances ( node : INode , context ?: IPublicTypeNodeInstance ) : IPublicTypeComponentInstance [ ] | null {
920+ const docId = node . document ?. id ;
921+ if ( ! docId ) {
922+ return null ;
923+ }
917924
918925 const instances = this . instancesMap [ docId ] ?. get ( node . id ) || null ;
919926 if ( ! instances || ! context ) {
@@ -946,7 +953,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
946953 /**
947954 * @see ISimulator
948955 */
949- computeRect ( node : Node ) : IPublicTypeRect | null {
956+ computeRect ( node : INode ) : IPublicTypeRect | null {
950957 const instances = this . getComponentInstances ( node ) ;
951958 if ( ! instances ) {
952959 return null ;
@@ -1042,7 +1049,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
10421049 /**
10431050 * 通过 DOM 节点获取节点,依赖 simulator 的接口
10441051 */
1045- getNodeInstanceFromElement ( target : Element | null ) : IPublicTypeNodeInstance < IPublicTypeComponentInstance > | null {
1052+ getNodeInstanceFromElement ( target : Element | null ) : IPublicTypeNodeInstance < IPublicTypeComponentInstance , INode > | null {
10461053 if ( ! target ) {
10471054 return null ;
10481055 }
@@ -1215,7 +1222,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
12151222 return null ;
12161223 }
12171224 const dropContainer = this . getDropContainer ( e ) ;
1218- const lockedNode = getClosestNode ( dropContainer ?. container as Node , ( node ) => node . isLocked ) ;
1225+ const lockedNode = getClosestNode ( dropContainer ?. container , ( node ) => node . isLocked ) ;
12191226 if ( lockedNode ) return null ;
12201227 if (
12211228 ! dropContainer
@@ -1257,7 +1264,8 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
12571264 e . dragObject &&
12581265 e . dragObject . nodes &&
12591266 e . dragObject . nodes . length &&
1260- e . dragObject . nodes [ 0 ] . componentMeta . isModal
1267+ e . dragObject . nodes [ 0 ] . componentMeta . isModal &&
1268+ document . focusNode
12611269 ) {
12621270 return this . designer . createLocation ( {
12631271 target : document . focusNode ,
@@ -1372,8 +1380,8 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
13721380 const isAny = isDragAnyObject ( dragObject ) ;
13731381 const document = this . project . currentDocument ! ;
13741382 const { currentRoot } = document ;
1375- let container : INode ;
1376- let nodeInstance : IPublicTypeNodeInstance < IPublicTypeComponentInstance > | undefined ;
1383+ let container : INode | null ;
1384+ let nodeInstance : IPublicTypeNodeInstance < IPublicTypeComponentInstance , INode > | undefined ;
13771385
13781386 if ( target ) {
13791387 const ref = this . getNodeInstanceFromElement ( target ) ;
@@ -1391,8 +1399,8 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
13911399 container = currentRoot ;
13921400 }
13931401
1394- if ( ! container . isParental ( ) ) {
1395- container = container . parent || currentRoot ;
1402+ if ( ! container ? .isParental ( ) ) {
1403+ container = container ? .parent || currentRoot ;
13961404 }
13971405
13981406 // TODO: use spec container to accept specialData
@@ -1402,7 +1410,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
14021410 }
14031411
14041412 // get common parent, avoid drop container contains by dragObject
1405- const drillDownExcludes = new Set < Node > ( ) ;
1413+ const drillDownExcludes = new Set < INode > ( ) ;
14061414 if ( isDragNodeObject ( dragObject ) ) {
14071415 const { nodes } = dragObject ;
14081416 let i = nodes . length ;
@@ -1414,7 +1422,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
14141422 }
14151423 if ( p !== container ) {
14161424 container = p || document . focusNode ;
1417- drillDownExcludes . add ( container ) ;
1425+ container && drillDownExcludes . add ( container ) ;
14181426 }
14191427 }
14201428
@@ -1425,11 +1433,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
14251433 } else {
14261434 instance = this . getClosestNodeInstance (
14271435 nodeInstance . instance as any ,
1428- container . id ,
1436+ container ? .id ,
14291437 ) ?. instance ;
14301438 }
14311439 } else {
1432- instance = this . getComponentInstances ( container ) ?. [ 0 ] ;
1440+ instance = container && this . getComponentInstances ( container ) ?. [ 0 ] ;
14331441 }
14341442
14351443 let dropContainer : DropContainer = {
@@ -1457,7 +1465,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
14571465 container = container . parent ;
14581466 instance = this . getClosestNodeInstance ( dropContainer . instance , container . id ) ?. instance ;
14591467 dropContainer = {
1460- container : container as INode ,
1468+ container : container ,
14611469 instance,
14621470 } ;
14631471 } else {
@@ -1500,7 +1508,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
15001508 */
15011509 getNearByContainer (
15021510 { container, instance } : DropContainer ,
1503- drillDownExcludes : Set < Node > ,
1511+ drillDownExcludes : Set < INode > ,
15041512 e : ILocateEvent ,
15051513 ) {
15061514 const { children } = container ;
@@ -1519,7 +1527,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
15191527 }
15201528 if ( child . conditionGroup ) {
15211529 const bn = child . conditionGroup ;
1522- i = bn . index + bn . length - 1 ;
1530+ i = ( bn . index || 0 ) + bn . length - 1 ;
15231531 child = bn . visibleNode ;
15241532 }
15251533 if ( ! child . isParental ( ) || drillDownExcludes . has ( child ) ) {
0 commit comments