Files
aida_front/src/component/Canvas/CanvasEditor/commands/ToolCommands.js
X1627315083 c266967f16 接入画布
2025-06-09 10:25:54 +08:00

55 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Command } from "./Command";
/**
* 工具切换命令
* 用于切换编辑器的工具模式(如绘画、选择、橡皮擦等)
*/
export class ToolCommand extends Command {
/**
* 创建一个工具切换命令
* @param {Object} options 配置选项
* @param {Object} options.toolManager 工具管理器实例
* @param {String} options.tool 要设置的工具名称
* @param {String} options.previousTool 先前的工具名称(可选,如果不提供会在执行时记录)
* @param {Boolean} options.saveState 是否保存画布状态默认为false
*/
constructor(options) {
super({
...options,
name: `切换工具: ${options.tool}`,
description: `将工具切换为 ${options.tool}`,
});
this.toolManager = options.toolManager;
this.tool = options.tool;
this.previousTool = options.previousTool || null;
}
/**
* 执行工具切换
* @returns {String} 设置的工具名称
*/
execute() {
if (!this.toolManager) return null;
// 记录当前工具(用于撤销)
if (!this.previousTool) {
this.previousTool = this.toolManager.getCurrentTool();
}
// 切换工具
return this.toolManager.setTool(this.tool);
}
/**
* 撤销工具切换
* @returns {String} 恢复的工具名称
*/
undo() {
if (!this.toolManager || !this.previousTool) return null;
// 恢复到先前工具
return this.toolManager.setTool(this.previousTool);
}
}