From 24c5b74bf2196ce3a1c76d0cf5626cd249a2a5fd Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Tue, 7 May 2024 19:10:58 +0800 Subject: [PATCH] improve: control operation history boundary #540 --- src/editor/core/draw/Draw.ts | 55 ++++++++++++----------- src/editor/core/draw/control/Control.ts | 7 +++ src/editor/core/history/HistoryManager.ts | 4 ++ 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/editor/core/draw/Draw.ts b/src/editor/core/draw/Draw.ts index 023f578..8c39fe5 100644 --- a/src/editor/core/draw/Draw.ts +++ b/src/editor/core/draw/Draw.ts @@ -2304,31 +2304,7 @@ export class Draw { (isSubmitHistory && !isFirstRender) || (curIndex !== undefined && this.historyManager.isStackEmpty()) ) { - const oldElementList = getSlimCloneElementList(this.elementList) - const oldHeaderElementList = getSlimCloneElementList( - this.header.getElementList() - ) - const oldFooterElementList = getSlimCloneElementList( - this.footer.getElementList() - ) - const oldRange = deepClone(this.range.getRange()) - const pageNo = this.pageNo - const oldPositionContext = deepClone(positionContext) - const zone = this.zone.getZone() - this.historyManager.execute(() => { - this.zone.setZone(zone) - this.setPageNo(pageNo) - this.position.setPositionContext(deepClone(oldPositionContext)) - this.header.setElementList(deepClone(oldHeaderElementList)) - this.footer.setElementList(deepClone(oldFooterElementList)) - this.elementList = deepClone(oldElementList) - this.range.replaceRange(deepClone(oldRange)) - this.render({ - curIndex, - isSubmitHistory: false, - isSourceHistory: true - }) - }) + this.submitHistory(curIndex) } // 信息变动回调 nextTick(() => { @@ -2359,6 +2335,35 @@ export class Draw { }) } + public submitHistory(curIndex: number | undefined) { + const positionContext = this.position.getPositionContext() + const oldElementList = getSlimCloneElementList(this.elementList) + const oldHeaderElementList = getSlimCloneElementList( + this.header.getElementList() + ) + const oldFooterElementList = getSlimCloneElementList( + this.footer.getElementList() + ) + const oldRange = deepClone(this.range.getRange()) + const pageNo = this.pageNo + const oldPositionContext = deepClone(positionContext) + const zone = this.zone.getZone() + this.historyManager.execute(() => { + this.zone.setZone(zone) + this.setPageNo(pageNo) + this.position.setPositionContext(deepClone(oldPositionContext)) + this.header.setElementList(deepClone(oldHeaderElementList)) + this.footer.setElementList(deepClone(oldFooterElementList)) + this.elementList = deepClone(oldElementList) + this.range.replaceRange(deepClone(oldRange)) + this.render({ + curIndex, + isSubmitHistory: false, + isSourceHistory: true + }) + }) + } + public destroy() { this.container.remove() this.globalEvent.removeEvent() diff --git a/src/editor/core/draw/control/Control.ts b/src/editor/core/draw/control/Control.ts index b3f4cfc..a649236 100644 --- a/src/editor/core/draw/control/Control.ts +++ b/src/editor/core/draw/control/Control.ts @@ -420,11 +420,18 @@ export class Control { startElement.controlComponent === ControlComponent.PLACEHOLDER || nextElement.controlComponent === ControlComponent.PLACEHOLDER ) { + let isHasSubmitHistory = false let index = startIndex while (index < elementList.length) { const curElement = elementList[index] if (curElement.controlId !== startElement.controlId) break if (curElement.controlComponent === ControlComponent.PLACEHOLDER) { + // 删除占位符时替换前一个历史记录 + if (!isHasSubmitHistory) { + isHasSubmitHistory = true + this.draw.getHistoryManager().popUndo() + this.draw.submitHistory(startIndex) + } this.draw.spliceElementList(elementList, index, 1) } else { index++ diff --git a/src/editor/core/history/HistoryManager.ts b/src/editor/core/history/HistoryManager.ts index 2e2518f..7b589c1 100644 --- a/src/editor/core/history/HistoryManager.ts +++ b/src/editor/core/history/HistoryManager.ts @@ -54,4 +54,8 @@ export class HistoryManager { this.undoStack = [] this.redoStack = [] } + + public popUndo() { + return this.undoStack.pop() + } }