-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathnotecalc.html
1263 lines (1143 loc) · 47.5 KB
/
notecalc.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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NoteCalc</title>
<link rel="stylesheet" href="assets/bootstrap.min.css"/>
<link rel="stylesheet" href="assets/bootstrap-grid.min.css"/>
<link rel="shortcut icon" type="image/png" href="assets/favico192.png"/>
<style>
.unsaved a::before {
font-weight: bold;
/*color: navy;*/
content: "*";
}
a.nav-link.active {
font-weight: bold;
color: #007bff !important;
}
.nav-tabs .nav-link {
border: 2px solid transparent;
border-top-left-radius: .25rem;
border-top-right-radius: .25rem;
}
.nav-link {
display: block;
padding: .1rem 1rem;
}
</style>
</head>
<body id='body' class="" style="overflow: hidden;">
<nav aria-label="" class="d-none d-md-block">
<ul id="tabs" class="nav nav-tabs" style="margin-bottom: 0;margin-top: 0">
<li id="tablink_add" class="nav-item">
<a class="nav-link font-weight-bold" href="javascript: void(0)"
onclick="add_tab_and_switch_to_it('')">+</a>
</li>
<!-- offset-->
<li class="nav-item">
<a class="nav-link font-weight-bold" href="javascript: void(0)"></a>
</li>
<li class="nav-item">
<a class="nav-link font-weight-bold" href="javascript: void(0)"></a>
</li>
<!-- -->
<li class="nav-item">
<a class="badge badge-primary offset-1"
href="javascript: void(0)"
style=""
data-toggle="modal"
data-target="#modal_div"
onclick="show_content_in_modal()"
>
Export
</a>
</li>
<li class="nav-item">
<a id="theme_selector_btn"
class="badge badge-light offset-1"
href="javascript: void(0)"
onclick="on_theme_btn_click()"
>
Light theme
</a>
</li>
<li class="nav-item">
<a class="badge badge-secondary offset-1"
target="_blank"
href="https://bbodi.github.io/notecalc3/"
>
Home
</a>
</li>
<li class="nav-item">
<a class="badge badge-secondary offset-1"
target="_blank"
href="https://github.com/bbodi/notecalc3"
>
GitHub
</a>
</li>
</ul>
</nav>
<!--IF YOU MODIFY THIS, DONT FORGET TO MODIFY ONCLICK-->
<!-- margin-left: 10px; margin-top: 10px-->
<div class="tabcontent"
style="position: relative;"
id="containder-div"
>
<canvas id='canvas'></canvas>
<!-- <canvas-->
<!-- style="position: absolute;top: 0;left: 0;"-->
<!-- id='webgl_canvas'>-->
<!-- </canvas>-->
</div>
<div class="modal fade"
id="modal_div"
tabindex="-1" role="dialog" aria-labelledby="contentModalTitle"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="contentModalTitle">Export</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<pre id="modal_content"
class="modal-body"
style="
font-family: 'Courier New',monospace;
line-height: initial;
">
</pre>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary"
onclick="copy_modal_content_to_clipboard()"
data-toggle="tooltip"
id="copy_to_clipboard_btn">Copy to clipboard
</button>
</div>
</div>
</div>
</div>
<script src="assets/webfont_v1.6.26.js"></script>
<script>
let font_width;
const THEME_LIGHT = 0;
const THEME_DARK = 1;
const DARK_BG_COLOR = '#282a36';
const DARK_BG_COLOR_BRIGHTER = '#000000';
let theme = THEME_LIGHT;
let canvas_dirty = false;
let can_scroll = true;
let FONT_HEIGHT = 16;
let NORMAL_FONT = 'px JetBrainsMono-Regular';
let BOLD_FONT = 'px JetBrainsMono-ExtraBold';
let FONT_VERT_PADDING = 1;
let line_height = FONT_HEIGHT + 2 * FONT_VERT_PADDING;
let CLIENT_WIDTH_IN_CHARS = 0;
let CLIENT_HEIGHT_IN_CHARS = 0;
let app_initialized = false;
let active_tab_index = 0;
let content_was_modified = false;
let active_tab_btn_dom;
let backbuffer_textarea;
let tab_index_counter = 0;
let last_drag_event = {x: -1, y: -1};
const single_pulsing_rects = [];
const repeating_pulsing_rects = [];
let is_mobile = false;
let measure_start = 0;
let is_debug = false;
let next_measure_tick = 0;
let current_second_info = {
render_time: 0,
render_count: 0,
command_count: 0,
};
let per_second_info = {
render_time: 0,
render_count: 0,
command_count: 0,
};
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
//const webgl_canvas = document.getElementById("webgl_canvas");
//const webgl_ctx = webgl_canvas.getContext("webgl");
const cursor_styles = ['text', 'default', 'w-resize', 'pointer'];
let next_full_reparse_tick = 0;
let app_ptr;
let wasm;
function create_backbuffer_textarea() {
const el = document.createElement('textarea');
el.value = '';
el.setAttribute('autocorrect', 'off');
el.setAttribute('autocapitalize', 'off');
el.style.position = 'absolute';
el.style.left = '-9999px';
el.style.top = '0px';
// if it is less than 16 px, mobile browsers automatically zoom in to it
el.style.fontSize = '32px';
document.body.appendChild(el);
return el;
}
function init_canvas() {
let size = calc_client_width();
set_client_width(size);
const div = document.getElementById('containder-div');
div.style.width = (canvas.width + 2 * font_width) + 'px';
div.style.height = (canvas.height + 2 * line_height) + 'px';
// some browser extension add a class to the body, so rmeove them
let body = document.getElementsByTagName('body').item(0);
body.style = "margin: 0";
ctx.font = FONT_HEIGHT + NORMAL_FONT;
ctx.textBaseline = "bottom";
}
function calc_client_width() {
let width = ((window.innerWidth / font_width) | 0) * font_width - 4 * font_width;
let height = ((window.innerHeight / line_height) | 0) * line_height - (4 * line_height);
if (is_mobile) {
height /= 2;
}
return {
width_px: width,
height_px: height,
width_char: (width / font_width) | 0,
height_char: (height / line_height) | 0,
};
}
function set_client_width(size) {
let dpi = window.devicePixelRatio;
if (dpi <= 1) {
dpi = 1;
}
dpi = Math.ceil(dpi);
console.log(
'window.devicePixelRatio is', window.devicePixelRatio,
'canvas scale is', dpi
);
canvas.width = size.width_px * dpi;
canvas.height = size.height_px * dpi;
ctx.scale(dpi, dpi);
canvas.style.width = size.width_px + 'px';
canvas.style.height = size.height_px + 'px';
// WebGL Canvas
//webgl_canvas.width = size.width_px * dpi;
//webgl_canvas.height = size.height_px * dpi;
//webgl_canvas.style.width = size.width_px + 'px';
//webgl_canvas.style.height = size.height_px + 'px';
CLIENT_WIDTH_IN_CHARS = size.width_char;
CLIENT_HEIGHT_IN_CHARS = size.height_char;
if (CLIENT_WIDTH_IN_CHARS * font_width < size.width_px) {
CLIENT_WIDTH_IN_CHARS += 1;
}
}
function resizeCanvas() {
const new_size = calc_client_width();
// if only the height has changed
if (is_mobile &&
app_initialized &&
(CLIENT_HEIGHT_IN_CHARS > new_size.height_char) &&
(CLIENT_WIDTH_IN_CHARS === new_size.width_char)
) {
// virtual keyboard can resize the canvas, don't allow it
return;
}
set_client_width(new_size);
const div = document.getElementById('containder-div');
div.style.width = (canvas.width + 2 * font_width) + 'px';
div.style.height = (canvas.height + 2 * line_height) + 'px';
init_canvas();
measure_start = new Date().getTime();
wasm_bindgen.handle_resize(app_ptr, CLIENT_WIDTH_IN_CHARS);
render_for_reason('resize canvas');
}
function js_log(str) {
console.log(str);
}
function render_for_reason(reason) {
current_second_info.render_count += 1;
//console.log("DIRTY", reason);
canvas_dirty = true;
if (canvas_dirty) {
const now = new Date().getTime();
wasm_bindgen.render(app_ptr);
if (need_update_selected_text) {
need_update_selected_text = false;
// hack: it must be called after the render
update_selected_text();
}
let command_count = redraw(now);
current_second_info.command_count += command_count;
let took = new Date().getTime() - measure_start;
current_second_info.render_time += took;
canvas_dirty = false;
can_scroll = true;
}
}
let need_update_selected_text = false;
function tick(_now) {
let now = new Date().getTime();
measure_start = new Date().getTime();
if (next_full_reparse_tick <= now) {
console.log("FREE");
console.log("memory: ", wasm_bindgen.get_allocated_bytes_count(app_ptr));
wasm_bindgen.reparse_everything(app_ptr);
console.log("memory: ", wasm_bindgen.get_allocated_bytes_count(app_ptr));
next_full_reparse_tick = now + 10_000;
} else if (wasm_bindgen.handle_time(app_ptr, now)) {
render_for_reason('tick');
if (content_was_modified) {
save_content();
}
} else if (single_pulsing_rects.length > 0 || repeating_pulsing_rects.length) {
render_for_reason('pulse update');
}
if (next_measure_tick <= now) {
per_second_info.render_time = current_second_info.render_time / current_second_info.render_count;
per_second_info.command_count = current_second_info.command_count / current_second_info.render_count;
per_second_info.render_count = current_second_info.render_count;
function round(num) { return Math.round((num + Number.EPSILON) * 100) / 100;}
console.log('Avg render_time: ', round(per_second_info.render_time));
console.log('Avg command_count: ', round(per_second_info.command_count));
console.log('render_cound per second: ', per_second_info.render_count);
next_measure_tick = now + 1000;
current_second_info.render_time = 0;
current_second_info.render_count = 0;
current_second_info.command_count = 0;
}
requestAnimationFrame(tick);
}
function save_content() {
let content = wasm_bindgen.get_compressed_encoded_content(app_ptr);
history.replaceState(undefined, undefined, '#' + content)
let notecalc_data = localStorage.getItem('notecalc');
if (notecalc_data === null) {
notecalc_data = {
tabs: [],
theme: THEME_LIGHT
};
} else {
notecalc_data = JSON.parse(notecalc_data);
}
notecalc_data.tabs[active_tab_index].encoded_content = content;
localStorage.setItem('notecalc', JSON.stringify(notecalc_data));
content_was_modified = false;
if (!is_mobile) {
active_tab_btn_dom.className = 'tablinks nav-item'; // remove 'unsaved' class
active_tab_btn_dom.children.item(0).className = 'nav-link active';
}
}
function on_mouse_up(e) {
//console.log("Mouse up")
wasm_bindgen.handle_mouse_up(app_ptr);
}
function on_wheel(evt) {
if (evt.ctrlKey || can_scroll === false) {
return;
}
evt.preventDefault();
let dir;
if (evt.deltaY > 0) {
// down
dir = 1;
} else if (evt.deltaY < 0) {
dir = 0;
} else {
return;
}
measure_start = new Date().getTime();
if (wasm_bindgen.handle_wheel(app_ptr, dir) === true) {
can_scroll = false;
render_for_reason('wheel');
}
}
function on_mouse_down(e) {
if (e.buttons === 1) {
const char_x = (e.offsetX) / font_width;
const char_y = (e.offsetY) / line_height;
measure_start = new Date().getTime();
wasm_bindgen.handle_click(app_ptr, char_x | 0, char_y | 0);
render_for_reason('click');
}
}
function set_fixed_content_and_cursor_pos(backbuffer_textarea) {
backbuffer_textarea.value = 'XX';
backbuffer_textarea.selectionStart = 2;
}
function on_click() {
if (is_mobile) {
backbuffer_textarea.select();
set_fixed_content_and_cursor_pos(backbuffer_textarea);
}
}
function update_selected_text() {
let selected_text = wasm_bindgen.get_selected_text_and_clear_app_clipboard(app_ptr);
if (selected_text !== undefined) {
backbuffer_textarea.value = selected_text;
backbuffer_textarea.select();
}
}
function on_drag(e) {
const char_x = ((e.offsetX) / font_width) | 0;
const char_y = ((e.offsetY) / line_height) | 0;
if (e.buttons === 1) { //dragged with left mouse button
const need_update = last_drag_event.x !== char_x || last_drag_event.y !== char_y;
measure_start = new Date().getTime();
if (need_update && wasm_bindgen.handle_drag(app_ptr, char_x, char_y)) {
need_update_selected_text = true;
render_for_reason('dragging');
last_drag_event = {x: char_x, y: char_y};
}
} else {
measure_start = new Date().getTime();
canvas.style.cursor = cursor_styles[wasm_bindgen.handle_mouse_move(app_ptr, char_x, char_y)];
//webgl_canvas.style.cursor = cursor_styles[wasm_bindgen.handle_mouse_move(app_ptr, char_x, char_y)];
render_for_reason('mouse move');
}
}
function doKeyUp(e) {
if (e.key === 'Alt') {
measure_start = new Date().getTime();
wasm_bindgen.alt_key_released(app_ptr);
set_content_was_modified();
render_for_reason('key up');
e.preventDefault();
return false;
}
}
function set_content_was_modified() {
content_was_modified = true;
if (!is_mobile) {
active_tab_btn_dom.className = 'unsaved tablinks';
active_tab_btn_dom.children.item(0).className = 'nav-link active';
}
}
let expect_keypress = false;
function doKeyDown(e) {
// console.log(e.key, 'alt', e.altKey, 'ctrl', e.ctrlKey, 'shift', e.shiftKey);
expect_keypress = false;
let key;
if (is_mobile && e.handled_for_mobile === undefined) {
return;
} else if (e.shiftKey && e.ctrlKey && e.key === 'C') {
show_content_in_modal();
return false;
} else if (e.ctrlKey && e.key === 's') {
e.preventDefault();
save_content();
return false;
} else if (e.key[0] === 'F' && e.key.length > 1) {
return;
} else if (e.key === 'CapsLock') {
return
} else if (e.key === "Backspace") {
key = 1;
} else if (e.key === "Enter") {
key = 2;
} else if (e.key === "Home") {
key = 3;
} else if (e.key === "End") {
key = 4;
} else if (e.key === "ArrowUp") {
key = 5;
} else if (e.key === "ArrowDown") {
key = 6;
} else if (e.key === "ArrowLeft") {
key = 7;
} else if (e.key === "ArrowRight") {
key = 8;
} else if (e.key === "Delete") {
key = 9;
} else if (e.key === "Escape") {
key = 10;
} else if (e.key === "PageUp") {
key = 11;
} else if (e.key === "PageDown") {
key = 12;
} else if (e.key === 'Tab') {
key = 13;
} else if (e.key === "Shift") {
return;
} else if (e.key === "Control") {
return;
} else if (e.key === "Alt") {
return;
} else if (e.key === "AltGraph") {
return;
} else if (e.key === "Dead") {
expect_keypress = true;
return;
} else if (e.ctrlKey &&
(
e.key === 'v' ||
e.key === 't' ||
e.key === 'n' ||
e.key === 'w'
)
) {
return;
} else if (e.key.length > 1) {
return;
} else {
key = e.key.codePointAt(0);
}
let modifiers = 0;
if (e.ctrlKey) {
modifiers |= 2;
}
if (e.altKey) {
modifiers |= 4;
}
if (e.shiftKey) {
modifiers |= 1;
}
//console.log('key ' + key);
measure_start = new Date().getTime();
let content_was_modified = wasm_bindgen.handle_input(app_ptr, key, modifiers);
if (content_was_modified) {
set_content_was_modified();
}
if (e.ctrlKey && (e.key === 'c' || e.key === 'x')) {
update_selected_text();
if (e.key === 'x') {
render_for_reason('ctrl-x');
}
return true;
}
render_for_reason('keydown');
e.preventDefault();
return false;
}
let command_buffer_ptr;
function redraw(now) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
let command_count = -1;
let buf_index = 0;
const command_buffer = new Uint8Array(wasm.memory.buffer, command_buffer_ptr);
const debug_commands = [];
let is_header = false;
while (true) {
command_count += 1;
const command_id = command_buffer[buf_index];
buf_index += 1;
if (command_id === 0 || command_id === undefined) {
break;
} else if (command_id === 1) { // SetStyle
} else if (command_id === 2) { // SetColor
let a = command_buffer[buf_index + 0];
let b = command_buffer[buf_index + 1];
let g = command_buffer[buf_index + 2];
let r = command_buffer[buf_index + 3];
buf_index += 4;
let fillStyle = 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a / 255.0 + ')';
if (is_debug) {
debug_commands.push(fillStyle);
}
ctx.fillStyle = fillStyle;
} else if (command_id === 3) { // RenderChar
let column_i = read_u8(command_buffer, buf_index);
let row_i = read_u8(command_buffer, buf_index + 1);
let ch = read_u32(command_buffer, buf_index + 2);
buf_index += 1 + 1 + 4;
let rch = String.fromCharCode(ch);
if (is_debug) {
debug_commands.push([column_i, row_i, rch]);
}
ctx.fillText(
rch,
column_i * font_width,
line_height * row_i + line_height - FONT_VERT_PADDING
);
} else if (command_id === 4) { // RenderUtf8Text | RenderString
let row_i = read_u16(command_buffer, buf_index);
let column_i = read_u16(command_buffer, buf_index + 2);
let len = read_u16(command_buffer, buf_index + 4);
buf_index += 6;
let j;
let str = '';
for (j = 0; j < len; j++) {
let ch = read_u32(command_buffer, buf_index);
let rch = String.fromCharCode(ch);
str += rch;
ctx.fillText(
rch,
(column_i + j) * font_width,
line_height * row_i + line_height - FONT_VERT_PADDING
);
buf_index += 4;
}
if (is_debug) {
debug_commands.push([column_i, row_i, str]);
}
} else if (command_id === 5) { // RenderAsciiText
let row_i = read_u16(command_buffer, buf_index);
let column_i = read_u16(command_buffer, buf_index + 2);
let len = read_u16(command_buffer, buf_index + 4);
buf_index += 6;
let j;
let str = '';
for (j = 0; j < len; j++) {
let ch = read_u8(command_buffer, buf_index);
let rch = String.fromCharCode(ch);
str += rch;
ctx.fillText(
rch,
(column_i + j) * font_width,
line_height * row_i + line_height - FONT_VERT_PADDING
);
buf_index += 1;
}
if (is_debug) {
debug_commands.push(str);
}
} else if (command_id === 7) { // RenderRectangle
let x = command_buffer[buf_index + 0];
let y = command_buffer[buf_index + 1];
let w = command_buffer[buf_index + 2];
let h = command_buffer[buf_index + 3];
buf_index += 4;
if (is_debug) {
debug_commands.push([x, y, w, h]);
}
ctx.fillRect(
x * font_width,
y * line_height,
w * font_width,
h * line_height
);
} else if (command_id === 8) { // FollowingTextCommandsAreHeaders
is_header = read_u8(command_buffer, buf_index) === 1;
if (is_header) {
ctx.font = FONT_HEIGHT + BOLD_FONT;
} else {
ctx.font = FONT_HEIGHT + NORMAL_FONT;
}
ctx.textBaseline = "bottom";
if (is_debug) {
debug_commands.push('Set Header: ' + is_header);
}
buf_index += 1;
} else if (command_id === 9) { // RenderUnderline
let x = command_buffer[buf_index + 0];
let y = command_buffer[buf_index + 1];
let w = command_buffer[buf_index + 2];
let j;
for (j = 0; j < w; j++) {
let rch = '▁';
ctx.fillText(
rch,
(x + j) * font_width,
line_height * y + line_height - FONT_VERT_PADDING
);
}
buf_index += 3;
} else if (command_id === 10) { // UpdatePulses
draw_pulsing_rects(single_pulsing_rects, repeating_pulsing_rects, now);
} else if (command_id === 101) { // Clear Pulses
repeating_pulsing_rects.length = 0;
single_pulsing_rects.length = 0;
} else if (command_id === 100) { // Pulses
let count = command_buffer[buf_index + 0];
buf_index += 1;
for (let i = 0; i < count; i++) {
let x = command_buffer[buf_index + 0];
let y = command_buffer[buf_index + 1];
let w = command_buffer[buf_index + 2];
let h = command_buffer[buf_index + 3];
buf_index += 4;
const start_color = read_u32(command_buffer, buf_index);
const end_color = read_u32(command_buffer, buf_index + 4);
const animation_time_ms = read_u16(command_buffer, buf_index + 8);
const repeat = command_buffer[buf_index + 10] !== 0;
buf_index += 4 + 4 + 2 + 1;
let item = {
x, y, w, h,
start_color,
end_color,
start_time: now,
duration_ms: animation_time_ms,
repeat: repeat
};
if (is_debug) {
debug_commands.push(item);
}
if (repeat) {
repeating_pulsing_rects.push(item);
} else {
single_pulsing_rects.push(item);
}
}
}
}
if (is_debug) {
//console.log(debug_commands);
}
return command_count;
}
function draw_pulsing_rects(single_pulsing_rects, repeating_pulsing_rects, now) {
function interp(start, end, x) {
return start + (end - start) * x;
}
function draw_pulsing_rect(pulsing_rect, delta) {
let r = interp((pulsing_rect.start_color & 0xFF000000) >>> 24, (pulsing_rect.end_color & 0xFF000000) >>> 24, delta);
let g = interp((pulsing_rect.start_color & 0x00FF0000) >>> 16, (pulsing_rect.end_color & 0x00FF0000) >>> 16, delta);
let b = interp((pulsing_rect.start_color & 0x0000FF00) >>> 8, (pulsing_rect.end_color & 0x0000FF00) >>> 8, delta);
let a = interp((pulsing_rect.start_color & 0x000000FF) >>> 0, (pulsing_rect.end_color & 0x000000FF) >>> 0, delta);
// TODO opt
ctx.fillStyle = 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a / 255.0 + ')';
ctx.fillRect(
pulsing_rect.x * font_width,
pulsing_rect.y * line_height,
pulsing_rect.w * font_width,
pulsing_rect.h * line_height
);
}
for (const pulsing_rect of single_pulsing_rects) {
let delta = (now - pulsing_rect.start_time) / pulsing_rect.duration_ms;
if (delta > 1) {
single_pulsing_rects.shift();
continue;
}
draw_pulsing_rect(pulsing_rect, delta);
}
for (const pulsing_rect of repeating_pulsing_rects) {
let delta = (now - pulsing_rect.start_time) / pulsing_rect.duration_ms;
if (delta > 1) {
pulsing_rect.start_time = now;
delta = 0;
}
draw_pulsing_rect(pulsing_rect, delta);
}
}
function read_u8(buf, byte_pos) {
return buf[byte_pos];
}
function read_u16(buf, byte_pos) {
return (buf[byte_pos + 0] << 0) |
buf[byte_pos + 1] << 8;
}
function read_u32(buf, byte_pos) {
return (buf[byte_pos + 0] << 0) |
(buf[byte_pos + 1] << 8) |
(buf[byte_pos + 2] << 16) |
(buf[byte_pos + 3] << 24);
}
function paste_from_clipboard(pasted_text) {
measure_start = new Date().getTime();
wasm_bindgen.handle_paste(app_ptr, pasted_text);
render_for_reason('paste');
save_content();
return false;
}
function on_tab_click(index) {
if (!is_mobile) {
let tab_btn = document.getElementById('tablink_' + index);
if (tab_btn === null) {
return;
}
let tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = 'tablinks nav-item';
tablinks[i].children.item(0).className = 'nav-link';
tablinks[i].children.item(0).style.backgroundColor = null;
tablinks[i].children.item(0).style.borderColor = null;
}
tab_btn.className = 'tablinks nav-item';
tab_btn.children.item(0).className = 'nav-link active';
if (theme === THEME_DARK) {
tab_btn.children.item(0).style.backgroundColor = '#A6B6E8';
tab_btn.children.item(0).style.borderColor = DARK_BG_COLOR_BRIGHTER + ' ' + DARK_BG_COLOR_BRIGHTER + ' ' + DARK_BG_COLOR;
}
}
reload_content(index);
}
function on_tab_close(index, dangerous) {
let notecalc_data = JSON.parse(localStorage.getItem('notecalc'));
if (index === 0 && notecalc_data.tabs.length === 1) {
return;
}
if (dangerous || window.confirm("Are you sure to delete this tab?")) {
notecalc_data.tabs.splice(index, 1);
localStorage.setItem('notecalc', JSON.stringify(notecalc_data));
document.getElementById('tablink_' + index).remove();
let tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
let li = tablinks[i];
li.setAttribute('id', 'tablink_' + i);
li.setAttribute('onclick', 'on_tab_click(' + i + ')');
let dom_a = li.children.item(0);
let dom_a_text = dom_a.childNodes[0];
dom_a_text.nodeValue = 'Note ' + (i + 1) + ' ';
let close_btn = dom_a.childNodes[1];
close_btn.setAttribute('id', 'tablink_close' + i);
close_btn.setAttribute('onclick', 'on_tab_close(' + i + ')');
}
tab_index_counter = tablinks.length;
reload_content(Math.min(index, notecalc_data.tabs.length - 1));
active_tab_btn_dom.className = 'tablinks nav-item';
active_tab_btn_dom.children.item(0).className = 'nav-link active';
}
return false;
}
function set_active_tab_index(index) {
active_tab_index = index;
if (!is_mobile) {
active_tab_btn_dom = document.getElementsByClassName('tablinks')[index];
}
}
function insert_tab_dom() {
let index = tab_index_counter;
tab_index_counter += 1;
let li = document.createElement('li');
li.setAttribute('id', 'tablink_' + index);
li.className = 'tablinks nav-item';
li.onclick = function () {
on_tab_click(index)
};
li.innerHTML = "<a class=\"nav-link\" href=\"javascript: void(0)\">Note " + (index + 1) + " " +
" <button id=\"tablink_close" + index + "\" type=\"button\" class=\"close tablinks_close\" " +
" aria-label=\"Close\" " +
" onclick=\"on_tab_close(" + index + ")\" " +
" style=\"float: none;font-size: 1.2rem;\">\n" +
" <span aria-hidden=\"true\">×</span>\n" +
" </button>\n" +
" </a>";
document.getElementById('tabs').insertBefore(li, document.getElementById('tablink_add'));
return index;
}
function init_tabs_dom() {
let notecalc_data = JSON.parse(localStorage.getItem('notecalc'));
for (i = 0; i < notecalc_data.tabs.length; ++i) {
insert_tab_dom();
}
}
function add_tab_and_switch_to_it(encoded_content) {
let notecalc_data = JSON.parse(localStorage.getItem('notecalc'));
let index = insert_tab_dom();
notecalc_data.tabs.push({
encoded_content: encoded_content
});
localStorage.setItem('notecalc', JSON.stringify(notecalc_data));
on_tab_click(index);
}
function show_content_in_modal() {
const str = wasm_bindgen.get_selected_rows_with_results(app_ptr);
document.getElementById('modal_content').innerText = str;
}
function on_theme_btn_click() {
let notecalc_data = JSON.parse(localStorage.getItem('notecalc'));
let new_theme;
if (notecalc_data.theme === THEME_LIGHT) {
new_theme = THEME_DARK;
} else {
new_theme = THEME_LIGHT;
}
apply_theme(new_theme);
}
function apply_theme(new_theme) {
theme = new_theme;
let notecalc_data = JSON.parse(localStorage.getItem('notecalc'));
let btn = document.getElementById('theme_selector_btn');
notecalc_data.theme = theme;
if (theme === THEME_LIGHT) {
btn.className = "badge badge-dark offset-1";
btn.innerText = 'Dark theme'
document.getElementsByTagName('body')[0].style.backgroundColor = "#FFFFFF";
document.getElementById('tabs').style.borderBottom = null;
document.getElementById('body').className = '';
} else {
btn.className = "badge badge-light offset-1";
btn.innerText = 'Light theme'
document.getElementsByTagName('body')[0].style.backgroundColor = DARK_BG_COLOR;
document.getElementById('tabs').style.borderBottom = "1px solid " + DARK_BG_COLOR_BRIGHTER;
document.getElementById('body').className = 'bg-dark';
}
localStorage.setItem('notecalc', JSON.stringify(notecalc_data));
on_tab_click(active_tab_index);
wasm_bindgen.set_theme(app_ptr, theme);
render_for_reason('THEME changed');
}
function copy_modal_content_to_clipboard() {
let content = document.getElementById('modal_content');
content.focus();
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(content);
selection.removeAllRanges();
selection.addRange(range);
document.execCommand("copy");
selection.removeAllRanges();
$('#copy_to_clipboard_btn').tooltip({
placement: 'top',
title: 'Copied',
trigger: 'focus'
})
$('#copy_to_clipboard_btn').tooltip('show');
}
function reload_content(tab_index) {
single_pulsing_rects.length = 0;
repeating_pulsing_rects.length = 0;
ctx.clearRect(0, 0, canvas.width, canvas.height);
let notecalc_data = JSON.parse(localStorage.getItem('notecalc'));
let tabs = notecalc_data.tabs;
set_active_tab_index(tab_index);
measure_start = new Date().getTime();
wasm_bindgen.set_compressed_encoded_content(
app_ptr,
tabs[active_tab_index].encoded_content
);
history.replaceState(undefined, undefined, '#' + tabs[active_tab_index].encoded_content)
render_for_reason('reload content');
}
async function run() {
if (is_debug) {
wasm = await wasm_bindgen('frontend-web/pkg/frontend_web_bg.wasm?v=0.3.0');
} else {
wasm = await wasm_bindgen('assets/frontend_web_bg.wasm?v=0.3.0');
}
const size = calc_client_width();
set_client_width(size);
app_ptr = wasm.create_app(CLIENT_WIDTH_IN_CHARS, CLIENT_HEIGHT_IN_CHARS);
command_buffer_ptr = wasm.get_command_buffer_ptr();