-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhydra-debug.js
160 lines (147 loc) · 5.93 KB
/
hydra-debug.js
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
149
150
151
152
153
154
155
156
157
158
159
160
{
const getHydra = function () {
const whereami = window.location?.href?.includes("hydra.ojack.xyz")
? "editor"
: window.atom?.packages
? "atom"
: "idk";
if (whereami === "editor") {
return window.hydraSynth;
}
if (whereami === "atom") {
return global.atom.packages.loadedPackages["atom-hydra"]
.mainModule.main.hydra;
}
let _h = [
window.hydraSynth,
window._hydra,
window.hydra,
window.h,
window.H,
window.hy
].find(h => h?.regl);
return _h;
};
window._hydra = getHydra();
window._hydraScope = _hydra.sandbox.makeGlobal ? window : _hydra.synth;
}
{
async function main() {
await _hydraScope.loadScript(
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"
);
await _hydraScope.loadScript(
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/languages/glsl.min.js"
);
await _hydraScope.loadScript(
"https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.14.7/beautify.js"
);
hljs.configure({ ignoreUnescapedHTML: true });
const brPlugin = {
"before:highlightBlock": ({ block }) => {
block.innerHTML = block.innerHTML
.replace(/\n/g, "")
.replace(/<br[ /]*>/g, "\n");
},
"after:highlightBlock": ({ result }) => {
result.value = result.value.replace(/\n/g, "<br>");
},
};
hljs.addPlugin(brPlugin);
const link = document.createElement("link");
link.href =
"https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/base16/tomorrow-night.min.css";
link.type = "text/css";
link.rel = "stylesheet";
document.getElementsByTagName("head")[0].appendChild(link);
{
function cleanCode(code) {
return js_beautify(code).replace(/\n{2,}/gm, "\n\n") + "\n\n";
}
function logHighlightedCode(glslSource, output) {
let passes = glslSource.glsl();
let code = passes[0].frag;
document.getElementById("hydra-debug")?.remove();
const pre = document.createElement("pre");
pre.className = "hljs";
pre.style.position = "sticky";
pre.style.height = "96%";
pre.style.padding = "4%";
pre.style.overflow = "scroll";
const codeElement = document.createElement("code");
codeElement.className = "language-glsl";
pre.appendChild(codeElement);
codeElement.innerText = cleanCode(code)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
hljs.highlightElement(pre, {
language: "glsl",
ignoreIllegals: true,
});
const wrapper = document.createElement("div");
wrapper.id = "hydra-debug";
wrapper.style.zIndex = 999;
wrapper.style.overflow = "hidden";
wrapper.style.position = "fixed";
wrapper.style.width = "40%";
wrapper.style.height = "90%";
wrapper.style.left = "58%";
wrapper.style.top = "5%";
wrapper.style.fontSize = "14px";
const close = document.createElement("button");
close.innerText = "x";
close.style.position = "absolute";
close.style.right = "0px";
close.style.top = "2%";
close.style.fontSize = "20px";
close.style.backgroundColor = "white";
close.style.color = "black";
close.style.border = "none";
close.onclick = () => {
document.getElementById("hydra-debug")?.remove();
};
wrapper.appendChild(pre);
wrapper.appendChild(close);
if (output) {
pre.contentEditable = "true";
const run = document.createElement("button");
run.innerText = ">";
run.style.position = "absolute";
run.style.right = "30px";
run.style.top = "2%";
run.style.fontSize = "20px";
run.style.backgroundColor = "white";
run.style.color = "black";
run.style.border = "none";
run.onclick = () => {
codeElement.innerText = cleanCode(pre.innerText)
pre.innerHTML = "";
pre.appendChild(codeElement);
hljs.highlightElement(pre, {
language: "glsl",
ignoreIllegals: true,
});
passes[0].frag = pre.innerText
.replace("&", "&")
.replace("<", "<")
.replace(">", ">")
.replace(""", '"')
.replace("'", "'");
output.render(passes);
};
wrapper.appendChild(run);
}
document.body.append(wrapper);
}
const gS = _hydraScope.osc().constructor.prototype;
gS.debug = function (output) {
logHighlightedCode(this, output);
return output ? this.out(output) : this;
};
}
}
main();
}