File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ const jestConfig = {
1919 // testMatch: ['**/setting-field.test.ts'],
2020 // testMatch: ['**/node.test.ts'],
2121 // testMatch: ['**/builtin-hotkey.test.ts'],
22+ // testMatch: ['**/selection.test.ts'],
2223 transformIgnorePatterns : [
2324 `/node_modules/(?!${ esModules } )/` ,
2425 ] ,
Original file line number Diff line number Diff line change @@ -159,6 +159,9 @@ export interface IBaseNode<Schema extends IPublicTypeNodeSchema = IPublicTypeNod
159159 setProps ( props ?: IPublicTypePropsMap | IPublicTypePropsList | Props | null ) : void ;
160160
161161 mergeProps ( props : IPublicTypePropsMap ) : void ;
162+
163+ /** 是否可以选中 */
164+ canSelect ( ) : boolean ;
162165}
163166
164167/**
@@ -644,6 +647,12 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>
644647 return ! ! this . getExtraProp ( 'isLocked' ) ?. getValue ( ) ;
645648 }
646649
650+ canSelect ( ) : boolean {
651+ const onSelectHook = this . componentMeta ?. advanced ?. callbacks ?. onSelectHook ;
652+ const canSelect = typeof onSelectHook === 'function' ? onSelectHook ( this . internalToShellNode ( ) ! ) : true ;
653+ return canSelect ;
654+ }
655+
647656 /**
648657 * 选择当前节点
649658 */
Original file line number Diff line number Diff line change @@ -32,6 +32,12 @@ export class Selection implements ISelection {
3232 return ;
3333 }
3434
35+ const node = this . doc . getNode ( id ) ;
36+
37+ if ( ! node ?. canSelect ( ) ) {
38+ return ;
39+ }
40+
3541 this . _selected = [ id ] ;
3642 this . emitter . emit ( 'selectionchange' , this . _selected ) ;
3743 }
@@ -40,7 +46,18 @@ export class Selection implements ISelection {
4046 * 批量选中
4147 */
4248 selectAll ( ids : string [ ] ) {
43- this . _selected = ids ;
49+ const selectIds : string [ ] = [ ] ;
50+
51+ ids . forEach ( d => {
52+ const node = this . doc . getNode ( d ) ;
53+
54+ if ( node ?. canSelect ( ) ) {
55+ selectIds . push ( d ) ;
56+ }
57+ } ) ;
58+
59+ this . _selected = selectIds ;
60+
4461 this . emitter . emit ( 'selectionchange' , this . _selected ) ;
4562 }
4663
Original file line number Diff line number Diff line change @@ -122,7 +122,7 @@ describe('选择区测试', () => {
122122 selectionChangeHandler . mockClear ( ) ;
123123 } ) ;
124124
125- it ( 'dispose 方法 ' , ( ) => {
125+ it ( 'selectAll 包含不存在的 id ' , ( ) => {
126126 const project = new Project ( designer , {
127127 componentsTree : [
128128 formSchema ,
@@ -135,14 +135,7 @@ describe('选择区测试', () => {
135135
136136 selection . selectAll ( [ 'form' , 'node_k1ow3cbj' , 'form2' ] ) ;
137137
138- const selectionChangeHandler = jest . fn ( ) ;
139- selection . onSelectionChange ( selectionChangeHandler ) ;
140- selection . dispose ( ) ;
141-
142- expect ( selectionChangeHandler ) . toHaveBeenCalledTimes ( 1 ) ;
143- expect ( selectionChangeHandler . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ 'form' , 'node_k1ow3cbj' ] ) ;
144138 expect ( selection . selected ) . toEqual ( [ 'form' , 'node_k1ow3cbj' ] ) ;
145- selectionChangeHandler . mockClear ( ) ;
146139 } ) ;
147140
148141 it ( 'dispose 方法 - 选中的节点没有被删除的' , ( ) => {
Original file line number Diff line number Diff line change @@ -195,6 +195,9 @@ export interface IPublicTypeCallbacks {
195195 onMoveHook ?: ( currentNode : IPublicModelNode ) => boolean ;
196196 // thinkof 限制性拖拽
197197 onHoverHook ?: ( currentNode : IPublicModelNode ) => boolean ;
198+
199+ /** 选中 hook,如果返回值是 false,可以控制组件不可被选中 */
200+ onSelectHook ?: ( currentNode : IPublicModelNode ) => boolean ;
198201 onChildMoveHook ?: ( childNode : IPublicModelNode , currentNode : IPublicModelNode ) => boolean ;
199202
200203 // events
You can’t perform that action at this time.
0 commit comments