-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
1299 lines (1105 loc) · 55.2 KB
/
index.php
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
<?php
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<title>B-Editor</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="Bilinguator.com">
<link rel="shortcut icon" href="img/icon.png" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script defer src="scripts/jquery-3.6.0.min.js"></script>
<script type="text/javascript" src="print-bilingual-pdf/scripts/print_bilingual_pdf.js"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" rel="stylesheet"></link>
</head>
<body>
<form action="" method="get">
<header>
<div class="book-file-address-container">
<label class="book-file-address-label" for="book1-file-address">
Book 1
</label>
<input type="file" class="book-file-address book1-file-address" id="book1-file-address" name="book1">
</div>
<div class="logo">
<a href=".">
<img class="logo-img" src="img/logo.png" alt="B-Editor" />
</a>
</div>
<div class="book-file-address-container">
<label class="book-file-address-label" for="book2-file-address">
Book 2
</label>
<input type="file" class="book-file-address book2-file-address" id="book2-file-address" name="book2">
</div>
<div>
<button class="panel-button" type="submit" title="Launch">
<img class="panel-button-img" src="img/play.png" />
</button>
</div>
</form>
</header>
<div class="main-area">
<?php
// Check plugins
// print-bilingual-pdf
$printBilingualPDFExists = true;
$pathsCheck = ['print-bilingual-pdf/scripts/print_bilingual_pdf.js',
'print-bilingual-pdf/scripts/create_element.js',
'print-bilingual-pdf/scripts/runnings.js',
'print-bilingual-pdf/css/printed_book.css'];
foreach ($pathsCheck as $path) {
$printBilingualPDFExists = file_exists($path);
if (!$printBilingualPDFExists) {
break;
}
}
$printBilingualColsExists = file_exists('print-bilingual-pdf/scripts/print_bilingual_pdf_cols.js');
$printBilingualRowsExists = file_exists('print-bilingual-pdf/scripts/print_bilingual_pdf_rows.js');
// bilingual-formats
$bilingualFormatsExists = is_dir('bilingual-formats');
$saveBilingualTxtExists = file_exists('bilingual-formats/scripts/save_bilingual_txt.php');
$saveBilingualFb2Exists = file_exists('bilingual-formats/scripts/save_bilingual_fb2.php');
$saveBilingualEpubExists = file_exists('bilingual-formats/scripts/save_bilingual_epub.php');
if (@$_GET['book1'] == "" and @$_GET['book2'] != "")
echo "<p>Book 1 is not selected.</p>";
if (@$_GET['book1'] != "" and @$_GET['book2'] == "")
echo "<p>Book 2 is not selected.</p>";
if (@$_GET['book1'] == "" and @$_GET['book2'] == "")
echo "<p>Books are not selected.</p>";
if (@$_GET['book1'] == @$_GET['book2'] and @$_GET['book1'] != "" and @$_GET['book2'] != "")
echo "<p>The same book is selected twice!</p>";
// Beginning of the big condition "if both books are selected"
if (@$_GET['book1'] != '' and @$_GET['book2'] != '' and @$_GET['book1'] != @$_GET['book2']):
$bookPath1 = 'books/' . iconv('UTF-8', 'cp1251', stripslashes($_GET['book1']));
$bookPath2 = 'books/' . iconv('UTF-8', 'cp1251', stripslashes($_GET['book2']));
$bookContent1 = file_get_contents($bookPath1);
$bookContent2 = file_get_contents($bookPath2);
$book1 = preg_split("/[\r]*[\n]+/", $bookContent1);
$book2 = preg_split("/[\r]*[\n]+/", $bookContent2);
$arr1 = Array();
$a = -1;
for ($i = 0; $i < count($book1); $i++) {
if (strlen(trim($book1[$i])) != 0) {
$a++;
$arr1[$a] = $book1[$i];
}
}
$arr2 = Array();
$a = -1;
for ($i = 0; $i < count($book2); $i++) {
if (strlen(trim($book2[$i])) != 0) {
$a++;
$arr2[$a] = $book2[$i];
}
}
// Counting the longest book
if (count($arr1) > count($arr2)) {
$longestBookLength = count($arr1);
} else {
$longestBookLength = count($arr2);
}
// Getting languages
$lang1 = explode('.', $_GET['book1'])[0];
$lang1 = explode(' ', $lang1)[0];
$lang1 = explode('_', $lang1);
$lang1 = end($lang1);
$lang2 = explode('.', $_GET['book2'])[0];
$lang2 = explode(' ', $lang2)[0];
$lang2 = explode('_', $lang2);
$lang2 = end($lang2);
// Getting the bookmark number
$book1Name = explode('.', @$_GET['book1'])[0];
$book1Name = explode(' ', $book1Name)[0];
$book1Extension = explode('.', $bookPath1);
$book1Extension = end($book1Extension);
$bookmarkFileAddress = "books/bookmarks/bookmark_{$book1Name}_{$lang2}.{$book1Extension}";
$bookmark = '0';
if (file_exists($bookmarkFileAddress)) {
$bookmarkFileContents = file_get_contents($bookmarkFileAddress);
if (is_numeric($bookmarkFileContents)) {
$bookmark = file_get_contents($bookmarkFileAddress);
}
}
$br = '
';
// Beginning of the loop displaying couples of paragraphs
for ($i = 0; $i < $longestBookLength; $i++):
$bookmarkClass = ($bookmark == $i) ? ' paragraph-number-bookmark' : '';
?>
<div class="paragraph-couple">
<div class="left">
<div class="paragraph-panel panel-left">
<p class="paragraph-number paragraph-number-left<?=$bookmarkClass?>" onclick="setParagraphIndex(this.innerText, 'left')"><?=$i?></p>
<div class="addDelete">
<img class="paragraph-button add-paragraph-button" src="img/add.png" onclick="addNewParagraph(this)" />
<img class="paragraph-button delete-paragraph-button" src="img/delete.png" onclick="deleteParagraph(this)" />
</div>
</div>
<textarea lang="<?=$lang1?>" class="paragraph paragraph-left" contenteditable="true"><?=@$arr1[$i]?></textarea>
</div>
<div class="right">
<textarea lang="<?=$lang2?>" class="paragraph paragraph-right" contenteditable="true"><?=@$arr2[$i]?></textarea>
<div class="paragraph-panel paragraph-panel-right">
<p class="paragraph-number paragraph-number-right<?=$bookmarkClass?>" onclick="setParagraphIndex(this.innerText, 'right')"><?=$i?></p>
<div class="addDelete">
<img class="paragraph-button add-paragraph-button" src="img/add.png" onclick="addNewParagraph(this)" />
<img class="paragraph-button delete-paragraph-button" src="img/delete.png" onclick="deleteParagraph(this)" />
</div>
</div>
</div>
</div>
<?php
endfor;
endif;
?>
</div>
<footer class="footer">
<div class="file-panel panel">
<div class="save-panel panel subpanel">
<button class="panel-button save-sources-panel-button save-not-needed" name="save" title="Save source files<?=$br?>(Ctrl+S)">
<img class="panel-button-img" src="img/save.png" />
</button>
<?php
if ($printBilingualPDFExists || $bilingualFormatsExists):
?>
<button class="panel-button save-all-formats-panel-button" title="Save in all formats">
<img class="panel-button-img" src="img/save_all_formats.png" />
</button>
<?php
endif;
if ($bilingualFormatsExists):
if ($saveBilingualTxtExists) :
?>
<button class="panel-button save-txt-panel-button" title="Save txt">
<img class="panel-button-img" src="img/save_txt.png" />
</button>
<?php
endif;
if ($saveBilingualFb2Exists) :
?>
<button class="panel-button save-fb2-panel-button" title="Save fb2">
<img class="panel-button-img" src="img/save_fb2.png" />
</button>
<?php
endif;
if ($saveBilingualEpubExists) :
?>
<button class="panel-button save-epub-panel-button" title="Save epub">
<img class="panel-button-img" src="img/save_epub.png" />
</button>
<?php
endif;
endif;
?>
</div>
<?php
if ($printBilingualPDFExists):
?>
<div class="print-book-panel panel subpanel">
<?php
if ($printBilingualColsExists):
?>
<button class="panel-button cols-panel-button" title="Print columns PDF<?=$br?>(Ctrl+P)">
<img class="panel-button-img" src="img/cols.png" />
</button>
<?php
endif;
if ($printBilingualRowsExists):
?>
<button class="panel-button rows-panel-button" title="Print rows PDF<?=$br?>(Ctrl+Shift+P)">
<img class="panel-button-img" src="img/rows.png" />
</button>
<?php
endif;
?>
</div>
<?php
endif;
?>
</div>
<div class="edition-panel panel">
<div class="insertions-edition-panel subpanel">
<button class="panel-button insertions-panel-button heading-panel-button" title="Title<?=$br?>(Ctrl+H)">
<img class="panel-button-img" src="img/h.png" />
</button>
<button class="panel-button insertions-panel-button bold-panel-button" title="Bold<?=$br?>(Ctrl+B)">
<img class="panel-button-img" src="img/b.png" />
</button>
<button class="panel-button insertions-panel-button italic-panel-button" title="Italic<?=$br?>(Ctrl+I)">
<img class="panel-button-img" src="img/i.png" />
</button>
<button class="panel-button insertions-panel-button delimiter-panel-button" title="Add delimiter<?=$br?>(Ctrl+,)">
<img class="panel-button-img" src="img/delimiter.png" />
</button>
<button class="panel-button insertions-panel-button case-panel-button" title="Switch case">
<img class="panel-button-img" src="img/case.png" />
</button>
<button class="panel-button insertions-panel-button paste-img-panel-button" title="Add illustration<?=$br?>(Ctrl+L)">
<img class="panel-button-img" src="img/img.png" />
</button>
</div>
</div>
<div class="manipulations-panel panel">
<div class="division-concatenation-edition-panel subpanel">
<button class="panel-button division-panel-button" title="Divide paragraph<?=$br?>(Ctrl+D)" onmousedown="divideParagraph()">
<img class="panel-button-img" src="img/division.png" />
</button>
<button class="panel-button division-panel-button" title="Divide paragraph by newline<?=$br?>(Ctrl+Shift+D)" onmousedown="divideParagraphBySubstring('\n')">
<img class="panel-button-img" src="img/division_by_newline.png" />
</button>
<button class="panel-button concatenation-panel-button" title="Concatenate paragraphs<?=$br?>(Ctrl+M)" onmousedown="concatenateParagraphs('')">
<img class="panel-button-img" src="img/concatenation.png" />
</button>
<button class="panel-button concatenation-panel-button" title="Concatenate paragraphs via space<?=$br?>(Ctrl+U)" onmousedown="concatenateParagraphs(' ')">
<img class="panel-button-img" src="img/concatenation_space.png" />
</button>
<button class="panel-button concatenation-panel-button" title="Concatenate paragraphs via delimiter<?=$br?>(Ctrl+Y)" onmousedown="concatenateParagraphs('<delimiter>')">
<img class="panel-button-img" src="img/concatenation_delimiter.png" />
</button>
</div>
<div class="shift-area subpanel">
<div class="paragraph-indexes-container">
<input type="text" class="paragraph-index" rows="1" size="2" placeholder="№" value="<?=@$bookmark?>" />
<hr />
<input type="text" class="paragraph-index" rows="1" size="2" placeholder="№"/>
</div>
<button class="panel-button shift-panel-button" title="Shift">
<img class="panel-button-img" src="img/shift.png" />
</button>
<button class="panel-button delete-by-index-panel-button" title="Delete">
<img class="panel-button-img" src="img/delete_by_index.png" />
</button>
</div>
<div class="score-panel panel subpanel">
<button class="panel-button aim-button" onclick="focusOnParagraph()" title="To current position<?=$br?>(Ctrl+O)">
<img class="panel-button-img" src="img/focus.png" />
</button>
<button class="panel-button bookmark-panel-button" title="Set bookmark<?=$br?>(Ctrl+B)">
<img class="panel-button-img" src="img/bookmark.png" />
</button>
<div class="stats">
<div class="bookmark-score-container subpanel">
<p class="bookmark-number">
<?=@$bookmark?>
</p>
<p class="slash">
/
</p>
<p class="paragraphs-count">
-
</p>
</div>
</div>
<div class="scroll-panel subpanel">
<p class="percents">
0%
</p>
<div class="scroll-score"></div>
</div>
</div>
</div>
</footer>
<script type="text/javascript">
const book1FileAddress = document.querySelector('.book1-file-address');
const book2FileAddress = document.querySelector('.book2-file-address');
[book1FileAddress, book2FileAddress].forEach((item) => {
item.addEventListener('change', () => {
sessionStorage.setItem(item.classList.item(1), 'books/' + item.files[0].name);
})
});
const bookID = getBookID();
const lang1 = getBookLang(1);
const lang2 = getBookLang(2);
const illustrationsDir = 'books/illustrations';
const coverPath = `books/covers/${lang1}.png`
const saveDir = 'books/saved'
const fileNameBase = `${bookID}_${lang1}_${lang2}`;
const outputBase = `${saveDir}/${fileNameBase}`
// Get original array
function getParagraphsArray (side = 'left') {
let paragraphs = document.querySelectorAll('.paragraph-' + side);
let paragraphsArray = Array();
for (let i = 0; i < paragraphs.length; i++) {
paragraphsArray[i] = paragraphs[i].value;
}
return paragraphsArray;
}
// Get text, left or right
function getText (side = 'left') {
return getParagraphsArray(side).join('\n');
}
// Adjusting textarea height
function fixTextArea () {
let textareaNeeded = document.querySelectorAll('.paragraph');
function fixTextareaSize(textarea) {
textarea.style.height = '0px';
textarea.style.height = textarea.scrollHeight + 2 + 'px';
}
~function () {
for (let i = 0; i < textareaNeeded.length; i++) {
textareaNeeded[i].addEventListener('input', function (e) {
fixTextareaSize(e.target)
});
fixTextareaSize(textareaNeeded[i]);
}
}()
}
fixTextArea();
// Adjusting height of adjacent textareas
function alignSiblingTextareas (index = 'all') {
if (index === 'all') {
let paragraphCouplesNumber = document.querySelectorAll('.paragraph-couple').length;
for (let i = 0; i < paragraphCouplesNumber; i++) {
alignSiblingTextareas(i);
}
} else {
let textareaLeft = document.querySelectorAll('.paragraph-left')[index];
let textareaRight = document.querySelectorAll('.paragraph-right')[index];
let textareaLeftHeight = textareaLeft.offsetHeight;
let textareaRightHeight = textareaRight.offsetHeight;
if (textareaLeftHeight > textareaRightHeight) {
textareaRight.style.height = textareaLeft.offsetHeight + 'px';
} else {
textareaLeft.style.height = textareaRight.offsetHeight + 'px';
}
}
}
alignSiblingTextareas();
document.querySelectorAll('.paragraph').forEach((paragraph) => {
paragraph.addEventListener('input', () => {
let paragraphIndex = getParagraphIndex(paragraph);
alignSiblingTextareas(paragraphIndex);
document.querySelectorAll('.panel-button').forEach((button) => {
button.classList.remove('save-not-needed');
});
});
});
// Add padding below .main-area
function addMainAreaPaddingBottom () {
document.querySelector('.main-area').style.paddingBottom = document.querySelector('.footer').offsetHeight + 20 + 'px';
}
addMainAreaPaddingBottom();
window.addEventListener('resize', () => {
fixTextArea();
alignSiblingTextareas();
addMainAreaPaddingBottom();
});
// Focus on a given paragraph
function focusOnParagraph () {
let targetParagraphNumber = document.querySelectorAll('.paragraph-index')[0].value;
let targetParagraph = document.querySelectorAll('.paragraph-left')[targetParagraphNumber];
if (targetParagraph) {
targetParagraph.scrollIntoView();
}
}
// Set bookmark
function setBookmark () {
let targetParagraphNumber = document.querySelectorAll('.paragraph-index')[0].value;
if (Number.isInteger(Number(targetParagraphNumber))) {
let bookmarkNumber = document.querySelector('.bookmark-number');
bookmarkNumber.innerHTML = targetParagraphNumber;
$.ajax({
url: "save_bookmark.php",
type: "POST",
data: ({bookmark_number: targetParagraphNumber,
file_name_base: fileNameBase,
file1_extension: getBookExtension(1)}),
dataType: "html"
});
document.querySelectorAll('.paragraph-number').forEach((item) => {
item.classList.remove('paragraph-number-bookmark');
});
let targetLeftNumber = document.querySelectorAll('.paragraph-number-left')[targetParagraphNumber];
let targetRightNumber = document.querySelectorAll('.paragraph-number-right')[targetParagraphNumber];
[targetLeftNumber, targetRightNumber].forEach((item) => {
item.classList.add('paragraph-number-bookmark');
});
}
}
function saveAllFormats () {
saveSources();
saveTxt();
saveFb2();
saveEpub();
printCols();
printRows();
document.querySelector('.save-all-formats-panel-button').classList.add('save-not-needed');
document.querySelector('.save-txt-panel-button').classList.add('save-not-needed');
document.querySelector('.save-fb2-panel-button').classList.add('save-not-needed');
document.querySelector('.save-epub-panel-button').classList.add('save-not-needed');
}
document.querySelector('.save-all-formats-panel-button').addEventListener('click', () => {
saveAllFormats();
});
document.querySelector('.bookmark-panel-button').addEventListener('click', () => {
setBookmark();
});
function setParagraphIndex (paragraphIndex, side = 'left') {
if (side === 'left') {
document.querySelectorAll('.paragraph-index')[0].value = paragraphIndex;
}
if (side === 'right') {
document.querySelectorAll('.paragraph-index')[1].value = paragraphIndex;
}
}
// Focus on bookmark
function focusBookmark () {
let bookmarkNumber = Number(document.querySelector('.bookmark-number').innerHTML);
let targetParagraphCouple = document.querySelectorAll('.paragraph-couple')[bookmarkNumber];
targetParagraphCouple.scrollIntoView();
}
document.querySelector('.bookmark-number').addEventListener('click', focusBookmark);
// Recalculation of paragraph numbers
function recountParagraphNumbers () {
let leftParagraphNumbers = document.querySelectorAll('.paragraph-number-left');
let rightParagraphNumbers = document.querySelectorAll('.paragraph-number-right');
for (let i = 0; i < $('.paragraph-couple').length; i++) {
leftParagraphNumbers[i].innerText = i;
rightParagraphNumbers[i].innerText = i;
}
showParagraphsCount();
}
// Get language code
function getBookLang (bookIndex = 1) {
let fileAddress = sessionStorage.getItem('book' + bookIndex + '-file-address');
let fileName = fileAddress.split('/')[1].split('.')[0];
let semanticPart = fileName.split(' ')[0];
let lang = semanticPart.split('_');
lang = lang[lang.length - 1];
return lang;
}
function getBookID (bookIndex = 1) {
let fileAddress = sessionStorage.getItem('book' + bookIndex + '-file-address');
let fileName = fileAddress.split('/')[1];
let semanticPart = fileName.split('.')[0].split(' ')[0];
let bookID = semanticPart.slice(0, semanticPart.lastIndexOf('_'));
return bookID;
}
function getBookExtension (bookIndex = 1) {
let fileAddress = sessionStorage.getItem('book' + bookIndex + '-file-address');
return fileAddress.split('.').slice(-1)[0];
}
document.querySelector('.cols-panel-button').addEventListener('click', () => {
printCols();
});
document.querySelector('.rows-panel-button').addEventListener('click', () => {
printRows();
});
function printCols () {
printBilingualPDF (getText('left'),
getText('right'),
getBookLang(1),
getBookLang(2),
'cols',
coverPath,
`${bookID}_${getBookLang(1)}`,
illustrationsDir);
}
function printRows () {
printBilingualPDF (getText('left'),
getText('right'),
getBookLang(1),
getBookLang(2),
'rows',
coverPath,
`${bookID}_${getBookLang(1)}`,
illustrationsDir);
}
// Save sources
function saveSources () {
if (document.querySelectorAll('.paragraph-couple').length === 0) {
return false;
}
let paragraphsArrayLeft = getParagraphsArray('left').join('\n');
let paragraphsArrayRight = getParagraphsArray('right').join('\n');
$.ajax({
url: "save_sources.php",
type: "POST",
data: ({address1: sessionStorage.getItem('book1-file-address'),
address2: sessionStorage.getItem('book2-file-address'),
text1: paragraphsArrayLeft,
text2: paragraphsArrayRight
}),
dataType: "html"
});
document.querySelector('.save-sources-panel-button').classList.add('save-not-needed');
}
function saveTxt () {
$.ajax({
url: "save_bilingual_formats.php",
type: "POST",
data: ({format: 'txt',
address1: sessionStorage.getItem('book1-file-address'),
address2: sessionStorage.getItem('book2-file-address'),
outputPath: `${outputBase}.txt`
}),
dataType: "html"
});
document.querySelector('.save-txt-panel-button').classList.add('save-not-needed');
}
function saveFb2 () {
$.ajax({
url: "save_bilingual_formats.php",
type: "POST",
data: ({format: 'fb2',
address1: sessionStorage.getItem('book1-file-address'),
address2: sessionStorage.getItem('book2-file-address'),
outputPath: `${outputBase}.fb2`,
coverPath: coverPath,
illustrationsDir: illustrationsDir,
lang1: lang1,
lang2: lang2,
bookID: bookID
}),
dataType: "html"
});
document.querySelector('.save-fb2-panel-button').classList.add('save-not-needed');
}
function saveEpub () {
$.ajax({
url: "save_bilingual_formats.php",
type: "POST",
data: ({format: 'epub',
address1: sessionStorage.getItem('book1-file-address'),
address2: sessionStorage.getItem('book2-file-address'),
outputPath: `${outputBase}.epub`,
coverPath: coverPath,
illustrationsDir: illustrationsDir,
lang1: lang1,
lang2: lang2,
bookID: bookID
}),
dataType: "html"
});
document.querySelector('.save-epub-panel-button').classList.add('save-not-needed');
}
document.querySelector('.save-sources-panel-button').addEventListener('click', () => {
saveSources();
});
document.querySelector('.save-txt-panel-button').addEventListener('click', () => {
saveSources();
saveTxt();
});
document.querySelector('.save-fb2-panel-button').addEventListener('click', () => {
saveSources();
saveFb2();
});
document.querySelector('.save-epub-panel-button').addEventListener('click', () => {
saveSources();
saveEpub();
});
// Displaying number of paragraphs
function showParagraphsCount () {
let paragraphsCountArea = document.querySelector('.paragraphs-count');
let paragraphsArrayLeft = getParagraphsArray('left');
let paragraphsArrayRight = getParagraphsArray('right');
let maxParagraphsCount = Math.max(paragraphsArrayLeft.length, paragraphsArrayRight.length);
if (maxParagraphsCount === 0) {
paragraphsCountArea.innerText = '-';
} else {
paragraphsCountArea.innerText = maxParagraphsCount - 1;
}
}
showParagraphsCount();
// Class for the selected paragraph
class focusedParagraph {
constructor(paragraph, start = 0, end = 0) {
this._paragraph = paragraph;
this._start = start;
this._end = end;
}
get paragraph() {
return this._paragraph;
}
get start() {
return this._start;
}
get end() {
return this._end;
}
set paragraph(value) {
this._paragraph = value;
}
set start(value) {
this._start = value;
}
set end(value) {
this._end = value;
}
}
let activeParagraph = new focusedParagraph();
// Get image number
function getImgNumber () {
let targetParagraph = document.activeElement;
let joinedText = '';
if (targetParagraph.classList.contains('paragraph-left')) {
joinedText = getParagraphsArray('left').join('');
} else if (targetParagraph.classList.contains('paragraph-right')) {
joinedText = getParagraphsArray('right').join('');
}
return joinedText.split('<img').length;
}
// Inserting text to paragraph instead of selected text
function insertTextToParagraph (text, paragraph, isInserted = true) {
if (paragraph.classList.contains('paragraph') && text !== '') {
let start = paragraph.selectionStart;
let end = isInserted ? paragraph.selectionEnd : start;
let paragraphValue = paragraph.value;
let part1 = paragraphValue.substring(0, start);
let part3 = paragraphValue.substring(end);
paragraph.value = part1 + text + part3;
activeParagraph.paragraph = paragraph;
activeParagraph.start = isInserted ? start : start + text.length;
activeParagraph.end = start + text.length;
fixTextArea();
alignSiblingTextareas();
document.querySelectorAll('.panel-button').forEach((button) => {
button.classList.remove('save-not-needed');
});
} else {
return false;
}
}
// Switch case
function switchCase (text) {
let textLowered = text.toLowerCase();
let textUppered = text.toUpperCase();
if (text === textUppered) {
return textLowered;
} else {
return textUppered;
}
}
// Get selected text
function getSelectedText () {
if (document.activeElement.classList.contains('paragraph')) {
let paragraph = document.activeElement;
let start = paragraph.selectionStart;
let end = paragraph.selectionEnd;
let paragraphContent = paragraph.value;
return paragraphContent.substring(start, end);
} else {
return false;
}
}
document.querySelector('.delimiter-panel-button').addEventListener('mousedown', () => {
insertTextToParagraph('<delimiter>', document.activeElement, false);
});
document.querySelector('.paste-img-panel-button').addEventListener('mousedown', () => {
insertTextToParagraph('<img' + getImgNumber(document.activeElement) + '>', document.activeElement, false);
});
document.querySelector('.case-panel-button').addEventListener('mousedown', () => {
insertTextToParagraph(switchCase(getSelectedText()), document.activeElement);
document.querySelectorAll('.panel-button').forEach((button) => {
button.classList.remove('save-not-needed');
});
});
// Add tags
function addTagsToParagraph (tag1, tag2, paragraph) {
if (paragraph.classList.contains('paragraph')) {
let start = paragraph.selectionStart;
let end = paragraph.selectionEnd;
if (start !== end) {
let paragraphValue = paragraph.value;
let taggedParagraphValue = paragraphValue.substring(0, start) + tag1;
taggedParagraphValue += paragraphValue.substring(start, end) + tag2;
taggedParagraphValue += paragraphValue.substring(end);
paragraph.value = taggedParagraphValue;
activeParagraph.paragraph = paragraph;
activeParagraph.start = end + tag1.length + tag2.length;
activeParagraph.end = end + tag1.length + tag2.length;
} else {
return false;
}
}
document.querySelectorAll('.panel-button').forEach((button) => {
button.classList.remove('save-not-needed');
});
}
document.querySelector('.bold-panel-button').addEventListener('mousedown', () => {
addTagsToParagraph('<b>', '</b>', document.activeElement);
});
document.querySelector('.italic-panel-button').addEventListener('mousedown', () => {
addTagsToParagraph('<i>', '</i>', document.activeElement);
});
document.querySelector('.heading-panel-button').addEventListener('mousedown', () => {
addTagsToParagraph('<h1>', '</h1>', document.activeElement);
});
// For all insertions buttons
document.querySelectorAll('.insertions-panel-button').forEach((button) => {
button.addEventListener('mouseup', () => {
activeParagraph.paragraph.focus();
activeParagraph.paragraph.setSelectionRange(activeParagraph.start, activeParagraph.end);
})
});
// Get scroll percent
function getScrollPercent() {
let doc = document.documentElement;
let body = document.body;
let top = 'scrollTop';
let height = 'scrollHeight';
return Math.ceil((doc[top]||body[top]) / ((doc[height]||body[height]) - doc.clientHeight) * 100);
}
// Display scroll percent
window.addEventListener('scroll', function() {
let scrollPercent = getScrollPercent();
document.querySelector('.percents').innerHTML = scrollPercent + '%';
document.querySelector('.scroll-score').style.background = `linear-gradient(to right, rgba(250,143,56,1) 0%, rgba(250,143,56,1) ${scrollPercent}%, rgba(255,255,255,1) ${scrollPercent}%, rgba(255,255,255,1) 100%)`;
});
// Get side of paragraph
function getParagraphSide (paragraph) {
if (paragraph.classList.contains('paragraph-left')) {
return 'left';
}
if (paragraph.classList.contains('paragraph-right')) {
return 'right';
}
return false;
}
// Get paragraph index
function getParagraphIndex (paragraph) {
let paragraphSide = getParagraphSide(paragraph);
let paragraphsArray = document.querySelectorAll('.paragraph-' + paragraphSide);
for (let i = 0; i < paragraphsArray.length; i++) {
if (paragraphsArray[i] === paragraph) {
return i;
}
}
return false;
}
// Get element index
function getElementIndex (element, selector) {
let elementsArray = document.querySelectorAll(selector);
for (let i = 0; i < elementsArray.length; i++) {
if (elementsArray[i] === element) {
return i;
}
}
return false;
}
// Find child by class
function findChildByClass (element, childClass) {
let elementChildren = element.childNodes;
let result;
elementChildren.forEach((item) => {
if (item.classList) {
if (item.classList.contains(childClass)) {
result = item;
}
}
});
return result;
}
// Add couple of empty paragraphs to the end
function appendTerminalEmptyParagraphs () {
let clonedParagraphCouple = document.querySelectorAll('.paragraph-couple')[0].cloneNode(true);
let leftParagraph = findChildByClass(findChildByClass(clonedParagraphCouple, 'left'), 'paragraph-left');
let rightParagraph = findChildByClass(findChildByClass(clonedParagraphCouple, 'right'), 'paragraph-right');
leftParagraph.value = '';
rightParagraph.value = '';
document.querySelector('.main-area').append(clonedParagraphCouple);
document.querySelector('.main-area').append(clonedParagraphCouple);
recountParagraphNumbers();
}
// Delete empty paragraphs in the end
function deleteTerminalEmptyParagraphs () {
let paragraphCouples = document.querySelectorAll('.paragraph-couple');
let leftParagraphs = document.querySelectorAll('.paragraph-left');
let rightParagraphs = document.querySelectorAll('.paragraph-right');
let index = paragraphCouples.length - 1;
while (leftParagraphs[index].value.trim() === '' && rightParagraphs[index].value.trim() === '') {
paragraphCouples[index].remove();
index--;
}
}
// Add paragraph
function addNewParagraph (addButton, deleteTerminal = true) {
appendTerminalEmptyParagraphs();
let buttonIndex = getElementIndex(addButton, '.add-paragraph-button');
let side = buttonIndex % 2 === 0 ? 'left' : 'right';
let paragraphIndex = side === 'left' ? buttonIndex / 2 : (buttonIndex - 1) / 2;
let targetParagraphsArray = document.querySelectorAll('.paragraph-' + side);
for (let i = targetParagraphsArray.length - 1; i > paragraphIndex; i--) {
targetParagraphsArray[i].value = targetParagraphsArray[i - 1].value;
}
targetParagraphsArray[paragraphIndex].value = '';
fixTextArea();
alignSiblingTextareas();
if (deleteTerminal) {
deleteTerminalEmptyParagraphs();
}
document.querySelectorAll('.panel-button').forEach((button) => {
button.classList.remove('save-not-needed');
});
}
// Delete paragraph
function deleteParagraph (deleteButton, deleteTerminal = true) {
let buttonIndex = getElementIndex(deleteButton, '.delete-paragraph-button');
let side = buttonIndex % 2 === 0 ? 'left' : 'right';
let paragraphIndex = side === 'left' ? buttonIndex / 2 : (buttonIndex - 1) / 2;
let targetParagraphsArray = document.querySelectorAll('.paragraph-' + side);
for (let i = paragraphIndex; i < targetParagraphsArray.length - 1; i++) {
targetParagraphsArray[i].value = targetParagraphsArray[i + 1].value;
}
targetParagraphsArray[targetParagraphsArray.length - 1].value = '';
fixTextArea();
alignSiblingTextareas();
if (deleteTerminal) {
deleteTerminalEmptyParagraphs();
}
showParagraphsCount();
document.querySelectorAll('.panel-button').forEach((button) => {
button.classList.remove('save-not-needed');