-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.js
97 lines (79 loc) · 2.34 KB
/
editor.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
const editor = document.querySelector('.editor');
const preview = document.querySelector('.preview');
const source = document.getElementById("source");
const errors = document.getElementById("errors");
const resizer = document.querySelector('.resizer');
let isResizing = false;
resizer.addEventListener('mousedown', (e) => {
isResizing = true;
});
document.addEventListener('mousemove', (e) => {
if (!isResizing) return;
currentX = e.clientX;
editor.style.width = `${e.clientX}px`;
resizer.style.left = `${e.clientX}px`
preview.style.left = `${e.clientX}px`;
});
document.addEventListener('mouseup', (e) => {
isResizing = false;
});
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key === 'Enter') {
// Trigger your button's click event here
document.getElementById('compile').click();
}
});
let rom = [];
function compile() {
errors.innerHTML = '';
try {
rom = msm.assemble(source.value)
msm.run(rom);
} catch(e) {
errors.innerHTML = e.toString()
}
source.focus();
display_update();
display_rom();
display_share();
}
function to_hex(n) {
return n.toString(16).padStart(2, 0);
}
function display_cells(id, cells) {
var $target = document.getElementById(id);
$target.innerHTML = cells.map(to_hex).join(" ");
}
function display_buffer() {
display_cells("buffer", msm.buffer);
}
function display_ram() {
display_cells("ram", msm.memory);
}
function display_rom() {
display_cells("rom", rom);
document.getElementById("rom").innerHTML += "<br><br>(" + rom.length + " bytes)";
}
function display_share() {
document.getElementById("share").innerHTML = `<a href="/system.html?r=${btoa(rom.map(e => String.fromCharCode(e)).join(''))}">link<a>`;
const bytes = new Uint8Array(rom);
const blob = new Blob([bytes], { type: 'application/octet-stream' });
document.getElementById("share").innerHTML += ` <a href="${URL.createObjectURL(blob)}" download="game.rom">download<a>`;
}
function display_stack() {
display_cells("wstack", msm.wstack);
display_cells("rstack", msm.rstack);
}
function display_update() {
display_buffer();
display_ram();
display_stack();
}
function toggle() {
let body = document.querySelector('body');
if (body.classList.contains('toggle')) {
body.classList.remove('toggle');
} else {
body.classList.add('toggle');
}
}