/** * 查询类命令示例 - 不需要撤销 */ export class GetCanvasInfoCommand { constructor(options) { this.canvas = options.canvas; this.layerManager = options.layerManager; this.undoable = false; // 明确标记为不可撤销 } execute() { return { width: this.canvas.getWidth(), height: this.canvas.getHeight(), zoom: this.canvas.getZoom(), layers: this.layerManager?.getLayers()?.length || 0, objects: this.canvas.getObjects().length, }; } // 查询类命令不需要实现 undo 方法 } /** * 导出类命令示例 - 不需要撤销 */ export class ExportCanvasCommand { constructor(options) { this.canvas = options.canvas; this.format = options.format || "png"; this.quality = options.quality || 1; this.undoable = false; } execute() { return this.canvas.toDataURL(`image/${this.format}`, this.quality); } } /** * 验证类命令示例 - 不需要撤销 */ export class ValidateCanvasCommand { constructor(options) { this.canvas = options.canvas; this.undoable = false; } execute() { const objects = this.canvas.getObjects(); const errors = []; objects.forEach((obj, index) => { if (!obj.left || !obj.top) { errors.push(`对象 ${index} 位置无效`); } if (obj.width <= 0 || obj.height <= 0) { errors.push(`对象 ${index} 尺寸无效`); } }); return { isValid: errors.length === 0, errors, objectCount: objects.length, }; } } /** * 系统清理命令示例 - 不可逆操作,不需要撤销 */ export class CleanupTempDataCommand { constructor(options) { this.canvas = options.canvas; this.undoable = false; } execute() { // 清理临时数据 const cleaned = []; // 移除无效对象 const objects = this.canvas.getObjects(); objects.forEach((obj, index) => { if (obj._isTemp || obj._invalid) { this.canvas.remove(obj); cleaned.push(`临时对象 ${index}`); } }); // 清理缓存 if (this.canvas._clearCache) { this.canvas._clearCache(); cleaned.push("画布缓存"); } return { cleaned, count: cleaned.length, }; } }