-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-editor.js
More file actions
74 lines (64 loc) · 1.75 KB
/
code-editor.js
File metadata and controls
74 lines (64 loc) · 1.75 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
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
// Phosphor - a browser-based microcomputer
// Copyright (c) 2017-2018 Marc Lepage
'use strict';
const TextEditor = require('./text-editor.js');
const BG_COLOR = 21;
const FG_COLOR = 42;
const UI_COLOR = 42;
module.exports = class CodeEditor {
async main(...args) {
this.editor = new TextEditor();
}
draw() {
const P = this.P;
P.clear(BG_COLOR);
P.box(0, 0, 240, 8, UI_COLOR);
P.box(0, 152, 240, 8, UI_COLOR);
this.editor.draw(P);
}
onKeyDown(e) {
if (e.key.length == 1) {
const code = e.key.charCodeAt();
if (32 <= code && code < 127) {
this.editor.edit(e.key);
}
} else {
switch (e.key) {
case 'ArrowDown': this.editor.cursorDown(e.shiftKey); break;
case 'ArrowLeft': this.editor.cursorLeft(e.shiftKey); break;
case 'ArrowRight': this.editor.cursorRight(e.shiftKey); break;
case 'ArrowUp': this.editor.cursorUp(e.shiftKey); break;
case 'Backspace': this.editor.edit('\b'); break;
case 'Enter': this.editor.edit('\n'); break;
}
}
}
async onResume() {
const P = this.P;
const name = P.load();
const fd = P.open(name, 'r');
if (fd != -1) {
const contents = await P.read(fd, 'a');
P.close(fd);
this.editor.reset();
this.editor.setTextColor(FG_COLOR, BG_COLOR);
this.editor.edit(contents);
this.editor.fixCursorAfterReset();
} else {
this.editor.reset();
this.editor.setTextColor(FG_COLOR, BG_COLOR);
}
}
onSuspend() {
const P = this.P;
if (!this.editor.isModified())
return;
const name = P.load();
const fd = P.open(name, 'w');
if (fd != -1) {
const contents = this.editor.getText();
P.write(fd, contents);
P.close(fd);
}
}
};