cables_dev/cables_ui/src/ui/commands/commands.js
- import { uuid } from "cables/src/core/utils.js";
- import { CmdDebug } from "./cmd_debug.js";
- import { CmdFiles } from "./cmd_files.js";
- import { CmdOps } from "./cmd_op.js";
- import { CmdPatch } from "./cmd_patch.js";
- import { CmdRenderer } from "./cmd_renderer.js";
- import { CmdTimeline } from "./cmd_timeline.js";
- import { CmdUi } from "./cmd_ui.js";
- /**
- * @typedef CommandObject
- * @property {string} cmd
- * @property {function} func
- * @property {string} category
- * @property {string} [id]
- * @property {string} [icon]
- * @property {string} [infotext]
- * @property {string} [hotkey]
- * @property {boolean} [keybindable]
- * @property {("hasCommunity"|"showRemoteViewer")} [frontendOption]
- */
- export { Commands };
- class Commands
- {
- /** @type {CommandObject[]} */
- static commands = [];
- static init()
- {
- Commands.commands = Commands.commands.concat(CmdDebug.commands);
- Commands.commands = Commands.commands.concat(CmdPatch.commands);
- Commands.commands = Commands.commands.concat(CmdRenderer.commands);
- Commands.commands = Commands.commands.concat(CmdTimeline.commands);
- Commands.commands = Commands.commands.concat(CmdUi.commands);
- Commands.commands = Commands.commands.concat(CmdOps.commands);
- Commands.commands = Commands.commands.concat(CmdFiles.commands);
- for (let i = 0; i < Commands.commands.length; i++)
- {
- if (Commands.commands[i])
- {
- if (!Commands.commands[i].category) console.warn("cmd has no category ", Commands.commands[i].cmd);
- Commands.commands[i].id = uuid();
- }
- }
- CMD.FILES = CmdFiles;
- CMD.UI = CmdUi;
- CMD.DEBUG = CmdDebug;
- CMD.OP = CmdOps;
- CMD.PATCH = CmdPatch;
- CMD.RENDERER = CmdRenderer;
- CMD.TIMELINE = CmdTimeline;
- CMD.commands = Commands.commands;
- CMD.exec = Commands.exec;
- }
- /**
- * @param {string} id
- */
- static getById(id)
- {
- for (let i = 0; i < Commands.commands.length; i++)
- {
- if (Commands.commands[i].id == id)
- {
- return Commands.commands[i];
- }
- }
- }
- /**
- * @returns {CommandObject[]}
- */
- static getKeyBindableCommands()
- {
- let arr = [];
- for (let i = 0; i < Commands.commands.length; i++)
- if (Commands.commands[i].keybindable)
- arr.push(Commands.commands[i]);
- arr = arr.sort((a, b) =>
- {
- return (a.category + " " + a.cmd).localeCompare(b.category + " " + b.cmd);
- });
- return arr;
- }
- /**
- * @param {string} cmd
- */
- static exec(cmd)
- {
- const found = Commands.getCommandByName(cmd);
- if (found && found.func)
- {
- found.func();
- }
- else
- {
- console.warn("cmd has no func", cmd, found);
- }
- if (!found)console.warn("command not found:" + cmd);
- }
- /**
- * @param {string} name
- */
- static getCommandByName(name)
- {
- for (let i = 0; i < CMD.commands.length; i++)
- {
- if (Commands.commands[i].cmd == name)
- {
- return Commands.commands[i];
- }
- }
- }
- /**
- * @param {function} func
- */
- static getCommandByFunction(func)
- {
- let cmd = null;
- for (let i = 0; i < Commands.commands.length; i++)
- {
- if (Commands.commands[i].func == func)
- {
- cmd = Commands.commands[i];
- break;
- }
- }
- return cmd;
- }
- }
- const CMD = { };
- export default CMD;