1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 22:18:07 +08:00

fix: 修改一处let和ref混用的奇怪声明

This commit is contained in:
Wankko Ree 2023-04-05 22:51:12 +08:00 committed by zhengkunwang223
parent 155363afa6
commit 12beef49b5

View File

@ -34,8 +34,8 @@ const acceptParams = (props: WsProps) => {
const fitAddon = new FitAddon(); const fitAddon = new FitAddon();
const webSocketReady = ref(false); const webSocketReady = ref(false);
const termReady = ref(false); const termReady = ref(false);
let terminalSocket = ref(null) as unknown as WebSocket; const terminalSocket = ref<WebSocket>();
let term = ref(null) as unknown as Terminal; const term = ref<Terminal>();
const readyWatcher = watch( const readyWatcher = watch(
() => webSocketReady.value && termReady.value, () => webSocketReady.value && termReady.value,
@ -56,23 +56,29 @@ const onWSReceive = (message: any) => {
return; return;
} }
const data = JSON.parse(message.data); const data = JSON.parse(message.data);
term.element && term.focus(); if (term.value) {
term.write(data.Data); term.value.element && term.value.focus();
term.value.write(data.Data);
}
}; };
const errorRealTerminal = (ex: any) => { const errorRealTerminal = (ex: any) => {
let message = ex.message; let message = ex.message;
if (!message) message = 'disconnected'; if (!message) message = 'disconnected';
term.write(`\x1b[31m${message}\x1b[m\r\n`); if (term.value) {
term.value.write(`\x1b[31m${message}\x1b[m\r\n`);
}
}; };
const closeRealTerminal = (ev: CloseEvent) => { const closeRealTerminal = (ev: CloseEvent) => {
term.write(ev.reason); if (term.value) {
term.value.write(ev.reason);
}
}; };
const initErrorTerm = (errorInfo: string) => { const initErrorTerm = (errorInfo: string) => {
let ifm = document.getElementById('terminal-' + terminalID.value) as HTMLInputElement | null; let ifm = document.getElementById('terminal-' + terminalID.value) as HTMLInputElement | null;
term = new Terminal({ term.value = new Terminal({
lineHeight: 1.2, lineHeight: 1.2,
fontSize: 12, fontSize: 12,
fontFamily: "Monaco, Menlo, Consolas, 'Courier New', monospace", fontFamily: "Monaco, Menlo, Consolas, 'Courier New', monospace",
@ -85,9 +91,9 @@ const initErrorTerm = (errorInfo: string) => {
tabStopWidth: 4, tabStopWidth: 4,
}); });
if (ifm) { if (ifm) {
term.open(ifm); term.value.open(ifm);
term.write(errorInfo); term.value.write(errorInfo);
term.loadAddon(fitAddon); term.value.loadAddon(fitAddon);
fitAddon.fit(); fitAddon.fit();
termReady.value = true; termReady.value = true;
} }
@ -98,7 +104,7 @@ const initTerm = () => {
let href = window.location.href; let href = window.location.href;
let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss'; let protocol = href.split('//')[0] === 'http:' ? 'ws' : 'wss';
let ipLocal = href.split('//')[1].split('/')[0]; let ipLocal = href.split('//')[1].split('/')[0];
term = new Terminal({ term.value = new Terminal({
lineHeight: 1.2, lineHeight: 1.2,
fontSize: 12, fontSize: 12,
fontFamily: "Monaco, Menlo, Consolas, 'Courier New', monospace", fontFamily: "Monaco, Menlo, Consolas, 'Courier New', monospace",
@ -111,17 +117,17 @@ const initTerm = () => {
tabStopWidth: 4, tabStopWidth: 4,
}); });
if (ifm) { if (ifm) {
term.open(ifm); term.value.open(ifm);
terminalSocket = new WebSocket( terminalSocket.value = new WebSocket(
`${protocol}://${ipLocal}/api/v1/terminals?id=${wsID.value}&cols=${term.cols}&rows=${term.rows}`, `${protocol}://${ipLocal}/api/v1/terminals?id=${wsID.value}&cols=${term.value.cols}&rows=${term.value.rows}`,
); );
terminalSocket.onopen = runRealTerminal; terminalSocket.value.onopen = runRealTerminal;
terminalSocket.onmessage = onWSReceive; terminalSocket.value.onmessage = onWSReceive;
terminalSocket.onclose = closeRealTerminal; terminalSocket.value.onclose = closeRealTerminal;
terminalSocket.onerror = errorRealTerminal; terminalSocket.value.onerror = errorRealTerminal;
term.onData((data: any) => { term.value.onData((data: any) => {
if (isWsOpen()) { if (isWsOpen()) {
terminalSocket.send( terminalSocket.value!.send(
JSON.stringify({ JSON.stringify({
type: 'cmd', type: 'cmd',
cmd: Base64.encode(data), cmd: Base64.encode(data),
@ -129,8 +135,8 @@ const initTerm = () => {
); );
} }
}); });
term.loadAddon(new AttachAddon(terminalSocket)); term.value.loadAddon(new AttachAddon(terminalSocket.value));
term.loadAddon(fitAddon); term.value.loadAddon(fitAddon);
termReady.value = true; termReady.value = true;
} }
}; };
@ -140,22 +146,22 @@ const fitTerm = () => {
}; };
const isWsOpen = () => { const isWsOpen = () => {
const readyState = terminalSocket && terminalSocket.readyState; const readyState = terminalSocket.value && terminalSocket.value.readyState;
return readyState === 1; return readyState === 1;
}; };
function onClose() { function onClose() {
window.removeEventListener('resize', changeTerminalSize); window.removeEventListener('resize', changeTerminalSize);
try { try {
terminalSocket.close(); terminalSocket.value?.close();
} catch {} } catch {}
try { try {
term.dispose(); term.value?.dispose();
} catch {} } catch {}
} }
function onSendMsg(command: string) { function onSendMsg(command: string) {
terminalSocket.send( terminalSocket.value?.send(
JSON.stringify({ JSON.stringify({
type: 'cmd', type: 'cmd',
cmd: Base64.encode(command), cmd: Base64.encode(command),
@ -165,9 +171,9 @@ function onSendMsg(command: string) {
function changeTerminalSize() { function changeTerminalSize() {
fitTerm(); fitTerm();
const { cols, rows } = term; const { cols, rows } = term.value!;
if (isWsOpen()) { if (isWsOpen()) {
terminalSocket.send( terminalSocket.value!.send(
JSON.stringify({ JSON.stringify({
type: 'resize', type: 'resize',
cols: cols, cols: cols,
@ -184,14 +190,14 @@ function changeTerminalSize() {
const onTermWheel = (event: WheelEvent) => { const onTermWheel = (event: WheelEvent) => {
if (event.ctrlKey) { if (event.ctrlKey) {
event.preventDefault(); event.preventDefault();
if (term) { if (term.value) {
if (event.deltaY > 0) { if (event.deltaY > 0) {
// web font-size mini 12px // web font-size mini 12px
if (term.options.fontSize > 12) { if (term.value.options.fontSize > 12) {
term.options.fontSize = term.options.fontSize - 1; term.value.options.fontSize = term.value.options.fontSize - 1;
} }
} else { } else {
term.options.fontSize = term.options.fontSize + 1; term.value.options.fontSize = term.value.options.fontSize + 1;
} }
} }
} }