Skip to content

Commit 1c5ef78

Browse files
committed
fix: data retrieval error when control was destroyed #884
1 parent fbdd5f6 commit 1c5ef78

File tree

6 files changed

+23
-33
lines changed

6 files changed

+23
-33
lines changed

src/editor/core/draw/control/Control.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class Control {
7373
private options: DeepRequired<IEditorOption>
7474
private controlOptions: IControlOption
7575
private activeControl: IControlInstance | null
76+
private activeControlValue: IElement[]
7677

7778
constructor(draw: Draw) {
7879
this.controlBorder = new ControlBorder(draw)
@@ -86,6 +87,7 @@ export class Control {
8687
this.options = draw.getOptions()
8788
this.controlOptions = this.options.control
8889
this.activeControl = null
90+
this.activeControlValue = []
8991
}
9092

9193
// 搜索高亮匹配
@@ -271,6 +273,12 @@ export class Control {
271273
return this.activeControl
272274
}
273275

276+
public updateActiveControlValue() {
277+
if (this.activeControl) {
278+
this.activeControlValue = this.activeControl.getValue()
279+
}
280+
}
281+
274282
public initControl() {
275283
const elementList = this.getElementList()
276284
const range = this.getRange()
@@ -289,7 +297,11 @@ export class Control {
289297
}
290298
}
291299
const controlElement = this.activeControl.getElement()
292-
if (element.controlId === controlElement.controlId) return
300+
if (element.controlId === controlElement.controlId) {
301+
// 更新缓存控件数据
302+
this.updateActiveControlValue()
303+
return
304+
}
293305
}
294306
// 销毁旧激活控件
295307
this.destroyControl()
@@ -312,15 +324,18 @@ export class Control {
312324
this.activeControl = dateControl
313325
dateControl.awake()
314326
}
327+
// 缓存控件数据
328+
this.updateActiveControlValue()
315329
// 激活控件回调
316330
const isSubscribeControlChange = this.eventBus.isSubscribe('controlChange')
317331
if (this.listener.controlChange || isSubscribeControlChange) {
318332
let control: IControl
319-
const value = this.activeControl?.getValue()
333+
const value = this.activeControlValue
320334
if (value?.length) {
321335
control = zipElementList(value)[0].control!
322336
} else {
323337
control = pickElementAttr(deepClone(element)).control!
338+
control.value = []
324339
}
325340
const payload: IControlChangeResult = {
326341
control,
@@ -349,15 +364,13 @@ export class Control {
349364
this.eventBus.isSubscribe('controlChange')
350365
if (this.listener.controlChange || isSubscribeControlChange) {
351366
let control: IControl
352-
const value = this.activeControl.getValue({
353-
range: this.activeControl.activeRange,
354-
elementList: this.activeControl.activeElementList
355-
})
367+
const value = this.activeControlValue
356368
const activeElement = this.activeControl.getElement()
357369
if (value?.length) {
358370
control = zipElementList(value)[0].control!
359371
} else {
360372
control = pickElementAttr(deepClone(activeElement)).control!
373+
control.value = []
361374
}
362375
const payload: IControlChangeResult = {
363376
control,
@@ -372,6 +385,7 @@ export class Control {
372385
}
373386
// 清空变量
374387
this.activeControl = null
388+
this.activeControlValue = []
375389
}
376390

377391
public repaintControl(options: IRepaintControlOption = {}) {

src/editor/core/draw/control/checkbox/CheckboxControl.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@ import {
66
IControlRuleOption
77
} from '../../../../interface/Control'
88
import { IElement } from '../../../../interface/Element'
9-
import { IRange } from '../../../../interface/Range'
10-
import { deepClone } from '../../../../utils'
119
import { Control } from '../Control'
1210

1311
export class CheckboxControl implements IControlInstance {
14-
public activeRange: IRange
15-
public activeElementList: IElement[]
1612
protected element: IElement
1713
protected control: Control
1814

1915
constructor(element: IElement, control: Control) {
20-
const draw = control.getDraw()
21-
this.activeRange = deepClone(draw.getRange().getRange())
22-
this.activeElementList = draw.getElementList()
2316
this.element = element
2417
this.control = control
2518
}

src/editor/core/draw/control/date/DateControl.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ import {
1414
} from '../../../../interface/Control'
1515
import { IEditorOption } from '../../../../interface/Editor'
1616
import { IElement } from '../../../../interface/Element'
17-
import { IRange } from '../../../../interface/Range'
18-
import { deepClone, omitObject, pickObject } from '../../../../utils'
17+
import { omitObject, pickObject } from '../../../../utils'
1918
import { formatElementContext } from '../../../../utils/element'
2019
import { Draw } from '../../Draw'
2120
import { DatePicker } from '../../particle/date/DatePicker'
2221
import { Control } from '../Control'
2322

2423
export class DateControl implements IControlInstance {
2524
private draw: Draw
26-
public activeRange: IRange
27-
public activeElementList: IElement[]
2825
private element: IElement
2926
private control: Control
3027
private isPopup: boolean
@@ -35,8 +32,6 @@ export class DateControl implements IControlInstance {
3532
const draw = control.getDraw()
3633
this.draw = draw
3734
this.options = draw.getOptions()
38-
this.activeRange = deepClone(draw.getRange().getRange())
39-
this.activeElementList = draw.getElementList()
4035
this.element = element
4136
this.control = control
4237
this.isPopup = false

src/editor/core/draw/control/select/SelectControl.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ import {
1818
} from '../../../../interface/Control'
1919
import { IEditorOption } from '../../../../interface/Editor'
2020
import { IElement } from '../../../../interface/Element'
21-
import { IRange } from '../../../../interface/Range'
22-
import { deepClone, omitObject, pickObject, splitText } from '../../../../utils'
21+
import { omitObject, pickObject, splitText } from '../../../../utils'
2322
import { formatElementContext } from '../../../../utils/element'
2423
import { Control } from '../Control'
2524

2625
export class SelectControl implements IControlInstance {
27-
public activeRange: IRange
28-
public activeElementList: IElement[]
2926
private element: IElement
3027
private control: Control
3128
private isPopup: boolean
@@ -35,8 +32,6 @@ export class SelectControl implements IControlInstance {
3532
constructor(element: IElement, control: Control) {
3633
const draw = control.getDraw()
3734
this.options = draw.getOptions()
38-
this.activeRange = deepClone(draw.getRange().getRange())
39-
this.activeElementList = draw.getElementList()
4035
this.element = element
4136
this.control = control
4237
this.isPopup = false

src/editor/core/draw/control/text/TextControl.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,18 @@ import {
1212
} from '../../../../interface/Control'
1313
import { IEditorOption } from '../../../../interface/Editor'
1414
import { IElement } from '../../../../interface/Element'
15-
import { IRange } from '../../../../interface/Range'
16-
import { deepClone, omitObject, pickObject } from '../../../../utils'
15+
import { omitObject, pickObject } from '../../../../utils'
1716
import { formatElementContext } from '../../../../utils/element'
1817
import { Control } from '../Control'
1918

2019
export class TextControl implements IControlInstance {
21-
public activeRange: IRange
22-
public activeElementList: IElement[]
2320
private element: IElement
2421
private control: Control
2522
private options: DeepRequired<IEditorOption>
2623

2724
constructor(element: IElement, control: Control) {
2825
const draw = control.getDraw()
2926
this.options = draw.getOptions()
30-
this.activeRange = deepClone(draw.getRange().getRange())
31-
this.activeElementList = draw.getElementList()
3227
this.element = element
3328
this.control = control
3429
}

src/editor/interface/Control.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ export interface IControlInitResult {
112112
}
113113

114114
export interface IControlInstance {
115-
activeRange: IRange
116-
activeElementList: IElement[]
117115
setElement(element: IElement): void
118116
getElement(): IElement
119117
getValue(context?: IControlContext): IElement[]

0 commit comments

Comments
 (0)