From aa7f2b2fe176d82323d2e696f6893c16d74011ad Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Wed, 20 Nov 2024 22:12:55 +0800 Subject: [PATCH] fix: data retrieval error when control was destroyed #884 --- src/editor/core/draw/control/Control.ts | 16 +++++++++++----- .../draw/control/checkbox/CheckboxControl.ts | 7 ------- src/editor/core/draw/control/date/DateControl.ts | 7 +------ .../core/draw/control/select/SelectControl.ts | 7 +------ src/editor/core/draw/control/text/TextControl.ts | 7 +------ src/editor/interface/Control.ts | 2 -- 6 files changed, 14 insertions(+), 32 deletions(-) diff --git a/src/editor/core/draw/control/Control.ts b/src/editor/core/draw/control/Control.ts index 8a224afcb..497e2a848 100644 --- a/src/editor/core/draw/control/Control.ts +++ b/src/editor/core/draw/control/Control.ts @@ -73,6 +73,7 @@ export class Control { private options: DeepRequired private controlOptions: IControlOption private activeControl: IControlInstance | null + private activeControlValue: IElement[] constructor(draw: Draw) { this.controlBorder = new ControlBorder(draw) @@ -86,6 +87,7 @@ export class Control { this.options = draw.getOptions() this.controlOptions = this.options.control this.activeControl = null + this.activeControlValue = [] } // 搜索高亮匹配 @@ -288,6 +290,8 @@ export class Control { this.activeControl.awake() } } + // 更新缓存控件数据 + this.activeControlValue = this.activeControl.getValue() const controlElement = this.activeControl.getElement() if (element.controlId === controlElement.controlId) return } @@ -312,11 +316,15 @@ export class Control { this.activeControl = dateControl dateControl.awake() } + // 缓存控件数据 + if (this.activeControl) { + this.activeControlValue = this.activeControl.getValue() + } // 激活控件回调 const isSubscribeControlChange = this.eventBus.isSubscribe('controlChange') if (this.listener.controlChange || isSubscribeControlChange) { let control: IControl - const value = this.activeControl?.getValue() + const value = this.activeControlValue if (value?.length) { control = zipElementList(value)[0].control! } else { @@ -349,10 +357,7 @@ export class Control { this.eventBus.isSubscribe('controlChange') if (this.listener.controlChange || isSubscribeControlChange) { let control: IControl - const value = this.activeControl.getValue({ - range: this.activeControl.activeRange, - elementList: this.activeControl.activeElementList - }) + const value = this.activeControlValue const activeElement = this.activeControl.getElement() if (value?.length) { control = zipElementList(value)[0].control! @@ -372,6 +377,7 @@ export class Control { } // 清空变量 this.activeControl = null + this.activeControlValue = [] } public repaintControl(options: IRepaintControlOption = {}) { diff --git a/src/editor/core/draw/control/checkbox/CheckboxControl.ts b/src/editor/core/draw/control/checkbox/CheckboxControl.ts index 3300cb506..a4251ab41 100644 --- a/src/editor/core/draw/control/checkbox/CheckboxControl.ts +++ b/src/editor/core/draw/control/checkbox/CheckboxControl.ts @@ -6,20 +6,13 @@ import { IControlRuleOption } from '../../../../interface/Control' import { IElement } from '../../../../interface/Element' -import { IRange } from '../../../../interface/Range' -import { deepClone } from '../../../../utils' import { Control } from '../Control' export class CheckboxControl implements IControlInstance { - public activeRange: IRange - public activeElementList: IElement[] protected element: IElement protected control: Control constructor(element: IElement, control: Control) { - const draw = control.getDraw() - this.activeRange = deepClone(draw.getRange().getRange()) - this.activeElementList = draw.getElementList() this.element = element this.control = control } diff --git a/src/editor/core/draw/control/date/DateControl.ts b/src/editor/core/draw/control/date/DateControl.ts index da4034817..1a841143e 100644 --- a/src/editor/core/draw/control/date/DateControl.ts +++ b/src/editor/core/draw/control/date/DateControl.ts @@ -14,8 +14,7 @@ import { } from '../../../../interface/Control' import { IEditorOption } from '../../../../interface/Editor' import { IElement } from '../../../../interface/Element' -import { IRange } from '../../../../interface/Range' -import { deepClone, omitObject, pickObject } from '../../../../utils' +import { omitObject, pickObject } from '../../../../utils' import { formatElementContext } from '../../../../utils/element' import { Draw } from '../../Draw' import { DatePicker } from '../../particle/date/DatePicker' @@ -23,8 +22,6 @@ import { Control } from '../Control' export class DateControl implements IControlInstance { private draw: Draw - public activeRange: IRange - public activeElementList: IElement[] private element: IElement private control: Control private isPopup: boolean @@ -35,8 +32,6 @@ export class DateControl implements IControlInstance { const draw = control.getDraw() this.draw = draw this.options = draw.getOptions() - this.activeRange = deepClone(draw.getRange().getRange()) - this.activeElementList = draw.getElementList() this.element = element this.control = control this.isPopup = false diff --git a/src/editor/core/draw/control/select/SelectControl.ts b/src/editor/core/draw/control/select/SelectControl.ts index 40474b0d5..ca6492aaf 100644 --- a/src/editor/core/draw/control/select/SelectControl.ts +++ b/src/editor/core/draw/control/select/SelectControl.ts @@ -18,14 +18,11 @@ import { } from '../../../../interface/Control' import { IEditorOption } from '../../../../interface/Editor' import { IElement } from '../../../../interface/Element' -import { IRange } from '../../../../interface/Range' -import { deepClone, omitObject, pickObject, splitText } from '../../../../utils' +import { omitObject, pickObject, splitText } from '../../../../utils' import { formatElementContext } from '../../../../utils/element' import { Control } from '../Control' export class SelectControl implements IControlInstance { - public activeRange: IRange - public activeElementList: IElement[] private element: IElement private control: Control private isPopup: boolean @@ -35,8 +32,6 @@ export class SelectControl implements IControlInstance { constructor(element: IElement, control: Control) { const draw = control.getDraw() this.options = draw.getOptions() - this.activeRange = deepClone(draw.getRange().getRange()) - this.activeElementList = draw.getElementList() this.element = element this.control = control this.isPopup = false diff --git a/src/editor/core/draw/control/text/TextControl.ts b/src/editor/core/draw/control/text/TextControl.ts index 5710cf4ae..346b29fa4 100644 --- a/src/editor/core/draw/control/text/TextControl.ts +++ b/src/editor/core/draw/control/text/TextControl.ts @@ -12,14 +12,11 @@ import { } from '../../../../interface/Control' import { IEditorOption } from '../../../../interface/Editor' import { IElement } from '../../../../interface/Element' -import { IRange } from '../../../../interface/Range' -import { deepClone, omitObject, pickObject } from '../../../../utils' +import { omitObject, pickObject } from '../../../../utils' import { formatElementContext } from '../../../../utils/element' import { Control } from '../Control' export class TextControl implements IControlInstance { - public activeRange: IRange - public activeElementList: IElement[] private element: IElement private control: Control private options: DeepRequired @@ -27,8 +24,6 @@ export class TextControl implements IControlInstance { constructor(element: IElement, control: Control) { const draw = control.getDraw() this.options = draw.getOptions() - this.activeRange = deepClone(draw.getRange().getRange()) - this.activeElementList = draw.getElementList() this.element = element this.control = control } diff --git a/src/editor/interface/Control.ts b/src/editor/interface/Control.ts index 8d4e9f309..564579aaf 100644 --- a/src/editor/interface/Control.ts +++ b/src/editor/interface/Control.ts @@ -112,8 +112,6 @@ export interface IControlInitResult { } export interface IControlInstance { - activeRange: IRange - activeElementList: IElement[] setElement(element: IElement): void getElement(): IElement getValue(context?: IControlContext): IElement[]