-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractive.js
132 lines (123 loc) · 3.39 KB
/
interactive.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
import "xterm/css/xterm.css";
import { Terminal } from "xterm";
import { FitAddon } from "xterm-addon-fit";
import { AttachAddon } from "xterm-addon-attach";
let currentProgram = null;
let InitialConnectedToAPI = false;
let API_endpoint = "adk-api.replit.app";
function reconnectToExecApi(isManual) {
if (
window.AARDVARK_API_WEBSOCKET &&
window.AARDVARK_API_WEBSOCKET.readyState === 1
)
window.AARDVARK_API_WEBSOCKET.close();
window.AARDVARK_API_WEBSOCKET = new WebSocket(`wss://${API_endpoint}`);
window.AARDVARK_API_WEBSOCKET.addEventListener("message", (ev) => {
const data = JSON.parse(ev.data);
if (typeof data.output === "string") {
terminal.write(data.output);
}
if (typeof data.state === "number") {
if (data.state === 2) {
currentProgram = "adk";
} else if (currentProgram === "adk") {
currentProgram = null;
}
if (data.state === 0) {
if (InitialConnectedToAPI) clearLine(false);
InitialConnectedToAPI = true;
}
}
});
window.AARDVARK_API_WEBSOCKET.addEventListener("open", () => {
if (isManual) terminal.write("Connected to the code execution api.\r\n");
});
window.AARDVARK_API_WEBSOCKET.addEventListener("close", () => {
window.alert(
"Lost connection to the code execution api. (or didn't even connect)"
);
});
}
function checkCodeExecLocalStatus() {
const status =
window.AARDVARK_API_WEBSOCKET &&
window.AARDVARK_API_WEBSOCKET.readyState === 1;
window.alert(`You are${!status ? " not" : ""} connected to the api.`);
}
class CodeBlock {
constructor(code) {
this.code = code;
}
}
var terminal;
window.addEventListener("load", () => {
reconnectToExecApi(true);
terminal = new Terminal({
fontFamily: "Monospace",
theme: {
background: "#1d1d1d",
},
fontSize: 14,
cursorBlink: true,
cursorStyle: "bar",
cursorInactiveStyle: "none",
cursorWidth: 2,
});
const termEl = document.getElementById("terminal");
const fit = new FitAddon();
terminal.loadAddon(fit);
terminal.open(termEl);
fit.fit();
let modifiers = {
shift: false,
ctrl: false,
alt: false,
meta: false,
};
const isMac = navigator.userAgent.includes("Mac");
terminal.onData((data) => {
let code = data.charCodeAt();
if (currentProgram === "adk")
return AARDVARK_API_WEBSOCKET.send(JSON.stringify({ input: data }));
else if (data === "\r") return terminal.write("\r\n");
else if (data === "\n") return terminal.write("\r\n");
else if (
(code === 127) &
((isMac & modifiers.meta) | (!isMac & modifiers.ctrl))
)
return terminal.write("\x1b[2K\r");
if (code === 127) {
return terminal.write("\b \b");
}
if (data === "e") {
console.log("DOING IT");
window.AARDVARK_API_WEBSOCKET.send(
JSON.stringify({
runProgram: {
files: {
"main.adk": "let x = 5",
},
args: [],
silenced: false,
},
})
);
}
terminal.write(data);
});
window.addEventListener("keydown", (e) => {
if (e.key === "Shift") modifiers.shift = true;
if (e.key === "Control") modifiers.ctrl = true;
if (e.key === "Alt") modifiers.alt = true;
if (e.key === "Meta") modifiers.meta = true;
});
let keyup = (e) => {
if (e.key === "Shift") modifiers.shift = false;
if (e.key === "Control") modifiers.ctrl = false;
if (e.key === "Alt") modifiers.alt = false;
if (e.key === "Meta") modifiers.meta = false;
};
window.addEventListener("keyup", keyup);
window.addEventListener("blur", keyup);
currentProgram = "adk";
});