opencode commited on
Commit
4db130b
·
1 Parent(s): 53d84c3

終端機分頁支援系統剪貼簿:Ctrl+Shift+C 複製、Ctrl+V 貼上

Browse files
Files changed (2) hide show
  1. README.md +4 -1
  2. app.py +89 -12
README.md CHANGED
@@ -65,7 +65,10 @@ OpenCode 在啟動時安裝,並重設使用者層級設定為預設值。
65
  LXDE 畫面;關掉瀏覽器分頁不會終止桌面,重新整理或重新連線都會接回原畫面。
66
  - 這個分頁是固定的:按它的 `×` 不會刪除分頁,而是重新啟動一份新的桌面
67
  (`POST /api/desktop/restart`),永遠維持一個。
68
- - 舊的 tmux 終端機分頁、上傳/下載/複製/貼上等行為完全沒有改變。
 
 
 
69
  - 除錯端點:`GET /api/desktop/status`、`POST /api/desktop/restart`(程序 pid、VNC port、
70
  健康狀態);桌面 log 在 `/tmp/webterm-desktop.log`。
71
  - 可用 Space 變數覆寫:`DESKTOP_DISPLAY`(預設 `:99`)、`DESKTOP_GEOMETRY`
 
65
  LXDE 畫面;關掉瀏覽器分頁不會終止桌面,重新整理或重新連線都會接回原畫面。
66
  - 這個分頁是固定的:按它的 `×` 不會刪除分頁,而是重新啟動一份新的桌面
67
  (`POST /api/desktop/restart`),永遠維持一個。
68
+ - 終端機分頁也支援系統剪貼簿:選取後按 **Ctrl+Shift+C**(或 Ctrl+Insert)複製,
69
+ **Ctrl+V**、**Ctrl+Shift+V**(或 Shift+Insert)直接貼上;「📋 複製」/「📌 貼上」
70
+ 按鈕行為相同。貼上走 xterm.js 的 bracketed paste,與一般終端機一致。
71
+ - 上傳/下載等其餘行為完全沒有改變。
72
  - 除錯端點:`GET /api/desktop/status`、`POST /api/desktop/restart`(程序 pid、VNC port、
73
  健康狀態);桌面 log 在 `/tmp/webterm-desktop.log`。
74
  - 可用 Space 變數覆寫:`DESKTOP_DISPLAY`(預設 `:99`)、`DESKTOP_GEOMETRY`
app.py CHANGED
@@ -124,8 +124,8 @@ body{display:flex;flex-direction:column}
124
  <body>
125
  <div id="tab-bar">
126
  <button id="add-btn" title="New Terminal (Ctrl+Shift+T)">+</button>
127
- <button id="copy-btn" class="action-btn" title="Copy selected text">📋 複製</button>
128
- <button id="paste-btn" class="action-btn" title="Paste to terminal">📌 貼上</button>
129
  <button id="upload-btn" class="action-btn" title="Upload to current directory">📤 上傳</button>
130
  <button id="download-btn" class="action-btn" title="Browse and download server files">📥 下載</button>
131
  <div id="app-status" role="status" aria-live="polite" title="應用程式訊息">就緒</div>
@@ -296,6 +296,51 @@ function connect(tabObj) {
296
  ws.onerror = () => ws.close();
297
  }
298
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
299
  function createTab(sessionId = null, title = null, activateIt = true) {
300
  const id = ++cnt;
301
 
@@ -330,6 +375,41 @@ function createTab(sessionId = null, title = null, activateIt = true) {
330
  connect(obj);
331
  saveActive();
332
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
333
  term.onData(d => {
334
  if (obj.ws && obj.ws.readyState === WebSocket.OPEN) obj.ws.send(JSON.stringify({type: 'input', data: d}));
335
  });
@@ -524,12 +604,8 @@ copyBtn.onclick = async () => {
524
  }
525
  const text = t.term.getSelection();
526
  if (text) {
527
- try {
528
- await navigator.clipboard.writeText(text);
529
- setAppStatus('已複製選取文字到系統剪貼簿', 'success');
530
- } catch (err) {
531
- setAppStatus('複製失敗,請檢查瀏覽器權限', 'error');
532
- }
533
  } else {
534
  setAppStatus('請先在終端機中選取要複製的文字', 'warning');
535
  }
@@ -542,12 +618,13 @@ pasteBtn.onclick = async () => {
542
  setAppStatus('桌面分頁請使用桌面內應用程式的貼上功能', 'info');
543
  return;
544
  }
545
- try {
546
- const text = await navigator.clipboard.readText();
547
- if (t.ws && t.ws.readyState === WebSocket.OPEN) t.ws.send(JSON.stringify({type: 'input', data: text}));
548
- } catch (err) {
549
  setAppStatus('貼上失敗,請允許瀏覽器讀取剪貼簿權限', 'error');
 
550
  }
 
 
551
  };
552
 
553
  uploadBtn.onclick = () => {
 
124
  <body>
125
  <div id="tab-bar">
126
  <button id="add-btn" title="New Terminal (Ctrl+Shift+T)">+</button>
127
+ <button id="copy-btn" class="action-btn" title="複製選取文字(Ctrl+Shift+C)">📋 複製</button>
128
+ <button id="paste-btn" class="action-btn" title="從系統剪貼簿貼上(Ctrl+V、Ctrl+Shift+V、Shift+Insert)">📌 貼上</button>
129
  <button id="upload-btn" class="action-btn" title="Upload to current directory">📤 上傳</button>
130
  <button id="download-btn" class="action-btn" title="Browse and download server files">📥 下載</button>
131
  <div id="app-status" role="status" aria-live="polite" title="應用程式訊息">就緒</div>
 
296
  ws.onerror = () => ws.close();
297
  }
298
 
299
+ async function copyTextToClipboard(text) {
300
+ if (!text) return false;
301
+ try {
302
+ await navigator.clipboard.writeText(text);
303
+ return true;
304
+ } catch (err) {
305
+ try {
306
+ const ta = document.createElement('textarea');
307
+ ta.value = text;
308
+ ta.setAttribute('readonly', '');
309
+ ta.style.cssText = 'position:fixed;top:0;left:0;width:1px;height:1px;opacity:0;border:0;padding:0';
310
+ document.body.appendChild(ta);
311
+ ta.select();
312
+ ta.setSelectionRange(0, text.length);
313
+ const ok = document.execCommand('copy');
314
+ ta.remove();
315
+ return ok;
316
+ } catch (err2) {
317
+ return false;
318
+ }
319
+ }
320
+ }
321
+
322
+ async function readClipboardText() {
323
+ try {
324
+ return await navigator.clipboard.readText();
325
+ } catch (err) {
326
+ return null;
327
+ }
328
+ }
329
+
330
+ function pasteToTerm(t, text) {
331
+ if (!text) return false;
332
+ if (t.term && typeof t.term.paste === 'function') {
333
+ t.term.paste(text);
334
+ t.term.focus();
335
+ return true;
336
+ }
337
+ if (t.ws && t.ws.readyState === WebSocket.OPEN) {
338
+ t.ws.send(JSON.stringify({type: 'input', data: text}));
339
+ return true;
340
+ }
341
+ return false;
342
+ }
343
+
344
  function createTab(sessionId = null, title = null, activateIt = true) {
345
  const id = ++cnt;
346
 
 
375
  connect(obj);
376
  saveActive();
377
 
378
+ term.attachCustomKeyEventHandler(e => {
379
+ if (e.type !== 'keydown') return true;
380
+ const key = (e.key || '').toLowerCase();
381
+ const cmd = e.ctrlKey || e.metaKey;
382
+ const copyCombo = cmd && e.shiftKey && key === 'c' || e.ctrlKey && e.key === 'Insert';
383
+ const pasteCombo = cmd && key === 'v' || e.shiftKey && e.key === 'Insert';
384
+ if (copyCombo) {
385
+ e.preventDefault();
386
+ e.stopPropagation();
387
+ const sel = term.getSelection();
388
+ if (!sel) {
389
+ setAppStatus('請先在終端機中選取要複製的文字', 'warning');
390
+ return false;
391
+ }
392
+ copyTextToClipboard(sel).then(ok => {
393
+ setAppStatus(ok ? '已複製選取文字到系統剪貼簿' : '複製失敗,請檢查瀏覽器權限', ok ? 'success' : 'error');
394
+ });
395
+ return false;
396
+ }
397
+ if (pasteCombo) {
398
+ e.preventDefault();
399
+ e.stopPropagation();
400
+ readClipboardText().then(text => {
401
+ if (text === null) {
402
+ setAppStatus('貼上失敗,請允許瀏覽器讀取剪貼簿權限', 'error');
403
+ return;
404
+ }
405
+ if (pasteToTerm(obj, text)) setAppStatus('已從系統剪貼簿貼上', 'success');
406
+ else setAppStatus('貼上失敗,終端機尚未連線', 'error');
407
+ });
408
+ return false;
409
+ }
410
+ return true;
411
+ });
412
+
413
  term.onData(d => {
414
  if (obj.ws && obj.ws.readyState === WebSocket.OPEN) obj.ws.send(JSON.stringify({type: 'input', data: d}));
415
  });
 
604
  }
605
  const text = t.term.getSelection();
606
  if (text) {
607
+ const ok = await copyTextToClipboard(text);
608
+ setAppStatus(ok ? '已複製選取文字到系統剪貼簿' : '複製失敗,請檢查瀏覽器權限', ok ? 'success' : 'error');
 
 
 
 
609
  } else {
610
  setAppStatus('請先在終端機中選取要複製的文字', 'warning');
611
  }
 
618
  setAppStatus('桌面分頁請使用桌面內應用程式的貼上功能', 'info');
619
  return;
620
  }
621
+ const text = await readClipboardText();
622
+ if (text === null) {
 
 
623
  setAppStatus('貼上失敗,請允許瀏覽器讀取剪貼簿權限', 'error');
624
+ return;
625
  }
626
+ if (pasteToTerm(t, text)) setAppStatus('已從系統剪貼簿貼上', 'success');
627
+ else setAppStatus('貼上失敗��終端機尚未連線', 'error');
628
  };
629
 
630
  uploadBtn.onclick = () => {