-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
634 lines (540 loc) · 18.9 KB
/
renderer.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
const { sendMsg, sendErr, getMsg, waitMsg } = window.electron;
Toast.setPlacement(TOAST_PLACEMENT.BOTTOM_RIGHT);
Toast.setTheme(TOAST_THEME.DARK);
let isInitialized = false;
let translator = "nllb-0";
let translate = translateByParagraph; // function
let clipboard = false; // default 0
// default
async function translateByAll(text, from, to, cb) {
let result = "",
startedAt = new Date().valueOf();
console.log(`translate to ${to} from ${from}`);
// start
cb(0, result, 0);
result += await waitMsg("translate", {
translator,
text,
from,
to,
});
// end
cb(1, result, new Date().valueOf() - startedAt);
}
async function translateByParagraph(text, from, to, cb) {
let paragraphs = text.split(/(\r\n{2,}|\r{2,}|\n{2,})/g),
result = "",
totalParagraphs = paragraphs.length,
countParagraphs = 0,
startedAt = new Date().valueOf();
console.log(`found ${totalParagraphs} paragraphs`);
console.log(`translate to ${to} from ${from}`);
// start
cb(0, result, 0);
for (let i = 0 ; i < paragraphs.length; i++) {
const paragraph = paragraphs[i];
if (paragraph.trim() !== "") {
try {
result += await waitMsg("translate", {
translator,
text: paragraph,
from,
to,
});
} catch(err) {
console.error(err);
result += paragraph;
}
} else {
result += paragraph || "";
}
countParagraphs += 1;
cb(countParagraphs / totalParagraphs, result, new Date().valueOf() - startedAt);
}
// end
cb(1, result, new Date().valueOf() - startedAt);
}
async function translateByLine(text, from, to, cb) {
let lines = text.split(/(\r\n|\r|\n)/g),
result = "",
totalLines = lines.length,
countLines = 0,
startedAt = new Date().valueOf();
console.log(`found ${totalLines} lines`);
console.log(`translate to ${to} from ${from}`);
// start
cb(0, result, 0);
for (let i = 0 ; i < lines.length; i++) {
const line = lines[i];
if (line.trim() !== "") {
try {
result += await waitMsg("translate", {
translator,
text: line,
from,
to,
});
} catch(err) {
console.error(err);
result += line;
}
} else {
result += line || "";
}
countLines += 1;
cb(countLines / totalLines, result, new Date().valueOf() - startedAt);
}
// end
cb(1, result, new Date().valueOf() - startedAt);
}
async function translateBySentence(text, from, to, cb) {
function isFullstop(str) {
return /^(\u002E|\u0589|\u06D4|\u0701|\u0702|\u1362|\u166E|\u1803|\u1809|\u2488|\u2489|\u248A|\u248B|\u248C|\u248D|\u248E|\u248F|\u2490|\u2491|\u2492|\u2493|\u2494|\u2495|\u2496|\u2497|\u2498|\u2499|\u249A|\u249B|\u2CF9|\u2CFE|\u2E3C|\u3002|\uA4FF|\uA60E|\uA6F3|\uFE12|\uFE52|\uFF0E|\uFF61)$/.test(str);
}
function isEndByFullstop(str) {
return /(\u002E|\u0589|\u06D4|\u0701|\u0702|\u1362|\u166E|\u1803|\u1809|\u2488|\u2489|\u248A|\u248B|\u248C|\u248D|\u248E|\u248F|\u2490|\u2491|\u2492|\u2493|\u2494|\u2495|\u2496|\u2497|\u2498|\u2499|\u249A|\u249B|\u2CF9|\u2CFE|\u2E3C|\u3002|\uA4FF|\uA60E|\uA6F3|\uFE12|\uFE52|\uFF0E|\uFF61)\s+/.test(str);
}
function splitSentences(str) {
return str.split(/(\u002E|\u0589|\u06D4|\u0701|\u0702|\u1362|\u166E|\u1803|\u1809|\u2488|\u2489|\u248A|\u248B|\u248C|\u248D|\u248E|\u248F|\u2490|\u2491|\u2492|\u2493|\u2494|\u2495|\u2496|\u2497|\u2498|\u2499|\u249A|\u249B|\u2CF9|\u2CFE|\u2E3C|\u3002|\uA4FF|\uA60E|\uA6F3|\uFE12|\uFE52|\uFF0E|\uFF61)/);
}
const paragraphs = text.split(/\r\n|\r|\n/g);
let result = "",
totalSentences = 0,
countSentences = 0,
startedAt = new Date().valueOf();
// calc
for (const paragraph of paragraphs) {
const sentences = splitSentences(paragraph);
totalSentences += sentences.length;
}
console.log(`found ${totalSentences} sentences`);
console.log(`translate to ${to} from ${from}`);
// start
cb(0, result, new Date().valueOf() - startedAt);
for (let i = 0 ; i < paragraphs.length; i++) {
const paragraph = paragraphs[i];
const sentences = splitSentences(paragraph);
for (let j = 0; j < sentences.length; j++) {
const sentence = sentences[j];
if (isFullstop(sentence)) {
if (!isEndByFullstop(result)) {
result += sentence + " ";
}
} else if (sentence.trim() !== "") {
try {
result += await waitMsg("translate", {
translator,
text: sentence,
from,
to,
});
} catch(err) {
console.error(err);
result += sentence;
}
}
countSentences += 1;
cb(countSentences / totalSentences, result, new Date().valueOf() - startedAt);
// fix last fullstop
if (result.length > 0 && isFullstop(result[result.length - 1])) {
result += " ";
}
}
// pass last linebreak
if (i !== paragraph.length - 1) {
result += "\n";
}
}
// end
cb(1, result, new Date().valueOf() - startedAt);
}
// page 1
// translate text
(function() {
const inputLangOptionsElement = document.getElementById("text-input-lang-options");
const outputLangOptionsElement = document.getElementById("text-output-lang-options");
const inputLangElement = document.getElementById("text-input-lang");
const outputLangElement = document.getElementById("text-output-lang");
const inputTextElement = document.getElementById("text-input-text");
const outputTextElement = document.getElementById("text-output-text");
const inputButtonElement = document.getElementById("text-input-button");
const outputButtonElement = document.getElementById("text-output-button");
let langs = [];
function setSpinner(element) {
const spinner = document.createElement("span");
spinner.className = "spinner-border spinner-border-sm mr-3";
element.innerHTML = "";
element.appendChild(spinner);
element.disabled = true;
}
function startTranslate() {
// set spinner
setSpinner(inputButtonElement);
setSpinner(outputButtonElement);
}
function endTranslate() {
// remove spinner
inputButtonElement.innerHTML = "Translate A to B";
inputButtonElement.disabled = false;
outputButtonElement.innerHTML = "Translate B to A";
outputButtonElement.disabled = false;
}
function isLangValid(str) {
return langs.indexOf(str) > -1;
}
function isLangsValid() {
return isLangValid(inputLangElement.value) && isLangValid(outputLangElement.value);
}
// set validation
[inputLangElement, outputLangElement]
.forEach(function(el, idx) {
// validation
el.addEventListener("input", function(e) {
const elem = e.target;
if (isLangValid(elem.value)) {
elem.classList.remove("is-invalid");
} else {
elem.classList.add("is-invalid");
}
});
});
// language detection
[inputTextElement, outputTextElement]
.forEach(function(el, idx) {
const langElement = idx === 0 ? inputLangElement : outputLangElement;
// validation
el.addEventListener("input", async function(e) {
const elem = e.target;
try {
const lang = await waitMsg("get-lang", elem.value);
// console.log(`lang: ${lang}`);
if (isLangValid(lang)) {
langElement.value = lang;
langElement.classList.remove("is-invalid");
}
} catch(err) {
console.error(err);
}
});
});
// set datasets
waitMsg("get-lang-list")
.then(function(obj) {
langs = Object.keys(obj);
for (const item of langs) {
const io = document.createElement("option");
io.value = item;
inputLangOptionsElement.appendChild(io);
const oo = document.createElement("option");
oo.value = item;
outputLangOptionsElement.appendChild(oo);
}
});
// set text translate event
[inputButtonElement, outputButtonElement]
.forEach(function(el, idx, arr) {
const srcTextElement = idx === 0 ? inputTextElement : outputTextElement;
const srcLangElement = idx === 0 ? inputLangElement : outputLangElement;
const dstTextElement = idx === 0 ? outputTextElement : inputTextElement;
const dstLangElement = idx === 0 ? outputLangElement : inputLangElement;
el.addEventListener("click", async function(e) {
if (!isInitialized) {
Toast.create({
title: "InitializeError",
message: "Not initialized.",
status: TOAST_STATUS.DANGER,
timeout: 3000
});
return;
}
if (!isLangsValid()) {
Toast.create({
title: "LanguageError",
message: "Invalid language.",
status: TOAST_STATUS.DANGER,
timeout: 3000
});
return;
}
if (srcTextElement.value.trim() == "") {
Toast.create({
title: "ValueError",
message: "Input must have at least 1 character.",
status: TOAST_STATUS.DANGER,
timeout: 3000
});
return;
}
const srcText = srcTextElement.value;
const srcLang = srcLangElement.value;
const dstLang = dstLangElement.value;
startTranslate();
translate(srcText, srcLang, dstLang, function(progress, result, timestamp) {
dstTextElement.value = result;
if (progress === 1) {
endTranslate();
console.log(`translated ${srcText.length} characters for ${timestamp} ms`);
}
});
});
});
// clipboard handler
getMsg("clipboard", function(err, msg, event) {
if (clipboard) {
inputTextElement.value = msg;
inputButtonElement.click();
}
});
})();
// page 2
// translate txt files
(function() {
const inputLangOptionsElement = document.getElementById("txt-input-lang-options");
const outputLangOptionsElement = document.getElementById("txt-output-lang-options");
const inputLangElement = document.getElementById("txt-input-lang");
const outputLangElement = document.getElementById("txt-output-lang");
const inputPathElement = document.getElementById("txt-input-path");
const outputPathElement = document.getElementById("txt-output-path");
const inputTextElement = document.getElementById("txt-input-text");
const outputTextElement = document.getElementById("txt-output-text");
const inputButtonElement = document.getElementById("txt-input-button");
const outputButtonElement = document.getElementById("txt-output-button");
function setSpinner(element) {
const spinner = document.createElement("span");
spinner.className = "spinner-border spinner-border-sm mr-3";
element.innerHTML = "";
element.appendChild(spinner);
element.disabled = true;
}
function startTranslate() {
// set spinner
setSpinner(inputButtonElement);
setSpinner(outputButtonElement);
}
function endTranslate() {
// remove spinner
inputButtonElement.innerHTML = "Translate A to B";
inputButtonElement.disabled = false;
outputButtonElement.innerHTML = "Translate B to A";
outputButtonElement.disabled = false;
}
waitMsg("get-lang-list")
.then(function(obj) {
const langs = Object.keys(obj);
for (const item of langs) {
const io = document.createElement("option");
io.value = item;
inputLangOptionsElement.appendChild(io);
const oo = document.createElement("option");
oo.value = item;
outputLangOptionsElement.appendChild(oo);
}
function isLangValid(str) {
return langs.indexOf(str) > -1;
}
async function isPathValid(str) {
return await waitMsg("is-exists", str);
}
function isLangsValid() {
return isLangValid(inputLangElement.value) && isLangValid(outputLangElement.value);
}
function isPathsValid() {
return isPathValid(inputPathElement.value) && isPathValid(outputPathElement.value);
}
async function getTextsFromDir(dirPath) {
return await waitMsg("get-text-files", dirPath);
}
// set validation
[inputLangElement, outputLangElement]
.forEach(function(el, idx) {
el.addEventListener("input", function(e) {
const elem = e.target;
if (isLangValid(elem.value)) {
elem.classList.remove("is-invalid");
} else {
elem.classList.add("is-invalid");
}
});
});
[inputPathElement, outputPathElement]
.forEach(function(el, idx) {
const langElement = idx === 0 ? inputLangElement : outputLangElement;
// path validation
el.addEventListener("input", async function(e) {
const elem = e.target;
try {
if (await isPathValid(elem.value)) {
elem.classList.remove("is-invalid");
} else {
elem.classList.add("is-invalid");
}
} catch(err) {
console.error(err);
elem.classList.add("is-invalid");
}
});
// language detection
el.addEventListener("change", async function(e) {
const elem = e.target;
try {
const texts = await getTextsFromDir(elem.value);
const filteredLangs = texts
.filter(function(item) {
return item.lang;
})
.map(function(item) {
return item.lang;
});
const lang = jsutl.mode(filteredLangs);
// console.log(`lang: ${lang}`);
if (isLangValid(lang)) {
langElement.value = lang;
langElement.classList.remove("is-invalid");
}
} catch(err) {
console.error(err);
}
});
});
// set text translate event
[inputButtonElement, outputButtonElement]
.forEach(function(el, idx, arr) {
const srcPathElement = idx === 0 ? inputPathElement : outputPathElement;
const srcLangElement = idx === 0 ? inputLangElement : outputLangElement;
const dstPathElement = idx === 0 ? outputPathElement : inputPathElement;
const dstLangElement = idx === 0 ? outputLangElement : inputLangElement;
el.addEventListener("click", async function(e) {
if (!isInitialized) {
Toast.create({
title: "InitializeError",
message: "Not initialized.",
status: TOAST_STATUS.DANGER,
timeout: 3000
});
return;
}
if (!isLangsValid()) {
Toast.create({
title: "LanguageError",
message: "Invalid language.",
status: TOAST_STATUS.DANGER,
timeout: 3000
});
return;
}
if (!isPathsValid()) {
Toast.create({
title: "PathError",
message: "Invalid directory path.",
status: TOAST_STATUS.DANGER,
timeout: 3000
});
return;
}
const srcPath = srcPathElement.value;
const dstPath = dstPathElement.value;
const srcLang = srcLangElement.value;
const dstLang = dstLangElement.value;
function translateFile(file) {
return new Promise(function(resolve, reject) {
// preview
inputTextElement.value = file.text;
translate(file.text, file.lang || srcLang, dstLang, function(progress, result, timestamp) {
console.log(`${file.name}: ${progress}`);
// preview
outputTextElement.value = result;
if (progress === 1) {
console.log(`translated ${file.text.length} characters for ${timestamp} ms`);
resolve(result);
}
});
});
}
async function writeTextFile(filename, dirPath, text) {
await waitMsg("write-text-file", {
filename, dirPath, text,
});
}
startTranslate();
const files = await getTextsFromDir(srcPath);
for (const file of files) {
const text = await translateFile(file);
const filename = file.name;
const dirPath = dstPath;
try {
await writeTextFile(filename, dirPath, text);
} catch(err) {
console.error(err);
Toast.create({
title: "WriteError",
message: err.message,
status: TOAST_STATUS.DANGER,
timeout: 3000
});
}
}
endTranslate();
});
});
});
})();
// page 3
// settings
(function() {
// type
document.querySelectorAll("input[name='translate-type']").forEach(function(el) {
el.addEventListener("change", function(e) {
const value = e.target.value;
translator = value;
console.log(`translate type changed: ${translator}`);
});
});
// unit
document.querySelectorAll("input[name='translate-unit']").forEach(function(el) {
el.addEventListener("change", function(e) {
const value = e.target.value;
if (value === "all") {
translate = translateByAll;
} else if (value === "paragraph") {
translate = translateByParagraph;
} else if (value === "line") {
translate = translateByLine;
} else if (value === "sentence") {
translate = translateBySentence;
} else {
translate = translateByAll;
}
console.log(`translate unit changed: ${value}`);
});
});
// clipboard
document.querySelectorAll("input[name='translate-clipboard']").forEach(function(el) {
el.addEventListener("change", function(e) {
const value = e.target.value;
clipboard = value === "1";
console.log(`translate clipboard changed: ${value}`);
});
});
})();
getMsg("init", function(err, msg, event) {
const progressElement = document.getElementById("loading").querySelector(".progress-bar");
const messageElement = document.getElementById("loading").querySelector(".message");
if (err) {
console.error(err);
progressElement.style.width = "0%";
messageElement.innerHTML = err;
return;
}
const { progress, message } = msg;
progressElement.style.width = (progress * 100) + "%";
messageElement.innerHTML = message;
if (progress === 1) {
setTimeout(function() {
document.getElementById("loading").style.display = "none";
isInitialized = true;
}, 1000);
}
});
getMsg("test", function(err, msg, event) {
console.log(`test: ${msg}`);
});