blob: 9a1282fb78c4329306be61f4d9aa95e8450bdc00 (
plain)
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
|
Wish the web server handles route {/page/(.*)$} with handler {
if {[regexp -all {/page/(\d*)$} $path whole_match program_id]} {
set filename "../folk-printed-programs/$program_id.folk"
set fp [open $filename r]
set file_data [read $fp]
close $fp
} elseif {[regexp -all {/page/(.*)$} $path whole_match program_id]} {
set filename "virtual-programs/$program_id.folk"
set fp [open $filename r]
set file_data [read $fp]
close $fp
}
html [string map [list file_data [htmlEscape $file_data] program_id $program_id file_name $filename] {
<html>
<body>
<div>
<span id="status">Status</span>
<button onclick="handleSave()">Save</button>
<button id="print" onclick="handlePrint()">Print</button>
</div>
<textarea id="code" style="width: 100%;height: 95vh;">file_data</textarea>
<pre id="error"></pre>
<script src="/lib/folk.js"></script>
<script>
const isVirtualProgram = 'file_name'.includes('virtual-programs');
if (isVirtualProgram) {
document.getElementById("print").disabled = true;
}
const codeEle = document.getElementById("code");
const errorEle = document.getElementById("error");
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
// Cmd + S || Ctrl + S => Save
document.addEventListener('keydown', function(e) {
if ((window.navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey) && e.keyCode == 83) {
e.preventDefault();
handleSave();
}
}, false);
// Cmd + P || Ctrl + P => Print
document.addEventListener('keydown', function(e) {
if ((window.navigator.platform.match('Mac') ? e.metaKey : e.ctrlKey) && e.keyCode == 80) {
e.preventDefault();
handlePrint();
}
}, false);
const ws = new FolkWS(document.getElementById('status'));
ws.watchCollected(tcl`${program} has error /something/ with info /errorInfo/`, errors => {
errorEle.style.backgroundColor = errors.length ? "#f55" : "";
errorEle.innerText = errors.map(e => e.errorInfo).join('\n');
});
function handleSave() {
const code = document.getElementById("code").value;
ws.send(tcl`
set fp [open file_name w]
puts -nonewline $fp ${code}
close $fp
puts "Saved program_id.folk"
`);
if (isVirtualProgram) {
ws.send(tcl`EditVirtualProgram file_name ${code}`)
}
}
let jobid;
function handlePrint() {
const code = document.getElementById("code").value;
jobid = String(Math.random());
ws.send(tcl`Wish to print program program_id with code ${code} with job-id ${jobid}`);
}
</script>
</body>
</html>
}]
}
|