-
Notifications
You must be signed in to change notification settings - Fork 13
/
editor.js
2168 lines (1908 loc) · 74.5 KB
/
editor.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
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
/*
LogicEmu
Copyright (c) 2018-2023 Lode Vandevenne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
How the cursor and selection region should work:
SHORT SUMMARY:
xb,yb-xe,ye: main active sel coords (xe,ye=blinking cursor). xe may be < xb and ye may be < yb. ye inclusive. xe inclusive if overwrite mode.
xi,yi-xa,ya: minimum and maximum of xb,yb-xe,ye
xr0,yr0-xr1,yr1: rectangular region, always exclusive
xm0,ym0-xm1,ym1: classic multiline region, always exclusive, first line is at ym0 from xm0 to end, last line is at ym1-1 from 0 to xm1-1.
x0,y0-x1,y1: don't exist here, use locally derived from whichever of the above you want (prefer exclusive end coordinates for this naming)
LONGER EXPLANATION:
xb,yb - xe,ye describes the user selection (xe exclusive in insert mode, inclusive in overwrite mode. ye inclusive)
--> note that xe,ye usually represents the actual active cursor position
xi,yi - xa,ya represent min and max of those values respectively
The region:
in the case of rectangular mode:
-in insert mode, xi,yi - xa,ya is the rectangular area, excluding x1 and including y1
-in overwrite mode, xi,yi - xa,ya is the rectangular area, including x1 and y1 (so one bigger horizontally)
in the case of classic multiline mode:
-in insert mode, selection spans (yi-ya+1) lines (ya is inclusive)
--the line belonging to yb goes from xb to end of line
--the line belonging to ye goes from begin of line to xe
--the lines in between go from begin of line to end of line
-in overwrite mode, same except on the last line, xe is included instead of excluded
The cursor:
-in insert mode, a blinking vertical line located on line ye in front of character xe
-in overwrite mode, a blinking horizontal line located on line ye under character xe
ye is allowed to be smaller than yb and xe is allowed to be smaller than xb. For some purposes, you want to use xi,yi and xa,ya instead
In insert mode, the cursor can have a thickness of 0.
xr0,yr0 - xr1,yr1 represents region for rectangle with exclusive coordinates, and with taking overwrite mode's switch between inclusive and exclusive into account
-xr0 and yr0 are the same as respectively xi and yi
-yr1 is ya + 1 (so exclusive instead of inclusive)
-xr1 is xa in insert mode, or xa + 1 in overwrite mode (so always exclusive)
--> inclusive top left and exclusive bottom right respectively (so the relation to xa depends on overwrite mode)
xm0,ym0 - xm1,ym1 are coordinates for classical multiline mode (note that the shape is ALWAYS such that top line has selection on right and not on left, even if xe < xb, that's how it always works in text editors)
-ym0 is same as yi, the first line
-ym1 is same as ya + 1 (so exclusive), after the last line
-xm0 is the x coordinate belonging to line ym0, and characters left of it are not selected
-xm1 is the x coordinate belonging to line ym1, and characters left of it are selected (xm1 itself is exclusive)
Why so many "inclusive" end coordinates are used:
in y direction, and for overwrite in x direction, you never want something of 0 height or 0 thickness to be selected
so the case of yi==ya must have a height of 1, not 0. Hence ya as inclusive coordinate.
For naming "x0,y0,x1,y1": set those to whichever of the above x/y combinations you need depending on the situation (prefer exclusive end coordinates for this naming)
*/
function LineOp(opt_editop, opt_y, opt_x) {
this.y = opt_y || 0; // y position of this line
this.rem0 = 0; // start x position of part to remove
this.rem1 = 0; // end x position of part to remove
this.add0 = opt_x || 0; // start x position of part where to insert
this.add = ''; // text to insert there
if(opt_editop) opt_editop.ops1.push(this);
}
// operation on a single line
function EditorOp() {
this.ops0 = []; // line ops to do before rem/add below
this.rem0 = 0; // start y position of lines to remove
this.rem1 = 0; // end y position of lines to remove
this.add0 = 0; // start y position of lines to add
this.add1 = 0; // end y position of lines to add, must be > add0 to add anything at all (is exclusive end coordinate)
this.ops1 = []; // line ops to do after rem/add above
//this is filled in by doOps and used by undo, no need to fill it in yourself
this.selxb = 0;
this.selyb = 0;
this.selxe = 0;
this.selye = 0;
this.selrect = false;
this.mergeundo = false;
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// TODO: use own fields for width and height rather than global w and h (use nothing global at all)
function EditComponent() {
this.div = undefined;
this.textarea = undefined; // the hidden textarea for typing and pasting in
this.elgrid = []; // divs per cell, for all rows
this.elrow = []; // div's per row, undefined if this row has no content
this.chargrid = []; // 2D grid of all chars, space ' ' may also mean empty
this.linelen = []; // actually used length of each line
this.areadiv = undefined; // contains the hidden textarea inside
// cursor or selection area
this.cursordiv = undefined; // blinking cursor (vertical if insert mode, horizontal if insert mode)
this.cursorcenterdiv = undefined; // center of rectangle (if any)
this.cursortopdiv = undefined; // top one if not rectangle
this.cursorbottomdiv = undefined; // bottom one if not rectangle
this.cursorblinkinterval = undefined;
this.pastepreview = undefined; // contains preview of what gets pasted that follows mouse when in paste mode
this.pastprevieww = 0;
this.pastpreviewh = 0;
this.pasting = false; // if true, shows the preview
this.pastecontent = 's->a->l\n # \ns->#';
// On a change in the text content
this.onchange = undefined;
this.onchange_ = function() {
if(this.onchange) this.onchange();
};
// On a change in the selection or cursor position
this.onchangesel = undefined;
this.onchangesel_ = function() {
if(this.onchangesel) this.onchangesel();
};
// On a change in a mode (e.g. overwrite mode)
this.onchangemode = undefined;
this.onchangemode_ = function() {
if(this.onchangemode) this.onchangemode();
};
// overwritemode is like overwrite (as opposed to insert) in standard text editors, and a bit more too:
// it also preserves witdh of lines when backspacing
this.overwritemode = true;
// freecursormode allows cursor to move beyond end of line
// this one is actually linked together with overwritemode by the code later on
this.freecursormode = this.overwritemode;
// in block mode, selections become rectangular, else they are like traditional multiline text selection
// this one, too, gets linked with overwritemode
// NOTE: this doesn't say whether the selection itself is rectangular or not, only the default behavior of dragging. See this.isrectangular instead for that.
this.blockmode = this.overwritemode;
// remember how large the div style was set last, to avoid updating it when already equal to w
this.divWidth_ = 0;
this.divHeight_ = 0;
// see the long explanation at the top of hte file for the meaning of all these coordinates
// selection coordinates. xb/yb is where dragging started, xe/ye is end drag corner
this.xb = 0;
this.yb = 0;
this.xe = 0; // inclusive
this.ye = 0; // inclusive as well
// unlike selection coordinates, these return top left or bottom right corner, with same inclusivity modes
this.xi = function() { return Math.min(this.xb, this.xe); };
this.yi = function() { return Math.min(this.yb, this.ye); };
this.xa = function() { return Math.max(this.xb, this.xe); };
this.ya = function() { return Math.max(this.yb, this.ye); };
// begin and end of rectangle, exclusive (unlike b/e and i/a which have some inclusive bounds), and updated for overwrite mode when needed
// see info at top of file for more
this.xr0 = function() { return Math.min(this.xb, this.xe); };
this.yr0 = function() { return Math.min(this.yb, this.ye); };
this.xr1 = function() { return Math.max(this.xb, this.xe) + (this.overwritemode ? 1 : 0); };
this.yr1 = function() { return 1 + Math.max(this.yb, this.ye); };
// this one is for classic multiline selection (arbitrary naming: s=start, f=finish), inclusive for y, exclusive for x end
// see info at top of file for more
this.xm0 = function() { return (this.yb == this.ye) ? this.xr0() : ((this.yb < this.ye) ? this.xb : this.xe) };
this.ym0 = function() { return this.yi(); };
this.xm1 = function() { return (this.yb == this.ye) ? this.xr1() : (((this.yb < this.ye) ? this.xe : this.xb) + (this.overwritemode ? 1 : 0)); };
this.ym1 = function() { return this.ya() + 1; };
// whether the current x0,y0,x1,y1 combination represents rectangular or classical multiline region (has no meaning if isRegion() is false)
// while blockmode tells what form new selections must have, isrectangular tells what form the current selection has
this.isrectangular = false;
// returns whether the selection is any region, whether rectangular shaped or classic multiline shaped
this.isRegion = function() { return this.xb != this.xe || this.yb != this.ye; };
// unlike isrectangular (without capital r), this also includes the trivial rectangle of having single cell reflected, and even the case of multiline which is only a single line, so anything that's mathematically speaking a rectangle
// if this returns false, we have the classical multiline case
this.isAnyRectangular = function() { return this.isrectangular || (this.yb == this.ye); };
this.isFullyRectangular = function() { return this.isrectangular && this.isRegion() };
// includes case of being single-line
this.isAnyClassicMultiline = function() { return !this.isrectangular; };
// only if truly multiple lines and not rectangular shape
this.isFullyClassicMultiline = function() { return !this.isrectangular && (this.ya() > this.yi()) };
// The selection can be seen as either rectangular or classic multiline, e.g. if it's only a single line
// TODO: There are some multiline cases that also could be both, include those? Or else rename the function to "issingleline"
this.couldBeBoth = function() { return this.yb == this.ye; };
// the regular ops done, for redo
this.redoops = [];
// the inverted ops, for undo
this.undoops = [];
this.undopos = 0;
this.avoidcontextmenu = false; // used for when using right click for some other goal. But normally allow it.
this.printDebug = function() {
//console.log('area contents: ' + this.area.value);
//console.log('area selection range: ' + this.area.selectionStart + ' ' + this.area.selectionEnd);
};
// may modify op and store them in undo array
this.doLineOp_ = function(op) {
var y = op.y;
var add0 = op.add0;
var add1 = op.add0 + op.add.length;
var rem0 = op.rem0;
var rem1 = op.rem1;
this.ensureHeight(y + 1);
var oldlen = this.linelen[y];
var numadd = add1 - Math.min(add0, oldlen);
var numrem = Math.min(rem1, oldlen) - Math.min(rem0, oldlen);
if(add1 > add0 && rem0 == add0 && rem1 == add1) {
this.ensureWidth(add0 + 1); // add1 not necessary, but add0 yes
if(!this.elrow[y]) this.elrow[y] = makeDiv(0, y * th, tw * w, th, this.div);
var row = this.elrow[y];
for(var x = add0; x < add1; x++) {
var c = op.add[x - add0];
this.chargrid[y][x] = c;
if(c == ' ') {
if(this.elgrid[y][x]) {
util.removeElement(this.elgrid[y][x]);
this.elgrid[y][x] = undefined;
}
} else {
if(!this.elgrid[y][x]) this.elgrid[y][x] = makeDiv(x * tw, 0, tw, th, row);
this.elgrid[y][x].innerText = c;
}
}
this.linelen[y] += numadd - numrem; // in theory 0 except if rem was out of bounds
} else {
if(rem1 > rem0) {
for(var x = rem0; x < this.linelen[y]; x++) {
var x2 = x - rem0 + rem1;
var c2 = x2 < this.linelen[y] ? this.chargrid[y][x2] : ' ';
var g2 = x2 < this.linelen[y] ? this.elgrid[y][x2] : undefined;
this.chargrid[y][x] = c2;
if(this.elgrid[y][x]) util.removeElement(this.elgrid[y][x]);
this.elgrid[y][x] = g2;
if(this.elgrid[y][x]) this.elgrid[y][x].style.left = (x * tw) + 'px';
if(x2 < this.linelen[y]) {
this.chargrid[y][x2] = ' ';
this.elgrid[y][x2] = undefined;
}
}
this.linelen[y] -= numrem;
}
if(add1 > add0) {
if(y < 0 || y >= this.elgrid.length) return;
this.linelen[y] += numadd;
this.ensureWidth(this.linelen[y]);
if(!this.elrow[y]) this.elrow[y] = makeDiv(0, y * th, tw * w, th, this.div);
var row = this.elrow[y];
for(var x = this.linelen[y] - 1; x >= add1; x--) {
var x2 = x - add1 + add0;
this.chargrid[y][x] = this.chargrid[y][x2];
this.elgrid[y][x] = this.elgrid[y][x2];
if(this.elgrid[y][x]) this.elgrid[y][x].style.left = (x * tw) + 'px';
this.elgrid[y][x2] = undefined;
}
for(var x = add0; x < add1; x++) {
var c = op.add[x - add0];
this.chargrid[y][x] = c;
this.elgrid[y][x] = undefined;
if(c != ' ') {
this.elgrid[y][x] = makeDiv(x * tw, 0, tw, th, row);
this.elgrid[y][x].innerText = c;
}
}
}
}
this.onchange_();
};
// this one does not alter undo arrays
this.doOpWithoutUndo = function(op) {
for(var i = 0; i < op.ops0.length; i++) {
this.doLineOp_(op.ops0[i]);
}
if(op.rem1 > op.rem0) this.removeLines_(op.rem0, op.rem1);
if(op.add1 > op.add0) this.insertLines_(op.add0, op.add1);
for(var i = 0; i < op.ops1.length; i++) {
this.doLineOp_(op.ops1[i]);
}
this.fitDiv();
};
this.storeUndo_ = function(op) {
// TODO: merge undo ops if they're typing single characters (this doesn't refer to using the "mergeundo" flag, but actually turning it into a single op for performance reasons)
var u = this.invertOp(op);
u.selxb = this.xb;
u.selyb = this.yb;
u.selxe = this.xe;
u.selye = this.ye;
u.selrect = this.isrectangular;
if(this.undopos > this.undoops.length) this.undopos = this.undoops.length;
if(this.undopos != this.undoops.length) {
this.undoops.length = this.undopos;
this.redoops.length = this.undopos;
}
this.redoops.push(op);
this.undoops.push(u);
this.undopos++;
// prune
if(this.undoops.length > 200) {
var undoops2 = [];
var redoops2 = [];
for(var i = 0; i < 100; i++) {
undoops2.push(this.undoops[100 + i]);
redoops2.push(this.redoops[100 + i]);
}
this.undoops = undoops2;
this.redoops = redoops2;
this.undopos = this.undoops.length;
}
};
this.doOp = function(op) {
this.storeUndo_(op);
this.doOpWithoutUndo(op);
};
this.undo = function() {
// TODO: also save cursor position
console.log('undo: undopos: ' + this.undopos + ', len: ' + this.undoops.length);
if(this.undopos > this.undoops.length) this.undopos = this.undoops.length;
if(this.undopos == 0) return;
this.undopos--;
var rop = this.redoops[this.undopos];
rop.selxb = this.xb;
rop.selyb = this.yb;
rop.selxe = this.xe;
rop.selye = this.ye;
rop.selrect = this.isrectangular;
var op = this.undoops[this.undopos];
this.doOpWithoutUndo(op);
this.selectRegion(op.selxb, op.selyb, op.selxe, op.selye, op.selrect);
if(op.mergeundo) this.undo();
};
this.redo = function() {
if(this.undopos < 0) this.undopos = 0;
if(this.undopos >= this.redoops.length) return;
var op = this.redoops[this.undopos];
this.undopos++;
this.doOpWithoutUndo(op);
this.selectRegion(op.selxb, op.selyb, op.selxe, op.selye, op.selrect);
if(this.undopos < this.redoops.length && this.redoops[this.undopos].mergeundo) this.redo();
};
// mark the last two operations as merged for undo (doesn't help performance, but makes the user undo button combine them if there was some intermediate operation that shouldn't be user visible)
this.markMergeUndo = function() {
if((this.undopos - 1 < 0) || (this.undopos - 1 >= this.undoops.length)) return;
this.undoops[this.undopos - 1].mergeundo = true;
this.redoops[this.undopos - 1].mergeundo = true;
};
this.invertLineOp = function(op) {
var y = op.y;
var result = new LineOp();
result.add = '';
for(var x = op.rem0; x < op.rem1; x++) {
if(this.chargrid[y]) {
result.add += (this.chargrid[y][x] || ' ');
} else {
result.add += ' ';
}
}
result.add0 = op.rem0;
result.rem0 = op.add0;
result.rem1 = op.add0 + op.add.length;
result.y = op.y;
return result;
};
// inverts an op to create the undo version
this.invertOp = function(op) {
var result = new EditorOp();
result.add0 = op.rem0;
result.add1 = op.rem1;
result.rem0 = op.add0;
result.rem1 = op.add1;
result.mergeundo = op.mergeundo;
for(var y = op.rem0; y < op.rem1; y++) {
var l = new LineOp();
l.y = y;
l.add = '';
for(var x = 0; x < this.linelen[y]; x++) l.add += this.chargrid[y][x];
result.ops1.push(l);
}
for(var i = 0; i < op.ops1.length; i++) {
result.ops0.push(this.invertLineOp(op.ops1[i]));
}
for(var i = 0; i < op.ops0.length; i++) {
result.ops1.push(this.invertLineOp(op.ops0[i]));
}
return result;
};
// clears all text, using the operations so it supports undo
this.clear = function() {
var op = new EditorOp();
op.rem0 = 0;
op.rem1 = this.chargrid.length;
this.doOp(op);
};
this.renderCursorDivs = function() {
var xi = this.xi();
var yi = this.yi();
var xa = this.xa();
var ya = this.ya();
var xb = this.xb;
var yb = this.yb;
var xe = this.xe;
var ye = this.ye;
var xr0 = this.xr0();
var yr0 = this.yr0();
var xr1 = this.xr1();
var yr1 = this.yr1();
var xm0 = this.xm0();
var ym0 = this.ym0();
var xm1 = this.xm1();
var ym1 = this.ym1();
var rectangular = this.isrectangular;
// the blinking cursor is at xe,ye, NOT at xb,yb nor min/max, because it should be at the last corner you selected
if(this.overwritemode) {
this.cursordiv.style.left = (xe * tw) + 'px';
this.cursordiv.style.width = (tw) + 'px';
this.cursordiv.style.top = ((ye + 1) * th - 4) + 'px';
this.cursordiv.style.height = 2 + 'px';
} else {
this.cursordiv.style.left = (xe * tw + 2) + 'px';
this.cursordiv.style.width = 2 + 'px';
this.cursordiv.style.top = (ye * th) + 'px';
this.cursordiv.style.height = (th) + 'px';
}
if(rectangular) {
if(this.cursortopdiv.style.visibility != 'hidden') {
this.cursortopdiv.style.visibility = 'hidden';
this.cursorbottomdiv.style.visibility = 'hidden';
}
this.cursortopdiv.style.width = '0';
this.cursorbottomdiv.style.width = '0';
this.cursorcenterdiv.style.left = xr0 * tw + 'px';
this.cursorcenterdiv.style.top = yr0 * th + 'px';
this.cursorcenterdiv.style.width = Math.max(2, (xr1 - xr0) * tw) + 'px';
this.cursorcenterdiv.style.height = (yr1 - yr0) * th + 'px';
} else { // classic multiline
//this.cursortopdiv.style.backgroundColor = 'blue';
//this.cursorbottomdiv.style.backgroundColor = 'yellow';
if(yi == ya) {
if(this.cursortopdiv.style.visibility != 'hidden') {
this.cursortopdiv.style.visibility = 'hidden';
this.cursorbottomdiv.style.visibility = 'hidden';
}
} else {
if(this.cursortopdiv.style.visibility != 'visible') {
this.cursortopdiv.style.visibility = 'visible';
this.cursorbottomdiv.style.visibility = 'visible';
}
}
var boardwidth = w;
//var selh = ya - yi;
this.cursortopdiv.style.width = '0';
this.cursorbottomdiv.style.width = '0';
this.cursorcenterdiv.style.width = '0';
if(yi == ya) {
this.cursortopdiv.style.width = '0';
this.cursorbottomdiv.style.width = '0';
if(xi != xa) {
this.cursorcenterdiv.style.left = xi * tw + 'px';
this.cursorcenterdiv.style.top = yi * th + 'px';
this.cursorcenterdiv.style.width = (xa - xi) * tw + 'px';
this.cursorcenterdiv.style.height = th + 'px';
}
} else {
this.cursortopdiv.style.left = xm0 * tw + 'px';
this.cursortopdiv.style.top = ym0 * th + 'px';
this.cursortopdiv.style.width = (boardwidth - xm0) * tw + 'px';
this.cursortopdiv.style.height = th + 'px';
this.cursorcenterdiv.style.left = 0 + 'px';
this.cursorcenterdiv.style.top = (yi + 1) * th + 'px';
this.cursorcenterdiv.style.width = boardwidth * tw + 'px';
this.cursorcenterdiv.style.height = Math.max(0, ya - yi - 1) * th + 'px';
this.cursorbottomdiv.style.left = 0 * tw + 'px';
this.cursorbottomdiv.style.top = (ym1 - 1) * th + 'px';
this.cursorbottomdiv.style.width = xm1 * tw + 'px';
this.cursorbottomdiv.style.height = th + 'px';
}
/*var rx = 0, ry = 0, rw = 0, rh = 0;
var rx0 = 0, rw0 = 0, rh0 = 0, ry0 = 0;
var rx1 = 0, ry1 = 0, rw1 = 0, rh1 = 0;
if(selh > 2) {
rx0 = xi;
ry0 = yi;
rw0 = (boardwidth - xi);
rh0 = 1;
rx = 0;
ry = (yi + 1);
rw = boardwidth;
rh = (ya - yi - 2);
rx1 = 0;
ry1 = ya - 1;
rw1 = xa;
rh1 = 1;
} else if(selh == 2) {
rx0 = xi;
ry0 = yi;
rw0 = boardwidth - xi + 1;
rh0 = 1;
rx1 = 0;
ry1 = ya - 1;
rw1 = xa;
rh1 = 1;
} else if(selh == 1) {
rx = xi;
ry = yi;
rw = xa - xi;
rh = 1;
}
this.cursortopdiv.style.left = rx0 * tw + 'px';
this.cursortopdiv.style.top = ry0 * th + 'px';
this.cursortopdiv.style.width = rw0 * tw + 'px';
this.cursortopdiv.style.height = rh0 * th + 'px';
this.cursorcenterdiv.style.left = rx * tw + 'px';
this.cursorcenterdiv.style.top = ry * th + 'px';
this.cursorcenterdiv.style.width = rw * tw + 'px';
this.cursorcenterdiv.style.height = rh * th + 'px';
this.cursorbottomdiv.style.left = rx1 * tw + 'px';
this.cursorbottomdiv.style.top = ry1 * th + 'px';
this.cursorbottomdiv.style.width = rw1 * tw + 'px';
this.cursorbottomdiv.style.height = rh1 * th + 'px';*/
}
};
// is like a single press select, place the cursor in one spot, no region, actually even cell is wrong word except in overwrite mode
// this will also reset the "isrectangular" value to whether or not blockmode is enabled
this.selectCell = function(x, y) {
if(!this.freecursormode && x > this.linelen[y]) {
x = this.linelen[y];
}
this.selectRegion(x, y, x, y, this.blockmode);
this.onchangesel_();
//this.printDebug();
};
// rectangular = whether block selection (if true), or classic line-based text selection (if false)
// for the xb..ye params, use selection start/end, not topleft/bottomright convention
this.selectRegion = function(xb, yb, xe, ye, rectangular) {
this.xb = xb;
this.yb = yb;
this.xe = xe;
this.ye = ye;
this.isrectangular = rectangular;
console.log('selectRegion: ' + xb + ' ' + yb + ' ' + xe + ' ' + ye + ' | ' + rectangular);
this.renderCursorDivs();
// now switch them to the topleft/bottomright convention
var x0 = this.xi();
var y0 = this.yi();
var x1 = this.xa();
var y1 = this.ya();
var text = this.extractSelectedText();
// Put the text in the hidden textarea, so standard browser methods of copying
// the text (ctrl+c, middle click selection, ...) work the default OS way
// without having to handle it ourselves here.
this.textarea.value = text;
this.textarea.setSelectionRange(0, this.textarea.value.length);
console.log('set selrange: ' + this.textarea.value.length);
console.log('text: "' + text + '"');
this.printDebug();
this.onchangesel_();
//this.printDebug();
};
// extracts and returns the text in the selected region
this.extractSelectedText = function() {
var text = '';
if(this.isrectangular) {
var x0 = this.xr0();
var y0 = this.yr0();
var x1 = this.xr1();
var y1 = this.yr1();
for(var y = y0; y < y1; y++) {
if(y != y0) text += '\n';
if(y >= this.chargrid.length) break;
for(var x = x0; x < x1; x++) {
if(x >= this.chargrid[y].length) break;
text += this.chargrid[y][x];
}
}
} else { // classic multiline
var x0 = this.xm0();
var y0 = this.ym0();
var x1 = this.xm1();
var y1 = this.ym1();
var boardwidth = w;
var selh = y1 - y0;
var rx0 = 0, rw0 = 0, rh0 = 0, ry0 = 0;
var rx = 0, ry = 0, rw = 0, rh = 0;
var rx1 = 0, ry1 = 0, rw1 = 0, rh1 = 0;
if(selh > 2) {
rx0 = x0;
ry0 = y0;
rw0 = (boardwidth - x0);
rh0 = 1;
rx = 0;
ry = (y0 + 1);
rw = boardwidth;
rh = (y1 - y0 - 2);
rx1 = 0;
ry1 = y1 - 1;
rw1 = x1;
rh1 = 1;
} else if(selh == 2) {
rx0 = x0;
ry0 = y0;
rw0 = boardwidth - x0 + 1;
rh0 = 1;
rx1 = 0;
ry1 = y1 - 1;
rw1 = x1;
rh1 = 1;
} else if(selh == 1) {
rx = x0;
ry = y0;
rw = x1 - x0;
rh = 1;
}
for(var y = ry0; y < ry0 + rh0; y++) {
if(y != y0) text += '\n';
if(y >= this.chargrid.length) break;
for(var x = rx0; x <= rx0 + rw0; x++) {
if(x >= this.chargrid[y].length) break;
text += this.chargrid[y][x];
}
}
for(var y = ry; y < ry + rh; y++) {
if(y != y0) text += '\n';
if(y >= this.chargrid.length) break;
for(var x = rx; x < rx + rw; x++) {
if(x >= this.chargrid[y].length) break;
text += this.chargrid[y][x];
}
}
for(var y = ry1; y < ry1 + rh1; y++) {
if(y != y0) text += '\n';
if(y >= this.chargrid.length) break;
for(var x = rx1; x < rx1 + rw1; x++) {
if(x >= this.chargrid[y].length) break;
text += this.chargrid[y][x];
}
}
}
return text;
};
// same as selectRegion, but preserves this.isrectangular, that is, doesn't take it as parameter
// can also be used to make region smaller, move it, ..., the name is not perfect
this.changeRegion = function(xb, yb, xe, ye) {
this.selectRegion(xb, yb, xe, ye, this.isrectangular);
};
// any region becomes normal cursor instead
this.unselectRegion = function() {
this.selectCell(this.xb, this.yb);
this.onchangesel_();
};
this.reselectRegion = function() {
this.selectRegion(this.xb, this.yb, this.xe, this.ye, this.isrectangular);
this.focusTextArea();
};
this.fillPastePreview = function() {
this.pastepreview.innerHTML = '';
if(!this.pasting) {
this.pastepreview.style.visibility = 'hidden';
return;
} else {
this.pastepreview.style.visibility = 'visible';
}
this.unselectRegion();
this.pastepreview.style.backgroundColor = BGCOLOR;
this.pastepreview.style.zIndex = '10';
var v = this.pastecontent;
var pw = 0; // horizontal paste content size
var ph = 0; // vertical paste content size
var x = 0;
var y = 0;
for(var i = 0; i < v.length; i++) {
if(v[i] == '\n') {
if(i + 1 == v.length) break;
x = 0;
y++;
continue;
}
x++;
pw = Math.max(x, pw);
}
ph = y + 1;
this.pastprevieww = pw;
this.pastpreviewh = ph;
this.pastepreview.style.width = (pw * tw) + 'px';
this.pastepreview.style.height = (ph * th) + 'px';
x = y = 0;
for(var i = 0; i < v.length; i++) {
var c = v[i];
if(c == '\n') {
if(i + 1 == v.length) break;
x = 0;
y++;
continue;
}
var cell = makeDiv(x * tw, y * th, tw, th, this.pastepreview);
cell.innerText = c;
cell.style.color = ONCOLOR;
x++;
}
};
this.focusTextArea = function() {
// avoid screen scrolling around by placing the hidden cursor focused element at the correct spot
this.areadiv.style.left = (this.xe * tw) + 'px';
this.areadiv.style.top = (this.ye * th) + 'px';
this.textarea.focus();
};
this.setUp = function(x, y, parent) {
var self = this;
this.div = makeDiv(x, y, tw, th, parent);
this.div.contenteditable = true;
this.div.style.fontSize = Math.ceil(tw * 0.75) + 'px';
this.div.style.textAlign = 'center';
this.div.style.fontFamily = 'monospace';
this.div.style.border = '1px solid ' + OFFCOLOR;
this.elgrid = [];
this.chargrid = [];
this.linelen = [];
this.areadiv = makeDiv(10, 10, 100, 0, this.div);
this.areadiv.style.overflow = 'hidden';
this.textarea = util.makeAbsElement('textarea', 0, 0, 100, 40, this.areadiv);
this.textarea.rows = 1;
this.textarea.cols = 20;
this.textarea.value = '';
this.textarea.style.fontSize = '12px';
this.textarea.style.zIndex = '1';
this.textarea.autocorrect = 'off';
this.textarea.spellcheck = false;
this.textarea.autocapitalize = 'off';
this.div.style.whiteSpace = 'pre';
this.div.style.backgroundColor = BGCOLOR;
this.div.style.color = OFFCOLOR;
this.pastepreview = makeDiv(0, 0, tw * 6, th * 6, this.div);
this.pastepreview.style.border = '1px solid ' + ONCOLOR;
this.fillPastePreview(); // actually empties it now.
this.cursordiv = makeDiv(0, 0, 1, 1, this.div);
this.cursordiv.style.backgroundColor = ONCOLOR;
this.cursordiv.style.animation = '1s blink step-end infinite';
this.cursorcenterdiv = makeDiv(0, 0, tw, th, this.div);
this.cursorcenterdiv.style.backgroundColor = util.addAlpha(ONCOLOR, 0.65);
this.cursortopdiv = makeDiv(0, 0, tw, th, this.div);
this.cursortopdiv.style.backgroundColor = util.addAlpha(ONCOLOR, 0.65);
this.cursortopdiv.style.visibility = 'hidden';
this.cursorbottomdiv = makeDiv(0, 0, tw, th, this.div);
this.cursorbottomdiv.style.backgroundColor = util.addAlpha(ONCOLOR, 0.65);
this.cursorbottomdiv.style.visibility = 'hidden';
var self = this;
this.resetCursorBlinkInterval();
this.fitDiv();
this.selectCell(Math.floor(w / 2), Math.floor(h / 2));
this.div.oncontextmenu = function(e) {
if(self.avoidcontextmenu || self.pasting) {
self.avoidcontextmenu = false;
return false;
}
};
this.div.onmouseup = function(e) {
console.log('mouse up');
self.printDebug();
}
this.div.onmousedown = function(e) {
console.log('mouse down');
self.printDebug();
var rect = self.div.getBoundingClientRect();
var x = e.x - rect.left;
var y = e.y - rect.top;
var x2 = self.mouseSelXb(x);
var y2 = self.mouseSelYb(y);
if(e.buttons == 1) {
self.avoidcontextmenu = false;
}
var inregion = (self.isRegion() && x2 >= self.xi() && x2 < self.xa() && y2 >= self.yi() && y2 < self.ya());
if(self.pasting) {
if(e.buttons == 2) { // right mouse button
self.avoidcontextmenu = true;
self.pasting = false;
self.fillPastePreview();
}
self.ensureWidth(x2 + self.pastprevieww);
self.ensureHeight(y2 + self.pastpreviewh);
if(e.buttons == 1) {
self.pasteText(self.pastecontent, x2, y2, true, false, true);
}
e.stopPropagation();
return false;
}
if(this.blockmode && inregion) {
e.stopPropagation();
return false;
}
if(e.buttons == 1) {
// ctrl+dragging to copy
if(self.isrectangular && inregion && e.ctrlKey && !self.pasting) {
self.pastecontent = '';
for(var py = self.yi(); py <= self.ya(); py++) {
for(var px = self.xi(); px <= self.xa(); px++) {
self.pastecontent += self.chargrid[py][px];
}
self.pastecontent += '\n';
}
self.pasting = true;
self.fillPastePreview();
e.stopPropagation();
return false;
}
}
// standard single left click cursor placement
if(e.buttons == 1) {
if(y2 >= h) {
e.stopPropagation();
return false;
}
//var x = Math.floor((e.x - rect.left) / tw);
//var y = Math.floor((e.y - rect.top) / th);
// move the hidden textarea to here. TODO: should this be in selectCell instead?
self.textarea.style.left = (x - 20) + 'px';
self.textarea.style.top = (y - 20) + 'px';
self.selectCell(x2, y2);
self.resetCursorBlinkInterval();
document.activeElement.blur();
self.focusTextArea();
}
e.stopPropagation();
return false;
};
// [search terms: doubleclick double click dclick dblclick]
this.div.ondblclick = function(e) {
var rect = self.div.getBoundingClientRect();
var x = e.x - rect.left;
var y = e.y - rect.top;
var x2 = self.mouseSelXb(x);
var y2 = self.mouseSelYb(y);
//self.selectRegion(x2, y2, x2 + 4, y2 + 2, false);
if(y2 >= self.chargrid.length || x2 >= self.chargrid[y2].length) return;
var line = self.chargrid[y2];
var c = line[x2];
var x0 = x2;
while(x0 >= 0 && line[x0] != ' ') x0--; x0++;
var x1 = x2;
while(x1 < line.length && line[x1] != ' ') x1++; x1--;
self.selectRegion(x0, y2, x1 + 1, y2, self.blockmode);
};
this.div.onmousemove = function(e) {
//console.log('mousemove: ' + e.x + ',' + e.y);
if(!self.pasting && !e.buttons) return;
var rect = self.div.getBoundingClientRect();
var x = e.x - rect.left;
var y = e.y - rect.top;
if(self.pasting) {
var x2 = self.mouseSelXb(x);