Home Reference Source

cables_dev/cables_ui/src/ui/components/tabs/tab_keybinds.js

  1. import { ele } from "cables-shared-client";
  2. import defaultOps from "../../defaultops.js";
  3. import Tab from "../../elements/tabpanel/tab.js";
  4. import TabPanel from "../../elements/tabpanel/tabpanel.js";
  5. import { gui } from "../../gui.js";
  6. import { getHandleBarHtml } from "../../utils/handlebars.js";
  7. import { editorSession } from "../../elements/tabpanel/editor_session.js";
  8. import opNames from "../../opnameutils.js";
  9. import { PortDir, portType } from "../../core_constants.js";
  10. import ModalDialog from "../../dialogs/modaldialog.js";
  11. import { Commands } from "../../commands/commands.js";
  12. import { InputBindings } from "../../inputbindings.js";
  13. export default class TabInputBindings
  14. {
  15. static TABSESSION_NAME = "InputBindings";
  16. lastCount = 0;
  17. #timeOut = null;
  18. #tab;
  19. /**
  20. * @param {TabPanel} [tabs]
  21. */
  22. constructor(tabs)
  23. {
  24. tabs ||= gui.mainTabs;
  25. this.#tab = new Tab(TabInputBindings.TABSESSION_NAME, { "icon": "list", "singleton": true, "infotext": "tab_keybindings", "padding": true });
  26. tabs.addTab(this.#tab, true);
  27. editorSession.rememberOpenEditor(TabInputBindings.TABSESSION_NAME, TabInputBindings.TABSESSION_NAME, { }, true);
  28. let html = getHandleBarHtml("tab_keybindings");
  29. this.#tab.html(html);
  30. this.update();
  31. gui.maintabPanel.show(true);
  32. ele.clickable(ele.byId("keybind_add"), (o) =>
  33. {
  34. this.editBinding({});
  35. });
  36. this.#tab.on("close", () =>
  37. {
  38. clearInterval(this.#timeOut);
  39. editorSession.remove(TabInputBindings.TABSESSION_NAME, TabInputBindings.TABSESSION_NAME);
  40. });
  41. }
  42. update()
  43. {
  44. gui.corePatch().tempData.continueStepDebugLog = gui.corePatch().tempData.continueStepDebugLog || [];
  45. let html = "";
  46. html += "<table class=\"editor_spreadsheet\" style=\"width:100%\">";
  47. html += "<tr>";
  48. html += "<td class=\"colname\">Category</td>";
  49. html += "<td class=\"colname\">Action</td>";
  50. html += "<td class=\"colname\">Command</td>";
  51. html += "<td class=\"colname\"> </td>";
  52. html += "<tr>";
  53. for (let i = 0; i < InputBindings.ACTIONS.length; i++)
  54. {
  55. let cmd = Commands.getCommandByFunction(InputBindings.ACTIONS[i].func);
  56. const cmdTitle = cmd?.cmd || "none";
  57. html += "<tr>";
  58. html += "<td>";
  59. html += InputBindings.ACTIONS[i].category;
  60. html += "</td>";
  61. html += "<td>";
  62. html += InputBindings.ACTIONS[i].title;
  63. html += "</td>";
  64. html += "<td>" + cmdTitle;
  65. html += "</td>";
  66. html += "</td>";
  67. html += "<td>";
  68. html += "<a id=\"inpbind" + i + "\" class=\"button-small\" >edit</a>";
  69. html += "</td>";
  70. html += "</tr>";
  71. }
  72. html += "</table>";
  73. ele.byId("debugger_log").innerHTML = html;
  74. for (let i = 0; i < InputBindings.ACTIONS.length; i++)
  75. {
  76. const action = InputBindings.ACTIONS[i].id;
  77. ele.clickable(ele.byId("inpbind" + i), () =>
  78. {
  79. this.editBinding(action);
  80. });
  81. }
  82. }
  83. editBinding(actionId)
  84. {
  85. let html = "";
  86. html += "Command:";
  87. html += "<select id=\"cmdselect\">";
  88. html += "<option value=\"default\">default</option>";
  89. const cmds = Commands.getKeyBindableCommands();
  90. for (let i = 0; i < cmds.length; i++)
  91. {
  92. html += "<option value=\"" + cmds[i].cmd + "\"";
  93. if (cmds[i].func == gui.inputBindings.getBind(actionId).func) html += " selected=\"selected\" ";
  94. html += ">";
  95. html += cmds[i].category + ": " + cmds[i].cmd + "</option>";
  96. }
  97. html += "</select>";
  98. const dialog = new ModalDialog({
  99. "title": "New Binding",
  100. "text": "",
  101. "html": html,
  102. "showOkButton": true,
  103. "okButton": {
  104. "callback": () =>
  105. {
  106. const cmdname = ele.byId("cmdselect").value;
  107. let cmd;
  108. if (cmdname == "default")
  109. {
  110. const bi = gui.inputBindings.getBind(actionId);
  111. console.log("bi", bi);
  112. cmd = Commands.getCommandByFunction(bi.default);
  113. if (!cmd)cmd = {};
  114. }
  115. else
  116. cmd = Commands.getCommandByName(cmdname);
  117. gui.inputBindings.setBindingFunc(actionId, cmd.func, true);
  118. this.update();
  119. }
  120. }
  121. });
  122. }
  123. }
  124. editorSession.addListener(TabInputBindings.TABSESSION_NAME, (id, data) =>
  125. {
  126. new TabInputBindings(gui.mainTabs);
  127. });