-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtrashow.js
398 lines (348 loc) · 11 KB
/
trashow.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
'use strict';
const electron = require('electron');
const fs = require('fs');
const path_mod = require('path');
const fonts = require('./fonts.js');
const searchbar = require('./searchbar.js');
const traread = require('./traread.js');
var tra_filename = null;
var tra_path = null;
var loading_visible = null;
function load_transcript(arg)
{
tra_filename = arg.filename;
tra_path = arg.path;
document.title = arg.title + ' - Transcript';
async function readall() {
var iter = traread.stanza_reader(tra_path);
for await (var obj of iter) {
add_stanza(obj);
}
}
readall()
.then(() => {
hide_loading();
})
.catch((ex) => {
glkote_error('Error reading transcript: ' + ex);
});
}
/* Add a stanza to the output "buffer window". This is almost exactly
what glkote.js does when an update message arrives. It is also
almost the same code as stanzas_write_to_file(), just creating DOM
elements rather than writing text.
Differences:
- We only pay attention to buffer windows. The status line (assuming
that's a grid) is lost.
- If there's more than one buffer window, the output is interleaved
in a single display stream.
- Window-clear events are shown as horizontal rules.
- Hyperlinks are clickable but do nothing.
- Graphics are currently not supported.
*/
function add_stanza(obj)
{
if (obj.metadata) {
var bioel = null;
for (var key of traread.metadata_keylist) {
if (obj.metadata[key]) {
if (!bioel) {
bioel = $('<div>', { 'class':'MetadataBox' });
$('#window').append(bioel);
}
var metel = $('<div>');
metel.append($('<span>', { 'class':'MetadataKey' }).text(key+':'));
metel.append($('<span>').text(' '));
var valel = $('<span>', { 'class':'MetadataValue' });
if (key == 'title')
valel.addClass('MetadataTitle');
valel.text(obj.metadata[key]);
metel.append(valel);
bioel.append(metel);
}
}
if (bioel) {
add_hrule();
}
}
if (obj.output) {
if (obj.output.content) {
for (var dat of obj.output.content) {
/* We assume that if a content stanza has "text", it's a
buffer window. It would be tidier to track open window
types, but this might be a partial transcript with no
"arrange" event, so we can't do that. */
if (dat.text) {
if (dat.clear) {
add_hrule();
}
if (dat.text) {
add_stanza_linedata(dat.text, obj.timestamp, obj.outtimestamp);
}
}
}
}
}
}
function add_stanza_linedata(text, intimestamp, outtimestamp)
{
var frameel = $('#window');
var firsttime = true;
for (let ix=0; ix<text.length; ix++) {
const textarg = text[ix];
const content = textarg.content;
let divel = null;
if (textarg.append) {
if (!content || !content.length)
continue;
divel = null;
var udivel = last_child_of(frameel); /* not wrapped */
if (udivel && udivel.tagName == 'DIV')
divel = $(udivel);
}
if (divel == null) {
/* Create a new paragraph div */
divel = $('<div>', { 'class': 'BufferLine BlankPara' });
divel.data('blankpara', true);
frameel.append(divel);
}
// skip textarg.flowbreak for now
if (!content || !content.length) {
if (divel.data('blankpara'))
divel.append($('<span>', { 'class':'BlankLineSpan' }).text(' '));
continue;
}
if (divel.data('blankpara')) {
divel.data('blankpara', false);
divel.removeClass('BlankPara');
divel.empty();
}
if (firsttime) {
firsttime = false;
var date = new Date(intimestamp);
var intimestr = date.toTimeString().slice(0, 8) + ', ' + date.toDateString().slice(4);
var durstr = 'executed in ' + (outtimestamp - intimestamp) + ' ms';
divel.find('.TimeAnchor').remove(); // only one per paragraph please
const timeel = $('<div>', { 'class':'TimeAnchor' });
timeel.append($('<div>', { 'class':'Dot' }).text('\u25C6'));
const popel = $('<div>', { 'class':'Popup' }).text(intimestr);
popel.append($('<br>'));
popel.append($('<span>').text(durstr));
timeel.prepend(popel);
divel.prepend(timeel);
}
for (let sx=0; sx<content.length; sx++) {
const rdesc = content[sx];
let rstyle, rtext, rlink;
if (!(typeof rdesc === 'string' || rdesc instanceof String)) {
if (rdesc.special !== undefined) {
if (rdesc.special == 'image') {
var val;
if (rdesc.alttext)
val = '[image: ' + rdesc.alttext + ']';
else
val = '[image ' + rdesc.image + ']';
const specel = $('<span>', { 'class': 'Special_Image' } );
specel.text(val);
divel.append(specel);
}
continue;
}
rstyle = rdesc.style;
rtext = rdesc.text;
rlink = rdesc.hyperlink;
}
else {
rstyle = rdesc;
sx++;
rtext = content[sx];
rlink = undefined;
}
const el = $('<span>',
{ 'class': 'Style_' + rstyle } );
if (rlink == undefined) {
// Autodetect URLs?
el.append(document.createTextNode(rtext));
}
else {
const ael = $('<a>',
{ 'href': '#', 'class': 'Internal' } );
ael.text(rtext);
ael.on('click', (ev) => {}); // ignore clicks
el.append(ael);
}
divel.append(el);
}
}
}
function add_hrule()
{
var frameel = $('#window');
var el = $('<hr>');
frameel.append(el);
}
function hide_loading() {
if (loading_visible == false)
return;
loading_visible = false;
const el = document.getElementById('loadingpane');
if (el) {
el.style.display = 'none'; /* el.hide() */
}
}
function glkote_error(msg) {
if (!msg)
msg = '???';
let el = document.getElementById('errorcontent');
if (!el) return;
remove_children(el);
el.appendChild(document.createTextNode(msg));
el = document.getElementById('errorpane');
if (el.className == 'WarningPane')
el.className = null;
el.style.display = ''; /* el.show() */
//error_visible = true;
hide_loading();
}
function remove_children(parent) {
const ls = parent.childNodes;
while (ls.length > 0) {
const obj = ls.item(0);
parent.removeChild(obj);
}
}
function last_child_of(obj) {
const ls = obj.children();
if (!ls || !ls.length)
return null;
return ls.get(ls.length-1);
}
function set_show_timestamps(flag)
{
var bodyel = $('body');
if (flag) {
bodyel.addClass('DisplayAnchors');
}
else {
bodyel.removeClass('DisplayAnchors');
}
}
function evhan_delete_transcript()
{
electron.ipcRenderer.send('delete_transcript', tra_filename, true);
}
function evhan_save_transcript_text()
{
electron.ipcRenderer.send('save_transcript_text', tra_filename, true);
}
/* Preference-handling functions are copied from apphooks.js. Could be
refactored. */
function set_zoom_factor(val)
{
var webFrame = electron.webFrame;
webFrame.setZoomFactor(val);
}
function set_margin_level(val)
{
var str = '0px ' + (5*val) + '%';
$('#gameport').css({'margin':str});
}
function set_color_theme(obj)
{
var val = obj.theme;
var darklight_flag = obj.darklight;
// System-reactive themes:
if (val == 'lightdark') {
val = (darklight_flag ? 'dark' : 'light');
}
else if (val == 'sepiaslate') {
val = (darklight_flag ? 'slate' : 'sepia');
}
var bodyel = $('body');
bodyel.removeClass('SepiaTheme');
bodyel.removeClass('SlateTheme');
bodyel.removeClass('DarkTheme');
var search_body_el = searchbar.get_search_body();
if (search_body_el) {
search_body_el.removeClass('SepiaTheme');
search_body_el.removeClass('SlateTheme');
search_body_el.removeClass('DarkTheme');
}
switch (val) {
case 'sepia':
bodyel.addClass('SepiaTheme');
if (search_body_el)
search_body_el.addClass('SepiaTheme');
break;
case 'slate':
bodyel.addClass('SlateTheme');
if (search_body_el)
search_body_el.addClass('SlateTheme');
break;
case 'dark':
bodyel.addClass('DarkTheme');
if (search_body_el)
search_body_el.addClass('DarkTheme');
break;
default:
/* Light theme is the default. */
break;
}
}
function set_font(obj)
{
var fontline = fonts.get_fontline(obj.font, obj.customfont);
var fontclass = '.BufferWindow';
var el = $('#fontcss');
if (!fontline) {
el.remove();
}
else {
if (!el.length) {
el = $('<style>', { id:'fontcss', type:'text/css' });
$('#bodycss').before(el);
}
var text = '@@2 { font-family: @@1; }\n@@2 .Input { font-family: @@1; }\n';
text = text.replace(/@@1/g, fontline);
text = text.replace(/@@2/g, fontclass);
el.text(text);
}
}
function sequence(argls)
{
/* The argument is a list of { key, arg } pairs. */
for (var arg of argls) {
var func = namespace[arg.key];
if (!func) {
console.log('sequence: unable to find handler: ' + arg.key);
continue;
}
func(arg.arg);
}
}
const namespace = {
load_transcript : load_transcript,
set_show_timestamps : set_show_timestamps,
set_zoom_factor : set_zoom_factor,
set_margin_level : set_margin_level,
set_color_theme : set_color_theme,
set_font : set_font,
search_request : searchbar.search_request,
sequence : sequence
};
/* We hook up the namespace to IPC events, so that the main process can
send to it.
This requires a utility function because of Javascript's lousy closures.
*/
function attach(name, func)
{
require('electron').ipcRenderer.on(name, function(ev, arg) {
func(arg);
});
}
for (var name in namespace) {
attach(name, namespace[name]);
}
$(document).ready(function() {
searchbar.construct_searchbar();
});