All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
6.0 KiB

1 year ago
  1. /* -------------< Ace >------------ */
  2. Hooks.once("init", () => {
  3. ["ace/mode/javascript", "ace/ext/language_tools", "ace/ext/error_marker", "ace/theme/twilight", "ace/snippets/javascript"].forEach((s) => ace.config.loadModule(s));
  4. });
  5. /* -------------< End Ace >------------ */
  6. Hooks.on("init", () => {
  7. Macros.registerSheet?.("macroeditor", AceMacroConfig, {
  8. makeDefault: true,
  9. label: "Ace Macro Editor",
  10. // types: ["script", "chat"],
  11. });
  12. });
  13. class AceMacroConfig extends MacroConfig {
  14. async _render(...args) {
  15. await super._render(...args);
  16. const configElement = this.element;
  17. configElement
  18. .find("div.form-group.stacked.command")
  19. .append(
  20. `<button type="button" class="macro-editor-expand" title="Expand Editor"><i class="fas fa-expand-alt"></i></button><div class="macro-editor" id="macroEditor-${this.object.id}"></div>`
  21. );
  22. if (game.settings.get("macroeditor", "defaultShow")) {
  23. configElement.find('.command textarea[name="command"]').css("display", "none");
  24. const furnace = this.element.find("div.furnace-macro-command");
  25. if (furnace.length !== 0) {
  26. furnace.css("display", "none");
  27. }
  28. } else {
  29. configElement.find(".macro-editor").css("display", "none");
  30. configElement.find(".macro-editor-expand").css("display", "none");
  31. }
  32. configElement
  33. .find(".sheet-footer")
  34. .append('<button type="button" class="macro-editor-button" title="Toggle Code Editor" name="editorButton"><i class="fas fa-terminal"></i></button>');
  35. let editor = (this.editor = ace.edit(`macroEditor-${this.object.id}`));
  36. this.editor.session.on("changeMode", function (e, session) {
  37. if ("ace/mode/javascript" === session.getMode().$id) {
  38. if (!!session.$worker) {
  39. session.$worker.send("setOptions", [
  40. {
  41. esversion: 9,
  42. esnext: false,
  43. },
  44. ]);
  45. }
  46. }
  47. });
  48. // Merge ace-lib user-settings with module settings
  49. this.editor.setOptions(
  50. mergeObject(ace.userSettings, {
  51. mode: "ace/mode/javascript",
  52. })
  53. );
  54. configElement.find(".macro-editor-button").on("click", (event) => {
  55. event.preventDefault();
  56. if (configElement.find(".macro-editor").css("display") == "none") {
  57. configElement.find('.command textarea[name="command"]').css("display", "none");
  58. configElement.find(".macro-editor").css("display", "");
  59. configElement.find(".macro-editor-expand").css("display", "");
  60. this.editor.setValue(configElement.find('.command textarea[name="command"]').val(), -1);
  61. // furnace compat / advanced macros
  62. const furnace = configElement.find("div.furnace-macro-command");
  63. if (furnace.length !== 0) {
  64. furnace.css("display", "none");
  65. }
  66. } else {
  67. configElement.find('.command textarea[name="command"]').css("display", "");
  68. configElement.find(".macro-editor").css("display", "none");
  69. configElement.find(".macro-editor-expand").css("display", "none");
  70. // furnace compat / advanced macros
  71. const furnace = configElement.find("div.furnace-macro-command");
  72. if (furnace.length !== 0) {
  73. furnace.css("display", "");
  74. furnace.trigger("change");
  75. }
  76. }
  77. });
  78. configElement.find(".macro-editor-expand").on("click", (event) => {
  79. event.preventDefault();
  80. if (configElement.find(".macro-editor").hasClass("fullscreen")) {
  81. configElement.find(".macro-editor").removeClass("fullscreen");
  82. configElement.find(".macro-editor-expand").removeClass("fullscreen");
  83. configElement.find(".macro-editor-expand").prop("title", "Expand Editor");
  84. configElement.find(".macro-editor-expand i.fas.fa-compress-alt").attr("class", "fas fa-expand-alt");
  85. configElement.find(".window-resizable-handle").css("display", "");
  86. } else {
  87. configElement.find(".macro-editor").addClass("fullscreen");
  88. configElement.find(".macro-editor-expand").addClass("fullscreen");
  89. configElement.find(".macro-editor-expand").prop("title", "Shrink Editor");
  90. configElement.find(".macro-editor-expand i.fas.fa-expand-alt").attr("class", "fas fa-compress-alt");
  91. configElement.find(".window-resizable-handle").css("display", "none");
  92. }
  93. });
  94. this.editor.setValue(configElement.find('textarea[name="command"]').val(), -1);
  95. this.editor.getSession().on("change", () => {
  96. configElement.find('textarea[name="command"]').val(editor.getSession().getValue());
  97. });
  98. this.editor.commands.addCommand({
  99. name: "Save",
  100. bindKey: { win: "Ctrl-S", mac: "Command-S" },
  101. exec: () => configElement.find("form.editable").trigger("submit"),
  102. });
  103. this.editor.commands.addCommand({
  104. name: "Execute",
  105. bindKey: { win: "Ctrl-E", mac: "Command-E" },
  106. exec: () => configElement.find("button.execute").trigger("click"),
  107. });
  108. // watch for resizing of editor
  109. new ResizeObserver(() => {
  110. editor.resize();
  111. editor.renderer.updateFull();
  112. }).observe(this.editor.container);
  113. }
  114. close(...args) {
  115. this.editor.destroy();
  116. super.close(...args);
  117. }
  118. }
  119. Hooks.once("init", function () {
  120. game.settings.register("macroeditor", "defaultShow", {
  121. name: "Show Macro editor by default",
  122. hint: "Shows the code editor by default instead of the default editor",
  123. default: true,
  124. type: Boolean,
  125. scope: "client",
  126. config: true,
  127. });
  128. });
  129. // Item Macro Compat
  130. Hooks.once("init", async function () {
  131. // only trigger if item macro is active and the compatible version isn't V9 yet
  132. if (!game.modules.get("itemacro")?.active || game.modules.get("itemacro")?.data?.compatibleCoreVersion?.match(/\./g)?.length !== 2) return;
  133. let classLoc = game.modules.get("itemacro")?.esmodules?.[0]?.replace("hooks", "ItemMacroConfig");
  134. if (classLoc) {
  135. let theClass = (await import(window.location.origin + "/" + classLoc)).ItemMacroConfig;
  136. theClass.prototype._render = AceMacroConfig.prototype._render;
  137. theClass.prototype.close = AceMacroConfig.prototype.close;
  138. }
  139. });