要实现编辑器撤销和重做的功能,本质上是要在做输入操作时记录包括内容、光标位置等信息,从而可以在撤销和重做的时候可以恢复原状。因此需要对输入操作进行改造,基于输入命令二次封装,增加undo函数撤销输入的内容同时恢复光标位置,同时我们还需要用栈来记录历史的操作信息。下面是一些核心的步骤:
具体代码如下:
// 1. 抽象命令基类
export abstract class EditorCommand {
abstract execute(controller: RichEditorController);
abstract undo(controller: RichEditorController);
}
// 2. 插入文本命令类
export class InsertTextCommand extends EditorCommand {
private text: string;
private prevOffset: number | null = 0;
private curOffset: number | null = 0;
private options: RichEditorTextSpanOptions;
constructor(text: string, options: RichEditorTextSpanOptions) {
super();
this.text = text;
this.options = JSON.parse(JSON.stringify(options));
}
execute(controller: RichEditorController) {
// 移动到插入位置并插入文本
this.prevOffset = controller.getCaretOffset();
this.options.offset = this.prevOffset;
controller.addTextSpan(this.text, this.options);
this.curOffset = controller.getCaretOffset();
}
undo(controller: RichEditorController) {
// 计算要删除的范围
const start = this.prevOffset;
const end = this.curOffset;
// 删除插入的文本
controller.deleteSpans({ start: start, end: end });
// 恢复原始光标位置
controller.setCaretOffset(start);
}
}
// 历史输入管理类
@ObservedV2
export class EditorHistoryManager {
@Trace private undoStack: EditorCommand[] = [];
@Trace private redoStack: EditorCommand[] = [];
private maxHistorySize: number = 100;
private controller: RichEditorController;
constructor(controller: RichEditorController) {
this.controller = controller;
}
@Computed
get canUndo(): boolean {
return this.undoStack.length > 0;
}
@Computed
get canRedo(): boolean {
return this.redoStack.length > 0;
}
// 执行新命令
executeCommand(command: EditorCommand) {
command.execute(this.controller);
this.undoStack.push(command);
// 清空重做栈
this.redoStack = [];
// 控制历史记录大小
this.trimHistory();
}
undo() {
if (this.undoStack.length === 0) {
return;
}
const command = this.undoStack.pop()!;
command.undo(this.controller);
this.redoStack.push(command);
}
redo() {
if (this.redoStack.length === 0) {
return;
}
const command = this.redoStack.pop()!;
command.execute(this.controller);
this.undoStack.push(command);
}
clearHistory(): void {
this.undoStack = [];
this.redoStack = [];
}
private trimHistory(): void {
if (this.undoStack.length > this.maxHistorySize) {
this.undoStack.shift();
}
}
}
@Entry
@Component
struct Index {
private message: string = 'Hello World';
private editorOptions: RichEditorOptions = { controller: new RichEditorController() };
private historyManager: EditorHistoryManager = new EditorHistoryManager(this.editorOptions.controller);
build() {
Column() {
Row({ space: 10 }) {
Button('前进').width(100).onClick(() => {
this.historyManager.redo();
}).enabled(this.historyManager.canRedo)
Button('回退').width(100).onClick(() => {
this.historyManager.undo();
}).enabled(this.historyManager.canUndo)
}
.width('100%')
.height(100)
.justifyContent(FlexAlign.Center)
RichEditor(this.editorOptions)
.placeholder(this.message)
.height('100%')
.caretColor(Color.Orange)
.aboutToIMEInput((value) => {
let command = new InsertTextCommand(value.insertValue, {});
this.historyManager?.executeCommand(command);
return false;
})
.layoutWeight(1)
}
.height('100%')
.width('100%')
}
}