|
|
|
@ -1,99 +1,189 @@
|
|
|
|
|
import { Intent } from "@blueprintjs/core";
|
|
|
|
|
import { toJS } from "mobx";
|
|
|
|
|
import hotkeys from "hotkeys-js-ext";
|
|
|
|
|
import { ConfigUrls } from "../Common/HostUrl";
|
|
|
|
|
import { PostJson, RequestStatus } from "../Common/Request";
|
|
|
|
|
import { Singleton } from "../Common/Singleton";
|
|
|
|
|
import { GetIndexDBID } from "../Common/Utils";
|
|
|
|
|
import { Command } from "../Editor/CommandMachine";
|
|
|
|
|
import { commandMachine } from "../Editor/CommandMachine";
|
|
|
|
|
import { IndexedDbStore, StoreName } from "../IndexedDb/IndexedDbStore";
|
|
|
|
|
import { CommandList, ICommand } from "../UI/Components/CommandPanel/CommandList";
|
|
|
|
|
import { AppToaster } from "../UI/Components/Toaster";
|
|
|
|
|
import { userConfigStore } from "../UI/Store/UserConfigStore";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 保存CAD的自定义命令,统一由这里经过CRUD操作
|
|
|
|
|
*/
|
|
|
|
|
export class CommandServer extends Singleton
|
|
|
|
|
export class CommandServer
|
|
|
|
|
{
|
|
|
|
|
private _customCommand: { [key: string]: ICommand; } = {};
|
|
|
|
|
private _needUpload = false;
|
|
|
|
|
CommandListMap = new Map<string, string>();
|
|
|
|
|
HotKeyListMap = new Map<string, string>();
|
|
|
|
|
AddCustonCommand(icommand: ICommand)
|
|
|
|
|
//记录需要上传的数据
|
|
|
|
|
private _NeedUpload = false;
|
|
|
|
|
private _CustomCommand: { [key: string]: ICommand; } = {};
|
|
|
|
|
|
|
|
|
|
//记录自定义命令映射
|
|
|
|
|
private _CustomCommandMap = new Map<string, string>();//<用户自定义命令|默认自定义命令,原始命令>
|
|
|
|
|
private _CommandCustomMap = new Map<string, string>();//<原始命令,用户自定义命令|默认自定义命令>
|
|
|
|
|
|
|
|
|
|
//记录组合键映射
|
|
|
|
|
private _CommandHotKeyMap = new Map<string, string>(); //<原始命令,组合键>
|
|
|
|
|
private _HotKeyCommandMap = new Map<string, string>(); //<组合键,原始命令>
|
|
|
|
|
|
|
|
|
|
private constructor() { }
|
|
|
|
|
private static _SingleInstance: CommandServer;
|
|
|
|
|
static GetInstance(): CommandServer
|
|
|
|
|
{
|
|
|
|
|
this.CommandListMap.set(icommand.customize || icommand.defaultCustom, icommand.command);
|
|
|
|
|
if (icommand.command !== icommand.customize && icommand.customize !== icommand.defaultCustom)
|
|
|
|
|
if (this._SingleInstance) return this._SingleInstance;
|
|
|
|
|
this._SingleInstance = new CommandServer;
|
|
|
|
|
return this._SingleInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get CustomCommandMap() { return this._CustomCommandMap; }
|
|
|
|
|
get HotKeyCommandMap() { return this._HotKeyCommandMap; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//UI只需要调用这个方法
|
|
|
|
|
ChangeCustonCommand(icommand: ICommand)
|
|
|
|
|
{
|
|
|
|
|
this._NeedUpload = true;
|
|
|
|
|
delete this._CustomCommand[icommand.command];
|
|
|
|
|
|
|
|
|
|
//删除原先注册的命令
|
|
|
|
|
this.DeleteCustomCommand(icommand.command);
|
|
|
|
|
|
|
|
|
|
//注册新的命令
|
|
|
|
|
let newCmd = icommand.customize || icommand.defaultCustom;
|
|
|
|
|
if (!this._CustomCommandMap.has(newCmd)) //避免覆盖已经设置的命令
|
|
|
|
|
this.RegistCustomCommand(newCmd, icommand.command);
|
|
|
|
|
|
|
|
|
|
//删除原先注册的组合键
|
|
|
|
|
this.DeleteHotkey(icommand.command);
|
|
|
|
|
|
|
|
|
|
//注册新的组合键
|
|
|
|
|
let newHotkey = icommand.hotkeysCustomize ?? icommand.defaultHotkeys;
|
|
|
|
|
if (!this._HotKeyCommandMap.has(newHotkey))
|
|
|
|
|
this.RegistHotkey(newHotkey, icommand.command);
|
|
|
|
|
|
|
|
|
|
//设置要上传的命令信息
|
|
|
|
|
if (
|
|
|
|
|
(icommand.customize !== undefined && icommand.customize !== icommand.command && icommand.customize !== icommand.defaultCustom)//"" 或者其他自定义
|
|
|
|
|
|| (icommand.hotkeysCustomize !== undefined && icommand.hotkeysCustomize !== icommand.defaultHotkeys))//必须自定义快捷键 并且不是原先的默认快捷键
|
|
|
|
|
{
|
|
|
|
|
this._needUpload = true;
|
|
|
|
|
this._customCommand[icommand.command] = toJS(icommand);
|
|
|
|
|
this._NeedUpload = true;
|
|
|
|
|
this._CustomCommand[icommand.command] = {
|
|
|
|
|
customize: icommand.customize,
|
|
|
|
|
hotkeysCustomize: icommand.hotkeysCustomize
|
|
|
|
|
} as ICommand;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DeleteCustonCommand(icommand: ICommand)
|
|
|
|
|
|
|
|
|
|
GetCommandCanUseDefaultCustomCmd(cmd: ICommand)
|
|
|
|
|
{
|
|
|
|
|
this._needUpload = true;
|
|
|
|
|
delete this._customCommand[icommand.command];
|
|
|
|
|
if (!cmd.defaultCustom) return false;
|
|
|
|
|
|
|
|
|
|
let command = this._CustomCommandMap.get(cmd.defaultCustom);
|
|
|
|
|
|
|
|
|
|
return !command || command === cmd.command;//如果不相等 证明被别的命令注册了
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GetCommandCanUseDefaultHotkey(cmd: ICommand)
|
|
|
|
|
{
|
|
|
|
|
if (!cmd.defaultHotkeys) return false;
|
|
|
|
|
|
|
|
|
|
let command = this._HotKeyCommandMap.get(cmd.defaultHotkeys);
|
|
|
|
|
|
|
|
|
|
return !command || command === cmd.command;//如果不相等 证明被别的命令注册了
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//清空所有的自定义命令和组合键
|
|
|
|
|
async Reset()
|
|
|
|
|
{
|
|
|
|
|
this._needUpload = true;
|
|
|
|
|
this._customCommand = {};
|
|
|
|
|
this._NeedUpload = true;
|
|
|
|
|
this._CustomCommand = {};
|
|
|
|
|
this.ClearCustonCommand();
|
|
|
|
|
await this.Upload();
|
|
|
|
|
await this.InitCommandCustomCmdAndHotkey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Upload()
|
|
|
|
|
{
|
|
|
|
|
if (!this._needUpload) return;
|
|
|
|
|
if (!this._NeedUpload) return;
|
|
|
|
|
let dbStore = await IndexedDbStore.CADStore();
|
|
|
|
|
let data = await PostJson(ConfigUrls.Edit, { key: CommandServer.name, value: JSON.stringify(this._customCommand) });
|
|
|
|
|
let data = await PostJson(ConfigUrls.Edit, { key: CommandServer.name, value: JSON.stringify(this._CustomCommand) });
|
|
|
|
|
if (data.err_code === RequestStatus.Ok)
|
|
|
|
|
{
|
|
|
|
|
dbStore.Put(StoreName.ConfigData, GetIndexDBID(CommandServer.name), this._customCommand);
|
|
|
|
|
dbStore.Put(StoreName.ConfigData, GetIndexDBID(CommandServer.name), this._CustomCommand);
|
|
|
|
|
dbStore.Put(StoreName.ConfigVersion, GetIndexDBID(CommandServer.name), data.version);
|
|
|
|
|
this._needUpload = false;
|
|
|
|
|
this._NeedUpload = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//从服务器下载自定义和组合键并且注册它.
|
|
|
|
|
async InitCommandCustomCmdAndHotkey()
|
|
|
|
|
{
|
|
|
|
|
let commandList = await this.ReadCustomCommandlist();
|
|
|
|
|
this._CustomCommandMap.clear();
|
|
|
|
|
this._CommandHotKeyMap.clear();
|
|
|
|
|
|
|
|
|
|
//注册自定义(命令和组合键)
|
|
|
|
|
for (let cmd of commandList)
|
|
|
|
|
{
|
|
|
|
|
if (cmd.customize)
|
|
|
|
|
this.RegistCustomCommand(cmd.customize, cmd.command);
|
|
|
|
|
|
|
|
|
|
//组合键
|
|
|
|
|
if (cmd.systemHotkeys)//系统 一定注册
|
|
|
|
|
this.RegistHotkey(cmd.systemHotkeys, cmd.command);
|
|
|
|
|
else if (cmd.hotkeysCustomize)
|
|
|
|
|
this.RegistHotkey(cmd.hotkeysCustomize, cmd.command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//注册默认(命令和组合键)(如果可以的话)
|
|
|
|
|
for (let cmd of commandList)
|
|
|
|
|
{
|
|
|
|
|
//默认自定义命令
|
|
|
|
|
if (!cmd.customize && this.GetCommandCanUseDefaultCustomCmd(cmd))
|
|
|
|
|
this.RegistCustomCommand(cmd.defaultCustom, cmd.command);//注册默认自定义命令
|
|
|
|
|
|
|
|
|
|
//默认组合键
|
|
|
|
|
if (!cmd.hotkeysCustomize && this.GetCommandCanUseDefaultHotkey(cmd))
|
|
|
|
|
this.RegistHotkey(cmd.defaultHotkeys, cmd.command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hotkeys.setScope("custom");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//清除所有的自定义命令和组合键
|
|
|
|
|
ClearCustonCommand()
|
|
|
|
|
{
|
|
|
|
|
for (let cmd of CommandList)
|
|
|
|
|
{
|
|
|
|
|
//为了删除感知里的命令列表,所以调用这个删除,否则直接清空列表就可以了
|
|
|
|
|
this.DeleteCustomCommand(cmd.command);
|
|
|
|
|
cmd.customize = cmd.defaultCustom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//组合键清空
|
|
|
|
|
this._CommandHotKeyMap.clear();
|
|
|
|
|
this._HotKeyCommandMap.clear();
|
|
|
|
|
hotkeys.deleteScope('custom');
|
|
|
|
|
}
|
|
|
|
|
async ReadCommandlist(): Promise<ICommand[]>
|
|
|
|
|
|
|
|
|
|
//从服务器下载自定义命令列表(自定义命令+组合键)
|
|
|
|
|
private async ReadCustomCommandlist(): Promise<ICommand[]>
|
|
|
|
|
{
|
|
|
|
|
let config = await userConfigStore.GetAllConfigs(CommandServer.name);
|
|
|
|
|
if (config)
|
|
|
|
|
{
|
|
|
|
|
this._customCommand = config as any;
|
|
|
|
|
this._needUpload = false;
|
|
|
|
|
this._CustomCommand = config as any;
|
|
|
|
|
this._NeedUpload = false;
|
|
|
|
|
for (let cmd of CommandList)
|
|
|
|
|
{
|
|
|
|
|
let cusCmd = config[cmd.command];
|
|
|
|
|
if (cusCmd)
|
|
|
|
|
{
|
|
|
|
|
cmd.customize = cusCmd.customize;
|
|
|
|
|
cmd.customizeed = cusCmd.customize;
|
|
|
|
|
cmd.hotkeysCustomize = cusCmd.hotkeysCustomize;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return CommandList;
|
|
|
|
|
}
|
|
|
|
|
async SaveOldDataToServer()
|
|
|
|
|
{
|
|
|
|
|
let store = await IndexedDbStore.CADStore();
|
|
|
|
|
let customMap = await store.GetDataMap(StoreName.Command) as Map<string, ICommand>;
|
|
|
|
|
for (let [k, cmd] of customMap)
|
|
|
|
|
{
|
|
|
|
|
this._customCommand[k] = cmd;
|
|
|
|
|
}
|
|
|
|
|
this._needUpload = true;
|
|
|
|
|
await this.Upload();
|
|
|
|
|
|
|
|
|
|
if (!this._needUpload)
|
|
|
|
|
{
|
|
|
|
|
AppToaster.show({
|
|
|
|
|
message: "数据迁移成功,并清理旧数据",
|
|
|
|
|
intent: Intent.SUCCESS,
|
|
|
|
|
timeout: 2000,
|
|
|
|
|
}, "upgrade");
|
|
|
|
|
store.Clear(StoreName.Command);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//保存滚动条高度
|
|
|
|
|
private _LastScrollTop: number;
|
|
|
|
@ -111,14 +201,56 @@ export class CommandServer extends Singleton
|
|
|
|
|
this._LastScrollTop = await store.Get(StoreName.FileId, "scrollTop");
|
|
|
|
|
return this._LastScrollTop;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class UpgradeData implements Command
|
|
|
|
|
{
|
|
|
|
|
async exec()
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
//内部使用 注册自定义命令和快捷键
|
|
|
|
|
|
|
|
|
|
//增加自定义命令的注册
|
|
|
|
|
private RegistCustomCommand(customCmd: string, cmd: string)
|
|
|
|
|
{
|
|
|
|
|
const server = CommandServer.GetInstance() as CommandServer;
|
|
|
|
|
await server.SaveOldDataToServer();
|
|
|
|
|
if (!customCmd) return;
|
|
|
|
|
|
|
|
|
|
this._CustomCommandMap.set(customCmd, cmd);//注册自定义命令
|
|
|
|
|
this._CommandCustomMap.set(cmd, customCmd);
|
|
|
|
|
commandMachine.CommandNameSet.add(customCmd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//删除自定义命令注册
|
|
|
|
|
private DeleteCustomCommand(cmd: string)
|
|
|
|
|
{
|
|
|
|
|
let custom = this._CommandCustomMap.get(cmd);
|
|
|
|
|
if (custom)
|
|
|
|
|
{
|
|
|
|
|
this._CommandCustomMap.delete(cmd);
|
|
|
|
|
this._CustomCommandMap.delete(custom);
|
|
|
|
|
commandMachine.CommandNameSet.delete(custom);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RegistHotkey(hotkey: string, command: string)
|
|
|
|
|
{
|
|
|
|
|
if (!hotkey) return;
|
|
|
|
|
|
|
|
|
|
hotkeys(hotkey, "custom", (e) =>
|
|
|
|
|
{
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
commandMachine.ExecCommand(command);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this._HotKeyCommandMap.set(hotkey, command);
|
|
|
|
|
this._CommandHotKeyMap.set(command, hotkey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DeleteHotkey(command: string)
|
|
|
|
|
{
|
|
|
|
|
let hotkey = this._CommandHotKeyMap.get(command);
|
|
|
|
|
if (hotkey)
|
|
|
|
|
{
|
|
|
|
|
this._CommandHotKeyMap.delete(command);
|
|
|
|
|
this._HotKeyCommandMap.delete(hotkey);
|
|
|
|
|
|
|
|
|
|
hotkeys.unbind(hotkey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|