From ec31bf9ea588db46d7472ad0f86722977ad806cb Mon Sep 17 00:00:00 2001 From: "lihao.ylh" Date: Wed, 1 Sep 2021 21:10:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20prop=20=E7=9A=84=E5=AD=90=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E4=B8=8D=E5=86=8D=E5=BB=B6=E8=BF=9F=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96,=20=E4=B9=9F=E4=B8=8D=E5=86=8D=20node=20=E6=9C=AA?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E5=AE=8C=E6=88=90=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E5=B0=B1=E8=B0=83=E7=94=A8=20dispose=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/designer/src/document/node/node.ts | 5 +---- .../designer/src/document/node/props/prop.ts | 11 +++++++++-- .../__snapshots__/document-model.test.ts.snap | 16 ++++++++-------- .../document-model/document-model.test.ts | 2 +- .../tests/document/node/props/prop.test.ts | 1 + 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/packages/designer/src/document/node/node.ts b/packages/designer/src/document/node/node.ts index 32fa5fdeb..62cb3bf84 100644 --- a/packages/designer/src/document/node/node.ts +++ b/packages/designer/src/document/node/node.ts @@ -157,7 +157,7 @@ export class Node { return this.componentMeta.icon; } - private isInited = false; + isInited = false; constructor(readonly document: DocumentModel, nodeSchema: Schema, options: any = {}) { const { componentName, id, children, props, ...extras } = nodeSchema; @@ -168,9 +168,6 @@ export class Node { children: isDOMText(children) || isJSExpression(children) ? children : '', }); } else { - // 这里 props 被初始化两次,一次 new,一次 import,new 的实例需要给 propsReducer 的钩子去使用, - // import 是为了使用钩子返回的值,并非完全幂等的操作,部分行为执行两次会有 bug, - // 所以在 props 里会对 new / import 做一些区别化的解析 this.props = new Props(this, props, extras); this._children = new NodeChildren(this as ParentalNode, this.initialChildren(children)); this._children.internalInitParent(); diff --git a/packages/designer/src/document/node/props/prop.ts b/packages/designer/src/document/node/props/prop.ts index 448f72f9c..4a654a34a 100644 --- a/packages/designer/src/document/node/props/prop.ts +++ b/packages/designer/src/document/node/props/prop.ts @@ -55,6 +55,12 @@ export class Prop implements IPropParent { if (value !== UNSET) { this.setValue(value); } + this.setupItems(); + } + + // TODO: 先用调用方式触发子 prop 的初始化,后续须重构 + private setupItems() { + return this.items; } /** @@ -247,7 +253,9 @@ export class Prop implements IPropParent { }; } - this.dispose(); + if (this.owner.isInited) { + this.dispose(); + } if (oldValue !== this._value) { editor?.emit('node.innerProp.change', { @@ -302,7 +310,6 @@ export class Prop implements IPropParent { owner.addSlot(this._slotNode); this._slotNode.internalSetSlotFor(this); } - this.dispose(); } /** diff --git a/packages/designer/tests/document/document-model/__snapshots__/document-model.test.ts.snap b/packages/designer/tests/document/document-model/__snapshots__/document-model.test.ts.snap index 186dd0161..49bcbba08 100644 --- a/packages/designer/tests/document/document-model/__snapshots__/document-model.test.ts.snap +++ b/packages/designer/tests/document/document-model/__snapshots__/document-model.test.ts.snap @@ -166,7 +166,7 @@ Object { "labelTextAlign": "right", "labelTipsIcon": "", "labelTipsText": Object { - "en_US": null, + "en_US": "", "type": "i18n", "use": "zh_CN", "zh_CN": "", @@ -240,7 +240,7 @@ Object { "labelTextAlign": "right", "labelTipsIcon": "", "labelTipsText": Object { - "en_US": null, + "en_US": "", "type": "i18n", "use": "zh_CN", "zh_CN": "", @@ -310,7 +310,7 @@ Object { "labelTextAlign": "right", "labelTipsIcon": "", "labelTipsText": Object { - "en_US": null, + "en_US": "", "type": "i18n", "use": "zh_CN", "zh_CN": "", @@ -392,7 +392,7 @@ Object { "labelTextAlign": "right", "labelTipsIcon": "", "labelTipsText": Object { - "en_US": null, + "en_US": "", "type": "i18n", "use": "zh_CN", "zh_CN": "", @@ -478,7 +478,7 @@ Object { "labelTextAlign": "right", "labelTipsIcon": "", "labelTipsText": Object { - "en_US": null, + "en_US": "", "type": "i18n", "use": "zh_CN", "zh_CN": "", @@ -620,7 +620,7 @@ Object { "labelTextAlign": "right", "labelTipsIcon": "", "labelTipsText": Object { - "en_US": null, + "en_US": "", "type": "i18n", "use": "zh_CN", "zh_CN": "", @@ -694,7 +694,7 @@ Object { "labelTextAlign": "right", "labelTipsIcon": "", "labelTipsText": Object { - "en_US": null, + "en_US": "", "type": "i18n", "use": "zh_CN", "zh_CN": "", @@ -776,7 +776,7 @@ Object { "labelTextAlign": "right", "labelTipsIcon": "", "labelTipsText": Object { - "en_US": null, + "en_US": "", "type": "i18n", "use": "zh_CN", "zh_CN": "", diff --git a/packages/designer/tests/document/document-model/document-model.test.ts b/packages/designer/tests/document/document-model/document-model.test.ts index 5e24aef5b..d4515aa2d 100644 --- a/packages/designer/tests/document/document-model/document-model.test.ts +++ b/packages/designer/tests/document/document-model/document-model.test.ts @@ -99,7 +99,7 @@ describe('document-model 测试', () => { expect(doc.suspensed).toBeFalsy(); doc.suspense(); - doc.active(); + doc.activate(); doc.close(); doc.remove(); diff --git a/packages/designer/tests/document/node/props/prop.test.ts b/packages/designer/tests/document/node/props/prop.test.ts index 3c01de7ec..88d6701f8 100644 --- a/packages/designer/tests/document/node/props/prop.test.ts +++ b/packages/designer/tests/document/node/props/prop.test.ts @@ -27,6 +27,7 @@ const mockedOwner = { }, designer: {}, }, + isInited: true, }; const mockedPropsInst = {