101 lines
2.2 KiB
JavaScript
101 lines
2.2 KiB
JavaScript
// import { EraserCommand } from "../commands/EraserCommand";
|
|
class EraserCommand { }
|
|
|
|
/**
|
|
* 橡皮擦状态管理器
|
|
* 用于管理橡皮擦操作的状态快照
|
|
*/
|
|
export class EraserStateManager {
|
|
constructor(canvas, layerManager) {
|
|
this.canvas = canvas;
|
|
this.layerManager = layerManager;
|
|
this.currentSnapshot = null;
|
|
this.pendingCommand = null;
|
|
}
|
|
|
|
setLayerManager(layerManager) {
|
|
this.layerManager = layerManager;
|
|
}
|
|
|
|
/**
|
|
* 开始橡皮擦操作 - 捕获初始状态
|
|
*/
|
|
startErasing() {
|
|
console.log("橡皮擦操作开始 - 捕获状态快照");
|
|
this.currentSnapshot = this._captureCanvasSnapshot();
|
|
}
|
|
|
|
/**
|
|
* 结束橡皮擦操作 - 创建命令
|
|
* @param {Array} affectedObjects 受影响的对象
|
|
* @returns {EraserCommand|null} 创建的橡皮擦命令
|
|
*/
|
|
endErasing(affectedObjects = []) {
|
|
if (!this.currentSnapshot) {
|
|
console.warn("没有初始状态快照,无法创建橡皮擦命令");
|
|
return null;
|
|
}
|
|
|
|
if (!affectedObjects || affectedObjects.length === 0) {
|
|
console.log("没有对象被擦除,不创建命令");
|
|
this.currentSnapshot = null;
|
|
return null;
|
|
}
|
|
|
|
console.log(`橡皮擦操作结束 - 影响了 ${affectedObjects.length} 个对象`);
|
|
|
|
// 捕获擦除后的状态
|
|
const afterSnapshot = this._captureCanvasSnapshot();
|
|
|
|
// 创建橡皮擦命令
|
|
const command = new EraserCommand({
|
|
canvas: this.canvas,
|
|
layerManager: this.layerManager,
|
|
affectedObjects: affectedObjects,
|
|
beforeSnapshot: this.currentSnapshot,
|
|
afterSnapshot: afterSnapshot,
|
|
});
|
|
|
|
// 重置状态
|
|
this.currentSnapshot = null;
|
|
|
|
return command;
|
|
}
|
|
|
|
/**
|
|
* 捕获画布状态快照
|
|
* @returns {Object} 画布状态快照
|
|
* @private
|
|
*/
|
|
_captureCanvasSnapshot() {
|
|
try {
|
|
return this.canvas.toJSON([
|
|
"id",
|
|
"type",
|
|
"layerId",
|
|
"layerName",
|
|
"isBackground",
|
|
"isLocked",
|
|
"isVisible",
|
|
"isFixed",
|
|
"parentId",
|
|
"eraser",
|
|
"eraserable",
|
|
"erasable",
|
|
]);
|
|
} catch (error) {
|
|
console.error("捕获画布状态快照失败:", error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 取消当前擦除操作
|
|
*/
|
|
cancelErasing() {
|
|
this.currentSnapshot = null;
|
|
this.pendingCommand = null;
|
|
}
|
|
dispose() {}
|
|
}
|