-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1202 lines (1075 loc) · 45.3 KB
/
Copy pathindex.html
File metadata and controls
1202 lines (1075 loc) · 45.3 KB
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="zh-Hant"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="google-site-verification" content="n6IXOw_C8MkE0aGJvjbgorhKOjqAZrTPnVv4uln8i6o" />
<title>三碼中文輸入法 - 查碼、練打網站</title>
<meta name="description" content="可以在網路上直接以鍵盤及網頁虛擬鍵盤使用「三碼中文輸入法」查碼、練打的網站。一字最多三碼,取決按鍵字元,符號輸入容易,速度潛力無限。您只需準備一段想要輸打的文字,然後貼在「查碼區」,即可出現每個文字的編碼,然後在英文模式下,直接使用鍵盤輸入對應的編碼,即可如同真的使用「三碼」在打字喔!是一個快速學習「三碼中文輸入法」的最佳網站!">
<link rel="preconnect" href="https://fonts.googleapis.com/">
<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin="">
<link href="./3code2_files/css2" rel="stylesheet">
<style>
/* 基礎樣式與變數定義 */
:root {
--primary-color: #3f51b5;
--primary-light: #5c6bc0;
--text-color: #212121;
--background-color: #f5f6fa;
--container-bg: #ffffff;
--border-color: #e0e0e0;
--box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
--key-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
--unified-font-size: 20px;
}
body {
font-family: 'Noto Sans TC', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: var(--background-color);
padding: 20px;
color: var(--text-color);
}
.container {
width: 95%;
max-width: 800px;
background-color: var(--container-bg);
border-radius: 12px;
box-shadow: var(--box-shadow);
padding: 15px;
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
/* 主標題及其圖片的容器 */
.main-title-container {
display: flex;
align-items: center;
justify-content: center; /* 使標題和圖片整體居中 */
margin-bottom: 5px;
}
h1 {
color: var(--primary-color);
margin: 0; /* 移除 h1 自身的 margin */
font-weight: 700;
font-size: 28px;
}
/* 新圖片樣式 */
.logo {
height: 35px; /* 調整圖片高度,使其與文字大致對齊 */
width: auto; /* 保持圖片比例 */
margin-left: 10px; /* 在標題和圖片之間添加間距 */
}
h2 {
font-size: 20px;
color: var(--primary-color);
margin: 15px 0 5px 0;
font-weight: 700;
align-self: flex-start;
}
.header-section {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
gap: 10px;
}
.download-section, .mode-switcher {
display: flex;
justify-content: center;
align-items: center; /* 確保 flex 項目垂直置中 */
gap: 10px;
flex-wrap: wrap;
}
.download-section h2, .mode-switcher h2 {
font-size: 16px;
color: var(--primary-light);
margin: 0;
white-space: nowrap;
line-height: 1.5;
}
.download-section ul {
list-style: none;
padding: 0;
display: flex;
justify-content: center;
flex-wrap: wrap;
margin: 0;
gap: 8px;
}
.download-section li {
font-size: 16px;
line-height: 1.5;
}
.download-section a, .mode-switcher label {
color: var(--primary-color);
text-decoration: none;
font-weight: 700;
transition: color 0.2s ease-in-out;
font-size: 16px;
line-height: 1.5;
cursor: pointer;
}
.download-section a:hover {
color: var(--primary-light);
text-decoration: underline;
}
/* 模式切換器樣式 */
.mode-switcher label {
display: flex;
align-items: center;
gap: 5px;
}
.mode-switcher input[type="radio"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
width: 16px;
height: 16px;
border: 2px solid var(--primary-color);
border-radius: 50%;
transition: all 0.2s ease-in-out;
cursor: pointer;
position: relative;
}
.mode-switcher input[type="radio"]:checked {
background-color: var(--primary-color);
border-color: var(--primary-color);
}
.mode-switcher input[type="radio"]:checked::after {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 6px;
height: 6px;
border-radius: 50%;
background-color: white;
}
/* Facebook 標誌圖片樣式 */
.facebook-logo {
width: 20px; /* 調整圖片大小 */
height: 20px;
vertical-align: middle; /* 確保圖片與文字垂直對齊 */
margin-right: 0px; /* 在圖片和文字之間添加一點間距 */
flex-shrink: 0; /* 防止圖片被壓縮 */
}
/* Facebook 連結樣式 */
.facebook-link {
font-size: 16px; /* 與下載連結區的字體大小一致 */
line-height: 1.5; /* 確保行高一致 */
color: var(--primary-color);
text-decoration: none;
font-weight: 700;
transition: color 0.2s ease-in-out;
display: flex; /* 啟用 flex 佈局,以便與圖片對齊 */
align-items: center; /* 確保圖片和文字垂直置中 */
}
.facebook-link:hover {
color: var(--primary-light);
text-decoration: underline;
}
/* 輸入與顯示區塊 */
.text-output-area,
.candidate-area,
#main-input,
#phrase-input {
width: 100%;
border-radius: 8px;
border: 1px solid var(--border-color);
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
transition: all 0.2s ease-in-out;
box-sizing: border-box;
font-size: var(--unified-font-size);
}
/* 統一所有主要欄位的內距 */
.text-output-area,
#main-input,
#phrase-input,
.candidate-area {
padding: 8px;
}
/* 調整輸入框高度 */
.text-output-area,
#phrase-input {
min-height: 50px;
height: 50px;
background-color: #fafafa;
resize: none;
}
.input-area {
width: 100%;
display: flex;
flex-direction: column;
gap: 8px;
}
#main-input {
height: 40px;
background-color: #e8eaf6;
}
/* 候選字區塊樣式 */
.candidate-area {
min-height: 60px;
display: flex;
flex-wrap: wrap; /* 允許內容換行 */
justify-content: flex-start; /* 讓項目靠左對齊 */
align-items: center;
padding: 8px;
gap: 6px; /* 縮小欄位間距 */
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
background-color: #e8eaf6;
}
.candidate-area::-webkit-scrollbar {
display: none; /* 隱藏 Chrome, Safari 的捲軸 */
}
.candidate-area span {
cursor: pointer;
padding: 5px 4px; /* 縮小內距 */
border: 1px solid transparent;
border-radius: 4px;
transition: all 0.2s;
user-select: none;
white-space: nowrap;
font-size: 18px; /* 調整字形大小 */
flex-shrink: 0;
text-align: center;
}
.candidate-area span:hover {
background-color: #e8eaf6;
border-color: #b3b9d8;
}
#mode-display {
font-size: 16px;
color: #757575;
align-self: flex-start;
}
/* 新增的查碼練習區域樣式 */
.lookup-section {
width: 100%;
margin-top: 0;
display: flex;
flex-direction: column;
gap: 10px;
padding-top: 0;
}
.lookup-section h2 {
font-size: 20px;
color: var(--primary-color);
margin: 0;
font-weight: 700;
}
#phrase-input {
width: 100%;
min-height: 50px;
height: 50px;
resize: none;
font-size: var(--unified-font-size);
padding: 8px;
}
#encoding-results {
width: 100%;
display: grid;
grid-template-columns: repeat(10, 1fr);
gap: 8px;
padding: 8px 0;
overflow-x: auto;
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
#encoding-results::-webkit-scrollbar {
display: none;
}
.char-encoding-column {
display: flex;
flex-direction: column;
align-items: flex-start;
width: 70px; /* 固定寬度,足夠容納四碼 */
min-height: 120px; /* 確保至少有足夠的高度 */
flex-shrink: 0;
padding: 5px;
border: 1px solid var(--border-color);
border-radius: 8px;
background-color: #e8eaf6;
box-sizing: border-box;
gap: 2px;
}
.char-encoding-column.highlight-char {
border: 2px solid var(--primary-color);
background-color: #d1d9ff;
}
.char-encoding-column .char-label {
font-weight: bold;
font-size: 24px;
margin-bottom: 5px;
line-height: 1;
align-self: center; /* 讓漢字本身維持置中 */
min-height: 24px; /* 確保漢字區域的高度 */
}
.char-encoding-column .code-label {
font-family: monospace;
white-space: nowrap;
line-height: 1.2;
text-align: left; /* 編碼文字左對齊 */
min-height: 16px; /* 確保編碼行的高度 */
}
/* 新增的顏色樣式 */
.correct-char {
color: green;
}
.incorrect-char {
color: red;
}
/* 鍵盤樣式 */
.keyboard-container {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
margin-top: 5px;
}
.key-row {
display: flex;
justify-content: center;
width: 100%;
gap: 5px;
}
.key {
flex: 1;
padding: 8px 0;
border-radius: 6px;
background-color: #e8eaf6;
font-size: 18px;
text-align: center;
cursor: pointer;
user-select: none;
transition: all 0.1s ease-out;
box-shadow: var(--key-shadow);
border: 1px solid var(--border-color);
}
.key.function-key {
flex: 1.5;
background-color: #c5cae9;
font-size: 16px;
}
.key.space-key {
flex: 4;
background-color: #c5cae9;
}
.key.page-key {
flex: 0.5;
background-color: #c5cae9;
font-size: 16px;
}
/* 按鍵互動效果 */
.key:hover {
background-color: #b3b9d8;
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}
.key:active,
.key.active {
background-color: #7986cb;
color: #fff;
transform: translateY(0);
box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
}
/* 版權宣告樣式 */
.copyright {
margin-top: 20px;
font-size: 14px;
color: #757575;
text-align: center;
}
.copyright a {
color: #757575;
text-decoration: none;
font-weight: bold;
transition: color 0.2s ease-in-out;
}
.copyright a:hover {
color: var(--primary-light);
text-decoration: underline;
}
</style>
</head>
<body>
<div class="container">
<div class="main-title-container">
<h1>三碼中文輸入法 - 查碼、練打網站</h1>
<a href="https://www.facebook.com/unbook.tw/" target="_blank"><img src="logo.png" alt="logo" class="logo"></a>
</div>
<div class="header-section">
<div class="download-section">
<h2>檔案下載區:</h2>
<ul>
<li><a href="3code.pdf" download="" target="_blank">[輸入法說明檔]</a></li>
<li><a href="3code.zip" download="" target="_blank">[輸入法壓縮檔]</a></li>
<li><a href="3code.txt" download="" target="_blank">[常用字碼檔]</a></li>
<li><a href="3code2.txt" download="" target="_blank">[次常用字碼檔]</a></li>
<li><a href="3code.cin" download="" target="_blank">[CIN字碼檔]</a></li>
<li><a href="https://lime-ime.github.io/limeime/" download="" target="_blank">[萊姆輸入法]</a></li>
</ul>
</div>
<div class="mode-switcher">
<h2>輸入法模式:</h2>
<label>
<input type="radio" name="input_mode" value="primary" checked="">
三碼 (常用字)
</label>
<label>
<input type="radio" name="input_mode" value="secondary">
三碼次常 (次常用字)
</label>
<img src="facebook_logo.png" alt="Facebook Logo" class="facebook-logo">
<a href="https://www.facebook.com/groups/756940847492021" target="_blank" class="facebook-link">臉書社團:三碼中文輸入法【無書自通】</a>
</div>
</div>
<div class="lookup-section">
<textarea id="phrase-input" placeholder="查碼區:在此貼上一段文字查碼!"></textarea>
<div id="encoding-results"></div>
</div>
<textarea class="text-output-area" id="text-output" placeholder="練打區:點此輸入編碼練習打字!"></textarea>
<div class="input-area">
<input type="text" id="main-input" placeholder="編碼顯示在此" readonly="">
<div id="candidate-display" class="candidate-area"></div>
</div>
<div id="mode-display">模式:三碼</div>
<div class="keyboard-container">
<div class="key-row">
<div class="key" data-code="1">1</div>
<div class="key" data-code="2">2</div>
<div class="key" data-code="3">3</div>
<div class="key" data-code="4">4</div>
<div class="key" data-code="5">5</div>
<div class="key" data-code="6">6</div>
<div class="key" data-code="7">7</div>
<div class="key" data-code="8">8</div>
<div class="key" data-code="9">9</div>
<div class="key" data-code="0">0</div>
</div>
<div class="key-row">
<div class="key" data-code="q">Q</div>
<div class="key" data-code="w">W</div>
<div class="key" data-code="e">E</div>
<div class="key" data-code="r">R</div>
<div class="key" data-code="t">T</div>
<div class="key" data-code="y">Y</div>
<div class="key" data-code="u">U</div>
<div class="key" data-code="i">I</div>
<div class="key" data-code="o">O</div>
<div class="key" data-code="p">P</div>
</div>
<div class="key-row">
<div class="key" data-code="a">A</div>
<div class="key" data-code="s">S</div>
<div class="key" data-code="d">D</div>
<div class="key" data-code="f">F</div>
<div class="key" data-code="g">G</div>
<div class="key" data-code="h">H</div>
<div class="key" data-code="j">J</div>
<div class="key" data-code="k">K</div>
<div class="key" data-code="l">L</div>
<div class="key" data-code=";">;</div>
<div class="key" data-code="'">〃</div>
</div>
<div class="key-row">
<div class="key" data-code="z">Z</div>
<div class="key" data-code="x">X</div>
<div class="key" data-code="c">C</div>
<div class="key" data-code="v">V</div>
<div class="key" data-code="b">B</div>
<div class="key" data-code="n">N</div>
<div class="key" data-code="m">M</div>
<div class="key" data-code=",">,</div>
<div class="key" data-code=".">.</div>
<div class="key" data-code="/">/</div>
</div>
<div class="key-row">
<div class="key function-key" data-code="shift">Shift</div>
<div class="key space-key" data-code=" ">空白鍵</div>
<div class="key function-key" data-code="enter">Enter</div>
<div class="key function-key" data-code="backspace">刪除鍵</div>
<div class="key page-key" data-code="<"><</div>
<div class="key page-key" data-code=">">></div>
</div>
</div>
</div>
<div class="copyright">
Copying © 2026 <a href="https://www.facebook.com/groups/756940847492021" target="_blank">三碼中文輸入法</a> by <a href="https://www.facebook.com/unbook.tw/" target="_blank">無書自通</a>:「一字最多三碼,取決按键字元,符號輸入容易,速度潛力無限。」
</div>
<script>
// --- 資料處理與解析 ---
let primaryCodemap = null;
let primaryReverseCodemap = null;
let secondaryCodemap = null;
let secondaryReverseCodemap = null;
let currentCodemap = {};
let currentReverseCodemap = {};
const codeKeys = "abcdefghijklmnopqrstuvwxyz;'./,";
const keyname_map_upper = {
'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D', 'e': 'E', 'f': 'F', 'g': 'G',
'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L', 'm': 'M', 'n': 'N',
'o': 'O', 'p': 'P', 'q': 'Q', 'r': 'R', 's': 'S', 't': 'T', 'u': 'U',
'v': 'V', 'w': 'W', 'x': 'X', 'y': 'Y', 'z': 'Z',
'1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9', '0': '0',
"'": "〃", ';': ';', ',': ',', '.': '.', '/': '/', '<': '<', '>': '>', ' ': '空', 'escape': 'Esc',
'shift': 'Shift', 'enter': 'Enter', 'backspace': '刪除鍵',
};
const keyname_map_lower = {
'a': 'a', 'b': 'b', 'c': 'c', 'd': 'd', 'e': 'e', 'f': 'f', 'g': 'g',
'h': 'h', 'i': 'i', 'j': 'j', 'k': 'k', 'l': 'l', 'm': 'm', 'n': 'n',
'o': 'o', 'p': 'p', 'q': 'q', 'r': 'r', 's': 's', 't': 't', 'u': 'u',
'v': 'v', 'w': 'w', 'x': 'x', 'y': 'y', 'z': 'z',
'1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9', '0': '0',
"'": "'", ';': ';', ',': ',', '.': '.', '/': '/', '<': '<', '>': '>', ' ': '空', 'escape': 'Esc',
'shift': 'Shift', 'enter': 'Enter', 'backspace': '刪除鍵',
};
const keyname_map_chinese_shifted = {
'1': '!', '2': '@', '3': '#', '4': '$', '5': '%', '6': '^', '7': '&', '8': '*', '9': '(', '0': ')',
'q': 'Q', 'w': 'W', 'e': 'E', 'r': 'R', 't': 'T', 'y': 'Y', 'u': 'U', 'i': 'I', 'o': 'O', 'p': 'P',
'a': 'A', 's': 'S', 'd': 'D', 'f': 'F', 'g': 'G', 'h': 'H', 'j': 'J', 'k': 'K', 'l': 'L',
';': ':', "'": '"',
'z': 'Z', 'x': 'X', 'c': 'C', 'v': 'V', 'b': 'B', 'n': 'N', 'm': 'M',
',': '<', '.': '>', '/': '?',
};
const keyname_map_english_shifted = {
'1': '!', '2': '@', '3': '#', '4': '$', '5': '%', '6': '^', '7': '&', '8': '*', '9': '(', '0': ')',
'q': 'Q', 'w': 'W', 'e': 'E', 'r': 'R', 't': 'T', 'y': 'Y', 'u': 'U', 'i': 'I', 'o': 'O', 'p': 'P',
'a': 'A', 's': 'S', 'd': 'D', 'f': 'F', 'g': 'G', 'h': 'H', 'j': 'J', 'k': 'K', 'l': 'L',
';': ':', "'": '"',
'z': 'Z', 'x': 'X', 'c': 'C', 'v': 'V', 'b': 'B', 'n': 'N', 'm': 'M',
',': '<', '.': '>', '/': '?',
};
const fullWidthDigits = {
'1': '1', '2': '2', '3': '3', '4': '4', '5': '5',
'6': '6', '7': '7', '8': '8', '9': '9', '0': '0'
};
const textOutput = document.getElementById('text-output');
const mainInput = document.getElementById('main-input');
const candidateDisplay = document.getElementById('candidate-display');
const phraseInput = document.getElementById('phrase-input');
const encodingResults = document.getElementById('encoding-results');
const keyboardContainer = document.querySelector('.keyboard-container');
const modeDisplay = document.getElementById('mode-display');
const modeRadioButtons = document.querySelectorAll('input[name="input_mode"]');
const MAX_CODE_LENGTH = 3;
const SHIFT_HOLD_THRESHOLD = 300; // 300毫秒
let currentCode = '';
let currentCandidates = [];
let currentPage = 0;
const candidatesPerPage = 10;
let isEnglishMode = false;
let isShiftDown = false;
let shiftTimer = null;
let isLongPressActive = false;
let currentInputMethod = 'primary';
// 函式:格式化碼表資料為物件
function parseCinData(data) {
const newCodemap = {};
const newReverseCodemap = {};
const lines = data.trim().split('\n');
lines.forEach(line => {
const parts = line.split('\t');
if (parts.length >= 2 && !line.startsWith('#')) {
const code = parts[0].trim();
const char = parts[1].trim();
if (code && char) {
if (!newCodemap[code]) {
newCodemap[code] = [];
}
newCodemap[code].push(char);
if (!newReverseCodemap[char]) {
newReverseCodemap[char] = [];
}
if (!newReverseCodemap[char].some(item => item.code === code)) {
newReverseCodemap[char].push({
code: code,
index: newCodemap[code].length - 1
});
}
}
}
});
return { newCodemap, newReverseCodemap };
}
async function loadCodemap(fileName) {
try {
const response = await fetch(fileName);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const text = await response.text();
return parseCinData(text);
} catch (error) {
console.error(`載入 ${fileName} 時發生錯誤:`, error);
alert(`載入 ${fileName} 失敗,請確認檔案是否存在於相同目錄下並檢查檔案格式。`);
return { newCodemap: {}, newReverseCodemap: {} };
}
}
async function switchInputMethod(mode) {
currentInputMethod = mode;
if (mode === 'primary') {
if (!primaryCodemap) {
const { newCodemap, newReverseCodemap } = await loadCodemap('./3codes.txt');
primaryCodemap = newCodemap;
primaryReverseCodemap = newReverseCodemap;
}
currentCodemap = primaryCodemap;
currentReverseCodemap = primaryReverseCodemap;
} else if (mode === 'secondary') {
if (!secondaryCodemap) {
const { newCodemap, newReverseCodemap } = await loadCodemap('./3codes2.txt');
secondaryCodemap = newCodemap;
secondaryReverseCodemap = newReverseCodemap;
}
currentCodemap = secondaryCodemap;
currentReverseCodemap = secondaryReverseCodemap;
}
resetInputState();
updateModeDisplay();
updatePhraseLookup();
}
modeRadioButtons.forEach(radio => {
radio.addEventListener('change', (event) => {
switchInputMethod(event.target.value);
});
});
function insertTextAtCursor(element, text) {
const start = element.selectionStart;
const end = element.selectionEnd;
element.value = element.value.substring(0, start) + text + element.value.substring(end);
element.selectionStart = element.selectionEnd = start + text.length;
element.focus();
}
function deleteTextAtCursor(element) {
const start = element.selectionStart;
if (start > 0) {
const end = element.selectionEnd;
let text = element.value;
element.value = text.substring(0, start - 1) + text.substring(end);
element.selectionStart = element.selectionEnd = start - 1;
element.focus();
}
}
function handleShiftedVirtualInput(code) {
let outputChar = '';
if (isEnglishMode) {
outputChar = keyname_map_english_shifted[code];
} else {
outputChar = keyname_map_chinese_shifted[code];
}
if (outputChar) {
insertTextAtCursor(textOutput, outputChar);
return true;
}
return false;
}
keyboardContainer.addEventListener('click', (event) => {
const key = event.target.closest('.key');
if (!key) return;
const code = key.dataset.code;
if (code === 'shift') {
if (isLongPressActive) {
isLongPressActive = false;
updateKeyboardDisplay();
} else {
isEnglishMode = !isEnglishMode;
updateModeDisplay();
updateKeyboardDisplay();
resetInputState();
}
return;
}
if (isLongPressActive) {
if (handleShiftedVirtualInput(code)) {
updatePhraseLookup();
return;
}
}
if (code === '<') {
handlePageChange('previous');
} else if (code === '>') {
handlePageChange('next');
} else if (code === 'backspace') {
deleteTextAtCursor(textOutput);
updatePhraseLookup();
return;
} else if (code === 'enter') {
insertTextAtCursor(textOutput, '\n');
updatePhraseLookup();
return;
}
if (isEnglishMode) {
handleEnglishInput(code);
} else {
handleChineseInput(code);
}
});
function handleChineseInput(code) {
if (code.match(/^[1-90]$/)) {
if (currentCandidates.length > 1) { // 數字選字要確保有重碼字
const num = code === '0' ? 10 : parseInt(code, 10);
selectCandidate(num - 1);
} else {
insertTextAtCursor(textOutput, code);
updatePhraseLookup();
}
return;
}
if (code === 'backspace') {
if (currentCode.length > 0) {
currentCode = currentCode.slice(0, -1);
updateStateAndDisplay();
} else {
deleteTextAtCursor(textOutput);
updatePhraseLookup();
}
return;
}
if (code === 'enter') {
insertTextAtCursor(textOutput, '\n');
updatePhraseLookup();
return;
}
if (code === 'escape') {
resetInputState();
updatePhraseLookup();
return;
}
if (code === ' ') {
if (currentCandidates.length > 0) {
selectCandidate(0);
} else {
insertTextAtCursor(textOutput, ' ');
updatePhraseLookup();
}
return;
}
const inputChar = code.toLowerCase();
if (inputChar.match(/^[a-z;'./,]$/)) {
if (currentCode.length >= MAX_CODE_LENGTH && currentCandidates.length > 0) {
selectCandidate(0);
currentCode = inputChar;
} else {
currentCode += inputChar;
}
updateStateAndDisplay();
}
}
function handleEnglishInput(char) {
if (char === 'backspace') {
deleteTextAtCursor(textOutput);
updatePhraseLookup();
return;
}
if (char === 'enter') {
insertTextAtCursor(textOutput, '\n');
updatePhraseLookup();
return;
}
if (char === 'escape') {
resetInputState();
updatePhraseLookup();
return;
}
if (char === ' ') {
insertTextAtCursor(textOutput, ' ');
updatePhraseLookup();
return;
}
if (char.length === 1) {
insertTextAtCursor(textOutput, char);
updatePhraseLookup();
}
}
function handlePageChange(direction) {
if (currentCandidates.length === 0) return;
if (direction === 'previous') {
if (currentPage > 0) {
currentPage--;
showCandidates();
}
} else if (direction === 'next') {
const totalPages = Math.ceil(currentCandidates.length / candidatesPerPage);
if (currentPage + 1 < totalPages) {
currentPage++;
showCandidates();
}
}
}
function updateStateAndDisplay() {
mainInput.value = formatCode(currentCode);
currentPage = 0;
currentCandidates = currentCodemap[currentCode] || [];
if (currentCode.length === MAX_CODE_LENGTH) {
if (currentCandidates.length === 1) {
insertTextAtCursor(textOutput, currentCandidates[0]);
resetInputState();
updatePhraseLookup();
return;
} else if (currentCandidates.length === 0) {
resetInputState();
updatePhraseLookup();
return;
}
}
showCandidates();
updatePhraseLookup();
}
function showCandidates() {
candidateDisplay.innerHTML = '';
const start = currentPage * candidatesPerPage;
const end = start + candidatesPerPage;
const displayedCandidates = currentCandidates.slice(start, end);
displayedCandidates.forEach((candidate, index) => {
const span = document.createElement('span');
const digit = (index === 9) ? '0' : (index + 1).toString();
const displayIndex = fullWidthDigits[digit] || digit;
span.textContent = `${displayIndex}. ${candidate}`;
span.style.cursor = 'pointer';
span.style.userSelect = 'none';
span.onclick = () => selectCandidate(index);
candidateDisplay.appendChild(span);
});
if (currentCandidates.length > candidatesPerPage) {
const navSpan = document.createElement('span');
navSpan.textContent = ` (第 ${currentPage + 1}/${Math.ceil(currentCandidates.length / candidatesPerPage)} 頁)`;
navSpan.classList.add('page-info');
candidateDisplay.appendChild(navSpan);
}
}
function selectCandidate(index) {
const selectedCandidate = currentCandidates[currentPage * candidatesPerPage + index];
if (selectedCandidate) {
insertTextAtCursor(textOutput, selectedCandidate);
resetInputState();
updatePhraseLookup();
}
}
function resetInputState() {
currentCode = '';
currentCandidates = [];
currentPage = 0;
mainInput.value = '';
showCandidates();
}
function formatCode(code) {
let displayString = '';
for (const char of code) {
displayString += keyname_map_upper[char] || char.toUpperCase();
}
return displayString;
}
function updateKeyboardDisplay() {
let map = isEnglishMode ? keyname_map_lower : keyname_map_upper;
if (isLongPressActive) {
map = isEnglishMode ? keyname_map_english_shifted : keyname_map_chinese_shifted;
}
document.querySelectorAll('.key[data-code]').forEach(keyElement => {
const code = keyElement.dataset.code;
if (map[code]) {
keyElement.textContent = map[code];
}
});
}
function updateModeDisplay() {
const currentModeText = currentInputMethod === 'primary' ? '三碼' : '三碼次常';
modeDisplay.textContent = isEnglishMode ? '模式:英文' : `模式:${currentModeText}`;
}
function getKeyCode(event) {
const code = event.code;
if (code.startsWith('Key')) {
return code.replace('Key', '').toLowerCase();
}
if (code.startsWith('Digit')) {
return code.replace('Digit', '');
}
switch (code) {
case 'Backspace': return 'backspace';
case 'Enter': return 'enter';
case 'Escape': return 'escape';
case 'ShiftLeft':
case 'ShiftRight': return 'shift';
case 'Space': return ' ';
case 'Comma': return ',';
case 'Period': return '.';
case 'Slash': return '/';
case 'Semicolon': return ';';
case 'Quote': return "'";
default: return null;
}
}