-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotnetconsole.main.js
153 lines (116 loc) · 3.67 KB
/
dotnetconsole.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
Console = (() => {
let bufferHeight = 24;
let bufferWidth = 80;
let windowHeight = 24;
let windowWidth = 80;
let cursorLeft = 0;
let cursorTop = 0;
let cursorVisible = true;
let foregroundColor = 15;
let backgroundColor = 0;
const bufferTable = document.createElement('table');
let worker;
// Private methods
function getCursorDataCell() {
return document.querySelector('.cursor') || getCell(cursorLeft, cursorTop);
}
function getCell(x, y) {
return bufferTable.childNodes[y].childNodes[x];
}
// Row manipulations
function addRow() {
const row = document.createElement('tr');
for (let columnIndex = 0; columnIndex < windowWidth; columnIndex++) {
const dataCell = document.createElement('td');
dataCell.innerHTML = ' ';
dataCell.className = `foreground-${foregroundColor} background-${backgroundColor}`;
row.appendChild(dataCell);
}
bufferTable.appendChild(row);
}
function removeFirstRow() {
bufferTable.childNodes[0].remove();
}
// Operations
function scroll() {
// Scroll by one row.
removeFirstRow();
addRow();
// Reposition cursor.
setCursorPosition({cursorLeft, cursorTop});
}
function setColors(options) {
foregroundColor = options.foregroundColor >= 0 ? options.foregroundColor : 15;
backgroundColor = options.backgroundColor >= 0 ? options.backgroundColor : 0;
}
function setCursorPosition(options) {
// Remove cursor class from previous data cell.
oldCursorDataCell = getCursorDataCell();
oldCursorDataCell.classList.remove('cursor');
cursorLeft = options.cursorLeft;
cursorTop = options.cursorTop;
newCursorDataCell = getCursorDataCell();
newCursorDataCell.classList.add('cursor');
}
function writeCharacter({character, x, y}) {
dataCell = getCell(x, y);
if (character === ' ') {
dataCell.innerHTML = ' ';
} else {
dataCell.innerText = character;
}
// Change colors.
dataCell.className = `foreground-${foregroundColor} background-${backgroundColor}`;
if (cursorLeft === x && cursorTop === y) {
dataCell.classList.add('cursor');
}
}
function clear() {
for (let i = 0; i < bufferHeight; i++) {
scroll();
}
}
let operations = {scroll, setColors, setCursorPosition, writeCharacter, clear};
// Initialize table.
for (let rowIndex = 0; rowIndex < bufferHeight; rowIndex++) {
addRow();
}
setCursorPosition({cursorLeft: 0, cursorTop: 0});
// Add table to body after DOM is loaded.
document.addEventListener("DOMContentLoaded", () => {
const body = document.querySelector('body');
body.appendChild(bufferTable);
});
// Listen to keys.
document.addEventListener("keydown", event => {
if (event.isComposing) {
return;
}
worker.postMessage({
operation: 'keydown',
key: event.key,
code: event.code,
altKey: event.altKey,
ctrlKey: event.ctrlKey,
shiftKey: event.shiftKey
});
});
// Listen to focus events.
const html = document.querySelector('html')
window.addEventListener('blur', event => {
html.classList.add('inactive');
});
window.addEventListener('focus', event => {
html.classList.remove('inactive');
});
return class Console {
static run(stringUrl) {
document.addEventListener("DOMContentLoaded", () => {
worker = new Worker(stringUrl);
worker.onmessage = messageEvent => {
operations[messageEvent.data.operation](messageEvent.data);
}
});
}
}
})();