Skip to content

Commit

Permalink
fix: fix onChangeNodeVisible & onChangeNodeChildren cannot be trigger…
Browse files Browse the repository at this point in the history
…ed successfully in some cases
  • Loading branch information
liujuping committed Jan 3, 2023
1 parent bc5ec03 commit bfa67c8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
18 changes: 18 additions & 0 deletions packages/designer/src/document/document-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
IPublicApiProject,
IPublicModelDropLocation,
IPublicEnumTransformStage,
IPublicOnChangeOptions,
EDITOR_EVENT,
} from '@alilc/lowcode-types';
import { Project } from '../project';
import { ISimulatorHost } from '../simulator';
Expand Down Expand Up @@ -158,6 +160,22 @@ export class DocumentModel implements IDocumentModel {
this.inited = true;
}

onChangeNodeVisible(fn: (node: IPublicModelNode, visible: boolean) => void): () => void {
this.designer.editor.eventBus.on(EDITOR_EVENT.NODE_CHILDREN_CHANGE, fn);

return () => {
this.designer.editor.eventBus.off(EDITOR_EVENT.NODE_CHILDREN_CHANGE, fn);
};
}

onChangeNodeChildren(fn: (info: IPublicOnChangeOptions) => void): () => void {
this.designer.editor.eventBus.on(EDITOR_EVENT.NODE_VISIBLE_CHANGE, fn);

return () => {
this.designer.editor.eventBus.off(EDITOR_EVENT.NODE_VISIBLE_CHANGE, fn);
};
}

@obx.shallow private willPurgeSpace: Node[] = [];

get modalNode() {
Expand Down
8 changes: 8 additions & 0 deletions packages/designer/src/document/node/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
IPublicModelNode,
IPublicModelExclusiveGroup,
IPublicEnumTransformStage,
EDITOR_EVENT,
} from '@alilc/lowcode-types';
import { compatStage, isDOMText, isJSExpression } from '@alilc/lowcode-utils';
import { SettingTopEntry } from '@alilc/lowcode-designer';
Expand Down Expand Up @@ -190,6 +191,13 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>

this.isInited = true;
this.emitter = createModuleEventBus('Node');
const editor = this.document.designer.editor;
this.onVisibleChange((visible: boolean) => {
editor.eventBus.emit(EDITOR_EVENT.NODE_VISIBLE_CHANGE, this, visible);
});
this.onChildrenChange((info?: { type: string; node: Node }) => {
editor.eventBus.emit(EDITOR_EVENT.NODE_VISIBLE_CHANGE, info);
});
}

_settingEntry: SettingTopEntry;
Expand Down
7 changes: 5 additions & 2 deletions packages/designer/tests/document/node/node.remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import set from 'lodash/set';
import cloneDeep from 'lodash/cloneDeep';
import '../../fixtures/window';
import { Project } from '../../../src/project/project';
import { Node } from '../../../src/document/node/node';
import { Designer } from '../../../src/designer/designer';
import formSchema from '../../fixtures/schema/form';
import { getIdsFromSchema, getNodeFromSchemaById } from '../../utils';
import { getIdsFromSchema } from '../../utils';
import EventEmitter from "events";

const mockCreateSettingEntry = jest.fn();
jest.mock('../../../src/designer/designer', () => {
Expand All @@ -22,6 +22,9 @@ jest.mock('../../../src/designer/designer', () => {
transformProps(props) { return props; },
createSettingEntry: mockCreateSettingEntry,
postEvent() {},
editor: {
eventBus: new EventEmitter(),
}
};
}),
};
Expand Down
1 change: 0 additions & 1 deletion packages/shell/src/api/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ export class Project implements IPublicApiProject {
*/
importSchema(schema?: IPublicTypeProjectSchema): void {
this[projectSymbol].load(schema, true);
// this[editorSymbol].emit(Events.IMPORT_SCHEMA, schema);
}

/**
Expand Down
25 changes: 10 additions & 15 deletions packages/shell/src/model/document-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,28 +289,23 @@ export class DocumentModel implements IPublicModelDocumentModel {
* @param fn
*/
onChangeNodeVisible(fn: (node: IPublicModelNode, visible: boolean) => void): void {
// TODO: history 变化时需要重新绑定
this[documentSymbol].nodesMap?.forEach((node) => {
node.onVisibleChange((flag: boolean) => {
fn(Node.create(node)!, flag);
});
this[documentSymbol].onChangeNodeVisible((node: IPublicModelNode, visible: boolean) => {
fn(Node.create(node)!, visible);
});
}

/**
* 当前 document 的节点 children 变更事件
* @param fn
*/
onChangeNodeChildren(fn: (info?: IPublicOnChangeOptions) => void): void {
// TODO: history 变化时需要重新绑定
this[documentSymbol].nodesMap?.forEach((node) => {
node.onChildrenChange((info?: InnerOnChangeOptions) => {
return info
? fn({
type: info.type,
node: Node.create(node)!,
})
: fn();
onChangeNodeChildren(fn: (info: IPublicOnChangeOptions) => void): void {
this[documentSymbol].onChangeNodeChildren((info?: IPublicOnChangeOptions) => {
if (!info) {
return;
}
fn({
type: info.type,
node: Node.create(info.node)!,
});
});
}
Expand Down
6 changes: 6 additions & 0 deletions packages/types/src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,9 @@ export interface PluginStatus {
export interface PluginStatusSet {
[key: string]: PluginStatus;
}

export enum EDITOR_EVENT {
NODE_CHILDREN_CHANGE = 'node.children.change',

NODE_VISIBLE_CHANGE = 'node.visible.change',
}
7 changes: 7 additions & 0 deletions packages/types/src/shell/model/document-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IPublicTypeRootSchema, IPublicTypeDragNodeDataObject, IPublicTypeDragNo
import { IPublicEnumTransformStage } from '../enum';
import { IPublicApiProject } from '../api';
import { IPublicModelDropLocation, IPublicModelDetecting, IPublicModelNode, IPublicModelSelection, IPublicModelHistory, IPublicModelModalNodesManager } from './';
import { IPublicOnChangeOptions } from '@alilc/lowcode-types';

export interface IPublicModelDocumentModel {

Expand Down Expand Up @@ -143,6 +144,12 @@ export interface IPublicModelDocumentModel {
onChangeNodeVisible(fn: (node: IPublicModelNode, visible: boolean) => void): void;


/**
* 当前 document 的节点 children 变更事件
* @param fn
*/
onChangeNodeChildren(fn: (info: IPublicOnChangeOptions) => void): void;

/**
* 当前 document 节点属性修改事件
* @param fn
Expand Down

0 comments on commit bfa67c8

Please sign in to comment.