feat: add componentMeta?.advanced?.callbacks?.onSelectHook api

This commit is contained in:
liujuping 2023-06-01 14:39:17 +08:00 committed by 林熠
parent 5feeab5aec
commit 03495ba9ef
5 changed files with 32 additions and 9 deletions

View File

@ -19,6 +19,7 @@ const jestConfig = {
// testMatch: ['**/setting-field.test.ts'],
// testMatch: ['**/node.test.ts'],
// testMatch: ['**/builtin-hotkey.test.ts'],
// testMatch: ['**/selection.test.ts'],
transformIgnorePatterns: [
`/node_modules/(?!${esModules})/`,
],

View File

@ -159,6 +159,9 @@ export interface IBaseNode<Schema extends IPublicTypeNodeSchema = IPublicTypeNod
setProps(props?: IPublicTypePropsMap | IPublicTypePropsList | Props | null): void;
mergeProps(props: IPublicTypePropsMap): void;
/** 是否可以选中 */
canSelect(): boolean;
}
/**
@ -644,6 +647,12 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>
return !!this.getExtraProp('isLocked')?.getValue();
}
canSelect(): boolean {
const onSelectHook = this.componentMeta?.advanced?.callbacks?.onSelectHook;
const canSelect = typeof onSelectHook === 'function' ? onSelectHook(this.internalToShellNode()!) : true;
return canSelect;
}
/**
*
*/

View File

@ -32,6 +32,12 @@ export class Selection implements ISelection {
return;
}
const node = this.doc.getNode(id);
if (!node?.canSelect()) {
return;
}
this._selected = [id];
this.emitter.emit('selectionchange', this._selected);
}
@ -40,7 +46,18 @@ export class Selection implements ISelection {
*
*/
selectAll(ids: string[]) {
this._selected = ids;
const selectIds: string[] = [];
ids.forEach(d => {
const node = this.doc.getNode(d);
if (node?.canSelect()) {
selectIds.push(d);
}
});
this._selected = selectIds;
this.emitter.emit('selectionchange', this._selected);
}

View File

@ -122,7 +122,7 @@ describe('选择区测试', () => {
selectionChangeHandler.mockClear();
});
it('dispose 方法', () => {
it('selectAll 包含不存在的 id', () => {
const project = new Project(designer, {
componentsTree: [
formSchema,
@ -135,14 +135,7 @@ describe('选择区测试', () => {
selection.selectAll(['form', 'node_k1ow3cbj', 'form2']);
const selectionChangeHandler = jest.fn();
selection.onSelectionChange(selectionChangeHandler);
selection.dispose();
expect(selectionChangeHandler).toHaveBeenCalledTimes(1);
expect(selectionChangeHandler.mock.calls[0][0]).toEqual(['form', 'node_k1ow3cbj']);
expect(selection.selected).toEqual(['form', 'node_k1ow3cbj']);
selectionChangeHandler.mockClear();
});
it('dispose 方法 - 选中的节点没有被删除的', () => {

View File

@ -195,6 +195,9 @@ export interface IPublicTypeCallbacks {
onMoveHook?: (currentNode: IPublicModelNode) => boolean;
// thinkof 限制性拖拽
onHoverHook?: (currentNode: IPublicModelNode) => boolean;
/** 选中 hook如果返回值是 false可以控制组件不可被选中 */
onSelectHook?: (currentNode: IPublicModelNode) => boolean;
onChildMoveHook?: (childNode: IPublicModelNode, currentNode: IPublicModelNode) => boolean;
// events