Spaces:
Paused
Paused
Restore xterm UI after terminal refresh
Browse files
app.py
CHANGED
|
@@ -134,6 +134,7 @@ body{display:flex;flex-direction:column}
|
|
| 134 |
<script src="https://cdn.jsdelivr.net/npm/@xterm/xterm@5.5.0/lib/xterm.min.js"></script>
|
| 135 |
<script src="https://cdn.jsdelivr.net/npm/@xterm/addon-fit@0.10.0/lib/addon-fit.min.js"></script>
|
| 136 |
<script src="https://cdn.jsdelivr.net/npm/@xterm/addon-web-links@0.11.0/lib/addon-web-links.min.js"></script>
|
|
|
|
| 137 |
<script>
|
| 138 |
const bar = document.getElementById('tab-bar');
|
| 139 |
const box = document.getElementById('term-box');
|
|
@@ -162,6 +163,7 @@ let filePickerSelected = null;
|
|
| 162 |
let filePickerHistory = [];
|
| 163 |
|
| 164 |
let tabs = [], activeId = null, cnt = 0;
|
|
|
|
| 165 |
|
| 166 |
function activeTab() { return tabs.find(t => t.id === activeId); }
|
| 167 |
|
|
@@ -185,13 +187,29 @@ function wsUrl(sessionId, lastOutputSeq = 0) {
|
|
| 185 |
// ── 分頁狀態持久化:把「分頁 id + 對應的伺服器 session_id」存進 localStorage,
|
| 186 |
// 這樣重新整理整個網頁,甚至瀏覽器關掉重開,都能接回原本還在跑的終端機。
|
| 187 |
function saveTabs() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
try {
|
| 189 |
-
const data = tabs.map(t => ({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
localStorage.setItem('webterm_tabs', JSON.stringify(data));
|
| 191 |
localStorage.setItem('webterm_cnt', String(cnt));
|
| 192 |
} catch (e) { /* localStorage 不可用時忽略 */ }
|
| 193 |
}
|
| 194 |
|
|
|
|
|
|
|
| 195 |
function loadTabs() {
|
| 196 |
try {
|
| 197 |
const raw = localStorage.getItem('webterm_tabs');
|
|
@@ -206,6 +224,7 @@ function connect(tabObj) {
|
|
| 206 |
const ws = new WebSocket(wsUrl(tabObj.sessionId, tabObj.lastOutputSeq));
|
| 207 |
tabObj.ws = ws;
|
| 208 |
tabObj.reconnectDelay = tabObj.reconnectDelay || 500;
|
|
|
|
| 209 |
|
| 210 |
ws.onopen = () => {
|
| 211 |
const wasDown = tabObj.el.querySelector('.status-dot').classList.contains('down');
|
|
@@ -222,6 +241,13 @@ function connect(tabObj) {
|
|
| 222 |
// 伺服器告訴我們這條連線對應的 session id:記住它,下次斷線/重整才能接回同一個終端機
|
| 223 |
if (tabObj.sessionId !== m.id) tabObj.lastOutputSeq = 0;
|
| 224 |
tabObj.sessionId = m.id;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
if (m.reattached) setAppStatus(`終端機 ${tabObj.id} 已接回原工作階段`, 'success');
|
| 226 |
saveTabs();
|
| 227 |
} else if (m.type === 'output') {
|
|
@@ -235,7 +261,12 @@ function connect(tabObj) {
|
|
| 235 |
tabObj.currentDir = match[1];
|
| 236 |
}
|
| 237 |
m.data = m.data.replace(/\x1b\]0;PWD:.*?\x07/g, '');
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
tabObj.term.write(m.data);
|
|
|
|
| 239 |
} else if (m.type === 'system') {
|
| 240 |
const message = String(m.data).replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, '').trim();
|
| 241 |
const kind = /失敗|無法|錯誤|❌/i.test(message) ? 'error' : /⚠️|找不到|逾時/i.test(message) ? 'warning' : /成功|已重新連線|已接回|✅/i.test(message) ? 'success' : 'info';
|
|
@@ -258,8 +289,9 @@ function connect(tabObj) {
|
|
| 258 |
ws.onerror = () => ws.close();
|
| 259 |
}
|
| 260 |
|
| 261 |
-
function createTab(type = 'terminal', sessionId = null) {
|
| 262 |
const id = ++cnt;
|
|
|
|
| 263 |
|
| 264 |
const el = document.createElement('div');
|
| 265 |
el.className = 'tab';
|
|
@@ -274,6 +306,8 @@ function createTab(type = 'terminal', sessionId = null) {
|
|
| 274 |
|
| 275 |
const term = new Terminal({
|
| 276 |
cursorBlink: true,
|
|
|
|
|
|
|
| 277 |
fontSize: 14,
|
| 278 |
fontFamily: "'Cascadia Code', 'Fira Code', Consolas, monospace",
|
| 279 |
allowProposedApi: true,
|
|
@@ -283,10 +317,16 @@ function createTab(type = 'terminal', sessionId = null) {
|
|
| 283 |
const fit = new FitAddon.FitAddon();
|
| 284 |
term.loadAddon(fit);
|
| 285 |
term.loadAddon(new WebLinksAddon.WebLinksAddon());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
term.open(wrap);
|
| 287 |
-
fit.fit();
|
|
|
|
| 288 |
|
| 289 |
-
const obj = {id, type: 'terminal', el, wrap, term, fit, ws: null, closed: false, currentDir: '~', sessionId: sessionId || null, lastOutputSeq: 0};
|
| 290 |
tabs.push(obj);
|
| 291 |
connect(obj);
|
| 292 |
saveTabs();
|
|
@@ -573,7 +613,7 @@ document.addEventListener('keydown', e => {
|
|
| 573 |
if (Array.isArray(saved) && saved.length) {
|
| 574 |
for (const savedTab of saved) {
|
| 575 |
cnt = Math.max(cnt, Number(savedTab.id) || 0);
|
| 576 |
-
createTab('terminal', savedTab.sessionId || null);
|
| 577 |
}
|
| 578 |
} else {
|
| 579 |
if (savedCnt) cnt = savedCnt;
|
|
|
|
| 134 |
<script src="https://cdn.jsdelivr.net/npm/@xterm/xterm@5.5.0/lib/xterm.min.js"></script>
|
| 135 |
<script src="https://cdn.jsdelivr.net/npm/@xterm/addon-fit@0.10.0/lib/addon-fit.min.js"></script>
|
| 136 |
<script src="https://cdn.jsdelivr.net/npm/@xterm/addon-web-links@0.11.0/lib/addon-web-links.min.js"></script>
|
| 137 |
+
<script src="https://cdn.jsdelivr.net/npm/@xterm/addon-serialize@0.13.0/lib/addon-serialize.min.js"></script>
|
| 138 |
<script>
|
| 139 |
const bar = document.getElementById('tab-bar');
|
| 140 |
const box = document.getElementById('term-box');
|
|
|
|
| 163 |
let filePickerHistory = [];
|
| 164 |
|
| 165 |
let tabs = [], activeId = null, cnt = 0;
|
| 166 |
+
let tabsSaveTimer = null;
|
| 167 |
|
| 168 |
function activeTab() { return tabs.find(t => t.id === activeId); }
|
| 169 |
|
|
|
|
| 187 |
// ── 分頁狀態持久化:把「分頁 id + 對應的伺服器 session_id」存進 localStorage,
|
| 188 |
// 這樣重新整理整個網頁,甚至瀏覽器關掉重開,都能接回原本還在跑的終端機。
|
| 189 |
function saveTabs() {
|
| 190 |
+
if (tabsSaveTimer) clearTimeout(tabsSaveTimer);
|
| 191 |
+
tabsSaveTimer = setTimeout(flushTabs, 300);
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
function flushTabs() {
|
| 195 |
+
tabsSaveTimer = null;
|
| 196 |
try {
|
| 197 |
+
const data = tabs.map(t => ({
|
| 198 |
+
id: t.id,
|
| 199 |
+
type: t.type,
|
| 200 |
+
sessionId: t.sessionId || null,
|
| 201 |
+
lastOutputSeq: t.lastOutputSeq || 0,
|
| 202 |
+
cols: t.term ? t.term.cols : 80,
|
| 203 |
+
rows: t.term ? t.term.rows : 24,
|
| 204 |
+
screen: t.serialize ? t.serialize.serialize({scrollback: 2000}) : ''
|
| 205 |
+
}));
|
| 206 |
localStorage.setItem('webterm_tabs', JSON.stringify(data));
|
| 207 |
localStorage.setItem('webterm_cnt', String(cnt));
|
| 208 |
} catch (e) { /* localStorage 不可用時忽略 */ }
|
| 209 |
}
|
| 210 |
|
| 211 |
+
window.addEventListener('pagehide', flushTabs);
|
| 212 |
+
|
| 213 |
function loadTabs() {
|
| 214 |
try {
|
| 215 |
const raw = localStorage.getItem('webterm_tabs');
|
|
|
|
| 224 |
const ws = new WebSocket(wsUrl(tabObj.sessionId, tabObj.lastOutputSeq));
|
| 225 |
tabObj.ws = ws;
|
| 226 |
tabObj.reconnectDelay = tabObj.reconnectDelay || 500;
|
| 227 |
+
let waitingForReconnectRedraw = false;
|
| 228 |
|
| 229 |
ws.onopen = () => {
|
| 230 |
const wasDown = tabObj.el.querySelector('.status-dot').classList.contains('down');
|
|
|
|
| 241 |
// 伺服器告訴我們這條連線對應的 session id:記住它,下次斷線/重整才能接回同一個終端機
|
| 242 |
if (tabObj.sessionId !== m.id) tabObj.lastOutputSeq = 0;
|
| 243 |
tabObj.sessionId = m.id;
|
| 244 |
+
if (m.reattached && tabObj.restoredScreen) {
|
| 245 |
+
tabObj.term.write('\x1b[2J\x1b[H' + tabObj.restoredScreen, () => {
|
| 246 |
+
tabObj.restoredScreen = '';
|
| 247 |
+
waitingForReconnectRedraw = true;
|
| 248 |
+
ws.send(JSON.stringify({type: 'resize', cols: tabObj.term.cols, rows: tabObj.term.rows}));
|
| 249 |
+
});
|
| 250 |
+
}
|
| 251 |
if (m.reattached) setAppStatus(`終端機 ${tabObj.id} 已接回原工作階段`, 'success');
|
| 252 |
saveTabs();
|
| 253 |
} else if (m.type === 'output') {
|
|
|
|
| 261 |
tabObj.currentDir = match[1];
|
| 262 |
}
|
| 263 |
m.data = m.data.replace(/\x1b\]0;PWD:.*?\x07/g, '');
|
| 264 |
+
if (waitingForReconnectRedraw) {
|
| 265 |
+
waitingForReconnectRedraw = false;
|
| 266 |
+
tabObj.term.write('\x1b[2J\x1b[H');
|
| 267 |
+
}
|
| 268 |
tabObj.term.write(m.data);
|
| 269 |
+
tabObj.term.write('', () => saveTabs());
|
| 270 |
} else if (m.type === 'system') {
|
| 271 |
const message = String(m.data).replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, '').trim();
|
| 272 |
const kind = /失敗|無法|錯誤|❌/i.test(message) ? 'error' : /⚠️|找不到|逾時/i.test(message) ? 'warning' : /成功|已重新連線|已接回|✅/i.test(message) ? 'success' : 'info';
|
|
|
|
| 289 |
ws.onerror = () => ws.close();
|
| 290 |
}
|
| 291 |
|
| 292 |
+
function createTab(type = 'terminal', sessionId = null, restored = null) {
|
| 293 |
const id = ++cnt;
|
| 294 |
+
const savedTab = restored || (sessionId ? loadTabs()?.find(saved => saved.sessionId === sessionId) : null);
|
| 295 |
|
| 296 |
const el = document.createElement('div');
|
| 297 |
el.className = 'tab';
|
|
|
|
| 306 |
|
| 307 |
const term = new Terminal({
|
| 308 |
cursorBlink: true,
|
| 309 |
+
cols: savedTab?.cols || 80,
|
| 310 |
+
rows: savedTab?.rows || 24,
|
| 311 |
fontSize: 14,
|
| 312 |
fontFamily: "'Cascadia Code', 'Fira Code', Consolas, monospace",
|
| 313 |
allowProposedApi: true,
|
|
|
|
| 317 |
const fit = new FitAddon.FitAddon();
|
| 318 |
term.loadAddon(fit);
|
| 319 |
term.loadAddon(new WebLinksAddon.WebLinksAddon());
|
| 320 |
+
const serialize = new SerializeAddon.SerializeAddon();
|
| 321 |
+
term.loadAddon(serialize);
|
| 322 |
+
if (savedTab && savedTab.screen) {
|
| 323 |
+
term.write(savedTab.screen);
|
| 324 |
+
}
|
| 325 |
term.open(wrap);
|
| 326 |
+
if (savedTab && savedTab.screen) requestAnimationFrame(() => fit.fit());
|
| 327 |
+
else fit.fit();
|
| 328 |
|
| 329 |
+
const obj = {id, type: 'terminal', el, wrap, term, fit, serialize, ws: null, closed: false, currentDir: '~', sessionId: sessionId || null, lastOutputSeq: savedTab ? (savedTab.lastOutputSeq || 0) : 0, restoredScreen: ''};
|
| 330 |
tabs.push(obj);
|
| 331 |
connect(obj);
|
| 332 |
saveTabs();
|
|
|
|
| 613 |
if (Array.isArray(saved) && saved.length) {
|
| 614 |
for (const savedTab of saved) {
|
| 615 |
cnt = Math.max(cnt, Number(savedTab.id) || 0);
|
| 616 |
+
createTab('terminal', savedTab.sessionId || null, savedTab);
|
| 617 |
}
|
| 618 |
} else {
|
| 619 |
if (savedCnt) cnt = savedCnt;
|