Home Reference Source

cables_dev/cables_ui/src/ui/commands/commands.js

  1. import { uuid } from "cables/src/core/utils.js";
  2. import { CmdDebug } from "./cmd_debug.js";
  3. import { CmdFiles } from "./cmd_files.js";
  4. import { CmdOps } from "./cmd_op.js";
  5. import { CmdPatch } from "./cmd_patch.js";
  6. import { CmdRenderer } from "./cmd_renderer.js";
  7. import { CmdTimeline } from "./cmd_timeline.js";
  8. import { CmdUi } from "./cmd_ui.js";
  9. /**
  10. * @typedef CommandObject
  11. * @property {string} cmd
  12. * @property {function} func
  13. * @property {string} category
  14. * @property {string} [id]
  15. * @property {string} [icon]
  16. * @property {string} [infotext]
  17. * @property {string} [hotkey]
  18. * @property {boolean} [keybindable]
  19. * @property {("hasCommunity"|"showRemoteViewer")} [frontendOption]
  20. */
  21. export { Commands };
  22. class Commands
  23. {
  24. /** @type {CommandObject[]} */
  25. static commands = [];
  26. static init()
  27. {
  28. Commands.commands = Commands.commands.concat(CmdDebug.commands);
  29. Commands.commands = Commands.commands.concat(CmdPatch.commands);
  30. Commands.commands = Commands.commands.concat(CmdRenderer.commands);
  31. Commands.commands = Commands.commands.concat(CmdTimeline.commands);
  32. Commands.commands = Commands.commands.concat(CmdUi.commands);
  33. Commands.commands = Commands.commands.concat(CmdOps.commands);
  34. Commands.commands = Commands.commands.concat(CmdFiles.commands);
  35. for (let i = 0; i < Commands.commands.length; i++)
  36. {
  37. if (Commands.commands[i])
  38. {
  39. if (!Commands.commands[i].category) console.warn("cmd has no category ", Commands.commands[i].cmd);
  40. Commands.commands[i].id = uuid();
  41. }
  42. }
  43. CMD.FILES = CmdFiles;
  44. CMD.UI = CmdUi;
  45. CMD.DEBUG = CmdDebug;
  46. CMD.OP = CmdOps;
  47. CMD.PATCH = CmdPatch;
  48. CMD.RENDERER = CmdRenderer;
  49. CMD.TIMELINE = CmdTimeline;
  50. CMD.commands = Commands.commands;
  51. CMD.exec = Commands.exec;
  52. }
  53. /**
  54. * @param {string} id
  55. */
  56. static getById(id)
  57. {
  58. for (let i = 0; i < Commands.commands.length; i++)
  59. {
  60. if (Commands.commands[i].id == id)
  61. {
  62. return Commands.commands[i];
  63. }
  64. }
  65. }
  66. /**
  67. * @returns {CommandObject[]}
  68. */
  69. static getKeyBindableCommands()
  70. {
  71. let arr = [];
  72. for (let i = 0; i < Commands.commands.length; i++)
  73. if (Commands.commands[i].keybindable)
  74. arr.push(Commands.commands[i]);
  75. arr = arr.sort((a, b) =>
  76. {
  77. return (a.category + " " + a.cmd).localeCompare(b.category + " " + b.cmd);
  78. });
  79. return arr;
  80. }
  81. /**
  82. * @param {string} cmd
  83. */
  84. static exec(cmd)
  85. {
  86. const found = Commands.getCommandByName(cmd);
  87. if (found && found.func)
  88. {
  89. found.func();
  90. }
  91. else
  92. {
  93. console.warn("cmd has no func", cmd, found);
  94. }
  95. if (!found)console.warn("command not found:" + cmd);
  96. }
  97. /**
  98. * @param {string} name
  99. */
  100. static getCommandByName(name)
  101. {
  102. for (let i = 0; i < CMD.commands.length; i++)
  103. {
  104. if (Commands.commands[i].cmd == name)
  105. {
  106. return Commands.commands[i];
  107. }
  108. }
  109. }
  110. /**
  111. * @param {function} func
  112. */
  113. static getCommandByFunction(func)
  114. {
  115. let cmd = null;
  116. for (let i = 0; i < Commands.commands.length; i++)
  117. {
  118. if (Commands.commands[i].func == func)
  119. {
  120. cmd = Commands.commands[i];
  121. break;
  122. }
  123. }
  124. return cmd;
  125. }
  126. }
  127. const CMD = { };
  128. export default CMD;