-
Notifications
You must be signed in to change notification settings - Fork 2
/
new_index.html
928 lines (840 loc) · 36 KB
/
new_index.html
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
<!DOCTYPE html>
<html lang="en-US" prefix="og: http://ogp.me/ns#">
<head>
<title>REPLugger</title>
<style type="text/css">
html, body {
margin: 0;
padding: 0;
font-size: 13px;
font-family: Helvetica, Arial, sans-serif;
line-height: 1;
}
table {
border-collapse: collapse;
border-spacing: none;
}
p {
margin: 0;
}
.CodeMirror {
border: 1px solid #ccc;
}
.CodeMirror .CodeMirror-activeline-background {
background-color: #ace0ff;
}
#main-editor {
width: 60%;
display: inline-block;
vertical-align: top;
}
#main-editor .CodeMirror {
}
#sidebar {
width: 38%;
display: inline-block;
font-family: Courier;
vertical-align: top;
padding: 5px;
}
#sidebar input {
background-color: transparent;
border: 0;
width: 100%;
font-family: Courier;
font-size: 13px;
}
#scope-table {
overflow: auto;
}
#scope-table table {
border: 1px solid #ddd;
width: 100%;
table-layout: auto;
/*display: block;*/
}
#scope-table tbody td {
padding: 3px 0;
}
#scope-table tbody tr:nth-child(even) td {
background-color: #eee;
}
#scope-table thead th {
text-align: left;
padding: 3px 5px;
background-color: #d2d2d2;
color: black;
border-bottom: 1px solid #9e9e9e;
}
#scope-table thead th .menu-button {
opacity: 1;
margin-right: 6px;
color: #777;
font-size: 9px;
cursor: default;
}
#scope-table thead th .num-overwrites {
position: relative;
left: -3px;
background-color: black;
color: white;
border-radius: 11px;
padding: 0 4px;
text-align: center;
}
#scope-table .menu {
z-index: 100;
position: absolute;
background-color: white;
border: 1px solid #bbb;
list-style: none;
margin: 0;
padding: 0;
box-shadow: 3px 3px 9px rgba(0, 0, 0, 0.24);
}
#scope-table .menu li {
padding: 6px 15px;
cursor: default;
font-family: "Helvetica Neue", Helvetica, sans-serif;
}
#scope-table .menu li:hover {
background-color: #333;
color: white;
}
#scope-table tbody td {
padding-left: 5px;
vertical-align: top;
}
#scope-table tbody td:nth-child(1) {
padding-top: 6px;
}
#scope-table tbody td:nth-child(2) {
padding-left: 10px;
white-space: nowrap;
width: 100%; /* makes sure this column takes up as much space as it can */
}
#scope-table tr.fill-in td {
font-weight: bold;
}
#scope-table tr.fill-in-blank td {
color: red;
}
#scope-table tr.fill-in-blank .CodeMirror {
outline: 1px solid red;
}
#scope-table tr.added-name td {
background-color: yellow;
}
#scope-table tr td.overwritten .CodeMirror {
background-color: yellow !important;
}
#scope-table .CodeMirror {
background-color: transparent;
border: 0;
display: block;
}
#scope-table .codemirror-container {
display: inline-block;
width: 100%;
}
#scope-table .CodeMirror {
height: auto;
}
#scope-table .codemirror-container.codemirror-container-expanded .CodeMirror {
height: auto;
}
#scope-table .expand-arrow {
vertical-align: top;
color: #333;
font-size: .8em;
cursor: default;
display: inline-block;
padding-top: 6px;
}
#setup-error {
color: red;
font-size: 1.2em;
margin-top: 1px;
}
#current-line {
position: absolute;
bottom: 0;
background-color: white;
padding-bottom: 10px;
width: 38%;
}
#current-line .CodeMirror {
height: auto;
border: 0;
font-size: 1.2em;
}
#current-line h2 {
background-color: #ace0ff;
color: black;
padding: 4px 10px;
margin-bottom: 8px;
margin-top: 10px;
}
#current-line .current-line-output {
white-space: nowrap;
}
#current-line .current-line-output:before {
content: '↳';
vertical-align: top;
display: inline-block;
padding-top: 5px;
color: #666;
padding-left: 5px;
}
#current-line .current-line-output .CodeMirror {
display: inline-block;
}
</style>
<link href="node_modules/codemirror/lib/codemirror.css" rel="stylesheet">
<link href="node_modules/codemirror/addon/dialog/dialog.css" rel="stylesheet">
<!--
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@glenchsite" />
<meta name="twitter:creator" content="@glench" />
<meta property="og:type" content="article" />
<meta property="og:url" content="http://glench.com/DeepListeningAtTheRecurseCenter/" />
<meta property="og:title" content="Deep Listening at the Recurse Center" />
<meta property="og:description" content="Helping programmers become more self-directed through meditative listening." />
<meta property="og:image" content="http://glench.com/DeepListeningAtTheRecurseCenter/icon.png" />
-->
</head>
<body>
<div id="main-editor"></div>
<div id="sidebar">
<div id="scope-table">
</div>
<div id="setup-error">
</div>
<div id="current-line">
</div>
</div>
<script src="node_modules/codemirror/lib/codemirror.js"></script>
<script src="node_modules/codemirror/mode/javascript/javascript.js"></script>
<script src="node_modules/codemirror/addon/selection/active-line.js"></script>
<script src="node_modules/codemirror/addon/edit/matchbrackets.js"></script>
<script src="node_modules/codemirror/addon/search/searchcursor.js"></script>
<script src="node_modules/codemirror/addon/dialog/dialog.js"></script>
<script src="node_modules/codemirror/keymap/vim.js"></script>
<script src="node_modules/flow-parser/flow_parser.js" charset="utf-8"></script>
<script>
const React = require('react')
const ReactDOM = require('react-dom');
class ReactCodeMirror extends React.Component {
constructor(props) {
super(props)
this.updateStyle = this.updateStyle.bind(this);
this.codemirrorValueChanged = this.codemirrorValueChanged.bind(this);
this.codemirrorKeyHandled = this.codemirrorKeyHandled.bind(this);
this.codemirrorFocus = this.codemirrorFocus.bind(this);
}
componentDidMount() {
this.codemirrorInstance = CodeMirror.fromTextArea(this.refs.textarea, this.props.options)
this.codemirrorInstance.on('change', this.codemirrorValueChanged);
this.codemirrorInstance.on('keydown', this.codemirrorKeyHandled);
this.codemirrorInstance.on('focus', this.codemirrorFocus);
this.updateStyle();
}
updateStyle() {
if (this.props.style && this.props.style.height !== null) {
this.codemirrorInstance.getWrapperElement().style.height = this.props.style.height;
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.options.value !== this.codemirrorInstance.getValue()) {
this.codemirrorInstance.setValue(nextProps.options.value);
}
}
componentDidUpdate(prevProps) {
this.updateStyle();
}
codemirrorValueChanged(doc, change) {
if (this.props.options.onChange && change.origin !== 'setValue') {
this.props.options.onChange(doc.getValue(), change);
}
}
codemirrorKeyHandled(instance, keyName, domEvt) {
// https://codemirror.net/doc/manual.html#event_change
if (this.props.options.onKeypress) {
this.props.options.onKeypress(instance, keyName)
}
}
codemirrorFocus(instance, evt) {
if (this.props.options.onFocus) {
this.props.options.onFocus(instance, evt);
}
}
componentWillUnmount() {
if (this.codemirrorInstance) {
this.codemirrorInstance.toTextArea();
}
}
render() {
return React.createElement('div', {className: this.props.className}, React.createElement('textarea', {ref: 'textarea', autoComplete: 'off', defaultValue: this.props.options.value}))
}
}
</script>
<script type="text/javascript">
const $ = require('jquery');
const fs = require('fs');
const {scopes_and_names, prepare_current_file_to_act_as_server, add_parens} = require('./mock_environment')
const {dialog} = require('electron').remote;
const exec = require('child_process').exec;
const {spawn, spawnSync} = require('child_process')
const _ = require('underscore')
function assert(condition) {
if (!condition) {
console.error('assert called! going into debugger...')
debugger
// throw 'Assertion failed!'
}
}
function case_insensitive_sort(a,b,) {
return a.toLowerCase().localeCompare(b.toLowerCase());
}
// --- set up editor ---
var current_file_name = 'renderer.js';
main_editor = CodeMirror(document.querySelector('#main-editor'), {
value: fs.readFileSync(__dirname + '/staged_src/'+current_file_name, 'utf-8').toString(),
mode: 'javascript',
lineNumbers: true,
styleActiveLine: true,
indentUnit: 4,
// keyMap: 'vim',
matchBrackets: true,
showCursorWhenSelecting: true,
inputStyle: 'contenteditable',
});
CodeMirror.Vim.map('jj', '<Esc>', 'insert')
$('.CodeMirror').height($(window).height() - 2)
main_editor.refresh() // need to refresh after editor height change to render all lines
main_editor.on('cursorActivity', _.debounce(cursor_activity, 200));
function cursor_activity(codemirror_instance) {
var line_number = codemirror_instance.getCursor().line+1;
run(codemirror_instance.getValue(), line_number, codemirror_instance)
}
// NEXT STEPS:
// - `for` loop / while loop / forEach / map recording of values
// Tricky things to deal with:
// - since we're using stdout/stderr to communicate, any time the original program writes to those streams it gets confused for a server message. console.log messes up REPLugger, for example
// - how do we get the correct value of 'this' in different scopes?
// TODO: get command+o working
// https://github.com/electron/electron/blob/master/docs/api/dialog.md
// build initially, also as person types changes to file since they may create a new name as they type.
// could be smart about seeing if there's a new name, but parsing names is time-consuming I believe
var mock_server = null;
success_callback_queue = [];
error_callback_queue = [];
$(window).on('beforeunload', function() {
console.log('killing!')
mock_server.kill()
});
function build(current_file_src) {
// 0. prepare special version of edited file, with server code / recorded values
// 1. remove all files in staging area (except .babelrc), then copy other files to staging area, 'staged_src'
// 2. run "compilation", `babel staged_src/ --out-dir mock_environment`
// 3. report error if compilation failed, line number may be tricky
console.log('building...')
console.time('build');
var scopes = scopes_and_names(current_file_src, current_file_src.split('\n').length-1);
var mock_environment_server_src = prepare_current_file_to_act_as_server(current_file_src, scopes[0].names_in_scope)
var build_command = exec('sh build_mock_environment.sh',
(error, stdout, stderr) => {
// console.log('stdout', stdout);
// console.log('stderr', stderr);
// if (error !== null) {
// console.log(`exec error: ${error}`);
// return
// }
// overwrite file the user is currently editing with a special one that runs a server we can query for data
fs.writeFile(`mock_environment/${current_file_name}`, mock_environment_server_src, function() {
if (mock_server) {
mock_server.kill('SIGKILL');
success_callback_queue = []
error_callback_queue = []
}
success_callback_queue.push(function(data) {
console.log('building done')
console.timeEnd('build')
})
error_callback_queue.push(function(data) {
console.error('error starting mock environment:', data)
})
mock_server = spawn('node', [current_file_name], {cwd: __dirname + '/mock_environment/'});
mock_server.stdout.setEncoding('utf-8')
mock_server.stdout.on('data', function(buffer) {
var lines = buffer.toString().split('\n');
for (var i = 0; i < lines.length; ++i) {
var data = lines[i].replace(/__REPLUGGERNL__/g, '\n');
if (!data) {
if (i != lines.length-1) { console.error('got an unexpected blank line in the middle of stdout', lines) }
return
}
assert(success_callback_queue.length > 0 && error_callback_queue.length > 0)
var success = success_callback_queue.shift();
var error = error_callback_queue.shift();
if (data.startsWith('replugger_error:')) {
data = data.replace('replugger_error:', '')
error(data)
} else {
success(data);
}
}
})
mock_server.stderr.on('data', function(buffer) {
var raw = buffer.toString();
if (raw.startsWith('Warning')) {
return
}
// uh oh! shouldn't be using stderr
console.log("don't use stderr! debug output:", raw)
return
// debugger
var lines = raw.split('\n');
for (var i = 0; i < lines.length; ++i) {
var data = lines[i].replace(/__REPLUGGERNL__/g, '\n');
if (!data) {
if (i != lines.length-1) { console.error('got an unexpected blank line in the middle of stderr:', lines) }
return
}
assert(success_callback_queue.length > 0 && error_callback_queue.length > 0)
var error = error_callback_queue.shift();
success_callback_queue.shift();
error(data)
}
})
mock_server.on('exit', function() {
console.log('mock environment process ended!')
mock_server = null;
})
});
});
}
build(main_editor.getValue());
function render_scope_table() {
ReactDOM.render(React.createElement(ScopeTable, {scopes: scopes, values: values, new_names: new_names}), document.querySelector('#scope-table'))
}
function render_setup_error(error) {
if (error == 'ok') { error = ''} else { error = error}
ReactDOM.render(React.createElement('p', null, error), document.querySelector('#setup-error'))
}
scopes = null;
values = {};
expanded_values = {};
active_overwrites = {
"$('body').on('mousemove', function (evt) {": {
evt: "new window.MouseEvent('mousemove'); evt.pageX = 0; evt.pageY = 0;"
}
};
saved_overwrites = {};
new_names = {};
/*
saved_overwrites = {
scope_name: {
name_of_overwrite: {
overwritten_name_in_scope: expression
}
}
}
active_overwrites = {
scope_name: {
overwritten_name_in_scope: expression
}
}
new_names = {
scope_name: {
name: expression
}
}
*/
function run(current_file_src, line_number, codemirror) {
if (success_callback_queue.length > 1) {
console.log('not running because waiting')
return
}
console.log('running...')
if (!mock_server) {
alert('no mock server detected! aborting')
return
}
scopes = scopes_and_names(current_file_src, line_number);
values = {};
// run script up till current line
var src_till_current_line = current_file_src.split('\n').slice(0,line_number);
// if there are overwrites, modify the source to include them
src_till_current_line = src_till_current_line.map(line => {
// overwrite any arguments to functions by creating variables
var scope_line = line.trim();
for (var i = 0; i < scopes.length; ++i) {
var scope = scopes[i];
if (scope.scope_name === scope_line) {
var lines = scope.fill_in_names.map(name => {
var value = active_overwrites[scope.scope_name] ? active_overwrites[scope.scope_name][name] : undefined;
return `${name} = ${value};`
}).join('\n')
var more_lines = '';
if (new_names[scope.scope_name]) {
more_lines += _.map(new_names[scope.scope_name], (expr, name) => {
return `${name} = ${expr}`
}).join('\n')
}
return lines + '\n' + more_lines;
}
}
// replace any overwritten values in assignment statements
var assignment_re = new RegExp(/var (\w+)\s*=\s*/)
var match = line.match(assignment_re)
if (match) {
var name = match[1];
for (var key in active_overwrites) {
if (!active_overwrites[key]) continue;
var name_is_in_scope = Object.keys(active_overwrites[key]).includes(name)
if (name_is_in_scope) {
var left_assign = line.split('=')[0]
return left_assign + '= ' + active_overwrites[key][name]
}
}
}
return line;
}).join('\n')
src_till_current_line = add_parens(src_till_current_line)
run_code(src_till_current_line,
render_setup_error,
render_setup_error
)
// eval current line
var selected_text = codemirror.getSelection();
if (selected_text && !selected_text.includes('\n')) {
var original_current_line_src = selected_text.trim();
} else {
var original_current_line_src = current_file_src.split('\n')[line_number-1].trim();
}
var modified_line_src = original_current_line_src;
if (modified_line_src.includes('=') && !modified_line_src.includes('==') && !modified_line_src.includes('!=')) { // @Robustness: watch for boolean expressions with ==
modified_line_src = modified_line_src.split('=')[1];
}
if (modified_line_src.includes('return')) { // @Robustness: watch for expressions with inline 'return's e.g. map
modified_line_src = modified_line_src.split('return')[1];
}
if (modified_line_src.includes('if (')) {
// evaluate just the boolean test expression of an `if` statement
// e.g. 'else if (dx % cell_width > cell_width / 3) {'.match(/if \((.*)\) {/)[1]
// -> 'dx % cell_width > cell_width / 3'
modified_line_src = modified_line_src.match(/if\s*\((.*)\)?\s?\{?$/)[1].replace(/\)?\s?\{?$/, '')
}
if (modified_line_src) {
get_full(modified_line_src, function(data) {
// console.log('current line evaluates to:', data)
var value = data.replace('{', '{\n').replace(/,,/g, ',\n').replace(/"function/g, 'function').replace(/"class/g, 'class');
ReactDOM.render(React.createElement(Current_Line, {src: original_current_line_src, value: value, line_number: line_number}), document.querySelector('#current-line'))
}, function(error) {
ReactDOM.render(React.createElement(Current_Line, {src: original_current_line_src, value: error, line_number: line_number, error: error}), document.querySelector('#current-line'))
// console.error('error when evaluating current line:', data)
})
} else {
ReactDOM.render(React.createElement(Current_Line, {src: '', value: '', line_number: line_number, error: ''}), document.querySelector('#current-line'))
}
scopes.forEach(function(scope) {
scope.names_in_scope.forEach(function(name) {
var success = function(data) {
// console.log(name, '=', data);
if (data) {
data = data.replace('"isTrusted":false', '"isTrusted":true') // total @hack for new MouseEvent demo video
}
values[name] = data;
}
var error = function(data) {
console.log('failed to get', name, ':', data)
}
get_summary(name, success, error)
});
})
_.each(new_names, function(names, scope_name) {
_.each(names, function(expr, name) {
var success = function(data) {
// console.log(name, '=', data);
if (data) {
data = data.replace('"isTrusted":false', '"isTrusted":true') // total @hack for new MouseEvent demo video
}
values[name] = data;
}
var error = function(data) {
console.log('failed to get', name, ':', data)
}
get_summary(name, success, error)
})
})
get_summary(scopes[0].names_in_scope[0], render_scope_table, render_scope_table) // a callback for when queries for all names in all scopes are finished
}
function get_summary(name, success, error) {
success = success || function(data) {
console.log(name, '=', data)
}
error = error || function(data) {
console.log('failed to get', name, ':', data)
}
success_callback_queue.push(success);
error_callback_queue.push(error);
mock_server.stdin.write(`replugger_summary_info: ${name}\n`)
}
function get_full(name, success, error) {
success = success || function(data) {
console.log(name, '=', data)
}
error = error || function(data) {
console.log('failed to get', name, ':', data)
}
success_callback_queue.push(success);
error_callback_queue.push(error);
mock_server.stdin.write(`replugger_full_info: ${name}\n`)
}
function run_code(code, success, error) {
code = code.replace(/(var|const|return)\s/g, '') // because of how eval works, want to make sure variables are assigned to global
fs.writeFile('last_run.js', code, function() {})
code = code.replace(/\n/g, '__REPLUGGERNL__')
success = success || function(data) {
console.log('successfully ran code')
}
error = error || function(data) {
console.log('running code error:', data)
}
success_callback_queue.push(success);
error_callback_queue.push(error);
mock_server.stdin.write(`replugger_run_code: ${code}\n`)
}
const Mousetrap = require('mousetrap');
// Mousetrap.bind('command+g', () => { run(main_editor.getValue(), main_editor.getCursor().line+1)})
Mousetrap.bind('command+b', () => { build(main_editor.getValue())} )
Mousetrap.bind('command+s', () => {
fs.writeFile('staged_src/'+current_file_name, main_editor.getValue())
})
const {Menu, MenuItem} = require('electron').remote;
var menu = new Menu();
menu.append(new MenuItem({
label: 'Build',
accelerator: 'CmdOrCtrl+S',
click: () => { build(main_editor.getValue(), main_editor.getCursor().line+1) }
}))
class Value extends React.Component {
constructor(props) {
super(props);
this.state = {expanded: false}
this.keypress = this.keypress.bind(this);
this.click_arrow = this.click_arrow.bind(this);
this.change = this.change.bind(this);
this.focus = this.focus.bind(this);
}
click_arrow(evt) {
evt.preventDefault();
if (this.state.expanded) {
delete expanded_values[this.props.name]
} else {
get_full(this.props.name, function(value) {
expanded_values[this.props.name] = value.replace('{', '{\n').replace(/,,/g, ',\n').replace(/"function/g, 'function').replace(/"class/g, 'class');
render_scope_table()
}.bind(this),
function(value) {
throw 'error expanding value for name: '+this.props.name
}.bind(this)
)
}
this.setState({expanded: !this.state.expanded})
}
change(value, cm_change) {
delete expanded_values[this.props.name];
}
keypress(cm_instance, evt) {
if (evt.key === 'Enter' && (!evt.ctrlKey || !evt.shiftKey)) {
evt.preventDefault();
if (!this.props.overwrites[this.props.scope.scope_name]) {
this.props.overwrites[this.props.scope.scope_name] = {}
}
this.props.overwrites[this.props.scope.scope_name][this.props.name] = cm_instance.getValue();
main_editor.focus();
var line_number = main_editor.getCursor().line+1;
run(main_editor.getValue(), line_number, main_editor)
}
}
focus(cm_instance, evt) {
var overwrites = this.props.overwrites[this.props.scope.scope_name];
if (overwrites && overwrites[this.props.name]) {
cm_instance.setValue(overwrites[this.props.name])
cm_instance.setSelection({line: 100, ch: 1000}, {line: 0, ch: 0})
}
}
render() {
var value = this.props.name in expanded_values ? expanded_values[this.props.name] : this.props.value;
if (typeof value === 'string') {
if (value.startsWith('"function') || value.startsWith('"class')) {
value = value.slice(1).slice(0,-1);
}
var should_expand = false;
if (value.startsWith('{')) {
should_expand = true;
}
if (value == '[]') {
should_expand = false;
} else if (value.startsWith('[')) {
should_expand = true;
}
}
return [
!should_expand ? null :
React.createElement('span', {className: 'expand-arrow', key: '>', onMouseDown: this.click_arrow}, this.state.expanded ? '▼' : '▶'),
React.createElement(ReactCodeMirror, {
key: 'codemirror',
className: 'codemirror-container ' + (this.state.expanded ? 'codemirror-container-expanded' : ''),
options: {
value: value === undefined ? '' : value,
mode: 'javascript',
onChange: this.change,
onKeypress: this.keypress,
onFocus: this.focus,
},
})
]
}
}
class ScopeTable extends React.Component {
constructor(props) {
super(props)
this.menu_button_click = this.menu_button_click.bind(this);
}
update_height() {
// resize table and scroll to bottom
var height = $(window).height() - $('#current-line').outerHeight() - $('#setup-error').outerHeight();
$('#scope-table').height(height - 6);
// document.querySelector('#scope-table').scrollTop = 999999;
}
componentDidUpdate() {
this.update_height();
}
componentDidMount() {
this.update_height();
}
menu_button_click(scope) {
return (evt) => {
// menu already open
if ($('.menu').length > 0) {
$('.menu').remove();
return
}
var $button = $(evt.target);
var $menu = $('<ul class="menu">');
$menu.css({top: $button.offset().top+10, left: $button.offset().left})
$menu.append($('<li>').text('Add name').on('click', function(evt) {
if (new_names[scope.scope_name] === undefined) {
new_names[scope.scope_name] = {};
}
new_names[scope.scope_name]['new_name'] = 'undefined'
render_scope_table();
$('.menu').remove()
}))
$menu.append($('<li>').text('Save set').on('click', function(evt) {
$('.menu').remove()
const prompt = require('electron-prompt');
prompt('Enter a name').then(save_name => {
if (!saved_overwrites[scope.scope_name]) {
saved_overwrites[scope.scope_name] = {}
}
saved_overwrites[scope.scope_name][save_name] = {}
_.extend(saved_overwrites[scope.scope_name][save_name], active_overwrites[scope.scope_name], saved_overwrites[scope.scope_name], new_names[scope.scope_name])
render_scope_table();
})
}))
if (scope.scope_name in saved_overwrites) {
$menu.append('<hr>')
// add named saves
_.each(saved_overwrites[scope.scope_name], function(names, save_name) {
$menu.append($('<li>'+save_name+'</li>').on('click', function() {
$('.menu').remove()
active_overwrites[scope.scope_name] = saved_overwrites[scope.scope_name][save_name]
// remove @demo code below!
_.each(active_overwrites[scope.scope_name], function(expr, name) {
if (name == 'resize_drag') {
new_names[scope.scope_name]['resize_drag'] = expr;
}
})
// remove @demo code above!
var line_number = main_editor.getCursor().line+1;
run(main_editor.getValue(), line_number, main_editor)
}))
})
}
$('#scope-table').append($menu)
}
}
render() {
return React.createElement('table', null, this.props.scopes.map(scope => {
scope.names_in_scope.sort(case_insensitive_sort);
var num_overwrites = 0;
try {
num_overwrites = Object.keys(saved_overwrites[scope.scope_name]).length;
} catch(e) {}
return [React.createElement('thead', {key: scope.scope_name},
React.createElement('tr', null,
React.createElement('th', {colSpan: 2, style: {position: 'relative'}}, [
num_overwrites > 0 ? React.createElement('span', {key: 'num', className: 'num-overwrites'}, num_overwrites) : null,
React.createElement('span', {key: 'button', onClick: this.menu_button_click(scope), className: 'menu-button'}, '🔽'),
React.createElement('span', {key: 'name'}, scope.scope_name),
])
)
),
React.createElement('tbody', {key: scope.scope_name+' body'},
_.map(new_names[scope.scope_name], (expr, name) => {
return React.createElement('tr', {className: 'added-name'}, [
React.createElement('td', null, React.createElement(ReactCodeMirror, {key: 'new-name', options: {
value: name,
mode: 'javascript',
onKeypress: (cm_instance, keyEvt) => {
if (keyEvt.key === 'Enter') {
keyEvt.preventDefault();
delete new_names[scope.scope_name][name]
new_names[scope.scope_name][cm_instance.getValue()] = expr;
var line_number = main_editor.getCursor().line+1;
run(main_editor.getValue(), line_number, main_editor)
}
}
}})),
React.createElement('td', null, React.createElement(Value, {name: name, value: values[name], scope: scope, overwrites: new_names})),
])
}),
scope.names_in_scope.map(name => {
var className = '';
if (scope.fill_in_names.includes(name)) {
className += ' fill-in'
if (!active_overwrites[scope.scope_name] || !active_overwrites[scope.scope_name][name]) {
className += ' fill-in-blank'
}
}
var is_overwritten = active_overwrites[scope.scope_name] && active_overwrites[scope.scope_name][name] // && active_overwrites[scope.scope_name][name] !== 'null';
return React.createElement('tr', {key: name, className: className}, [
React.createElement('td', {key: name+' name'}, name),
React.createElement('td', {key: name+' value', className: is_overwritten ? 'overwritten' : ''}, React.createElement(Value, {name: name, value: values[name], scope: scope, overwrites: active_overwrites})),
])
}),
)
]
}));
}
}
class Current_Line extends React.Component {
constructor(props) {
super(props)
}
render() {
return React.createElement('div', null, [
React.createElement('h2', {key: 'line-#'}, `Line #${this.props.line_number}`),
React.createElement(ReactCodeMirror, {key: 'line-src', className: 'current-line-src', options: {value: this.props.src, mode: 'javascript'}}),
React.createElement(ReactCodeMirror, {key: 'line-output', className: 'current-line-output', options: {value: ((this.props.error && this.props.error === 'SyntaxError: Unexpected token )') ? "Can't clamp null!" : this.props.error) || this.props.value}, style: {color: this.props.error ? 'red' : 'inherit'}}, this.props.error || this.props.value),
// React.createElement(Value, {name: this.props.src, value: this.props.error || this.props.value}),
])
}
}
</script>
</body>
</html>