claude-opencode-viewer 2.6.4 → 2.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/cov.js +32 -9
- package/index-pc.html +2207 -0
- package/package.json +1 -1
- package/server.js +38 -8
package/index-pc.html
ADDED
|
@@ -0,0 +1,2207 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
|
|
6
|
+
<title>Claude OpenCode Viewer [PC]</title>
|
|
7
|
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@xterm/xterm@5.5.0/css/xterm.min.css">
|
|
8
|
+
<style>
|
|
9
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
10
|
+
html, body { margin: 0; padding: 0; overflow: hidden; }
|
|
11
|
+
|
|
12
|
+
/* 参考 cc-viewer 的 App.jsx 行 1319: 移动端容器使用 100vw/100vh */
|
|
13
|
+
#layout {
|
|
14
|
+
width: 100vw;
|
|
15
|
+
height: 100vh;
|
|
16
|
+
display: flex;
|
|
17
|
+
flex-direction: column;
|
|
18
|
+
background: #000;
|
|
19
|
+
overflow: hidden;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/* 参考 cc-viewer 的 App.jsx 行 1320-1335: 顶部工具栏 */
|
|
23
|
+
#header {
|
|
24
|
+
padding: 10px 12px;
|
|
25
|
+
background: #111;
|
|
26
|
+
border-bottom: 1px solid #222;
|
|
27
|
+
display: flex;
|
|
28
|
+
align-items: center;
|
|
29
|
+
justify-content: space-between;
|
|
30
|
+
flex-shrink: 0;
|
|
31
|
+
height: 40px;
|
|
32
|
+
gap: 12px;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
#session-history-bar {
|
|
36
|
+
display: none;
|
|
37
|
+
position: absolute;
|
|
38
|
+
top: 0;
|
|
39
|
+
left: 0;
|
|
40
|
+
right: 0;
|
|
41
|
+
bottom: 0;
|
|
42
|
+
background: #0a0a0a;
|
|
43
|
+
z-index: 1000;
|
|
44
|
+
flex-direction: column;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#session-history-bar.visible {
|
|
48
|
+
display: flex;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#session-list-view {
|
|
52
|
+
display: flex;
|
|
53
|
+
flex-direction: column;
|
|
54
|
+
flex: 1;
|
|
55
|
+
min-height: 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#session-list-view.hidden {
|
|
59
|
+
display: none;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
#session-history-header {
|
|
63
|
+
display: flex;
|
|
64
|
+
align-items: center;
|
|
65
|
+
justify-content: space-between;
|
|
66
|
+
padding: 12px 16px;
|
|
67
|
+
background: #111;
|
|
68
|
+
border-bottom: 1px solid #222;
|
|
69
|
+
flex-shrink: 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#session-history-title {
|
|
73
|
+
font-size: 14px;
|
|
74
|
+
color: #ddd;
|
|
75
|
+
font-weight: 600;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
#session-history-actions {
|
|
79
|
+
display: flex;
|
|
80
|
+
gap: 8px;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#session-list-container {
|
|
84
|
+
flex: 1;
|
|
85
|
+
overflow-y: auto;
|
|
86
|
+
padding: 12px;
|
|
87
|
+
-webkit-overflow-scrolling: touch;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#session-detail-view {
|
|
91
|
+
display: none;
|
|
92
|
+
flex-direction: column;
|
|
93
|
+
flex: 1;
|
|
94
|
+
min-height: 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#session-detail-view.visible {
|
|
98
|
+
display: flex;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#session-detail-header {
|
|
102
|
+
padding: 12px 16px;
|
|
103
|
+
background: #0d0d0d;
|
|
104
|
+
border-bottom: 1px solid #222;
|
|
105
|
+
flex-shrink: 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
#session-detail-title {
|
|
109
|
+
font-size: 14px;
|
|
110
|
+
color: #ddd;
|
|
111
|
+
font-weight: 500;
|
|
112
|
+
margin-bottom: 4px;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#session-detail-meta {
|
|
116
|
+
font-size: 11px;
|
|
117
|
+
color: #888;
|
|
118
|
+
display: flex;
|
|
119
|
+
gap: 12px;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
#session-detail-content {
|
|
123
|
+
flex: 1;
|
|
124
|
+
overflow-y: auto;
|
|
125
|
+
padding: 16px;
|
|
126
|
+
-webkit-overflow-scrolling: touch;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
.message-item {
|
|
130
|
+
margin-bottom: 20px;
|
|
131
|
+
display: flex;
|
|
132
|
+
gap: 12px;
|
|
133
|
+
align-items: flex-start;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/* User 消息:左对齐 */
|
|
137
|
+
.message-user {
|
|
138
|
+
justify-content: flex-start;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/* Assistant 消息:右对齐 */
|
|
142
|
+
.message-assistant {
|
|
143
|
+
justify-content: flex-end;
|
|
144
|
+
flex-direction: row-reverse;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.message-avatar {
|
|
148
|
+
flex-shrink: 0;
|
|
149
|
+
width: 32px;
|
|
150
|
+
height: 32px;
|
|
151
|
+
border-radius: 50%;
|
|
152
|
+
background: #2a4a7c;
|
|
153
|
+
display: flex;
|
|
154
|
+
align-items: center;
|
|
155
|
+
justify-content: center;
|
|
156
|
+
font-size: 14px;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
.message-assistant .message-avatar {
|
|
160
|
+
background: #1a5a3a;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.message-content {
|
|
164
|
+
flex: 1;
|
|
165
|
+
min-width: 0;
|
|
166
|
+
max-width: 80%;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.message-header {
|
|
170
|
+
display: flex;
|
|
171
|
+
align-items: center;
|
|
172
|
+
gap: 8px;
|
|
173
|
+
margin-bottom: 6px;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/* User 的标题左对齐 */
|
|
177
|
+
.message-user .message-header {
|
|
178
|
+
justify-content: flex-start;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/* Assistant 的标题右对齐 */
|
|
182
|
+
.message-assistant .message-header {
|
|
183
|
+
justify-content: flex-end;
|
|
184
|
+
flex-direction: row-reverse;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
.message-role {
|
|
188
|
+
font-size: 11px;
|
|
189
|
+
color: #888;
|
|
190
|
+
font-weight: 600;
|
|
191
|
+
text-transform: uppercase;
|
|
192
|
+
letter-spacing: 0.5px;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
.message-text {
|
|
196
|
+
color: #ddd;
|
|
197
|
+
font-size: 13px;
|
|
198
|
+
line-height: 1.6;
|
|
199
|
+
white-space: pre-wrap;
|
|
200
|
+
word-break: break-word;
|
|
201
|
+
background: #141414;
|
|
202
|
+
padding: 12px;
|
|
203
|
+
border-radius: 8px;
|
|
204
|
+
border: 1px solid #222;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/* User 消息气泡 */
|
|
208
|
+
.message-user .message-text {
|
|
209
|
+
background: #1a2332;
|
|
210
|
+
border-color: #2a4a7c;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/* Assistant 消息气泡 */
|
|
214
|
+
.message-assistant .message-text {
|
|
215
|
+
background: #1a2e1a;
|
|
216
|
+
border-color: #2a5a3a;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
.message-tool-call {
|
|
220
|
+
margin-top: 8px;
|
|
221
|
+
padding: 8px 12px;
|
|
222
|
+
background: #1a1a0a;
|
|
223
|
+
border: 1px solid #333;
|
|
224
|
+
border-radius: 6px;
|
|
225
|
+
font-size: 12px;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
.message-tool-name {
|
|
229
|
+
color: #f0ad4e;
|
|
230
|
+
font-weight: 600;
|
|
231
|
+
display: flex;
|
|
232
|
+
align-items: center;
|
|
233
|
+
gap: 6px;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
.message-tool-result {
|
|
237
|
+
margin-top: 6px;
|
|
238
|
+
padding: 8px;
|
|
239
|
+
background: #0a0a0a;
|
|
240
|
+
border-radius: 4px;
|
|
241
|
+
font-size: 11px;
|
|
242
|
+
color: #999;
|
|
243
|
+
max-height: 100px;
|
|
244
|
+
overflow-y: auto;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
.message-empty {
|
|
248
|
+
text-align: center;
|
|
249
|
+
padding: 40px 20px;
|
|
250
|
+
color: #666;
|
|
251
|
+
font-size: 13px;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#session-list {
|
|
255
|
+
display: flex;
|
|
256
|
+
flex-direction: column;
|
|
257
|
+
gap: 6px;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
.session-item {
|
|
261
|
+
display: flex;
|
|
262
|
+
align-items: center;
|
|
263
|
+
gap: 8px;
|
|
264
|
+
padding: 8px 12px;
|
|
265
|
+
background: #1a1a1a;
|
|
266
|
+
border: 1px solid #333;
|
|
267
|
+
border-radius: 6px;
|
|
268
|
+
cursor: pointer;
|
|
269
|
+
transition: all 0.15s;
|
|
270
|
+
min-width: 0;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
.session-item:hover {
|
|
274
|
+
background: #252525;
|
|
275
|
+
border-color: #555;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
.session-item.active {
|
|
279
|
+
background: #2a4a7c;
|
|
280
|
+
border-color: #4a8cff;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
.session-icon {
|
|
284
|
+
flex-shrink: 0;
|
|
285
|
+
width: 8px;
|
|
286
|
+
height: 8px;
|
|
287
|
+
border-radius: 50%;
|
|
288
|
+
background: #666;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
.session-item.active .session-icon {
|
|
292
|
+
background: #4a8cff;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
.session-info {
|
|
296
|
+
flex: 1;
|
|
297
|
+
min-width: 0;
|
|
298
|
+
display: flex;
|
|
299
|
+
flex-direction: column;
|
|
300
|
+
gap: 2px;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
.session-title {
|
|
304
|
+
font-size: 13px;
|
|
305
|
+
color: #ddd;
|
|
306
|
+
font-weight: 500;
|
|
307
|
+
line-height: 1.4;
|
|
308
|
+
display: -webkit-box;
|
|
309
|
+
-webkit-line-clamp: 2;
|
|
310
|
+
-webkit-box-orient: vertical;
|
|
311
|
+
overflow: hidden;
|
|
312
|
+
text-overflow: ellipsis;
|
|
313
|
+
word-break: break-word;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
.session-item.active .session-title {
|
|
317
|
+
color: #fff;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
.session-meta {
|
|
321
|
+
font-size: 11px;
|
|
322
|
+
color: #888;
|
|
323
|
+
display: flex;
|
|
324
|
+
gap: 8px;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
.session-time {
|
|
328
|
+
flex-shrink: 0;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
.session-dir {
|
|
332
|
+
overflow: hidden;
|
|
333
|
+
text-overflow: ellipsis;
|
|
334
|
+
white-space: nowrap;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
.history-toggle-btn {
|
|
338
|
+
background: none;
|
|
339
|
+
border: 1px solid #333;
|
|
340
|
+
color: #aaa;
|
|
341
|
+
padding: 4px 10px;
|
|
342
|
+
font-size: 12px;
|
|
343
|
+
cursor: pointer;
|
|
344
|
+
border-radius: 4px;
|
|
345
|
+
display: flex;
|
|
346
|
+
align-items: center;
|
|
347
|
+
gap: 4px;
|
|
348
|
+
flex-shrink: 0;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
.history-toggle-btn:hover {
|
|
352
|
+
background: #2a2a2a;
|
|
353
|
+
color: #ddd;
|
|
354
|
+
border-color: #555;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
.history-toggle-btn svg {
|
|
358
|
+
width: 14px;
|
|
359
|
+
height: 14px;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
.session-loading {
|
|
363
|
+
text-align: center;
|
|
364
|
+
padding: 16px;
|
|
365
|
+
color: #888;
|
|
366
|
+
font-size: 12px;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
.session-empty {
|
|
370
|
+
text-align: center;
|
|
371
|
+
padding: 16px;
|
|
372
|
+
color: #666;
|
|
373
|
+
font-size: 12px;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
.session-delete-btn {
|
|
377
|
+
flex-shrink: 0;
|
|
378
|
+
width: 28px;
|
|
379
|
+
height: 28px;
|
|
380
|
+
border: none;
|
|
381
|
+
background: none;
|
|
382
|
+
color: #f85149;
|
|
383
|
+
font-size: 14px;
|
|
384
|
+
cursor: pointer;
|
|
385
|
+
border-radius: 50%;
|
|
386
|
+
display: flex;
|
|
387
|
+
align-items: center;
|
|
388
|
+
justify-content: center;
|
|
389
|
+
transition: all 0.15s;
|
|
390
|
+
-webkit-tap-highlight-color: transparent;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
.session-delete-btn:hover {
|
|
394
|
+
background: rgba(248, 81, 73, 0.15);
|
|
395
|
+
color: #f85149;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
.session-delete-btn:active {
|
|
399
|
+
background: rgba(248, 81, 73, 0.3);
|
|
400
|
+
color: #f85149;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
.session-restore-hint {
|
|
404
|
+
margin-top: 8px;
|
|
405
|
+
padding: 8px 12px;
|
|
406
|
+
background: #1a1a1a;
|
|
407
|
+
border: 1px solid #333;
|
|
408
|
+
border-radius: 6px;
|
|
409
|
+
font-size: 11px;
|
|
410
|
+
color: #888;
|
|
411
|
+
text-align: center;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
.session-restore-hint code {
|
|
415
|
+
color: #4a8cff;
|
|
416
|
+
background: #0a0a0a;
|
|
417
|
+
padding: 2px 6px;
|
|
418
|
+
border-radius: 3px;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
#mode-switcher {
|
|
422
|
+
display: flex;
|
|
423
|
+
gap: 4px;
|
|
424
|
+
align-items: center;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
#mode-label {
|
|
428
|
+
font-size: 12px;
|
|
429
|
+
color: #666;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
#mode-select {
|
|
433
|
+
background: #1a1a1a;
|
|
434
|
+
color: #d4d4d4;
|
|
435
|
+
border: 1px solid #333;
|
|
436
|
+
border-radius: 4px;
|
|
437
|
+
padding: 4px 24px 4px 8px;
|
|
438
|
+
font-size: 12px;
|
|
439
|
+
cursor: pointer;
|
|
440
|
+
-webkit-appearance: none;
|
|
441
|
+
appearance: none;
|
|
442
|
+
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='8' height='8' viewBox='0 0 10 10'%3E%3Cpath fill='%23888' d='M5 7L1 3h8z'/%3E%3C/svg%3E");
|
|
443
|
+
background-repeat: no-repeat;
|
|
444
|
+
background-position: right 6px center;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/* 参考 cc-viewer 的 App.jsx 行 1425: 内容区使用 flex: 1 + relative + overflow: hidden */
|
|
448
|
+
#content {
|
|
449
|
+
flex: 1;
|
|
450
|
+
position: relative;
|
|
451
|
+
overflow: hidden;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/* 参考 cc-viewer 的 TerminalPanel.jsx: 终端容器样式 */
|
|
455
|
+
#terminal-container {
|
|
456
|
+
height: 100%;
|
|
457
|
+
display: flex;
|
|
458
|
+
flex-direction: column;
|
|
459
|
+
background: #0a0a0a;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
#terminal {
|
|
463
|
+
flex: 1;
|
|
464
|
+
overflow: hidden;
|
|
465
|
+
touch-action: none;
|
|
466
|
+
overscroll-behavior: contain;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
#terminal.transitioning {
|
|
470
|
+
opacity: 0.3;
|
|
471
|
+
transition: opacity 0.3s ease;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
|
|
475
|
+
/* 选择模式:原位文本层 */
|
|
476
|
+
#terminal.select-mode .xterm-screen {
|
|
477
|
+
visibility: hidden;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
#select-text-layer {
|
|
481
|
+
display: none;
|
|
482
|
+
position: absolute;
|
|
483
|
+
top: 0; left: 0; right: 0; bottom: 0;
|
|
484
|
+
overflow-y: auto;
|
|
485
|
+
-webkit-overflow-scrolling: touch;
|
|
486
|
+
background: #0a0a0a;
|
|
487
|
+
padding: 4px 8px;
|
|
488
|
+
touch-action: auto;
|
|
489
|
+
z-index: 10;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
#select-text-layer.visible {
|
|
493
|
+
display: block;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
#select-hint {
|
|
497
|
+
position: sticky;
|
|
498
|
+
top: 0;
|
|
499
|
+
background: rgba(30,30,30,0.95);
|
|
500
|
+
color: #888;
|
|
501
|
+
font-size: 11px;
|
|
502
|
+
text-align: center;
|
|
503
|
+
padding: 6px 0;
|
|
504
|
+
border-bottom: 1px solid #333;
|
|
505
|
+
z-index: 1;
|
|
506
|
+
-webkit-user-select: none;
|
|
507
|
+
user-select: none;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
#select-text-layer pre {
|
|
511
|
+
margin: 0;
|
|
512
|
+
color: #d4d4d4;
|
|
513
|
+
font-family: Menlo, Monaco, "Courier New", monospace;
|
|
514
|
+
font-size: 11px;
|
|
515
|
+
line-height: 1.4;
|
|
516
|
+
white-space: pre-wrap;
|
|
517
|
+
word-break: break-all;
|
|
518
|
+
-webkit-user-select: text;
|
|
519
|
+
user-select: text;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
#select-mode-close {
|
|
523
|
+
position: absolute;
|
|
524
|
+
top: 6px;
|
|
525
|
+
right: 6px;
|
|
526
|
+
z-index: 20;
|
|
527
|
+
display: none;
|
|
528
|
+
background: rgba(50,50,50,0.9);
|
|
529
|
+
border: 1px solid #555;
|
|
530
|
+
color: #ccc;
|
|
531
|
+
width: 28px;
|
|
532
|
+
height: 28px;
|
|
533
|
+
border-radius: 50%;
|
|
534
|
+
font-size: 14px;
|
|
535
|
+
line-height: 26px;
|
|
536
|
+
text-align: center;
|
|
537
|
+
cursor: pointer;
|
|
538
|
+
-webkit-user-select: none;
|
|
539
|
+
user-select: none;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/* 复制成功提示 */
|
|
543
|
+
#copy-toast {
|
|
544
|
+
display: none;
|
|
545
|
+
position: fixed;
|
|
546
|
+
top: 50%;
|
|
547
|
+
left: 50%;
|
|
548
|
+
transform: translate(-50%, -50%);
|
|
549
|
+
background: rgba(40, 167, 69, 0.9);
|
|
550
|
+
color: #fff;
|
|
551
|
+
padding: 10px 24px;
|
|
552
|
+
border-radius: 8px;
|
|
553
|
+
font-size: 14px;
|
|
554
|
+
z-index: 9999;
|
|
555
|
+
pointer-events: none;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
#copy-toast.show {
|
|
559
|
+
display: block;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
#copy-popup {
|
|
563
|
+
display: none;
|
|
564
|
+
position: fixed;
|
|
565
|
+
top: 50%;
|
|
566
|
+
left: 50%;
|
|
567
|
+
transform: translate(-50%, -50%);
|
|
568
|
+
background: #1a1a1a;
|
|
569
|
+
border: 1px solid #444;
|
|
570
|
+
border-radius: 8px;
|
|
571
|
+
padding: 16px;
|
|
572
|
+
z-index: 10000;
|
|
573
|
+
max-width: 80vw;
|
|
574
|
+
max-height: 60vh;
|
|
575
|
+
}
|
|
576
|
+
#copy-popup.show {
|
|
577
|
+
display: flex;
|
|
578
|
+
flex-direction: column;
|
|
579
|
+
gap: 10px;
|
|
580
|
+
}
|
|
581
|
+
#copy-popup textarea {
|
|
582
|
+
width: 500px;
|
|
583
|
+
max-width: 70vw;
|
|
584
|
+
height: 120px;
|
|
585
|
+
background: #0a0a0a;
|
|
586
|
+
color: #ccc;
|
|
587
|
+
border: 1px solid #333;
|
|
588
|
+
border-radius: 4px;
|
|
589
|
+
padding: 8px;
|
|
590
|
+
font-family: Menlo, Monaco, monospace;
|
|
591
|
+
font-size: 12px;
|
|
592
|
+
resize: vertical;
|
|
593
|
+
}
|
|
594
|
+
#copy-popup .popup-actions {
|
|
595
|
+
display: flex;
|
|
596
|
+
justify-content: flex-end;
|
|
597
|
+
gap: 8px;
|
|
598
|
+
}
|
|
599
|
+
#copy-popup button {
|
|
600
|
+
padding: 6px 16px;
|
|
601
|
+
border: none;
|
|
602
|
+
border-radius: 4px;
|
|
603
|
+
cursor: pointer;
|
|
604
|
+
font-size: 13px;
|
|
605
|
+
}
|
|
606
|
+
#copy-popup .btn-copy {
|
|
607
|
+
background: #2563eb;
|
|
608
|
+
color: #fff;
|
|
609
|
+
}
|
|
610
|
+
#copy-popup .btn-close {
|
|
611
|
+
background: #333;
|
|
612
|
+
color: #ccc;
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/* Git Diff 面板 */
|
|
616
|
+
#git-diff-bar {
|
|
617
|
+
display: none;
|
|
618
|
+
position: absolute;
|
|
619
|
+
top: 0;
|
|
620
|
+
left: 0;
|
|
621
|
+
right: 0;
|
|
622
|
+
bottom: 0;
|
|
623
|
+
background: #0a0a0a;
|
|
624
|
+
z-index: 1000;
|
|
625
|
+
flex-direction: column;
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
#git-diff-bar.visible {
|
|
629
|
+
display: flex;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
#git-diff-header {
|
|
633
|
+
display: flex;
|
|
634
|
+
align-items: center;
|
|
635
|
+
justify-content: space-between;
|
|
636
|
+
padding: 12px 16px;
|
|
637
|
+
background: #111;
|
|
638
|
+
border-bottom: 1px solid #222;
|
|
639
|
+
flex-shrink: 0;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
#git-diff-title {
|
|
643
|
+
font-size: 14px;
|
|
644
|
+
color: #ddd;
|
|
645
|
+
font-weight: 600;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
.git-diff-file-list {
|
|
649
|
+
height: 250px;
|
|
650
|
+
flex-shrink: 0;
|
|
651
|
+
overflow-y: auto;
|
|
652
|
+
border-bottom: 1px solid #2a2a2a;
|
|
653
|
+
-webkit-overflow-scrolling: touch;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
.git-diff-file-item {
|
|
657
|
+
display: flex;
|
|
658
|
+
align-items: center;
|
|
659
|
+
padding: 6px 12px;
|
|
660
|
+
cursor: pointer;
|
|
661
|
+
color: #ccc;
|
|
662
|
+
font-size: 13px;
|
|
663
|
+
gap: 8px;
|
|
664
|
+
white-space: nowrap;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
.git-diff-file-item:hover {
|
|
668
|
+
background: #1a1a1a;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
.git-diff-file-item.active {
|
|
672
|
+
background: rgba(74, 158, 255, 0.12);
|
|
673
|
+
color: #fff;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
.git-diff-file-status {
|
|
677
|
+
width: 18px;
|
|
678
|
+
flex-shrink: 0;
|
|
679
|
+
font-size: 11px;
|
|
680
|
+
font-weight: 700;
|
|
681
|
+
text-align: center;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
.git-diff-file-name {
|
|
685
|
+
overflow: hidden;
|
|
686
|
+
text-overflow: ellipsis;
|
|
687
|
+
flex: 1;
|
|
688
|
+
font-family: Menlo, Monaco, monospace;
|
|
689
|
+
font-size: 12px;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
.git-diff-content-area {
|
|
693
|
+
flex: 1;
|
|
694
|
+
display: flex;
|
|
695
|
+
flex-direction: column;
|
|
696
|
+
min-height: 0;
|
|
697
|
+
overflow: hidden;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
.git-diff-content-header {
|
|
701
|
+
display: flex;
|
|
702
|
+
align-items: center;
|
|
703
|
+
gap: 10px;
|
|
704
|
+
padding: 8px 12px;
|
|
705
|
+
border-bottom: 1px solid #2a2a2a;
|
|
706
|
+
background: #111;
|
|
707
|
+
flex-shrink: 0;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
.git-diff-content-path {
|
|
711
|
+
font-size: 12px;
|
|
712
|
+
color: #ccc;
|
|
713
|
+
font-family: Menlo, Monaco, monospace;
|
|
714
|
+
overflow: hidden;
|
|
715
|
+
text-overflow: ellipsis;
|
|
716
|
+
white-space: nowrap;
|
|
717
|
+
flex: 1;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
.git-diff-badge {
|
|
721
|
+
padding: 2px 8px;
|
|
722
|
+
background: #2a2a2a;
|
|
723
|
+
border: 1px solid #444;
|
|
724
|
+
border-radius: 4px;
|
|
725
|
+
font-size: 10px;
|
|
726
|
+
font-weight: 600;
|
|
727
|
+
color: #e2c08d;
|
|
728
|
+
letter-spacing: 0.5px;
|
|
729
|
+
flex-shrink: 0;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
.git-diff-content-scroll {
|
|
733
|
+
flex: 1;
|
|
734
|
+
overflow: auto;
|
|
735
|
+
-webkit-overflow-scrolling: touch;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
.git-diff-placeholder {
|
|
739
|
+
flex: 1;
|
|
740
|
+
display: flex;
|
|
741
|
+
flex-direction: column;
|
|
742
|
+
align-items: center;
|
|
743
|
+
justify-content: center;
|
|
744
|
+
gap: 12px;
|
|
745
|
+
color: #333;
|
|
746
|
+
font-size: 13px;
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
.git-diff-loading {
|
|
750
|
+
text-align: center;
|
|
751
|
+
padding: 16px;
|
|
752
|
+
color: #888;
|
|
753
|
+
font-size: 12px;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
.git-diff-error {
|
|
757
|
+
color: #ff6b6b;
|
|
758
|
+
font-size: 12px;
|
|
759
|
+
padding: 16px 12px;
|
|
760
|
+
text-align: center;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/* Unified diff 行 */
|
|
764
|
+
.diff-table {
|
|
765
|
+
width: 100%;
|
|
766
|
+
border-collapse: collapse;
|
|
767
|
+
font-family: Menlo, Monaco, 'Courier New', monospace;
|
|
768
|
+
font-size: 12px;
|
|
769
|
+
line-height: 1.5;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
.diff-line {
|
|
773
|
+
border: none;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
.diff-line-num {
|
|
777
|
+
width: 28px;
|
|
778
|
+
min-width: 28px;
|
|
779
|
+
padding: 0 3px;
|
|
780
|
+
text-align: right;
|
|
781
|
+
color: #555;
|
|
782
|
+
font-size: 11px;
|
|
783
|
+
user-select: none;
|
|
784
|
+
-webkit-user-select: none;
|
|
785
|
+
vertical-align: top;
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
.diff-line-content {
|
|
789
|
+
padding: 0 8px;
|
|
790
|
+
white-space: pre-wrap;
|
|
791
|
+
word-break: break-all;
|
|
792
|
+
color: #d4d4d4;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
.diff-line-add {
|
|
796
|
+
background: rgba(35, 134, 54, 0.2);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
.diff-line-add .diff-line-content {
|
|
800
|
+
color: #7ee787;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
.diff-line-del {
|
|
804
|
+
background: rgba(248, 81, 73, 0.2);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
.diff-line-del .diff-line-content {
|
|
808
|
+
color: #ffa198;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
.diff-line-hunk {
|
|
812
|
+
background: rgba(56, 139, 253, 0.1);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
.diff-line-hunk .diff-line-content {
|
|
816
|
+
color: #79c0ff;
|
|
817
|
+
font-style: italic;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
.diff-file-count {
|
|
821
|
+
font-size: 10px;
|
|
822
|
+
color: #555;
|
|
823
|
+
background: #1a1a1a;
|
|
824
|
+
padding: 1px 6px;
|
|
825
|
+
border-radius: 8px;
|
|
826
|
+
}
|
|
827
|
+
</style>
|
|
828
|
+
</head>
|
|
829
|
+
<body>
|
|
830
|
+
<!-- 参考 cc-viewer 的 App.jsx 行 1315-1607: 完整的移动端布局结构 -->
|
|
831
|
+
<div id="layout">
|
|
832
|
+
<div id="header">
|
|
833
|
+
<div style="display: flex; gap: 8px; align-items: center;">
|
|
834
|
+
<button class="history-toggle-btn" id="new-session-btn" style="color:#73c991; border-color:#2a5a3a;">
|
|
835
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
836
|
+
<line x1="12" y1="5" x2="12" y2="19"></line>
|
|
837
|
+
<line x1="5" y1="12" x2="19" y2="12"></line>
|
|
838
|
+
</svg>
|
|
839
|
+
<span>新会话</span>
|
|
840
|
+
</button>
|
|
841
|
+
<button class="history-toggle-btn" id="history-toggle" style="color:#79c0ff; border-color:#2a4a7c;">
|
|
842
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
843
|
+
<circle cx="12" cy="12" r="10"></circle>
|
|
844
|
+
<polyline points="12 6 12 12 16 14"></polyline>
|
|
845
|
+
</svg>
|
|
846
|
+
<span>历史</span>
|
|
847
|
+
</button>
|
|
848
|
+
<button class="history-toggle-btn" id="diff-toggle" style="color:#e2c08d; border-color:#5a4a2a;">
|
|
849
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
850
|
+
<line x1="6" y1="3" x2="6" y2="15"></line>
|
|
851
|
+
<circle cx="18" cy="6" r="3"></circle>
|
|
852
|
+
<circle cx="6" cy="18" r="3"></circle>
|
|
853
|
+
<path d="M18 9a9 9 0 0 1-9 9"></path>
|
|
854
|
+
</svg>
|
|
855
|
+
<span>Diff</span>
|
|
856
|
+
</button>
|
|
857
|
+
</div>
|
|
858
|
+
<div id="mode-switcher">
|
|
859
|
+
<span id="mode-label"></span>
|
|
860
|
+
<select id="mode-select">
|
|
861
|
+
<option value="opencode">OpenCode</option>
|
|
862
|
+
<option value="claude">Claude</option>
|
|
863
|
+
</select>
|
|
864
|
+
</div>
|
|
865
|
+
</div>
|
|
866
|
+
|
|
867
|
+
<!-- 会话历史栏 -->
|
|
868
|
+
<div id="session-history-bar">
|
|
869
|
+
<!-- 会话列表视图 -->
|
|
870
|
+
<div id="session-list-view">
|
|
871
|
+
<div id="session-history-header">
|
|
872
|
+
<div id="session-history-title">历史会话</div>
|
|
873
|
+
<div id="session-history-actions">
|
|
874
|
+
<button class="history-toggle-btn" id="refresh-sessions">
|
|
875
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
876
|
+
<polyline points="23 4 23 10 17 10"></polyline>
|
|
877
|
+
<path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path>
|
|
878
|
+
</svg>
|
|
879
|
+
<span>刷新</span>
|
|
880
|
+
</button>
|
|
881
|
+
<button class="history-toggle-btn" id="close-history">
|
|
882
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
883
|
+
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
884
|
+
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
885
|
+
</svg>
|
|
886
|
+
<span>返回</span>
|
|
887
|
+
</button>
|
|
888
|
+
</div>
|
|
889
|
+
</div>
|
|
890
|
+
<div id="session-list-container">
|
|
891
|
+
<div id="session-list">
|
|
892
|
+
<div class="session-loading">加载历史会话...</div>
|
|
893
|
+
</div>
|
|
894
|
+
</div>
|
|
895
|
+
</div>
|
|
896
|
+
|
|
897
|
+
<!-- 会话详情视图 -->
|
|
898
|
+
<div id="session-detail-view">
|
|
899
|
+
<div id="session-history-header">
|
|
900
|
+
<div id="session-history-title">会话详情</div>
|
|
901
|
+
<div id="session-history-actions">
|
|
902
|
+
<button class="history-toggle-btn" id="restore-session" style="background: #1a5a3a; border-color: #2a7a4a;">
|
|
903
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
904
|
+
<polyline points="1 4 1 10 7 10"></polyline>
|
|
905
|
+
<path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"></path>
|
|
906
|
+
</svg>
|
|
907
|
+
<span>恢复会话</span>
|
|
908
|
+
</button>
|
|
909
|
+
<button class="history-toggle-btn" id="back-to-list">
|
|
910
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
911
|
+
<line x1="19" y1="12" x2="5" y2="12"></line>
|
|
912
|
+
<polyline points="12 19 5 12 12 5"></polyline>
|
|
913
|
+
</svg>
|
|
914
|
+
<span>返回列表</span>
|
|
915
|
+
</button>
|
|
916
|
+
</div>
|
|
917
|
+
</div>
|
|
918
|
+
<div id="session-detail-header">
|
|
919
|
+
<div id="session-detail-title">会话标题</div>
|
|
920
|
+
<div id="session-detail-meta">
|
|
921
|
+
<span id="session-detail-time">时间</span>
|
|
922
|
+
<span id="session-detail-dir">目录</span>
|
|
923
|
+
</div>
|
|
924
|
+
</div>
|
|
925
|
+
<div id="session-detail-content">
|
|
926
|
+
<div class="message-empty">加载中...</div>
|
|
927
|
+
</div>
|
|
928
|
+
</div>
|
|
929
|
+
</div>
|
|
930
|
+
|
|
931
|
+
<!-- Git Diff 面板 -->
|
|
932
|
+
<div id="git-diff-bar">
|
|
933
|
+
<div id="git-diff-header">
|
|
934
|
+
<div style="display: flex; align-items: center; gap: 8px;">
|
|
935
|
+
<span id="git-diff-title">Git Changes</span>
|
|
936
|
+
<span class="diff-file-count" id="git-diff-count">0</span>
|
|
937
|
+
</div>
|
|
938
|
+
<div style="display: flex; gap: 8px;">
|
|
939
|
+
<button class="history-toggle-btn" id="refresh-diff">
|
|
940
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
941
|
+
<polyline points="23 4 23 10 17 10"></polyline>
|
|
942
|
+
<path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path>
|
|
943
|
+
</svg>
|
|
944
|
+
<span>刷新</span>
|
|
945
|
+
</button>
|
|
946
|
+
<button class="history-toggle-btn" id="close-diff">
|
|
947
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
|
948
|
+
<line x1="18" y1="6" x2="6" y2="18"></line>
|
|
949
|
+
<line x1="6" y1="6" x2="18" y2="18"></line>
|
|
950
|
+
</svg>
|
|
951
|
+
<span>返回</span>
|
|
952
|
+
</button>
|
|
953
|
+
</div>
|
|
954
|
+
</div>
|
|
955
|
+
<div class="git-diff-file-list" id="git-diff-file-list">
|
|
956
|
+
<div class="git-diff-loading">加载中...</div>
|
|
957
|
+
</div>
|
|
958
|
+
<div class="git-diff-content-area" id="git-diff-content-area">
|
|
959
|
+
<div class="git-diff-placeholder">
|
|
960
|
+
<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#333" stroke-width="1.5">
|
|
961
|
+
<line x1="6" y1="3" x2="6" y2="15"></line>
|
|
962
|
+
<circle cx="18" cy="6" r="3"></circle>
|
|
963
|
+
<circle cx="6" cy="18" r="3"></circle>
|
|
964
|
+
<path d="M18 9a9 9 0 0 1-9 9"></path>
|
|
965
|
+
</svg>
|
|
966
|
+
<span>点击文件查看 diff</span>
|
|
967
|
+
</div>
|
|
968
|
+
</div>
|
|
969
|
+
</div>
|
|
970
|
+
|
|
971
|
+
<div id="content">
|
|
972
|
+
<div id="terminal-container">
|
|
973
|
+
<div id="terminal">
|
|
974
|
+
<div id="select-text-layer">
|
|
975
|
+
<div id="select-hint">长按选择文本 · 点右上角 ✕ 返回终端</div>
|
|
976
|
+
<pre id="select-text-pre"></pre>
|
|
977
|
+
</div>
|
|
978
|
+
<button id="select-mode-close">✕</button>
|
|
979
|
+
</div>
|
|
980
|
+
</div>
|
|
981
|
+
</div>
|
|
982
|
+
</div>
|
|
983
|
+
|
|
984
|
+
<div id="copy-toast">已复制</div>
|
|
985
|
+
<div id="copy-popup">
|
|
986
|
+
<div style="color:#ccc;font-size:13px;">剪贴板写入需要 HTTPS,请手动复制:</div>
|
|
987
|
+
<textarea id="copy-popup-text" readonly></textarea>
|
|
988
|
+
<div class="popup-actions">
|
|
989
|
+
<button class="btn-copy" onclick="document.getElementById('copy-popup-text').select();document.execCommand('copy');document.getElementById('copy-popup').classList.remove('show');">选中并复制</button>
|
|
990
|
+
<button class="btn-close" onclick="document.getElementById('copy-popup').classList.remove('show');">关闭</button>
|
|
991
|
+
</div>
|
|
992
|
+
</div>
|
|
993
|
+
<script src="https://cdn.jsdelivr.net/npm/@xterm/xterm@5.5.0/lib/xterm.min.js"></script>
|
|
994
|
+
<script>
|
|
995
|
+
(function() {
|
|
996
|
+
var isMobile = /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
|
|
997
|
+
var isIOS = /iPhone|iPad|iPod/i.test(navigator.userAgent);
|
|
998
|
+
var MOBILE_COLS = 60;
|
|
999
|
+
var fontSize = isMobile ? 11 : 13;
|
|
1000
|
+
var currentMode = 'opencode';
|
|
1001
|
+
var isTransitioning = false;
|
|
1002
|
+
var isBufferReplay = true; // 初始缓冲区回放中,不弹 toast
|
|
1003
|
+
|
|
1004
|
+
var term = new Terminal({
|
|
1005
|
+
cursorBlink: !isMobile,
|
|
1006
|
+
fontSize: fontSize,
|
|
1007
|
+
fontFamily: 'Menlo, Monaco, "Courier New", monospace',
|
|
1008
|
+
theme: {
|
|
1009
|
+
background: '#0a0a0a',
|
|
1010
|
+
foreground: '#d4d4d4',
|
|
1011
|
+
cursor: '#d4d4d4',
|
|
1012
|
+
selectionBackground: '#264f78',
|
|
1013
|
+
},
|
|
1014
|
+
allowProposedApi: true,
|
|
1015
|
+
scrollback: isIOS ? 200 : isMobile ? 1000 : 3000,
|
|
1016
|
+
smoothScrollDuration: 0,
|
|
1017
|
+
scrollOnUserInput: true,
|
|
1018
|
+
});
|
|
1019
|
+
|
|
1020
|
+
term.open(document.getElementById('terminal'));
|
|
1021
|
+
|
|
1022
|
+
// PC端复制:用 xterm.js selection API 获取纯文本,避免复制出乱码
|
|
1023
|
+
document.getElementById('terminal').addEventListener('copy', function(e) {
|
|
1024
|
+
var sel = term.getSelection();
|
|
1025
|
+
if (sel) {
|
|
1026
|
+
e.preventDefault();
|
|
1027
|
+
e.clipboardData.setData('text/plain', sel);
|
|
1028
|
+
}
|
|
1029
|
+
});
|
|
1030
|
+
|
|
1031
|
+
// OSC 52 剪贴板支持:拦截应用发送的剪贴板设置请求
|
|
1032
|
+
term.parser.registerOscHandler(52, function(data) {
|
|
1033
|
+
var idx = data.indexOf(';');
|
|
1034
|
+
if (idx === -1) return false;
|
|
1035
|
+
var b64 = data.substring(idx + 1);
|
|
1036
|
+
if (!b64 || b64 === '?') return false;
|
|
1037
|
+
if (isBufferReplay) return true;
|
|
1038
|
+
try {
|
|
1039
|
+
var text = atob(b64);
|
|
1040
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
1041
|
+
navigator.clipboard.writeText(text).then(function() {
|
|
1042
|
+
showCopyToast();
|
|
1043
|
+
}).catch(function() {
|
|
1044
|
+
showCopyPopup(text);
|
|
1045
|
+
});
|
|
1046
|
+
} else {
|
|
1047
|
+
showCopyPopup(text);
|
|
1048
|
+
}
|
|
1049
|
+
} catch (e) {}
|
|
1050
|
+
return true;
|
|
1051
|
+
});
|
|
1052
|
+
|
|
1053
|
+
var modeSelect = document.getElementById('mode-select');
|
|
1054
|
+
var terminalEl = document.getElementById('terminal');
|
|
1055
|
+
var ws = null;
|
|
1056
|
+
var writeBuffer = '';
|
|
1057
|
+
var writeTimer = null;
|
|
1058
|
+
var lastY = 0;
|
|
1059
|
+
var lastTime = 0;
|
|
1060
|
+
var pixelAccum = 0;
|
|
1061
|
+
var pendingDy = 0;
|
|
1062
|
+
var scrollRaf = null;
|
|
1063
|
+
var momentumRaf = null;
|
|
1064
|
+
var velocitySamples = [];
|
|
1065
|
+
|
|
1066
|
+
// 未发送消息缓存
|
|
1067
|
+
var currentInputBuffer = '';
|
|
1068
|
+
var CACHE_KEY = 'claude_opencode_input_cache';
|
|
1069
|
+
var cacheRestored = false; // 防止重复恢复
|
|
1070
|
+
|
|
1071
|
+
// 会话历史相关
|
|
1072
|
+
var sessions = [];
|
|
1073
|
+
var currentSessionId = null;
|
|
1074
|
+
var currentSessionData = null;
|
|
1075
|
+
var historyBarVisible = false;
|
|
1076
|
+
|
|
1077
|
+
function saveInputCache() {
|
|
1078
|
+
if (currentInputBuffer) {
|
|
1079
|
+
localStorage.setItem(CACHE_KEY, currentInputBuffer);
|
|
1080
|
+
console.log('[cache] saved:', currentInputBuffer);
|
|
1081
|
+
} else {
|
|
1082
|
+
localStorage.removeItem(CACHE_KEY);
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
function loadInputCache() {
|
|
1087
|
+
if (cacheRestored) return;
|
|
1088
|
+
cacheRestored = true;
|
|
1089
|
+
|
|
1090
|
+
var cached = localStorage.getItem(CACHE_KEY);
|
|
1091
|
+
if (cached) {
|
|
1092
|
+
console.log('[cache] restoring buffer:', cached);
|
|
1093
|
+
// 只恢复跟踪变量,不重新发送到 pty
|
|
1094
|
+
// 因为 outputBuffer 回放已经包含了之前的回显
|
|
1095
|
+
currentInputBuffer = cached;
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
|
|
1099
|
+
function clearInputCache() {
|
|
1100
|
+
currentInputBuffer = '';
|
|
1101
|
+
localStorage.removeItem(CACHE_KEY);
|
|
1102
|
+
console.log('[cache] cleared');
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
function getCellDims() {
|
|
1106
|
+
return term._core && term._core._renderService && term._core._renderService.dimensions && term._core._renderService.dimensions.css && term._core._renderService.dimensions.css.cell;
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
// PC端尺寸计算
|
|
1110
|
+
function resize() {
|
|
1111
|
+
var container = document.getElementById('terminal');
|
|
1112
|
+
var cellDims = getCellDims();
|
|
1113
|
+
if (!cellDims) return;
|
|
1114
|
+
var charW = cellDims.width;
|
|
1115
|
+
var charH = cellDims.height;
|
|
1116
|
+
var availW = container.clientWidth;
|
|
1117
|
+
var availH = container.clientHeight;
|
|
1118
|
+
var cols = Math.max(20, Math.floor(availW / charW));
|
|
1119
|
+
var rows = Math.max(10, Math.floor(availH / charH));
|
|
1120
|
+
term.resize(cols, rows);
|
|
1121
|
+
if (ws && ws.readyState === 1 && !isTransitioning) {
|
|
1122
|
+
ws.send(JSON.stringify({ type: 'resize', cols: cols, rows: rows }));
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
function flushWrite() {
|
|
1127
|
+
if (writeTimer) { cancelAnimationFrame(writeTimer); writeTimer = null; }
|
|
1128
|
+
if (!writeBuffer || !term) return;
|
|
1129
|
+
var buf = writeBuffer;
|
|
1130
|
+
writeBuffer = '';
|
|
1131
|
+
term.write(buf);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
function throttledWrite(data) {
|
|
1135
|
+
writeBuffer += data;
|
|
1136
|
+
if (!writeTimer) {
|
|
1137
|
+
writeTimer = requestAnimationFrame(flushWrite);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
function stopMomentum() {
|
|
1142
|
+
if (momentumRaf) { cancelAnimationFrame(momentumRaf); momentumRaf = null; }
|
|
1143
|
+
if (scrollRaf) { cancelAnimationFrame(scrollRaf); scrollRaf = null; }
|
|
1144
|
+
pendingDy = 0;
|
|
1145
|
+
pixelAccum = 0;
|
|
1146
|
+
}
|
|
1147
|
+
|
|
1148
|
+
// 触摸滚动实现 - 混合模式:alternate buffer 发鼠标滚轮,normal buffer 用 scrollLines
|
|
1149
|
+
var touchScreen = null;
|
|
1150
|
+
var touchEventsBound = false;
|
|
1151
|
+
|
|
1152
|
+
function isAlternateBuffer() {
|
|
1153
|
+
return term.buffer.active.type === 'alternate';
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
function emitWheelEvent(lines) {
|
|
1157
|
+
var screen = terminalEl.querySelector('.xterm-screen');
|
|
1158
|
+
if (!screen) return;
|
|
1159
|
+
var lh = getLineHeight();
|
|
1160
|
+
var rect = screen.getBoundingClientRect();
|
|
1161
|
+
var cx = rect.left + rect.width / 2;
|
|
1162
|
+
var cy = rect.top + rect.height / 2;
|
|
1163
|
+
var count = Math.abs(lines);
|
|
1164
|
+
var dy = lines < 0 ? -lh : lh;
|
|
1165
|
+
for (var i = 0; i < count; i++) {
|
|
1166
|
+
screen.dispatchEvent(new WheelEvent('wheel', {
|
|
1167
|
+
deltaY: dy,
|
|
1168
|
+
deltaMode: 0,
|
|
1169
|
+
clientX: cx,
|
|
1170
|
+
clientY: cy,
|
|
1171
|
+
bubbles: true,
|
|
1172
|
+
cancelable: true,
|
|
1173
|
+
}));
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
var altScrollAccum = 0;
|
|
1178
|
+
var ALT_SCROLL_THRESHOLD = 2;
|
|
1179
|
+
|
|
1180
|
+
function doScroll(lines) {
|
|
1181
|
+
if (lines === 0) return;
|
|
1182
|
+
if (isAlternateBuffer()) {
|
|
1183
|
+
altScrollAccum += lines;
|
|
1184
|
+
if (Math.abs(altScrollAccum) >= ALT_SCROLL_THRESHOLD) {
|
|
1185
|
+
var scrollLines = Math.trunc(altScrollAccum);
|
|
1186
|
+
emitWheelEvent(scrollLines);
|
|
1187
|
+
altScrollAccum -= scrollLines;
|
|
1188
|
+
}
|
|
1189
|
+
} else {
|
|
1190
|
+
term.scrollLines(lines);
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
function getLineHeight() {
|
|
1195
|
+
var cellDims = getCellDims();
|
|
1196
|
+
var height = (cellDims && cellDims.height) || 15;
|
|
1197
|
+
return height;
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
function stopMomentum() {
|
|
1201
|
+
if (momentumRaf) {
|
|
1202
|
+
cancelAnimationFrame(momentumRaf);
|
|
1203
|
+
momentumRaf = null;
|
|
1204
|
+
}
|
|
1205
|
+
if (scrollRaf) {
|
|
1206
|
+
cancelAnimationFrame(scrollRaf);
|
|
1207
|
+
scrollRaf = null;
|
|
1208
|
+
}
|
|
1209
|
+
pendingDy = 0;
|
|
1210
|
+
pixelAccum = 0;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
function flushScroll() {
|
|
1214
|
+
scrollRaf = null;
|
|
1215
|
+
if (pendingDy === 0) return;
|
|
1216
|
+
|
|
1217
|
+
pixelAccum += pendingDy;
|
|
1218
|
+
pendingDy = 0;
|
|
1219
|
+
|
|
1220
|
+
var lh = getLineHeight();
|
|
1221
|
+
var lines = Math.trunc(pixelAccum / lh);
|
|
1222
|
+
|
|
1223
|
+
if (lines !== 0) {
|
|
1224
|
+
doScroll(lines);
|
|
1225
|
+
pixelAccum -= lines * lh;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
// 长按检测
|
|
1230
|
+
var longPressTimer = null;
|
|
1231
|
+
var longPressTriggered = false;
|
|
1232
|
+
var LONG_PRESS_DELAY = 550; // ms
|
|
1233
|
+
|
|
1234
|
+
function clearLongPress() {
|
|
1235
|
+
if (longPressTimer) {
|
|
1236
|
+
clearTimeout(longPressTimer);
|
|
1237
|
+
longPressTimer = null;
|
|
1238
|
+
}
|
|
1239
|
+
// 始终恢复 xterm textarea,防止 disabled 残留
|
|
1240
|
+
var xtermTa = terminalEl.querySelector('.xterm-helper-textarea');
|
|
1241
|
+
if (xtermTa) xtermTa.removeAttribute('disabled');
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
function handleTouchStart(e) {
|
|
1245
|
+
stopMomentum();
|
|
1246
|
+
altScrollAccum = 0;
|
|
1247
|
+
longPressTriggered = false;
|
|
1248
|
+
clearLongPress();
|
|
1249
|
+
if (e.touches.length !== 1) return;
|
|
1250
|
+
lastY = e.touches[0].clientY;
|
|
1251
|
+
lastTime = performance.now();
|
|
1252
|
+
velocitySamples = [];
|
|
1253
|
+
|
|
1254
|
+
// 启动长按计时器
|
|
1255
|
+
// 在长按检测期间阻止 xterm textarea 获取焦点,防止弹出键盘
|
|
1256
|
+
var xtermTa = terminalEl.querySelector('.xterm-helper-textarea');
|
|
1257
|
+
if (xtermTa) {
|
|
1258
|
+
xtermTa.setAttribute('disabled', 'true');
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
longPressTimer = setTimeout(function() {
|
|
1262
|
+
longPressTriggered = true;
|
|
1263
|
+
longPressTimer = null;
|
|
1264
|
+
openSelectMode();
|
|
1265
|
+
}, LONG_PRESS_DELAY);
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
function handleTouchMove(e) {
|
|
1269
|
+
if (e.touches.length !== 1) return;
|
|
1270
|
+
// 有移动则取消长按
|
|
1271
|
+
clearLongPress();
|
|
1272
|
+
var y = e.touches[0].clientY;
|
|
1273
|
+
var now = performance.now();
|
|
1274
|
+
var dt = now - lastTime;
|
|
1275
|
+
var dy = lastY - y;
|
|
1276
|
+
|
|
1277
|
+
if (dt > 0) {
|
|
1278
|
+
var v = dy / dt * 16;
|
|
1279
|
+
velocitySamples.push({ v: v, t: now });
|
|
1280
|
+
while (velocitySamples.length > 0 && now - velocitySamples[0].t > 100) {
|
|
1281
|
+
velocitySamples.shift();
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
pendingDy += dy;
|
|
1286
|
+
console.log('[scroll] touchmove dy:', dy, 'pendingDy:', pendingDy);
|
|
1287
|
+
|
|
1288
|
+
if (!scrollRaf) scrollRaf = requestAnimationFrame(flushScroll);
|
|
1289
|
+
lastY = y;
|
|
1290
|
+
lastTime = now;
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
function handleTouchEnd() {
|
|
1294
|
+
console.log('[scroll] touchend');
|
|
1295
|
+
clearLongPress();
|
|
1296
|
+
|
|
1297
|
+
// 长按已触发,不执行滚动惯性
|
|
1298
|
+
if (longPressTriggered) {
|
|
1299
|
+
longPressTriggered = false;
|
|
1300
|
+
pendingDy = 0;
|
|
1301
|
+
pixelAccum = 0;
|
|
1302
|
+
velocitySamples = [];
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
|
|
1306
|
+
if (scrollRaf) {
|
|
1307
|
+
cancelAnimationFrame(scrollRaf);
|
|
1308
|
+
scrollRaf = null;
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
if (pendingDy !== 0) {
|
|
1312
|
+
pixelAccum += pendingDy;
|
|
1313
|
+
pendingDy = 0;
|
|
1314
|
+
var lh = getLineHeight();
|
|
1315
|
+
var lines = Math.trunc(pixelAccum / lh);
|
|
1316
|
+
if (lines !== 0) {
|
|
1317
|
+
doScroll(lines);
|
|
1318
|
+
}
|
|
1319
|
+
pixelAccum = 0;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
var velocity = 0;
|
|
1323
|
+
if (velocitySamples.length >= 2) {
|
|
1324
|
+
var totalWeight = 0;
|
|
1325
|
+
var weightedV = 0;
|
|
1326
|
+
var latest = velocitySamples[velocitySamples.length - 1].t;
|
|
1327
|
+
for (var i = 0; i < velocitySamples.length; i++) {
|
|
1328
|
+
var s = velocitySamples[i];
|
|
1329
|
+
var w = Math.max(0, 1 - (latest - s.t) / 100);
|
|
1330
|
+
weightedV += s.v * w;
|
|
1331
|
+
totalWeight += w;
|
|
1332
|
+
}
|
|
1333
|
+
velocity = totalWeight > 0 ? weightedV / totalWeight : 0;
|
|
1334
|
+
}
|
|
1335
|
+
velocitySamples = [];
|
|
1336
|
+
|
|
1337
|
+
console.log('[scroll] velocity:', velocity);
|
|
1338
|
+
if (Math.abs(velocity) < 0.5) return;
|
|
1339
|
+
|
|
1340
|
+
var friction = 0.95;
|
|
1341
|
+
var mAccum = 0;
|
|
1342
|
+
var tick = function() {
|
|
1343
|
+
if (Math.abs(velocity) < 0.3) {
|
|
1344
|
+
var lh = getLineHeight();
|
|
1345
|
+
var rest = Math.round(mAccum / lh);
|
|
1346
|
+
if (rest !== 0) {
|
|
1347
|
+
doScroll(rest);
|
|
1348
|
+
}
|
|
1349
|
+
momentumRaf = null;
|
|
1350
|
+
return;
|
|
1351
|
+
}
|
|
1352
|
+
mAccum += velocity;
|
|
1353
|
+
var lh = getLineHeight();
|
|
1354
|
+
var lines = Math.trunc(mAccum / lh);
|
|
1355
|
+
if (lines !== 0) {
|
|
1356
|
+
doScroll(lines);
|
|
1357
|
+
mAccum -= lines * lh;
|
|
1358
|
+
}
|
|
1359
|
+
velocity *= friction;
|
|
1360
|
+
momentumRaf = requestAnimationFrame(tick);
|
|
1361
|
+
};
|
|
1362
|
+
momentumRaf = requestAnimationFrame(tick);
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
function unbindTouchScroll() {
|
|
1366
|
+
if (touchScreen && touchEventsBound) {
|
|
1367
|
+
console.log('[scroll] unbinding touch events');
|
|
1368
|
+
touchScreen.removeEventListener('touchstart', handleTouchStart);
|
|
1369
|
+
touchScreen.removeEventListener('touchmove', handleTouchMove);
|
|
1370
|
+
touchScreen.removeEventListener('touchend', handleTouchEnd);
|
|
1371
|
+
touchEventsBound = false;
|
|
1372
|
+
touchScreen = null;
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
function setupMobileTouchScroll() {
|
|
1377
|
+
// 先解绑旧的
|
|
1378
|
+
unbindTouchScroll();
|
|
1379
|
+
|
|
1380
|
+
var screen = terminalEl.querySelector('.xterm-screen');
|
|
1381
|
+
if (!screen) {
|
|
1382
|
+
console.log('[scroll] .xterm-screen not found, retrying...');
|
|
1383
|
+
setTimeout(setupMobileTouchScroll, 50);
|
|
1384
|
+
return;
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
touchScreen = screen;
|
|
1388
|
+
screen.addEventListener('touchstart', handleTouchStart, { passive: true });
|
|
1389
|
+
screen.addEventListener('touchmove', handleTouchMove, { passive: true });
|
|
1390
|
+
screen.addEventListener('touchend', handleTouchEnd, { passive: true });
|
|
1391
|
+
touchEventsBound = true;
|
|
1392
|
+
console.log('[scroll] Touch events bound to .xterm-screen');
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
function rebindTouchScroll() {
|
|
1396
|
+
if (isMobile) {
|
|
1397
|
+
setTimeout(setupMobileTouchScroll, 100);
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
if (isMobile) {
|
|
1402
|
+
setupMobileTouchScroll();
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
function startTransition() {
|
|
1406
|
+
isTransitioning = true;
|
|
1407
|
+
terminalEl.classList.add('transitioning');
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
function endTransition(mode) {
|
|
1411
|
+
currentMode = mode;
|
|
1412
|
+
modeSelect.value = mode;
|
|
1413
|
+
terminalEl.classList.remove('transitioning');
|
|
1414
|
+
isTransitioning = false;
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
function switchMode(mode) {
|
|
1418
|
+
if (mode === currentMode || !ws || ws.readyState !== 1 || isTransitioning) return;
|
|
1419
|
+
startTransition();
|
|
1420
|
+
ws.send(JSON.stringify({ type: 'switch', mode: mode }));
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
modeSelect.addEventListener('change', function() {
|
|
1424
|
+
switchMode(modeSelect.value);
|
|
1425
|
+
});
|
|
1426
|
+
|
|
1427
|
+
// 设置初始选中项
|
|
1428
|
+
modeSelect.value = currentMode;
|
|
1429
|
+
|
|
1430
|
+
// 只绑定一次 term.onData,避免重连时重复绑定
|
|
1431
|
+
term.onData(function(d) {
|
|
1432
|
+
if (ws && ws.readyState === 1) {
|
|
1433
|
+
ws.send(JSON.stringify({ type: 'input', data: d }));
|
|
1434
|
+
|
|
1435
|
+
// 缓存管理:跟踪当前行的输入(仅在 opencode 模式且未在恢复中)
|
|
1436
|
+
if (currentMode === 'opencode' && cacheRestored) {
|
|
1437
|
+
if (d === '\r' || d === '\n' || d === '\r\n') {
|
|
1438
|
+
// 回车:命令已发送,清除缓存
|
|
1439
|
+
clearInputCache();
|
|
1440
|
+
} else if (d === '\x7f' || d === '\b') {
|
|
1441
|
+
// 退格:删除最后一个字符
|
|
1442
|
+
if (currentInputBuffer.length > 0) {
|
|
1443
|
+
currentInputBuffer = currentInputBuffer.slice(0, -1);
|
|
1444
|
+
saveInputCache();
|
|
1445
|
+
}
|
|
1446
|
+
} else if (d === '\x03') {
|
|
1447
|
+
// Ctrl+C:中断,清除缓存
|
|
1448
|
+
clearInputCache();
|
|
1449
|
+
} else if (d === '\x15') {
|
|
1450
|
+
// Ctrl+U:清空整行
|
|
1451
|
+
clearInputCache();
|
|
1452
|
+
} else if (d.charCodeAt(0) >= 32 && d.charCodeAt(0) !== 127) {
|
|
1453
|
+
// 可打印字符(含中文等多字节):添加到缓冲区
|
|
1454
|
+
currentInputBuffer += d;
|
|
1455
|
+
saveInputCache();
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
// 点击终端输入时,滚动到底部
|
|
1461
|
+
setTimeout(function() {
|
|
1462
|
+
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
|
|
1463
|
+
}, 50);
|
|
1464
|
+
});
|
|
1465
|
+
|
|
1466
|
+
function connect() {
|
|
1467
|
+
var proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
1468
|
+
ws = new WebSocket(proto + '//' + location.host + '/ws');
|
|
1469
|
+
|
|
1470
|
+
ws.onopen = function() {
|
|
1471
|
+
isBufferReplay = true;
|
|
1472
|
+
resize();
|
|
1473
|
+
rebindTouchScroll();
|
|
1474
|
+
setTimeout(function() { isBufferReplay = false; }, 500);
|
|
1475
|
+
};
|
|
1476
|
+
|
|
1477
|
+
ws.onclose = function() {
|
|
1478
|
+
ws = null;
|
|
1479
|
+
setTimeout(connect, 2000);
|
|
1480
|
+
};
|
|
1481
|
+
|
|
1482
|
+
ws.onmessage = function(e) {
|
|
1483
|
+
try {
|
|
1484
|
+
var msg = JSON.parse(e.data);
|
|
1485
|
+
if (msg.type === 'data') {
|
|
1486
|
+
if (!isCreatingNewSession) {
|
|
1487
|
+
throttledWrite(msg.data);
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
else if (msg.type === 'exit') {
|
|
1491
|
+
if (!isCreatingNewSession) {
|
|
1492
|
+
throttledWrite('\r\n\x1b[33m[进程已退出: ' + msg.exitCode + ']\x1b[0m\r\n');
|
|
1493
|
+
throttledWrite('\x1b[90m按 Enter 键重新启动 ' + currentMode + '...\x1b[0m\r\n');
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
else if (msg.type === 'mode') {
|
|
1497
|
+
endTransition(msg.mode);
|
|
1498
|
+
// 模式切换完成后,重新绑定触摸事件
|
|
1499
|
+
rebindTouchScroll();
|
|
1500
|
+
}
|
|
1501
|
+
else if (msg.type === 'switching') {
|
|
1502
|
+
// 服务端开始切换,前端清屏
|
|
1503
|
+
term.clear();
|
|
1504
|
+
writeBuffer = '';
|
|
1505
|
+
}
|
|
1506
|
+
else if (msg.type === 'state') {
|
|
1507
|
+
if (msg.mode) {
|
|
1508
|
+
currentMode = msg.mode;
|
|
1509
|
+
modeSelect.value = msg.mode;
|
|
1510
|
+
modeIndicator.textContent = msg.mode === 'claude' ? 'Claude' : 'OpenCode';
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
else if (msg.type === 'restored') {
|
|
1514
|
+
// 会话恢复成功
|
|
1515
|
+
term.write('\x1b[32m✓ 会话已恢复: ' + msg.sessionId + '\x1b[0m\r\n');
|
|
1516
|
+
term.write('\x1b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\x1b[0m\r\n');
|
|
1517
|
+
term.write('\r\n');
|
|
1518
|
+
}
|
|
1519
|
+
else if (msg.type === 'restore-error') {
|
|
1520
|
+
// 恢复失败
|
|
1521
|
+
term.write('\x1b[31m✗ 恢复失败: ' + msg.error + '\x1b[0m\r\n');
|
|
1522
|
+
}
|
|
1523
|
+
else if (msg.type === 'started') {
|
|
1524
|
+
rebindTouchScroll();
|
|
1525
|
+
}
|
|
1526
|
+
else if (msg.type === 'new-session-ok') {
|
|
1527
|
+
isCreatingNewSession = false;
|
|
1528
|
+
term.clear();
|
|
1529
|
+
}
|
|
1530
|
+
else if (msg.type === 'new-session-error') {
|
|
1531
|
+
isCreatingNewSession = false;
|
|
1532
|
+
term.write('\x1b[31m✗ 新会话启动失败: ' + msg.error + '\x1b[0m\r\n');
|
|
1533
|
+
}
|
|
1534
|
+
} catch(err) {}
|
|
1535
|
+
};
|
|
1536
|
+
|
|
1537
|
+
// 页面加载后尝试恢复缓存的输入(仅在 opencode 模式)
|
|
1538
|
+
setTimeout(function() {
|
|
1539
|
+
if (currentMode === 'opencode') {
|
|
1540
|
+
loadInputCache();
|
|
1541
|
+
} else {
|
|
1542
|
+
cacheRestored = true;
|
|
1543
|
+
}
|
|
1544
|
+
}, 800);
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
window.addEventListener('resize', resize);
|
|
1548
|
+
if (isMobile) {
|
|
1549
|
+
window.addEventListener('orientationchange', function() {
|
|
1550
|
+
setTimeout(resize, 200);
|
|
1551
|
+
});
|
|
1552
|
+
}
|
|
1553
|
+
|
|
1554
|
+
// 页面卸载前保存输入缓存
|
|
1555
|
+
window.addEventListener('beforeunload', function() {
|
|
1556
|
+
if (currentInputBuffer) {
|
|
1557
|
+
saveInputCache();
|
|
1558
|
+
}
|
|
1559
|
+
});
|
|
1560
|
+
|
|
1561
|
+
// 页面可见性变化时保存缓存
|
|
1562
|
+
document.addEventListener('visibilitychange', function() {
|
|
1563
|
+
if (document.hidden && currentInputBuffer) {
|
|
1564
|
+
saveInputCache();
|
|
1565
|
+
}
|
|
1566
|
+
});
|
|
1567
|
+
|
|
1568
|
+
// 虚拟按键映射表
|
|
1569
|
+
var KEY_MAP = {
|
|
1570
|
+
'up': '\x1b[A',
|
|
1571
|
+
'down': '\x1b[B',
|
|
1572
|
+
'left': '\x1b[D',
|
|
1573
|
+
'right': '\x1b[C',
|
|
1574
|
+
'esc': '\x1b',
|
|
1575
|
+
'ctrlc': '\x03',
|
|
1576
|
+
'tab': '\t',
|
|
1577
|
+
'enter': '\r'
|
|
1578
|
+
};
|
|
1579
|
+
|
|
1580
|
+
function sendKey(keyName) {
|
|
1581
|
+
var seq = KEY_MAP[keyName];
|
|
1582
|
+
if (seq && ws && ws.readyState === 1 && !isTransitioning) {
|
|
1583
|
+
ws.send(JSON.stringify({ type: 'input', data: seq }));
|
|
1584
|
+
}
|
|
1585
|
+
}
|
|
1586
|
+
|
|
1587
|
+
function scrollTerminal(lines) {
|
|
1588
|
+
if (!term) return;
|
|
1589
|
+
doScroll(lines);
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
|
|
1593
|
+
// 会话历史功能
|
|
1594
|
+
function formatTime(timestamp) {
|
|
1595
|
+
var date = new Date(timestamp);
|
|
1596
|
+
var now = new Date();
|
|
1597
|
+
var diff = now - date;
|
|
1598
|
+
var minutes = Math.floor(diff / 60000);
|
|
1599
|
+
var hours = Math.floor(diff / 3600000);
|
|
1600
|
+
var days = Math.floor(diff / 86400000);
|
|
1601
|
+
|
|
1602
|
+
if (minutes < 1) return '刚刚';
|
|
1603
|
+
if (minutes < 60) return minutes + '分钟前';
|
|
1604
|
+
if (hours < 24) return hours + '小时前';
|
|
1605
|
+
if (days < 7) return days + '天前';
|
|
1606
|
+
|
|
1607
|
+
return date.toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' });
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
function loadSessions() {
|
|
1611
|
+
var sessionList = document.getElementById('session-list');
|
|
1612
|
+
sessionList.innerHTML = '<div class="session-loading">加载历史会话...</div>';
|
|
1613
|
+
|
|
1614
|
+
fetch('/api/sessions')
|
|
1615
|
+
.then(function(response) { return response.json(); })
|
|
1616
|
+
.then(function(data) {
|
|
1617
|
+
sessions = data;
|
|
1618
|
+
renderSessions();
|
|
1619
|
+
})
|
|
1620
|
+
.catch(function(err) {
|
|
1621
|
+
console.error('[sessions] 加载失败:', err);
|
|
1622
|
+
sessionList.innerHTML = '<div class="session-empty">无法加载历史会话</div>';
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
function renderSessions() {
|
|
1627
|
+
var sessionList = document.getElementById('session-list');
|
|
1628
|
+
|
|
1629
|
+
if (sessions.length === 0) {
|
|
1630
|
+
sessionList.innerHTML = '<div class="session-empty">暂无历史会话</div>';
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1633
|
+
|
|
1634
|
+
sessionList.innerHTML = '';
|
|
1635
|
+
|
|
1636
|
+
sessions.forEach(function(session) {
|
|
1637
|
+
var item = document.createElement('div');
|
|
1638
|
+
item.className = 'session-item';
|
|
1639
|
+
if (session.id === currentSessionId) {
|
|
1640
|
+
item.classList.add('active');
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
var icon = document.createElement('div');
|
|
1644
|
+
icon.className = 'session-icon';
|
|
1645
|
+
|
|
1646
|
+
var info = document.createElement('div');
|
|
1647
|
+
info.className = 'session-info';
|
|
1648
|
+
|
|
1649
|
+
var title = document.createElement('div');
|
|
1650
|
+
title.className = 'session-title';
|
|
1651
|
+
// 使用预览文本,如果没有预览则使用标题
|
|
1652
|
+
title.textContent = session.preview || session.title;
|
|
1653
|
+
|
|
1654
|
+
var meta = document.createElement('div');
|
|
1655
|
+
meta.className = 'session-meta';
|
|
1656
|
+
|
|
1657
|
+
var time = document.createElement('span');
|
|
1658
|
+
time.className = 'session-time';
|
|
1659
|
+
time.textContent = formatTime(session.time_updated);
|
|
1660
|
+
|
|
1661
|
+
var dir = document.createElement('span');
|
|
1662
|
+
dir.className = 'session-dir';
|
|
1663
|
+
dir.textContent = session.directory.replace(/^\/Users\/[^\/]+/, '~');
|
|
1664
|
+
|
|
1665
|
+
meta.appendChild(time);
|
|
1666
|
+
meta.appendChild(dir);
|
|
1667
|
+
info.appendChild(title);
|
|
1668
|
+
info.appendChild(meta);
|
|
1669
|
+
|
|
1670
|
+
var deleteBtn = document.createElement('button');
|
|
1671
|
+
deleteBtn.className = 'session-delete-btn';
|
|
1672
|
+
deleteBtn.innerHTML = '<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path></svg>';
|
|
1673
|
+
deleteBtn.title = '删除会话';
|
|
1674
|
+
deleteBtn.addEventListener('click', function(e) {
|
|
1675
|
+
e.stopPropagation();
|
|
1676
|
+
deleteSession(session.id, item);
|
|
1677
|
+
});
|
|
1678
|
+
|
|
1679
|
+
item.appendChild(icon);
|
|
1680
|
+
item.appendChild(info);
|
|
1681
|
+
item.appendChild(deleteBtn);
|
|
1682
|
+
|
|
1683
|
+
item.addEventListener('click', function() {
|
|
1684
|
+
loadSession(session);
|
|
1685
|
+
});
|
|
1686
|
+
|
|
1687
|
+
sessionList.appendChild(item);
|
|
1688
|
+
});
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
function deleteSession(sessionId, itemEl) {
|
|
1692
|
+
if (!confirm('确定要删除这个会话吗?')) return;
|
|
1693
|
+
|
|
1694
|
+
itemEl.style.opacity = '0.4';
|
|
1695
|
+
itemEl.style.pointerEvents = 'none';
|
|
1696
|
+
|
|
1697
|
+
fetch('/api/session/' + sessionId, { method: 'DELETE' })
|
|
1698
|
+
.then(function(r) { return r.json(); })
|
|
1699
|
+
.then(function(data) {
|
|
1700
|
+
if (data.ok) {
|
|
1701
|
+
itemEl.style.transition = 'all 0.2s';
|
|
1702
|
+
itemEl.style.maxHeight = '0';
|
|
1703
|
+
itemEl.style.overflow = 'hidden';
|
|
1704
|
+
itemEl.style.padding = '0 12px';
|
|
1705
|
+
itemEl.style.margin = '0';
|
|
1706
|
+
itemEl.style.opacity = '0';
|
|
1707
|
+
setTimeout(function() {
|
|
1708
|
+
sessions = sessions.filter(function(s) { return s.id !== sessionId; });
|
|
1709
|
+
renderSessions();
|
|
1710
|
+
}, 200);
|
|
1711
|
+
} else {
|
|
1712
|
+
itemEl.style.opacity = '';
|
|
1713
|
+
itemEl.style.pointerEvents = '';
|
|
1714
|
+
alert('删除失败: ' + (data.error || '未知错误'));
|
|
1715
|
+
}
|
|
1716
|
+
})
|
|
1717
|
+
.catch(function(err) {
|
|
1718
|
+
itemEl.style.opacity = '';
|
|
1719
|
+
itemEl.style.pointerEvents = '';
|
|
1720
|
+
alert('删除失败: ' + err.message);
|
|
1721
|
+
});
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
function showSessionDetail() {
|
|
1725
|
+
document.getElementById('session-list-view').classList.add('hidden');
|
|
1726
|
+
document.getElementById('session-detail-view').classList.add('visible');
|
|
1727
|
+
}
|
|
1728
|
+
|
|
1729
|
+
function showSessionList() {
|
|
1730
|
+
document.getElementById('session-list-view').classList.remove('hidden');
|
|
1731
|
+
document.getElementById('session-detail-view').classList.remove('visible');
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
function loadSession(session) {
|
|
1735
|
+
console.log('[session] 加载会话:', session.title);
|
|
1736
|
+
|
|
1737
|
+
// 保存当前会话数据
|
|
1738
|
+
currentSessionData = session;
|
|
1739
|
+
|
|
1740
|
+
// 切换到详情视图
|
|
1741
|
+
showSessionDetail();
|
|
1742
|
+
|
|
1743
|
+
// 更新详情页标题和元信息
|
|
1744
|
+
document.getElementById('session-detail-title').textContent = session.preview || session.title;
|
|
1745
|
+
document.getElementById('session-detail-time').textContent = formatTime(session.time_updated);
|
|
1746
|
+
document.getElementById('session-detail-dir').textContent = session.directory.replace(/^\/Users\/[^\/]+/, '~');
|
|
1747
|
+
|
|
1748
|
+
var contentDiv = document.getElementById('session-detail-content');
|
|
1749
|
+
contentDiv.innerHTML = '<div class="message-empty">加载消息中...</div>';
|
|
1750
|
+
|
|
1751
|
+
// 获取会话的所有消息
|
|
1752
|
+
fetch('/api/session/' + session.id)
|
|
1753
|
+
.then(function(response) { return response.json(); })
|
|
1754
|
+
.then(function(messages) {
|
|
1755
|
+
console.log('[session] 收到', messages.length, '条消息');
|
|
1756
|
+
|
|
1757
|
+
if (messages.length === 0) {
|
|
1758
|
+
contentDiv.innerHTML = '<div class="message-empty">该会话暂无消息</div>';
|
|
1759
|
+
return;
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
// 渲染消息列表
|
|
1763
|
+
var html = '';
|
|
1764
|
+
messages.forEach(function(msg) {
|
|
1765
|
+
console.log('[render] 消息:', msg);
|
|
1766
|
+
|
|
1767
|
+
var roleClass = msg.role === 'assistant' ? 'message-assistant' : 'message-user';
|
|
1768
|
+
var avatar = msg.role === 'user' ? '👤' : '🤖';
|
|
1769
|
+
var roleName = msg.role === 'user' ? 'User' : 'Assistant';
|
|
1770
|
+
|
|
1771
|
+
html += '<div class="message-item ' + roleClass + '">';
|
|
1772
|
+
html += '<div class="message-avatar">' + avatar + '</div>';
|
|
1773
|
+
html += '<div class="message-content">';
|
|
1774
|
+
html += '<div class="message-header">';
|
|
1775
|
+
html += '<div class="message-role">' + roleName + '</div>';
|
|
1776
|
+
html += '</div>';
|
|
1777
|
+
|
|
1778
|
+
// 显示推理过程(仅 assistant)
|
|
1779
|
+
if (msg.reasoning) {
|
|
1780
|
+
html += '<div class="message-text" style="background: #1a1a0a; border-color: #333;">';
|
|
1781
|
+
html += '<div style="color: #f0ad4e; font-size: 11px; font-weight: 600; margin-bottom: 6px;">💭 思考过程</div>';
|
|
1782
|
+
html += '<div style="color: #bbb;">' + escapeHtml(msg.reasoning) + '</div>';
|
|
1783
|
+
html += '</div>';
|
|
1784
|
+
}
|
|
1785
|
+
|
|
1786
|
+
// 显示文本内容
|
|
1787
|
+
if (msg.text) {
|
|
1788
|
+
html += '<div class="message-text">' + escapeHtml(msg.text) + '</div>';
|
|
1789
|
+
} else if (!msg.reasoning && !msg.toolCalls && !msg.toolResults) {
|
|
1790
|
+
html += '<div class="message-text" style="color: #666; font-style: italic;">(无文本内容)</div>';
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
// 显示工具调用
|
|
1794
|
+
if (msg.toolCalls && msg.toolCalls.length > 0) {
|
|
1795
|
+
msg.toolCalls.forEach(function(tool) {
|
|
1796
|
+
html += '<div class="message-tool-call">';
|
|
1797
|
+
html += '<div class="message-tool-name">🔧 工具调用: ' + escapeHtml(tool.name || '未知工具') + '</div>';
|
|
1798
|
+
html += '</div>';
|
|
1799
|
+
});
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
// 显示工具结果
|
|
1803
|
+
if (msg.toolResults && msg.toolResults.length > 0) {
|
|
1804
|
+
msg.toolResults.forEach(function(result) {
|
|
1805
|
+
if (result.text) {
|
|
1806
|
+
var resultText = result.text.length > 200 ? result.text.substring(0, 200) + '...' : result.text;
|
|
1807
|
+
html += '<div class="message-tool-call">';
|
|
1808
|
+
html += '<div class="message-tool-name">📝 工具结果</div>';
|
|
1809
|
+
html += '<div class="message-tool-result">' + escapeHtml(resultText) + '</div>';
|
|
1810
|
+
html += '</div>';
|
|
1811
|
+
}
|
|
1812
|
+
});
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
html += '</div>';
|
|
1816
|
+
html += '</div>';
|
|
1817
|
+
});
|
|
1818
|
+
|
|
1819
|
+
contentDiv.innerHTML = html;
|
|
1820
|
+
|
|
1821
|
+
// 滚动到底部
|
|
1822
|
+
contentDiv.scrollTop = contentDiv.scrollHeight;
|
|
1823
|
+
})
|
|
1824
|
+
.catch(function(err) {
|
|
1825
|
+
console.error('[session] 加载消息失败:', err);
|
|
1826
|
+
contentDiv.innerHTML = '<div class="message-empty">加载失败: ' + escapeHtml(err.message) + '</div>';
|
|
1827
|
+
});
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1830
|
+
function escapeHtml(text) {
|
|
1831
|
+
var div = document.createElement('div');
|
|
1832
|
+
div.textContent = text;
|
|
1833
|
+
return div.innerHTML;
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
function toggleHistoryBar() {
|
|
1837
|
+
historyBarVisible = !historyBarVisible;
|
|
1838
|
+
var historyBar = document.getElementById('session-history-bar');
|
|
1839
|
+
|
|
1840
|
+
if (historyBarVisible) {
|
|
1841
|
+
historyBar.classList.add('visible');
|
|
1842
|
+
if (currentMode === 'opencode') {
|
|
1843
|
+
loadSessions();
|
|
1844
|
+
} else {
|
|
1845
|
+
// claude 模式暂无历史会话功能
|
|
1846
|
+
var sessionList = document.getElementById('session-list');
|
|
1847
|
+
sessionList.innerHTML = '<div class="session-empty">Claude Code 暂不支持历史会话</div>';
|
|
1848
|
+
}
|
|
1849
|
+
} else {
|
|
1850
|
+
historyBar.classList.remove('visible');
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
// 新会话按钮
|
|
1855
|
+
var isCreatingNewSession = false;
|
|
1856
|
+
document.getElementById('new-session-btn').addEventListener('click', function() {
|
|
1857
|
+
if (!ws || ws.readyState !== 1) return;
|
|
1858
|
+
isCreatingNewSession = true;
|
|
1859
|
+
term.clear();
|
|
1860
|
+
ws.send(JSON.stringify({ type: 'new-session' }));
|
|
1861
|
+
});
|
|
1862
|
+
|
|
1863
|
+
// 绑定历史按钮
|
|
1864
|
+
document.getElementById('history-toggle').addEventListener('click', function() {
|
|
1865
|
+
toggleHistoryBar();
|
|
1866
|
+
});
|
|
1867
|
+
|
|
1868
|
+
// 刷新会话列表
|
|
1869
|
+
document.getElementById('refresh-sessions').addEventListener('click', function(e) {
|
|
1870
|
+
e.stopPropagation();
|
|
1871
|
+
loadSessions();
|
|
1872
|
+
});
|
|
1873
|
+
|
|
1874
|
+
// 关闭历史栏
|
|
1875
|
+
document.getElementById('close-history').addEventListener('click', function(e) {
|
|
1876
|
+
e.stopPropagation();
|
|
1877
|
+
toggleHistoryBar();
|
|
1878
|
+
});
|
|
1879
|
+
|
|
1880
|
+
// 返回到会话列表
|
|
1881
|
+
document.getElementById('back-to-list').addEventListener('click', function(e) {
|
|
1882
|
+
e.stopPropagation();
|
|
1883
|
+
showSessionList();
|
|
1884
|
+
});
|
|
1885
|
+
|
|
1886
|
+
// 恢复会话
|
|
1887
|
+
document.getElementById('restore-session').addEventListener('click', function(e) {
|
|
1888
|
+
e.stopPropagation();
|
|
1889
|
+
if (!currentSessionData) {
|
|
1890
|
+
console.error('[restore] 没有当前会话数据');
|
|
1891
|
+
return;
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
console.log('[restore] 恢复会话:', currentSessionData.id);
|
|
1895
|
+
|
|
1896
|
+
// 关闭历史栏
|
|
1897
|
+
toggleHistoryBar();
|
|
1898
|
+
|
|
1899
|
+
// 清空终端
|
|
1900
|
+
term.clear();
|
|
1901
|
+
|
|
1902
|
+
// 显示正在恢复的提示
|
|
1903
|
+
term.write('\x1b[1;36m╔══════════════════════════════════════════════════════════════╗\x1b[0m\r\n');
|
|
1904
|
+
term.write('\x1b[1;36m║ \x1b[1;37m正在恢复会话... \x1b[1;36m║\x1b[0m\r\n');
|
|
1905
|
+
term.write('\x1b[1;36m╚══════════════════════════════════════════════════════════════╝\x1b[0m\r\n');
|
|
1906
|
+
term.write('\r\n');
|
|
1907
|
+
|
|
1908
|
+
// 发送恢复会话的请求到服务端
|
|
1909
|
+
if (ws && ws.readyState === 1) {
|
|
1910
|
+
if (currentMode !== 'opencode') {
|
|
1911
|
+
term.write('\x1b[31m错误: 请先切换到 OpenCode 模式\x1b[0m\r\n');
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
term.write('\x1b[33m正在重启 OpenCode 并恢复会话: ' + currentSessionData.id + '\x1b[0m\r\n');
|
|
1916
|
+
term.write('\r\n');
|
|
1917
|
+
|
|
1918
|
+
// 发送恢复请求
|
|
1919
|
+
ws.send(JSON.stringify({
|
|
1920
|
+
type: 'restore',
|
|
1921
|
+
sessionId: currentSessionData.id
|
|
1922
|
+
}));
|
|
1923
|
+
} else {
|
|
1924
|
+
term.write('\x1b[31m错误: WebSocket 未连接\x1b[0m\r\n');
|
|
1925
|
+
}
|
|
1926
|
+
});
|
|
1927
|
+
|
|
1928
|
+
// 提取终端缓冲区文本
|
|
1929
|
+
function getTerminalText() {
|
|
1930
|
+
var buf = term.buffer.active;
|
|
1931
|
+
var lines = [];
|
|
1932
|
+
for (var i = 0; i < buf.length; i++) {
|
|
1933
|
+
var line = buf.getLine(i);
|
|
1934
|
+
if (line) lines.push(line.translateToString(true));
|
|
1935
|
+
}
|
|
1936
|
+
// 去除尾部空行
|
|
1937
|
+
while (lines.length > 0 && lines[lines.length - 1].trim() === '') {
|
|
1938
|
+
lines.pop();
|
|
1939
|
+
}
|
|
1940
|
+
return lines.join('\n');
|
|
1941
|
+
}
|
|
1942
|
+
|
|
1943
|
+
// 复制到剪贴板
|
|
1944
|
+
function copyToClipboard(text) {
|
|
1945
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
1946
|
+
navigator.clipboard.writeText(text).then(showCopyToast).catch(function() {
|
|
1947
|
+
fallbackCopy(text);
|
|
1948
|
+
});
|
|
1949
|
+
} else {
|
|
1950
|
+
fallbackCopy(text);
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
function fallbackCopy(text) {
|
|
1955
|
+
var ta = document.createElement('textarea');
|
|
1956
|
+
ta.value = text;
|
|
1957
|
+
ta.style.cssText = 'position:fixed;left:-9999px;top:-9999px';
|
|
1958
|
+
document.body.appendChild(ta);
|
|
1959
|
+
ta.select();
|
|
1960
|
+
document.execCommand('copy');
|
|
1961
|
+
document.body.removeChild(ta);
|
|
1962
|
+
showCopyToast();
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
function showCopyPopup(text) {
|
|
1966
|
+
var popup = document.getElementById('copy-popup');
|
|
1967
|
+
var ta = document.getElementById('copy-popup-text');
|
|
1968
|
+
ta.value = text;
|
|
1969
|
+
popup.classList.add('show');
|
|
1970
|
+
ta.focus();
|
|
1971
|
+
ta.select();
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
function showCopyToast() {
|
|
1975
|
+
var toast = document.getElementById('copy-toast');
|
|
1976
|
+
toast.classList.add('show');
|
|
1977
|
+
setTimeout(function() { toast.classList.remove('show'); }, 1200);
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
|
|
1981
|
+
// 方案2: 长按进入选择模式 — 原位显示可选纯文本
|
|
1982
|
+
var selectTextLayer = document.getElementById('select-text-layer');
|
|
1983
|
+
var selectTextPre = document.getElementById('select-text-pre');
|
|
1984
|
+
var selectModeClose = document.getElementById('select-mode-close');
|
|
1985
|
+
var inSelectMode = false;
|
|
1986
|
+
|
|
1987
|
+
function openSelectMode() {
|
|
1988
|
+
if (inSelectMode) return;
|
|
1989
|
+
inSelectMode = true;
|
|
1990
|
+
// 收起键盘
|
|
1991
|
+
var xtermTa = terminalEl.querySelector('.xterm-helper-textarea');
|
|
1992
|
+
if (xtermTa) xtermTa.blur();
|
|
1993
|
+
document.activeElement && document.activeElement.blur();
|
|
1994
|
+
var text = getTerminalText();
|
|
1995
|
+
selectTextPre.textContent = text || '(终端内容为空)';
|
|
1996
|
+
terminalEl.classList.add('select-mode');
|
|
1997
|
+
selectTextLayer.classList.add('visible');
|
|
1998
|
+
selectModeClose.style.display = 'block';
|
|
1999
|
+
unbindTouchScroll();
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
function closeSelectMode() {
|
|
2003
|
+
if (!inSelectMode) return;
|
|
2004
|
+
inSelectMode = false;
|
|
2005
|
+
terminalEl.classList.remove('select-mode');
|
|
2006
|
+
selectTextLayer.classList.remove('visible');
|
|
2007
|
+
selectModeClose.style.display = 'none';
|
|
2008
|
+
window.getSelection().removeAllRanges();
|
|
2009
|
+
rebindTouchScroll();
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
selectModeClose.addEventListener('click', function(e) {
|
|
2013
|
+
e.preventDefault();
|
|
2014
|
+
e.stopPropagation();
|
|
2015
|
+
closeSelectMode();
|
|
2016
|
+
});
|
|
2017
|
+
|
|
2018
|
+
selectModeClose.addEventListener('touchend', function(e) {
|
|
2019
|
+
e.preventDefault();
|
|
2020
|
+
e.stopPropagation();
|
|
2021
|
+
closeSelectMode();
|
|
2022
|
+
});
|
|
2023
|
+
|
|
2024
|
+
// ======= Git Diff 功能 =======
|
|
2025
|
+
var diffBarVisible = false;
|
|
2026
|
+
var diffChanges = [];
|
|
2027
|
+
var diffSelectedFile = null;
|
|
2028
|
+
|
|
2029
|
+
var STATUS_COLORS = {
|
|
2030
|
+
'M': '#e2c08d', 'A': '#73c991', 'D': '#f14c4c',
|
|
2031
|
+
'R': '#73c991', 'C': '#73c991', 'U': '#e2c08d',
|
|
2032
|
+
'?': '#73c991', '??': '#73c991',
|
|
2033
|
+
};
|
|
2034
|
+
|
|
2035
|
+
function toggleDiffBar() {
|
|
2036
|
+
diffBarVisible = !diffBarVisible;
|
|
2037
|
+
var bar = document.getElementById('git-diff-bar');
|
|
2038
|
+
if (diffBarVisible) {
|
|
2039
|
+
bar.classList.add('visible');
|
|
2040
|
+
loadGitStatus();
|
|
2041
|
+
} else {
|
|
2042
|
+
bar.classList.remove('visible');
|
|
2043
|
+
diffSelectedFile = null;
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
function loadGitStatus() {
|
|
2048
|
+
var fileList = document.getElementById('git-diff-file-list');
|
|
2049
|
+
fileList.innerHTML = '<div class="git-diff-loading">加载中...</div>';
|
|
2050
|
+
document.getElementById('git-diff-count').textContent = '0';
|
|
2051
|
+
|
|
2052
|
+
fetch('/api/git-status')
|
|
2053
|
+
.then(function(r) { return r.json(); })
|
|
2054
|
+
.then(function(data) {
|
|
2055
|
+
diffChanges = data.changes || [];
|
|
2056
|
+
document.getElementById('git-diff-count').textContent = diffChanges.length;
|
|
2057
|
+
renderDiffFileList();
|
|
2058
|
+
})
|
|
2059
|
+
.catch(function() {
|
|
2060
|
+
fileList.innerHTML = '<div class="git-diff-error">无法加载 git status</div>';
|
|
2061
|
+
});
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
function renderDiffFileList() {
|
|
2065
|
+
var fileList = document.getElementById('git-diff-file-list');
|
|
2066
|
+
if (diffChanges.length === 0) {
|
|
2067
|
+
fileList.innerHTML = '<div class="git-diff-loading" style="color:#666;">没有变更文件</div>';
|
|
2068
|
+
return;
|
|
2069
|
+
}
|
|
2070
|
+
var html = '';
|
|
2071
|
+
diffChanges.forEach(function(c) {
|
|
2072
|
+
var color = STATUS_COLORS[c.status] || '#888';
|
|
2073
|
+
var label = c.status === '??' ? 'U' : c.status;
|
|
2074
|
+
var activeClass = diffSelectedFile === c.file ? ' active' : '';
|
|
2075
|
+
html += '<div class="git-diff-file-item' + activeClass + '" data-file="' + escapeHtml(c.file) + '">';
|
|
2076
|
+
html += '<span class="git-diff-file-status" style="color:' + color + '">' + label + '</span>';
|
|
2077
|
+
html += '<span class="git-diff-file-name">' + escapeHtml(c.file) + '</span>';
|
|
2078
|
+
html += '</div>';
|
|
2079
|
+
});
|
|
2080
|
+
fileList.innerHTML = html;
|
|
2081
|
+
|
|
2082
|
+
// 绑定点击事件
|
|
2083
|
+
fileList.querySelectorAll('.git-diff-file-item').forEach(function(item) {
|
|
2084
|
+
item.addEventListener('click', function() {
|
|
2085
|
+
var file = item.getAttribute('data-file');
|
|
2086
|
+
diffSelectedFile = file;
|
|
2087
|
+
// 更新选中状态
|
|
2088
|
+
fileList.querySelectorAll('.git-diff-file-item').forEach(function(el) {
|
|
2089
|
+
el.classList.toggle('active', el.getAttribute('data-file') === file);
|
|
2090
|
+
});
|
|
2091
|
+
loadDiffContent(file);
|
|
2092
|
+
});
|
|
2093
|
+
});
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
function loadDiffContent(file) {
|
|
2097
|
+
var area = document.getElementById('git-diff-content-area');
|
|
2098
|
+
area.innerHTML = '<div class="git-diff-loading">加载 diff...</div>';
|
|
2099
|
+
|
|
2100
|
+
fetch('/api/git-diff?files=' + encodeURIComponent(file))
|
|
2101
|
+
.then(function(r) { return r.json(); })
|
|
2102
|
+
.then(function(data) {
|
|
2103
|
+
if (!data.diffs || !data.diffs[0]) {
|
|
2104
|
+
area.innerHTML = '<div class="git-diff-error">无 diff 数据</div>';
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
var d = data.diffs[0];
|
|
2108
|
+
var html = '<div class="git-diff-content-header">';
|
|
2109
|
+
html += '<span class="git-diff-content-path">' + escapeHtml(d.file) + '</span>';
|
|
2110
|
+
html += '<span class="git-diff-badge">' + (d.is_new ? 'NEW' : d.is_deleted ? 'DEL' : 'DIFF') + '</span>';
|
|
2111
|
+
html += '</div>';
|
|
2112
|
+
html += '<div class="git-diff-content-scroll">';
|
|
2113
|
+
|
|
2114
|
+
if (d.is_binary) {
|
|
2115
|
+
html += '<div class="git-diff-loading" style="font-style:italic;">二进制文件</div>';
|
|
2116
|
+
} else if (d.is_large) {
|
|
2117
|
+
html += '<div class="git-diff-loading" style="color:#e2c08d;">文件过大: ' + (d.size / (1024 * 1024)).toFixed(2) + ' MB</div>';
|
|
2118
|
+
} else if (d.unified_diff) {
|
|
2119
|
+
html += renderUnifiedDiff(d.unified_diff);
|
|
2120
|
+
} else {
|
|
2121
|
+
html += '<div class="git-diff-loading" style="color:#666;">无变更内容</div>';
|
|
2122
|
+
}
|
|
2123
|
+
|
|
2124
|
+
html += '</div>';
|
|
2125
|
+
area.innerHTML = html;
|
|
2126
|
+
})
|
|
2127
|
+
.catch(function(err) {
|
|
2128
|
+
area.innerHTML = '<div class="git-diff-error">加载失败: ' + escapeHtml(err.message) + '</div>';
|
|
2129
|
+
});
|
|
2130
|
+
}
|
|
2131
|
+
|
|
2132
|
+
function renderUnifiedDiff(diffText) {
|
|
2133
|
+
var lines = diffText.split('\n');
|
|
2134
|
+
var html = '<table class="diff-table">';
|
|
2135
|
+
var oldLine = 0, newLine = 0;
|
|
2136
|
+
|
|
2137
|
+
for (var i = 0; i < lines.length; i++) {
|
|
2138
|
+
var line = lines[i];
|
|
2139
|
+
|
|
2140
|
+
// 跳过 diff 头部信息
|
|
2141
|
+
if (line.startsWith('diff --git') || line.startsWith('index ') ||
|
|
2142
|
+
line.startsWith('---') || line.startsWith('+++') ||
|
|
2143
|
+
line.startsWith('new file') || line.startsWith('deleted file') ||
|
|
2144
|
+
line.startsWith('old mode') || line.startsWith('new mode')) {
|
|
2145
|
+
continue;
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
if (line.startsWith('@@')) {
|
|
2149
|
+
// 解析 hunk header: @@ -old,count +new,count @@
|
|
2150
|
+
var match = line.match(/@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
|
|
2151
|
+
if (match) {
|
|
2152
|
+
oldLine = parseInt(match[1], 10);
|
|
2153
|
+
newLine = parseInt(match[2], 10);
|
|
2154
|
+
}
|
|
2155
|
+
html += '<tr class="diff-line diff-line-hunk">';
|
|
2156
|
+
html += '<td class="diff-line-num"></td><td class="diff-line-num"></td>';
|
|
2157
|
+
html += '<td class="diff-line-content">' + escapeHtml(line) + '</td></tr>';
|
|
2158
|
+
} else if (line.startsWith('+')) {
|
|
2159
|
+
html += '<tr class="diff-line diff-line-add">';
|
|
2160
|
+
html += '<td class="diff-line-num"></td>';
|
|
2161
|
+
html += '<td class="diff-line-num">' + newLine + '</td>';
|
|
2162
|
+
html += '<td class="diff-line-content">' + escapeHtml(line.substring(1)) + '</td></tr>';
|
|
2163
|
+
newLine++;
|
|
2164
|
+
} else if (line.startsWith('-')) {
|
|
2165
|
+
html += '<tr class="diff-line diff-line-del">';
|
|
2166
|
+
html += '<td class="diff-line-num">' + oldLine + '</td>';
|
|
2167
|
+
html += '<td class="diff-line-num"></td>';
|
|
2168
|
+
html += '<td class="diff-line-content">' + escapeHtml(line.substring(1)) + '</td></tr>';
|
|
2169
|
+
oldLine++;
|
|
2170
|
+
} else if (line.startsWith(' ') || (line === '' && i < lines.length - 1)) {
|
|
2171
|
+
html += '<tr class="diff-line">';
|
|
2172
|
+
html += '<td class="diff-line-num">' + oldLine + '</td>';
|
|
2173
|
+
html += '<td class="diff-line-num">' + newLine + '</td>';
|
|
2174
|
+
html += '<td class="diff-line-content">' + escapeHtml(line.substring(1) || '') + '</td></tr>';
|
|
2175
|
+
oldLine++;
|
|
2176
|
+
newLine++;
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
html += '</table>';
|
|
2181
|
+
return html;
|
|
2182
|
+
}
|
|
2183
|
+
|
|
2184
|
+
// Diff 按钮事件绑定
|
|
2185
|
+
document.getElementById('diff-toggle').addEventListener('click', toggleDiffBar);
|
|
2186
|
+
document.getElementById('close-diff').addEventListener('click', toggleDiffBar);
|
|
2187
|
+
document.getElementById('refresh-diff').addEventListener('click', function(e) {
|
|
2188
|
+
e.stopPropagation();
|
|
2189
|
+
loadGitStatus();
|
|
2190
|
+
// 重置 diff 内容区
|
|
2191
|
+
diffSelectedFile = null;
|
|
2192
|
+
document.getElementById('git-diff-content-area').innerHTML =
|
|
2193
|
+
'<div class="git-diff-placeholder">' +
|
|
2194
|
+
'<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="#333" stroke-width="1.5">' +
|
|
2195
|
+
'<line x1="6" y1="3" x2="6" y2="15"></line>' +
|
|
2196
|
+
'<circle cx="18" cy="6" r="3"></circle>' +
|
|
2197
|
+
'<circle cx="6" cy="18" r="3"></circle>' +
|
|
2198
|
+
'<path d="M18 9a9 9 0 0 1-9 9"></path>' +
|
|
2199
|
+
'</svg><span>点击文件查看 diff</span></div>';
|
|
2200
|
+
});
|
|
2201
|
+
|
|
2202
|
+
connect();
|
|
2203
|
+
setTimeout(resize, 100);
|
|
2204
|
+
})();
|
|
2205
|
+
</script>
|
|
2206
|
+
</body>
|
|
2207
|
+
</html>
|