|
17 | 17 | let ws: WebSocket; |
18 | 18 | let resizeObs: ResizeObserver; |
19 | 19 |
|
| 20 | + function copyToClipboard(text: string): void { |
| 21 | + if (navigator.clipboard?.writeText) { |
| 22 | + navigator.clipboard.writeText(text).catch(() => {}); |
| 23 | + return; |
| 24 | + } |
| 25 | + // Fallback for non-secure contexts (HTTP on non-localhost) |
| 26 | + const ta = document.createElement("textarea"); |
| 27 | + ta.value = text; |
| 28 | + ta.style.position = "fixed"; |
| 29 | + ta.style.opacity = "0"; |
| 30 | + document.body.appendChild(ta); |
| 31 | + ta.select(); |
| 32 | + document.execCommand("copy"); |
| 33 | + document.body.removeChild(ta); |
| 34 | + } |
| 35 | +
|
20 | 36 | export function sendSelectPane(pane: number) { |
21 | 37 | if (ws?.readyState === WebSocket.OPEN) { |
22 | 38 | ws.send(JSON.stringify({ type: "selectPane", pane })); |
|
57 | 73 | if (idx !== -1) { |
58 | 74 | const b64 = data.slice(idx + 1); |
59 | 75 | try { |
60 | | - const text = atob(b64); |
61 | | - navigator.clipboard.writeText(text); |
| 76 | + copyToClipboard(atob(b64)); |
62 | 77 | } catch {} |
63 | 78 | } |
64 | 79 | return true; |
|
68 | 83 | term.onSelectionChange(() => { |
69 | 84 | const sel = term.getSelection(); |
70 | 85 | if (sel) { |
71 | | - navigator.clipboard.writeText(sel); |
| 86 | + copyToClipboard(sel); |
72 | 87 | } |
73 | 88 | }); |
74 | 89 |
|
|
77 | 92 | term.attachCustomKeyEventHandler((e: KeyboardEvent) => { |
78 | 93 | if (e.type !== "keydown") return true; |
79 | 94 | const mod = e.metaKey || e.ctrlKey; |
| 95 | + if (mod && (e.key === "c" || e.key === "C")) { |
| 96 | + if (term.hasSelection()) { |
| 97 | + copyToClipboard(term.getSelection()); |
| 98 | + term.clearSelection(); |
| 99 | + return false; |
| 100 | + } |
| 101 | + return true; |
| 102 | + } |
80 | 103 | if (mod && (e.key === "ArrowUp" || e.key === "ArrowDown")) return false; |
81 | 104 | if (mod && (e.key === "k" || e.key === "K")) return false; |
82 | 105 | if (mod && (e.key === "m" || e.key === "M")) return false; |
|
0 commit comments