forked from TypeScriptToLua/TypeScriptToLua.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.ts
More file actions
45 lines (35 loc) · 1.29 KB
/
code.ts
File metadata and controls
45 lines (35 loc) · 1.29 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
import lzstring from "lz-string";
const example = `// Declare exposed API
type Vector = [number, number, number];
declare function findUnitsInRadius(this: void, center: Vector, radius: number): Unit[];
declare interface Unit {
isEnemy(other: Unit): boolean;
kill(): void;
}
// Use declared API in code
function onAbilityCast(this: void, caster: Unit, targetLocation: Vector) {
const units = findUnitsInRadius(targetLocation, 500);
const enemies = units.filter(unit => caster.isEnemy(unit));
for (const enemy of enemies) {
enemy.kill();
}
}
`;
export function getInitialCode() {
if (window.location.hash.startsWith("#src=")) {
const code = window.location.hash.replace("#src=", "").trim();
return decodeURIComponent(code);
}
if (window.location.hash.startsWith("#code/")) {
const code = window.location.hash.replace("#code/", "").trim();
return lzstring.decompressFromEncodedURIComponent(code) || "";
}
return example;
}
export function updateCodeHistory(code: string) {
const hash = `code/${lzstring.compressToEncodedURIComponent(code)}`;
window.history.replaceState({}, "", `#${hash}`);
}
export function getPlaygroundUrlForCode(code: string) {
return `/play/#code/${lzstring.compressToEncodedURIComponent(code)}`;
}