-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3code_mobile.html
More file actions
1751 lines (1516 loc) · 71.4 KB
/
Copy path3code_mobile.html
File metadata and controls
1751 lines (1516 loc) · 71.4 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="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<title>三碼中文輸入法 - 線上手機版</title>
<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=Noto+Sans+TC:wght@400;700&display=swap" rel="stylesheet">
<style>
/* ----------------------------------------------------- */
/* 樣式定義 */
/* ----------------------------------------------------- */
:root {
--primary-color: #3f51b5;
--text-color: #212121;
--background-color: #f5f6fa;
--container-bg: #ffffff;
--key-bg: #f9f9f9;
--key-active-bg: #a0a0a0;
--special-key-bg: #e5e5e5;
--border-color: #e0e0e0;
--keyboard-bg: #aeb5b5;
--deep-blue: #1a237e; /* 用於高亮符號的深藍色 */
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
-webkit-tap-highlight-color: transparent;
outline: none;
}
body {
font-family: 'Noto Sans TC', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
background-color: var(--background-color);
min-height: 100vh;
padding-bottom: 400px;
}
.search-area {
width: 100%;
max-width: 600px;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px;
padding: 8px;
background-color: var(--container-bg);
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
margin-bottom: 5px;
margin-top: 5px;
}
#search-input {
flex-grow: 0;
width: 140px;
height: 40px;
padding: 8px;
font-size: 1.0rem;
border: 1px solid var(--border-color);
border-radius: 6px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
}
#search-result {
flex-grow: 1;
min-height: 40px;
padding: 8px;
font-size: 1rem;
color: #555;
background-color: #fafafa;
border: 1px solid var(--border-color);
border-radius: 6px;
display: flex;
align-items: center;
}
#toolbar {
width: 100%;
max-width: 600px;
display: flex;
justify-content: space-around;
padding: 5px;
margin-bottom: 5px;
}
#toolbar button {
flex-grow: 1;
margin: 0 5px;
padding: 8px 15px;
font-size: 1.0rem;
border: none;
border-radius: 8px;
background-color: var(--primary-color);
color: white;
cursor: pointer;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
transition: background-color 0.2s;
}
#toolbar button:active {
background-color: #5c6bc0;
}
#app-container {
width: 100%;
max-width: 600px;
padding: 5px;
display: flex;
flex-direction: column;
align-items: center;
}
#text-output {
width: 100%;
background-color: var(--container-bg);
border: 1px solid var(--border-color);
border-radius: 8px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
margin-bottom: 5px;
padding: 8px;
height: 20vh;
font-size: 1.5rem;
line-height: 1.6;
white-space: pre-wrap;
word-break: break-word;
color: var(--text-color);
cursor: text;
overflow-y: auto;
}
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}
.cursor {
display: inline-block;
width: 0;
height: 1.2em;
vertical-align: middle;
position: relative;
animation: blink 1s step-end infinite;
}
.cursor::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 2px;
height: 100%;
background-color: var(--primary-color);
}
.composition-text {
text-decoration: underline 2px solid var(--primary-color);
font-weight: bold;
display: inline;
}
#input-area {
width: 100%;
max-width: 600px;
position: fixed;
bottom: 225px;
left: 50%;
transform: translateX(-50%);
z-index: 101;
background-color: var(--container-bg);
border-radius: 8px 8px 0 0;
box-shadow: 0 1px 0 var(--border-color), 0 -4px 10px rgba(0, 0, 0, 0.08);
overflow: hidden;
border-top: 1px solid var(--border-color);
border-left: 1px solid var(--border-color);
border-right: 1px solid var(--border-color);
}
#main-input {
width: 100%;
padding: 8px;
font-size: 1.2rem;
min-height: 40px;
line-height: 1.5;
border: none;
border-bottom: 1px solid var(--border-color);
background-color: #fafafa;
color: #757575;
white-space: nowrap;
overflow-x: auto;
}
#candidate-display {
display: flex;
align-items: center;
padding: 8px;
gap: 12px;
white-space: nowrap;
overflow-x: scroll;
-webkit-overflow-scrolling: touch;
min-height: 60px;
background-color: #fff;
box-shadow: 0 1px 0 var(--border-color);
}
#candidate-display::-webkit-scrollbar {
display: none;
}
#candidate-display span {
cursor: pointer;
padding: 5px 8px;
border-radius: 4px;
transition: all 0.2s;
user-select: none;
white-space: nowrap;
font-size: 1.4rem;
flex-shrink: 0;
text-align: center;
}
#candidate-display span:active, #candidate-display span:hover {
background-color: #e8eaf6;
}
/* Toolbar item styles */
#candidate-display span.toolbar-tool {
font-size: 1.2rem;
padding: 5px 15px;
background-color: #f0f0f0;
border: 1px solid #ddd;
}
#candidate-display span.toolbar-tool:active {
background-color: #e0e0e0;
}
#keyboard {
width: 100%;
max-width: 600px;
position: fixed;
bottom: 0;
left: 50%;
transform: translateX(-50%);
z-index: 100;
background: var(--keyboard-bg);
padding: 5px;
padding-bottom: env(safe-area-inset-bottom, 5px);
user-select: none;
}
.keyboard-row {
display: flex;
justify-content: space-around;
width: 100%;
gap: 5px;
margin-bottom: 5px;
}
.keyboard-key {
display: flex;
align-items: center;
justify-content: center;
flex-basis: 0;
flex-grow: 1;
padding: 0;
border-radius: 6px;
background-color: var(--key-bg);
border: none;
cursor: pointer;
user-select: none;
transition: background-color 0.1s;
box-shadow: 0 0 3px rgba(0, 0, 0, 0.2);
position: relative;
height: 50px;
font-size: 1.2rem;
color: var(--text-color);
min-width: 0;
}
.keyboard-key.highlighted {
background-color: var(--key-active-bg) !important;
color: white;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
transform: translateY(1px);
}
.visual-wrapper {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
line-height: 1;
}
.alt-note {
font-size: 0.6rem;
color: #757575;
position: absolute;
top: 2px;
right: 4px;
line-height: 1;
}
.kbr-key-active {
background-color: var(--primary-color) !important;
color: white;
font-weight: bold;
}
.symbol-highlight {
color: var(--deep-blue) !important;
font-weight: bold;
}
.special-key {
background-color: var(--special-key-bg);
}
.keyboard-key:active {
background-color: var(--key-active-bg);
color: white;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
transform: translateY(1px);
}
.keyboard-key > .visual-wrapper > span {
display: block;
text-align: center;
}
.emoji button {
border: none;
background: none;
font-size: 1.5rem;
padding: 0;
line-height: 1;
margin: 0;
display: inline-block;
vertical-align: middle;
}
.shift-symbol {
display: inline-block;
transform: scaleX(2);
}
/* Overlay styles */
#keyboard-overlay {
position: absolute; /* 定位會被 JS 覆蓋 */
left: 0;
top: 0;
z-index: 102; /* 確保在鍵盤上方 */
background-color: #fff;
overflow: hidden;
box-shadow: 0 1px 0 var(--border-color), 0 -4px 10px rgba(0, 0, 0, 0.08);
}
#keyboard-overlay.emoji button {
box-sizing: border-box;
text-align: center;
font-size: 1.8rem;
background-color: var(--key-bg); /* 使用鍵盤背景色 #f9f9f9 */
border: 1px solid var(--border-color); /* 添加邊框 */
border-radius: 6px; /* 圓角 */
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); /* 陰影效果 */
margin: 2px; /* 增加微小間隔,讓按鍵更明顯 */
}
/* 點擊時的反白效果 */
#keyboard-overlay.emoji button:active {
background-color: var(--key-active-bg); /* 使用點擊時的顏色 #a0a0a0 */
color: white; /* 讓表情符號更突出 */
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2);
transform: translateY(1px); /* 輕微下壓效果 */
}
#keyboard-overlay.editor {
background-color: #f0f0f0;
padding: 10px;
}
#keyboard-overlay.editor button {
position: absolute;
width: 20%;
height: 18%;
font-size: 1.2rem;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #fff;
transition: background-color 0.1s;
}
#keyboard-overlay.editor button:active {
background-color: #ddd;
}
</style>
</head>
<body>
<div class="search-area">
<input type="text" id="search-input" placeholder="在此輸入文字" maxlength="1">
<div id="search-result">查詢結果顯示在此</div>
</div>
<div id="toolbar">
<button id="copy-btn">📋 複製文字</button>
<button id="clear-btn">❌ 清空文字</button>
</div>
<div id="app-container">
<div id="text-output" spellcheck="false"></div>
</div>
<div id="input-area">
<div id="main-input">編碼顯示在此</div>
<div id="candidate-display">候選字顯示在此...</div>
</div>
<div id="keyboard">
</div>
<div id="keyboard-overlay" style="display:none;"></div>
<script>
// --- 狀態變數 ---
let currentCodemap = {};
let currentReverseCodemap = {};
let isEnglishMode = false;
let isSymbolMode = false;
let wasInEnglishModeBeforeSymbol = false;
let committedText = '';
let cursorPosition = 0;
let preferredOffset = -1;
// --- 簡繁切換狀態 ---
let isSimplifiedMode = false; // 預設為繁體模式
// 簡繁轉換表將從 ts.txt 非同步載入
let TS_MAP = null;
/**
* 從 ts.txt 檔案載入並解析簡繁轉換表。
* 檔案格式:每一行是「繁體字\t簡體字」。
*/
async function loadTSMap() {
console.log("Loading ts.txt for Traditional/Simplified conversion...");
try {
// 假設 ts.txt 位於同一個目錄
const response = await fetch('ts.txt');
if (!response.ok) {
throw new Error(`Failed to fetch ts.txt: ${response.statusText}`);
}
const text = await response.text();
const newMap = {};
// 每一行是一個轉換對:繁體字\t簡體字
const lines = text.trim().split('\n');
for (const line of lines) {
// 使用 Tab 符號 (\t) 作為分隔符
const parts = line.split('\t');
if (parts.length === 2 && parts[0].trim() && parts[1].trim()) {
const traditional = parts[0].trim();
const simplified = parts[1].trim();
newMap[traditional] = simplified;
}
}
TS_MAP = newMap; // 將解析後的結果賦值給全域變數
console.log(`T/S map loaded with ${Object.keys(TS_MAP).length} entries.`);
} catch (error) {
console.error('Error loading or parsing T/S map. T/S conversion disabled.', error);
// 載入失敗時,將 TS_MAP 設為空物件,避免後續函式出錯
TS_MAP = {};
}
}
// --- 長按相關狀態 ---
let longPressTimer = null;
let longPressCommitted = false;
const LONG_PRESS_TIMEOUT = 500;
// --- 游標 HTML 定義 ---
const CURSOR_HTML = '<span id="main-cursor" class="cursor">\u200c</span>';
const NON_BREAK_ANCHOR = '\ufeff';
// --- DOM 元素引用 ---
const textOutput = document.getElementById('text-output');
const mainInput = document.getElementById('main-input');
const candidateDisplay = document.getElementById('candidate-display');
const keyboardContainer = document.getElementById('keyboard');
const copyButton = document.getElementById('copy-btn');
const clearButton = document.getElementById('clear-btn');
const searchInput = document.getElementById('search-input');
const searchResult = document.getElementById('search-result');
const overlayDiv = document.getElementById('keyboard-overlay');
const MAX_CODE_LENGTH = 3;
const KEY_TAG="BUTTON";
// --- Key Codes ---
const KEYCODE_SHIFT=-1;
const KEYCODE_STATE=-2;
const KEYCODE_SWITCH_number=-10;
const KEYCODE_SWITCH_english=-11;
const KEYCODE_SWITCH_index0=-14;
const KEYCODE_SWITCH_index1=-15;
const KEYCODE_SWITCH_index2=-16;
const KEYCODE_DEL=0x08;
const KEYCODE_ENTER=0x0d;
const KEYCODE_SPACE=0x20;
// --- Cursor and forward delete keycodes ---
const KEYCODE_HOME=0xff50;
const KEYCODE_LEFT=0xff51;
const KEYCODE_UP=0xff52;
const KEYCODE_RIGHT=0xff53;
const KEYCODE_DOWN=0xff54;
const KEYCODE_DEL_F=0xff;
/**
* 執行簡體字轉換
*/
function convertToSimplified(text) {
// 檢查是否為簡體模式 *以及* TS_MAP 是否已載入
if (!isSimplifiedMode || !TS_MAP) {
return text; // 繁體模式或地圖未載入,不轉換
}
let simplifiedText = '';
for (const char of text) {
// 簡單的字符替換
simplifiedText += TS_MAP[char] || char;
}
return simplifiedText;
}
// --- 鍵盤配置 ---
var Keyboards = {
english: {
shifted : false,
alt : {
// 頂部兩排
'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',
// 底部一排 (zxcvbnm)
'z':'Z', 'x':'X', 'c':'C', 'v':'V', 'b':'B', 'n':'N', 'm':'M',
// 標點符號
';':';', // 全形
'\'':'〃', // 全形
',':',', // 全形
'.':'.', // 全形
'/':'/', // 全形
},
upper: {
'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':'H','k':'K','l':'L',
'z':'Z','x':'Z','c':'C','v':'V','b':'B', 'n':'N','m':'M',
},
key : [
[{v:'q',c:0},{v:'w',c:0},{v:'e',c:0},{v:'r',c:0},{v:'t',c:0},
{v:'y',c:0},{v:'u',c:0},{v:'i',c:0},{v:'o',c:0},{v:'p',c:0}],
[{v:'a',c:0},{v:'s',c:0},{v:'d',c:0},{v:'f',c:0},{v:'g',c:0},
{v:'h',c:0},{v:'j',c:0},{v:'k',c:0},{v:'l',c:0},{v:';',c:0}],
[{v:'⇪',c:KEYCODE_SHIFT,r:1.5,s:true},
{v:'z',c:0},{v:'x',c:0},{v:'c',c:0},{v:'v',c:0},{v:'b',c:0},{v:'n',c:0},{v:'m',c:0},
{v:'⌫',c:KEYCODE_DEL,r:1.5,s:true}],
[{v:'MODE',r:1.5,c:KEYCODE_STATE,s:true},
{v:'🌐',r:1.5,c:KEYCODE_SWITCH_number,s:true},
{v:' ',r:1.5,c:KEYCODE_SPACE,s:true},
{v:',',c:0},{v:'.',c:0},{v:'/',c:0},{v:'\'',c:0},
{v:'↵',r:1.5,c:KEYCODE_ENTER,s:true}]
]
},
number: {
shifted : false,
alt : {},
upper: {
"1":"`", "2":"~", "3":"_", "4":"^", "5":"±", "6":"|", "7":"[", "8":"]", "9":"{", "0":"}",
"@":"「", "#":"」", "$":"『", "%":"』", "&":"【", "*":"】", "-":"<", "+":">",
"(":"«", ")":"»", "!":"¥", "\"":"€", "'":"£", ":":"©", ";":"®", "/":"☆", "?":"★"
},
key : [
[{v:'1'},{v:'2'},{v:'3'},{v:'4'},{v:'5'},
{v:'6'},{v:'7'},{v:'8'},{v:'9'},{v:'0'}],
[{v:'@'},{v:'#'},{v:'$'},{v:'%'},{v:'&'},
{v:'*'},{v:'-'},{v:'+'},{v:'('},{v:')'}],
[{v:'⇪',c:KEYCODE_SHIFT,r:1.5,s:true},
{v:'!'},{v:'"'},{v:'\''},{v:':'},{v:';'},{v:'/'},{v:'?'},
{v:'⌫',c:KEYCODE_DEL,r:1.5,s:true}],
[{v:'MODE',r:1.5,c:KEYCODE_STATE,s:true},
{v:'🌎',r:1.5,c:KEYCODE_SWITCH_english,s:true},
{v:' ',r:2,c:KEYCODE_SPACE,s:false},
{v:',',s:false},{v:'.',s:false},
{v:'↵',r:3,c:KEYCODE_ENTER,s:true}]
]
},
number2: {
width:5,
index:0,
key0: [
[{v:'數字',c:KEYCODE_SWITCH_index0,s:true},{v:'1'},{v:'2'},{v:'3'},{v:'⌫',c:KEYCODE_DEL,s:true}],
[{v:'符號',c:KEYCODE_SWITCH_index1,s:true},{v:'4'},{v:'5'},{v:'6'},{v:'↵',c:KEYCODE_ENTER,s:true}],
[{v:'表情',c:KEYCODE_SWITCH_index2,s:true},{v:'7'},{v:'8'},{v:'9'},{v:'空格',c:KEYCODE_SPACE,s:true}],
[{v:'MODE',c:KEYCODE_STATE,s:true},{v:'/'},{v:'0'},{v:'.'},{v:'🌎',c:KEYCODE_SWITCH_english,s:true}]
],
key1: [
[{v:'數字',c:KEYCODE_SWITCH_index0,s:true},{v:'"'},{v:'\''},{v:'~'},{v:'⌫',c:KEYCODE_DEL,s:true}],
[{v:'符號',c:KEYCODE_SWITCH_index1,s:true},{v:'$'},{v:';'},{v:'@'},{v:'↵',c:KEYCODE_ENTER,s:true}],
[{v:'表情',c:KEYCODE_SWITCH_index2,s:true},{v:'!'},{v:'#'},{v:'%'},{v:'空格',c:KEYCODE_SPACE,s:true}],
[{v:'MODE',c:KEYCODE_STATE,s:true},{v:'\\'},{v:'['},{v:']'},{v:'🌎',c:KEYCODE_SWITCH_english,s:true}]
],
key2: [
[{v:'數字',c:KEYCODE_SWITCH_index0,s:true},{v:'😄'},{v:'😂'},{v:'😃'},{v:'⌫',c:KEYCODE_DEL,s:true}],
[{v:'符號',c:KEYCODE_SWITCH_index1,s:true},{v:'😅'},{v:'😆'},{v:'😇'},{v:'↵',c:KEYCODE_ENTER,s:true}],
[{v:'表情',c:KEYCODE_SWITCH_index2,s:true},{v:'😉'},{v:'😊'},{v:'😋'},{v:'空格',c:KEYCODE_SPACE,s:true}],
[{v:'MODE',c:KEYCODE_STATE,s:true},{v:'😌'},{v:'😍'},{v:'😏'},{v:'🌎',c:KEYCODE_SWITCH_english,s:true}]
]
}
};
Keyboards.number2.keys=[Keyboards.number2.key0,Keyboards.number2.key1,Keyboards.number2.key2];
let currentLayout = Keyboards.english;
function getCurrentLayoutKey() {
if (currentLayout === Keyboards.number2) {
return currentLayout.keys[currentLayout.index];
}
return currentLayout.key;
}
/**
* 鍵盤渲染函數
*/
function drawKeyboard() {
const layout = currentLayout;
const layoutKey = getCurrentLayoutKey();
const totalWidth = keyboardContainer.clientWidth;
const layoutWidth = layout.width || 10;
const placeHolderWidth = totalWidth / layoutWidth;
keyboardContainer.innerHTML = '';
layoutKey.forEach((row) => {
const rowDiv = document.createElement('div');
rowDiv.className = 'keyboard-row';
row.forEach((keyData) => {
const key = document.createElement(KEY_TAG);
key.className = 'keyboard-key';
let keyChar = keyData.v;
let code = keyData.c || 0;
const ratio = keyData.r || 1;
const keyWidth = placeHolderWidth * ratio;
let displayChar = keyChar;
let isSymbolShifted = false;
if (currentLayout === Keyboards.number && code === 0) {
if (layout.shifted && layout.upper && layout.upper[keyChar]) {
displayChar = layout.upper[keyChar];
isSymbolShifted = true;
}
}
// --- 1. 處理 QWERTY 鍵的顯示邏輯 (字母和標點) ---
if (currentLayout === Keyboards.english && code === 0) {
if (keyChar.match(/^[a-z]$/)) {
if (!isEnglishMode) {
displayChar = keyChar.toUpperCase();
} else {
if (layout.shifted) {
displayChar = keyChar.toUpperCase();
} else {
displayChar = keyChar;
}
}
} else if (keyChar.match(/^[';,./]$/)) {
if (!isEnglishMode) {
displayChar = keyChar;
} else {
displayChar = keyChar;
}
}
}
// --- 2. 處理功能鍵文字 (MODE, SHIFT 等,覆蓋 QWERTY 邏輯) ---
if (code === KEYCODE_STATE) {
if (currentLayout === Keyboards.english) {
displayChar = isEnglishMode ? 'En' : '三碼';
} else {
displayChar = wasInEnglishModeBeforeSymbol ? 'En' : '三碼';
}
} else if (code === KEYCODE_SHIFT) {
displayChar = '⇧';
}
if (keyData.s) {
key.classList.add('special-key');
}
key.style.width = keyWidth + 'px';
key.dataset.code = code;
key.dataset.label = keyChar;
if (keyData.r && ratio > 1) {
key.style.flexGrow = ratio;
}
// --- 1. 創建內部元素 ---
const wrapper = document.createElement('span');
wrapper.className = 'visual-wrapper';
const innerSymbolSpan = document.createElement('span');
innerSymbolSpan.innerHTML = displayChar;
wrapper.appendChild(innerSymbolSpan);
// --- 2. 處理 Shift 鍵視覺 ---
let applyScale = false;
if (code === KEYCODE_SHIFT) {
innerSymbolSpan.innerHTML = layout.shifted ? '⬆' : '⇧';
if (layout.shifted) {
innerSymbolSpan.classList.add('symbol-highlight');
}
applyScale = true;
}
if (applyScale) {
innerSymbolSpan.classList.add('shift-symbol');
}
if (isSymbolShifted) {
innerSymbolSpan.classList.add('symbol-highlight');
}
// --- 3. 處理其他功能鍵的藍底高亮 (number2 的數字/符號/表情切換鍵) ---
if (layout === Keyboards.number2) {
if (layout.index === 0 && keyData.v === '數字') {
key.classList.add('kbr-key-active');
} else if (layout.index === 1 && keyData.v === '符號') {
key.classList.add('kbr-key-active');
} else if (layout.index === 2 && keyData.v === '表情') {
key.classList.add('kbr-key-active');
}
}
// Alt Note (輔助碼)
if (layout.alt && layout.alt[keyChar]) {
const altNote = document.createElement('div');
altNote.className = 'alt-note';
altNote.innerHTML = layout.alt[keyChar];
wrapper.appendChild(altNote);
}
key.appendChild(wrapper);
rowDiv.appendChild(key);
});
keyboardContainer.appendChild(rowDiv);
});
bindKeyEvents();
if (overlayDiv.dataset.show) {
// 重新繪製鍵盤後,如果浮層開啟,則需要重新定位並渲染浮層內容
overlay(overlayDiv.className, parseInt(overlayDiv.dataset.show));
} else if (overlayDiv.style.display !== 'none') {
overlayDiv.style.display = 'none';
overlayDiv.innerHTML = '';
}
}
// --- 浮層處理函數 ---
/**
* 處理表情符號/編輯器的浮層顯示與隱藏
*/
function overlay(type, show) {
const div = overlayDiv;
const isShowing = div.dataset.show;
if (isShowing) {
div.style.display = 'none';
div.innerHTML = '';
delete div.dataset.show;
// 如果是嘗試關閉當前開啟的浮層,則直接返回
if (isShowing == show && div.className == type) return;
}
if (show === false) return;
const keyboard = document.getElementById('keyboard');
const rect = keyboard.getBoundingClientRect();
const width = rect.width;
const height = rect.height;
div.dataset.show = show;
div.style.display = 'block';
div.className = type;
div.style.position = 'fixed';
div.style.left = rect.left + 'px';
div.style.top = rect.top + 'px';
div.style.width = width + 'px';
div.style.height = height + 'px';
const div2 = document.createElement('DIV');
div2.style.width = width + 'px';
div2.style.height = height + 'px';
div2.style.overflowY = 'auto';
if (type === "emoji") {
div2.style.display = 'flex';
div2.style.flexWrap = 'wrap';
div2.style.alignContent = 'flex-start';
div2.style.padding = '3px';
}
div.innerHTML = '';
div.appendChild(div2);
if (type === "emoji") {
const emojis = [[
"😀","😁","😂","😃","😄","😅","😆","😇","😈","😉",
"😊","😋","😌","😍","😎","😏","😐","😑","😒","😓",
"😔","😕","😖","😗","😘","😙","😚","😛","😜","😝",
"😞","😟","😠","😡","😢","😣","😤","😥","😦","😧",
"😨","😩","😪","😫","😬","😭","😮","😯","😰","😱",
"😲","😳","😴","😵","😶","😷","😸","😹","😺","😻",
"😼","😽","😾","😿","🙀","🙁","🙂","🙃","🙄","🙅",
"🙆","🙇","🙈","🙉","🙊","🙋","🙌","🙍","🙎","🙏"
],[
"☀","☁","☎","☔","☕","☝","☠","☯","♿","⚒",
"⚓","⚔","⚡","⚪","⚫","⚽","⚾","⛄","⛅","⛔",
"⛪","⛲","⛳","⛵","⛺","⛽","✅","✊","✋","✌",
"❄","❌","❤","🆒","🆗","🆘","🆙","🆚","🌻","🌼",
"🌽","🌿","🍁","🍄","🍅","🍆","🍉","🍌","🍍","🍓",
"🍔","🍕","🍚","🍜","🍟","🍦","🍫","🍬","🍭","🍴",
"🎁","🎂","🎄","🎅","🎈","🎓","👌","👍","👎","👏",
"🎨","🎯","🎲","🏀","🏃","🏅","🏆","🏐","🏓","🏟",
"🏧","🐀","🐁","🐂","🐃","🐄","🐅","🐆","🐇","🐈",
"🐉","🐊","🐋","🐌","蛇","🐎","🐏","🐐","🐑","🐒",
"🐓","🐔","🐕","🐖","🐗","🐘","🐙","🐚","🐛","🐜",
"🐝","🐞","🐟","🐠","🐢","🐧","👣","👫","💊","💋"
]];
const emojiSet = emojis[show] || emojis[0];
const rowLength = 10;
const displayRows = 4;
const keyHeight = height / displayRows;
for (let i = 0; i < emojiSet.length; i++) {
const one = document.createElement('button');
one.textContent = emojiSet[i];
one.style.width = `calc(${100 / rowLength}% - 4px)`;
one.style.height = keyHeight - 4 + "px";
one.style.fontSize = '1.8rem';
div2.appendChild(one);
}
div2.addEventListener('click', function(e) {
if (e.target.tagName !== 'BUTTON') return;
const text = e.target.textContent;
if (text && text.length > 0 && text !== ' ')
commitText(text);
});
} else if (type === "editor") {
const actions = [
{ text: "←", left: "5%", top: "30%", click: () => handleFunctionKey(KEYCODE_LEFT, null) },
{ text: "↑", left: "20%", top: "10%", click: () => handleFunctionKey(KEYCODE_UP, null) },
{ text: "→", left: "35%", top: "30%", click: () => handleFunctionKey(KEYCODE_RIGHT, null) },
{ text: "↓", left: "20%", top: "50%", click: () => handleFunctionKey(KEYCODE_DOWN, null) },
{ text: "|←", left: "5%", top: "70%", click: () => handleFunctionKey(KEYCODE_HOME, null) },
{ text: "刪除", left: "35%", top: "70%", click: () => handleFunctionKey(KEYCODE_DEL_F, null) },
{ text: "全選", left: "70%", top: "5%", click: () => console.log('Editor action: selectAll (Not implemented)') },
{ text: "複製", left: "70%", top: "27%", click: () => copyTextToClipboard() },
{ text: "剪下", left: "70%", top: "49%", click: () => console.log('Editor action: cut (Not implemented)') },
{ text: "貼上", left: "70%", top: "71%", click: () => console.log('Editor action: paste (Not implemented)') }
];
for (let i = 0; i < actions.length; i++) {
const one = document.createElement('button');
one.textContent = actions[i].text;
one.style.position = 'absolute';
one.style.left = actions[i].left;
one.style.top = actions[i].top;
one.style.width = '20%';
one.style.height = '18%';
one.addEventListener('click', actions[i].click);
div2.appendChild(one);
}
}
}
function emoji(show) {
overlay("emoji", show);
}
function editor(show) {
overlay("editor", 0);
}
// --- 工具列邏輯 ---
function showToolbar() {
candidateDisplay.innerHTML = '';
// **CHANGE: Replaced '🎤' with '簡/繁' toggle**
const toolbarItems = [
{title: isSimplifiedMode ? "简" : "繁", tool:"1"}, // 簡繁切換鍵
{title:"😊",tool:"2"}, // Emoji 1
{title:"☀",tool:"3"}, // Emoji 2
{title:"🌀",tool:"4"}, // Placeholder
{title:"⇄",tool:"5"}, // Editor
{title:"❎",tool:"9"} // Hide/Close (切回 QWERTY)
];
const fragment = document.createDocumentFragment();
toolbarItems.forEach(item => {
const span = document.createElement('span');
span.innerHTML = item.title;
span.dataset.tool = item.tool;
span.classList.add("toolbar-tool");
// 綁定點擊事件
span.onclick = (e) => handleToolbarClick(e.currentTarget);
fragment.appendChild(span);
});
candidateDisplay.appendChild(fragment);
}
/**
* 處理工具列點擊事件 (已修正簡繁切換與浮層衝突)
*/
function handleToolbarClick(target) {
const tool = target.dataset.tool;
const isOverlayActive = !!overlayDiv.dataset.show; // 檢查是否有浮層正在開啟
// **修正**:如果浮層是開啟的,且點擊的是「簡繁切換鍵」(tool 1),則只切換模式並立即返回,不影響浮層狀態
if (isOverlayActive && tool === "1") {
isSimplifiedMode = !isSimplifiedMode;
target.innerHTML = isSimplifiedMode ? "简" : "繁"; // 立即更新按鈕文字
console.log("TS mode switched while overlay active. Overlay preserved.");
// 這裡不需要 showCandidates,因為浮層開啟時,下方的 candidateDisplay 應該保持清空或顯示工具列
// 但我們不希望它觸發重繪 (showCandidates) 導致浮層閃爍,因此直接 return
return;
}
if (tool === "1") {
// Simplified/Traditional Mode Toggle
isSimplifiedMode = !isSimplifiedMode;
showCandidates(); // Redraw toolbar to update "简" / "繁" label
console.log("Mode switched to:", isSimplifiedMode ? "Simplified" : "Traditional");
} else if (tool === "2") {
overlay("emoji", 0);
} else if (tool === "3") {
overlay("emoji", 1);
} else if (tool === "4") {
console.log('QR Code action triggered (Not implemented).');
} else if (tool === "5") {
overlay("editor", 0);
} else if (tool === "9") {
// 模擬 App.hide(),切換回 QWERTY (英文模式/三碼模式)
handleFunctionKey(KEYCODE_SWITCH_english, '🌐');
}
}
/**
* 處理長按事件:提交輔助字符
*/
function handleLongPress(keyElement, altChar) {
longPressCommitted = true;
keyElement.classList.remove('highlighted');
// 提交輔助字元 (altChar 是全形)
commitText(altChar);
}
/**
* 處理短按事件:執行編碼或普通字符輸入
*/