-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.js
185 lines (155 loc) · 5.46 KB
/
main.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Copyright 2023 Undistro Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AceEditor } from "./editor.js";
const selectInstance = NiceSelect.bind(document.getElementById("examples"));
// Add the following polyfill for Microsoft Edge 17/18 support:
// <script src="https://cdn.jsdelivr.net/npm/text-encoding@0.7.0/lib/encoding.min.js"></script>
// (see https://caniuse.com/#feat=textencoder)
if (!WebAssembly.instantiateStreaming) {
// polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const celEditor = new AceEditor("cel-input");
const dataEditor = new AceEditor("data-input");
function run() {
const data = dataEditor.getValue();
const expression = celEditor.getValue();
const output = document.getElementById("output");
output.value = "Evaluating...";
const result = eval(expression, data);
const { output: resultOutput, isError } = result;
output.value = `${resultOutput}`;
output.style.color = isError ? "red" : "white";
}
function share() {
const data = dataEditor.getValue();
const expression = celEditor.getValue();
const obj = {
data: data,
expression: expression,
};
const str = JSON.stringify(obj);
var compressed_uint8array = pako.gzip(str);
var b64encoded_string = btoa(
String.fromCharCode.apply(null, compressed_uint8array)
);
const url = new URL(window.location.href);
url.searchParams.set("content", b64encoded_string);
window.history.pushState({}, "", url.toString());
document.querySelector(".share-url__container").style.display = "flex";
document.querySelector(".share-url__input").value = url.toString();
}
var urlParams = new URLSearchParams(window.location.search);
if (urlParams.has("content")) {
const content = urlParams.get("content");
try {
const decodedUint8Array = new Uint8Array(
atob(content)
.split("")
.map(function (char) {
return char.charCodeAt(0);
})
);
const decompressedData = pako.ungzip(decodedUint8Array, { to: "string" });
if (!decompressedData) {
throw new Error("Invalid content parameter");
}
const obj = JSON.parse(decompressedData);
celEditor.setValue(obj.expression, -1);
dataEditor.setValue(obj.data, -1);
} catch (error) {
console.error(error);
}
}
function copy() {
const copyText = document.querySelector(".share-url__input");
copyText.select();
copyText.setSelectionRange(0, 99999);
navigator.clipboard.writeText(copyText.value);
window.getSelection().removeAllRanges();
const tooltip = document.querySelector(".share-url__tooltip");
tooltip.style.opacity = 1;
setTimeout(() => {
tooltip.style.opacity = 0;
}, 3000);
}
(async function loadAndRunGoWasm() {
const go = new Go();
const buffer = pako.ungzip(
await (await fetch("assets/main.wasm.gz")).arrayBuffer()
);
// A fetched response might be decompressed twice on Firefox.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=610679
if (buffer[0] === 0x1f && buffer[1] === 0x8b) {
buffer = pako.ungzip(buffer);
}
WebAssembly.instantiate(buffer, go.importObject)
.then((result) => {
go.run(result.instance);
document.getElementById("run").disabled = false;
document.getElementById("output").placeholder =
"Press 'Run' to evaluate your CEL expression.";
})
.catch((err) => {
console.error(err);
});
})();
const runButton = document.getElementById("run");
const shareButton = document.getElementById("share");
const copyButton = document.getElementById("copy");
runButton.addEventListener("click", run);
shareButton.addEventListener("click", share);
copyButton.addEventListener("click", copy);
document.addEventListener("keydown", (event) => {
if ((event.ctrlKey || event.metaKey) && event.code === "Enter") {
run();
}
});
fetch("../assets/data.json")
.then((response) => response.json())
.then(({ examples, versions }) => {
// Dynamically set the CEL Go version
document.getElementById("version").innerText = versions["cel-go"];
// Load the examples into the select element
const examplesList = document.getElementById("examples");
examples.forEach((example) => {
const option = document.createElement("option");
option.value = example.name;
option.innerText = example.name;
if (example.name === "default") {
if (!urlParams.has("content")) {
celEditor.setValue(example.cel, -1);
dataEditor.setValue(example.data, -1);
}
} else {
examplesList.appendChild(option);
}
});
selectInstance.update();
examplesList.addEventListener("change", (event) => {
const example = examples.find(
(example) => example.name === event.target.value
);
celEditor.setValue(example.cel, -1);
dataEditor.setValue(example.data, -1);
});
})
.catch((err) => {
console.error(err);
});