-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
102 lines (77 loc) · 2.46 KB
/
index.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
"use strict";
// TODO: factor out some of this UI logic
const DEBUG = false;
console.debug = function(...args) {
if (DEBUG) {
console.log(...args);
}
};
// -----
const S = new Seymour(microVizContainer, macroVizContainer);
S.addListener('codeChanged', code => {
clearError();
localStorage.setItem('seymour-program', S.editor.getValue());
});
S.addListener('run', (_, __) => toggleRunning(true));
S.addListener('error', e => displayError(e.toString()));
S.addListener('done', (_, __) => toggleRunning(false));
let running = false;
function toggleRunning(optFlag = null) {
if (optFlag !== null) {
running = optFlag;
} else {
running = !running;
}
workingIndicator.style.color = running ? 'red' : 'black';
}
// ERRORS
function clearError() {
errorDiv.innerHTML = '';
}
function displayError(message) {
errorDiv.innerHTML = '';
errorDiv.innerText = message;
}
// Local storage keys
const LS_KEY_PREFIX = 'app17392_';
const LS_editorsShareOfUsableHeight = LS_KEY_PREFIX + 'editorsShareOfUsableHeight';
// RESIZE LOGIC
let editorsShareOfUsableHeight = localStorage.getItem(LS_editorsShareOfUsableHeight) || 0.5;
fixHeights();
$(errorDiv).mousedown(e => {
$(errorDiv).data('lastY', e.clientY);
$('body').mousemove(bodyMouseMoveHandler);
$('body').mouseup(bodyMouseUpHandler);
function bodyMouseUpHandler(e) {
$(errorDiv).data('lastY', null);
$('body').off('mousemove', bodyMouseMoveHandler);
$('body').off('mouseup', bodyMouseUpHandler);
}
function bodyMouseMoveHandler(e) {
const lastY = $(errorDiv).data('lastY');
if (!lastY) {
return;
}
e.preventDefault();
const delta = e.clientY - lastY;
const newEditorHeight = $(topHalf).outerHeight() + delta;
const usableHeight = $(window).innerHeight() - $(errorDiv).outerHeight(true);
editorsShareOfUsableHeight = newEditorHeight / usableHeight;
localStorage.setItem(LS_editorsShareOfUsableHeight, editorsShareOfUsableHeight);
$(errorDiv).data('lastY', e.clientY);
fixHeights();
}
});
function fixHeights() {
const usableHeight = $(window).innerHeight() - $(errorDiv).outerHeight(true);
$(topHalf).outerHeight(usableHeight * editorsShareOfUsableHeight);
$(bottomHalf).outerHeight(usableHeight - $(topHalf).outerHeight());
}
const sampleProgram = `"hello world".println();
(3 + 4).println();
var sum = 0;
for 1 to: 10 do: {x |
sum = sum + x;
};
sum.println();`;
S.editor.setValue(localStorage.getItem('seymour-program') || sampleProgram);