1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-16 18:54:43 +08:00

fix: 修复终端在网络环境较差时无法正常初始化全屏尺寸的问题 (#505) (#507)

This commit is contained in:
Wankko Ree 2023-04-05 22:04:15 +08:00 committed by GitHub
parent 1d99559d4c
commit c501d9fefe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,7 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, nextTick, onBeforeUnmount } from 'vue'; import { ref, nextTick, onBeforeUnmount, watch } from 'vue';
import { Terminal } from 'xterm'; import { Terminal } from 'xterm';
import { AttachAddon } from 'xterm-addon-attach'; import { AttachAddon } from 'xterm-addon-attach';
import { Base64 } from 'js-base64'; import { Base64 } from 'js-base64';
@ -32,12 +32,23 @@ const acceptParams = (props: WsProps) => {
}; };
const fitAddon = new FitAddon(); const fitAddon = new FitAddon();
const loading = ref(true); const webSocketReady = ref(false);
const termReady = ref(false);
let terminalSocket = ref(null) as unknown as WebSocket; let terminalSocket = ref(null) as unknown as WebSocket;
let term = ref(null) as unknown as Terminal; let term = ref(null) as unknown as Terminal;
const readyWatcher = watch(
() => webSocketReady.value && termReady.value,
(ready) => {
if (ready) {
changeTerminalSize();
readyWatcher(); // unwatch self
}
},
);
const runRealTerminal = () => { const runRealTerminal = () => {
loading.value = false; webSocketReady.value = true;
}; };
const onWSReceive = (message: any) => { const onWSReceive = (message: any) => {
@ -78,6 +89,7 @@ const initErrorTerm = (errorInfo: string) => {
term.write(errorInfo); term.write(errorInfo);
term.loadAddon(fitAddon); term.loadAddon(fitAddon);
fitAddon.fit(); fitAddon.fit();
termReady.value = true;
} }
}; };
@ -119,18 +131,7 @@ const initTerm = () => {
}); });
term.loadAddon(new AttachAddon(terminalSocket)); term.loadAddon(new AttachAddon(terminalSocket));
term.loadAddon(fitAddon); term.loadAddon(fitAddon);
setTimeout(() => { termReady.value = true;
fitAddon.fit();
if (isWsOpen()) {
terminalSocket.send(
JSON.stringify({
type: 'resize',
cols: term.cols,
rows: term.rows,
}),
);
}
}, 30);
} }
}; };