Skip to content

Commit b91ce10

Browse files
committed
feat(ui): improve terminal dialog behavior and ESC key handling
Refactored TerminalDialog to enhance ESC key functionality. Double-tap ESC within 400ms now closes the dialog when the login form is hidden. Replaced @keyup.esc listener with a custom handler for precise control. Also fixed an issue where the login form didn't reset correctly when closing the dialog.
1 parent 0178555 commit b91ce10

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

ui/src/components/Terminal/TerminalDialog.vue

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
{{ online ? "Connect" : "Offline" }}
1313
</v-btn>
1414
</template>
15+
1516
<v-dialog
1617
v-model="showTerminal"
1718
:fullscreen="!showLoginForm || $vuetify.display.smAndDown"
1819
:max-width="$vuetify.display.smAndDown ? undefined : $vuetify.display.thresholds.sm"
1920
@click:outside="close"
20-
@keyup.esc="close()"
21-
persistent
2221
>
2322
<v-card data-test="terminal-card" class="bg-v-theme-surface">
2423
<v-card-title
@@ -29,8 +28,8 @@
2928
<v-icon @click="close()" data-test="close-btn" class="bg-primary" size="24">mdi-close</v-icon>
3029
</v-card-title>
3130

32-
<div class="ma-0 pa-0 w-100 fill-height position-relative">
33-
<div v-if="!showLoginForm" ref="terminal" class="terminal" />
31+
<div class="ma-0 pa-0 w-100 fill-height position-relative" v-if="!showLoginForm">
32+
<div ref="terminal" class="terminal" />
3433
</div>
3534

3635
<div class="mt-2" v-if="showLoginForm">
@@ -166,14 +165,16 @@ import {
166165
computed,
167166
nextTick,
168167
watch,
168+
onMounted,
169+
onBeforeUnmount,
169170
} from "vue";
170171
import { useField } from "vee-validate";
171172
import "xterm/css/xterm.css";
172173
import { Terminal } from "xterm";
173174
import { FitAddon } from "xterm-addon-fit";
174175
import * as yup from "yup";
175176
import axios from "axios";
176-
import { useEventListener, onKeyStroke } from "@vueuse/core";
177+
import { useEventListener } from "@vueuse/core";
177178
import { useRoute } from "vue-router";
178179
import { useStore } from "../../store";
179180
import {
@@ -253,14 +254,14 @@ const nameOfPrivateKeys = computed(() => {
253254
return list.map((item: IPrivateKey) => item.name);
254255
});
255256
256-
useEventListener(window, "resize", (evt) => {
257+
useEventListener(window, "resize", () => {
257258
nextTick(() => {
258259
fitAddon.value.fit();
259260
});
260261
});
261262
262263
watch(showTerminal, (value) => {
263-
if (value) showLoginForm.value = true;
264+
if (!value) showLoginForm.value = true;
264265
});
265266
266267
const encodeURLParams = (params: IParams) => Object.entries(params)
@@ -375,8 +376,8 @@ const open = () => {
375376
}
376377
};
377378
378-
watch(() => route.path, (Path) => {
379-
if (Path === `/devices/${props.uid}/terminal`) {
379+
watch(() => route.path, (path) => {
380+
if (path === `/devices/${props.uid}/terminal`) {
380381
open();
381382
}
382383
}, { immediate: true });
@@ -423,8 +424,24 @@ const close = () => {
423424
store.dispatch("modal/toggleTerminal", "");
424425
};
425426
426-
onKeyStroke("Escape", () => {
427-
close();
427+
let lastEscPress = 0;
428+
429+
const handleEscKey = (event: KeyboardEvent) => {
430+
if (event.key === "Escape" && !showLoginForm.value) {
431+
const currentTime = new Date().getTime();
432+
if (currentTime - lastEscPress < 400) {
433+
close();
434+
}
435+
lastEscPress = currentTime;
436+
}
437+
};
438+
439+
onMounted(() => {
440+
window.addEventListener("keyup", handleEscKey);
441+
});
442+
443+
onBeforeUnmount(() => {
444+
window.removeEventListener("keyup", handleEscKey);
428445
});
429446
430447
defineExpose({ open, showTerminal, showLoginForm, encodeURLParams, connect, privateKey, xterm, fitAddon, ws, close });

0 commit comments

Comments
 (0)