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