|
| 1 | +import fsOperation from "fileSystem"; |
| 2 | +import ajax from "@deadlyjack/ajax"; |
| 3 | +import loader from "dialogs/loader"; |
| 4 | +import helpers from "utils/helpers"; |
| 5 | +import Url from "utils/Url"; |
| 6 | +import constants from "./constants"; |
| 7 | + |
| 8 | +let erudaInstance = null; |
| 9 | +let isInitialized = false; |
| 10 | + |
| 11 | +/** |
| 12 | + * Developer tools module for debugging Acode |
| 13 | + */ |
| 14 | +const devTools = { |
| 15 | + /** |
| 16 | + * Check if Eruda is initialized |
| 17 | + * @returns {boolean} |
| 18 | + */ |
| 19 | + get isInitialized() { |
| 20 | + return isInitialized; |
| 21 | + }, |
| 22 | + |
| 23 | + /** |
| 24 | + * Get the Eruda instance |
| 25 | + * @returns {object|null} |
| 26 | + */ |
| 27 | + get eruda() { |
| 28 | + return erudaInstance; |
| 29 | + }, |
| 30 | + |
| 31 | + /** |
| 32 | + * Initialize Eruda for developer mode |
| 33 | + * @param {boolean} showLoader - Whether to show a loading dialog |
| 34 | + * @returns {Promise<void>} |
| 35 | + */ |
| 36 | + async init(showLoader = false) { |
| 37 | + if (isInitialized) return; |
| 38 | + |
| 39 | + try { |
| 40 | + const erudaPath = Url.join(DATA_STORAGE, "eruda.js"); |
| 41 | + const fs = fsOperation(erudaPath); |
| 42 | + |
| 43 | + if (!(await fs.exists())) { |
| 44 | + if (showLoader) { |
| 45 | + loader.create( |
| 46 | + strings["downloading file"]?.replace("{file}", "eruda.js") || |
| 47 | + "Downloading eruda.js...", |
| 48 | + strings["downloading..."] || "Downloading...", |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const erudaScript = await ajax({ |
| 54 | + url: constants.ERUDA_CDN, |
| 55 | + responseType: "text", |
| 56 | + contentType: "application/x-www-form-urlencoded", |
| 57 | + }); |
| 58 | + await fsOperation(DATA_STORAGE).createFile("eruda.js", erudaScript); |
| 59 | + } finally { |
| 60 | + if (showLoader) loader.destroy(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + const internalUri = await helpers.toInternalUri(erudaPath); |
| 65 | + |
| 66 | + await new Promise((resolve, reject) => { |
| 67 | + const script = document.createElement("script"); |
| 68 | + script.src = internalUri; |
| 69 | + script.id = "eruda-script"; |
| 70 | + script.onload = resolve; |
| 71 | + script.onerror = reject; |
| 72 | + document.head.appendChild(script); |
| 73 | + }); |
| 74 | + |
| 75 | + if (window.eruda) { |
| 76 | + window.eruda.init({ |
| 77 | + useShadowDom: true, |
| 78 | + autoScale: true, |
| 79 | + defaults: { |
| 80 | + displaySize: 50, |
| 81 | + }, |
| 82 | + }); |
| 83 | + |
| 84 | + window.eruda._shadowRoot.querySelector( |
| 85 | + ".eruda-entry-btn", |
| 86 | + ).style.display = "none"; |
| 87 | + |
| 88 | + erudaInstance = window.eruda; |
| 89 | + isInitialized = true; |
| 90 | + } |
| 91 | + } catch (error) { |
| 92 | + console.error("Failed to initialize developer tools", error); |
| 93 | + throw error; |
| 94 | + } |
| 95 | + }, |
| 96 | + |
| 97 | + /** |
| 98 | + * Show the inspector panel |
| 99 | + */ |
| 100 | + show() { |
| 101 | + if (!isInitialized) { |
| 102 | + window.toast?.("Developer mode is not enabled"); |
| 103 | + return; |
| 104 | + } |
| 105 | + const entryBtn = |
| 106 | + erudaInstance?._shadowRoot?.querySelector(".eruda-entry-btn"); |
| 107 | + if (entryBtn) entryBtn.style.display = ""; |
| 108 | + erudaInstance?.show(); |
| 109 | + }, |
| 110 | + |
| 111 | + /** |
| 112 | + * Hide the inspector panel |
| 113 | + */ |
| 114 | + hide() { |
| 115 | + if (!isInitialized) return; |
| 116 | + erudaInstance?.hide(); |
| 117 | + const entryBtn = |
| 118 | + erudaInstance?._shadowRoot?.querySelector(".eruda-entry-btn"); |
| 119 | + if (entryBtn) entryBtn.style.display = "none"; |
| 120 | + }, |
| 121 | + |
| 122 | + /** |
| 123 | + * Toggle the inspector panel visibility |
| 124 | + */ |
| 125 | + toggle() { |
| 126 | + if (!isInitialized) { |
| 127 | + window.toast?.("Developer mode is not enabled"); |
| 128 | + return; |
| 129 | + } |
| 130 | + if (erudaInstance?._isShow) { |
| 131 | + this.hide(); |
| 132 | + } else { |
| 133 | + this.show(); |
| 134 | + } |
| 135 | + }, |
| 136 | + |
| 137 | + /** |
| 138 | + * Destroy Eruda instance |
| 139 | + */ |
| 140 | + destroy() { |
| 141 | + if (!isInitialized) return; |
| 142 | + erudaInstance?.destroy(); |
| 143 | + erudaInstance = null; |
| 144 | + isInitialized = false; |
| 145 | + const script = document.getElementById("eruda-script"); |
| 146 | + if (script) script.remove(); |
| 147 | + }, |
| 148 | +}; |
| 149 | + |
| 150 | +export default devTools; |
0 commit comments