1- import { Component , MouseEvent as ReactMouseEvent } from 'react' ;
1+ import { MouseEvent as ReactMouseEvent , PureComponent } from 'react' ;
22import { isFormEvent , canClickNode , isShaken } from '@alilc/lowcode-utils' ;
33import { Tree } from '../controllers/tree' ;
44import TreeNodeView from './tree-node' ;
55import { IPublicEnumDragObjectType , IPublicModelPluginContext , IPublicModelNode } from '@alilc/lowcode-types' ;
6+ import TreeNode from '../controllers/tree-node' ;
67
78function getTreeNodeIdByEvent ( e : ReactMouseEvent , stop : Element ) : null | string {
89 let target : Element | null = e . target as Element ;
@@ -17,7 +18,7 @@ function getTreeNodeIdByEvent(e: ReactMouseEvent, stop: Element): null | string
1718 return ( target as HTMLDivElement ) . dataset . id || null ;
1819}
1920
20- export default class TreeView extends Component < {
21+ export default class TreeView extends PureComponent < {
2122 tree : Tree ;
2223 pluginContext : IPublicModelPluginContext ;
2324} > {
@@ -60,12 +61,12 @@ export default class TreeView extends Component<{
6061 const { id } = node ;
6162 const isMulti = e . metaKey || e . ctrlKey || e . shiftKey ;
6263 canvas . activeTracker ?. track ( node ) ;
63- if ( isMulti && ! node . contains ( focusNode ) && selection . has ( id ) ) {
64+ if ( isMulti && focusNode && ! node . contains ( focusNode ) && selection ? .has ( id ) ) {
6465 if ( ! isFormEvent ( e . nativeEvent ) ) {
6566 selection . remove ( id ) ;
6667 }
6768 } else {
68- selection . select ( id ) ;
69+ selection ? .select ( id ) ;
6970 const selectedNode = selection ?. getNodes ( ) ?. [ 0 ] ;
7071 const npm = selectedNode ?. componentMeta ?. npm ;
7172 const selected =
@@ -134,21 +135,23 @@ export default class TreeView extends Component<{
134135 const isMulti = e . metaKey || e . ctrlKey || e . shiftKey ;
135136 const isLeftButton = e . button === 0 ;
136137
137- if ( isLeftButton && ! node . contains ( focusNode ) ) {
138+ if ( isLeftButton && focusNode && ! node . contains ( focusNode ) ) {
138139 let nodes : IPublicModelNode [ ] = [ node ] ;
139140 this . ignoreUpSelected = false ;
140141 if ( isMulti ) {
141142 // multi select mode, directily add
142- if ( ! selection . has ( node . id ) ) {
143+ if ( ! selection ? .has ( node . id ) ) {
143144 canvas . activeTracker ?. track ( node ) ;
144- selection . add ( node . id ) ;
145+ selection ? .add ( node . id ) ;
145146 this . ignoreUpSelected = true ;
146147 }
147148 // todo: remove rootNodes id
148- selection . remove ( focusNode . id ) ;
149+ selection ? .remove ( focusNode . id ) ;
149150 // 获得顶层 nodes
150- nodes = selection . getTopNodes ( ) ;
151- } else if ( selection . has ( node . id ) ) {
151+ if ( selection ) {
152+ nodes = selection . getTopNodes ( ) ;
153+ }
154+ } else if ( selection ?. has ( node . id ) ) {
152155 nodes = selection . getTopNodes ( ) ;
153156 }
154157 this . boostEvent = e . nativeEvent ;
@@ -169,7 +172,9 @@ export default class TreeView extends Component<{
169172 doc ?. detecting . leave ( ) ;
170173 } ;
171174
172- state = {
175+ state : {
176+ root : TreeNode | null
177+ } = {
173178 root : null ,
174179 } ;
175180
0 commit comments