primevue-mirror/components/lib/terminal/Terminal.vue

58 lines
2.1 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-29 09:19:55 +00:00
<div :class="cx('root')" @click="onClick" v-bind="ptm('root')" data-pc-name="terminal">
2023-04-24 08:33:32 +00:00
<div v-if="welcomeMessage" v-bind="ptm('welcomeMessage')">{{ welcomeMessage }}</div>
2023-05-24 12:53:29 +00:00
<div :class="cx('content')" v-bind="ptm('content')">
2023-04-24 08:33:32 +00:00
<div v-for="(command, i) of commands" :key="command.text + i.toString()" v-bind="ptm('commands')">
2023-05-24 12:53:29 +00:00
<span :class="cx('prompt')" v-bind="ptm('prompt')">{{ prompt }}</span>
<span :class="cx('command')" v-bind="ptm('command')">{{ command.text }}</span>
<div :class="cx('response')" aria-live="polite" v-bind="ptm('response')">{{ command.response }}</div>
2022-09-06 12:03:37 +00:00
</div>
</div>
2023-05-24 12:53:29 +00:00
<div :class="cx('container')" v-bind="ptm('container')">
<span :class="cx('prompt')" v-bind="ptm('prompt')">{{ prompt }}</span>
<input ref="input" v-model="commandText" type="text" :class="cx('commandText')" autocomplete="off" @keydown="onKeydown" v-bind="ptm('commandText')" />
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
import TerminalService from 'primevue/terminalservice';
2023-05-29 09:19:55 +00:00
import BaseTerminal from './BaseTerminal.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'Terminal',
2023-05-24 12:53:29 +00:00
extends: BaseTerminal,
2022-09-06 12:03:37 +00:00
data() {
return {
commandText: null,
commands: []
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
mounted() {
TerminalService.on('response', this.responseListener);
this.$refs.input.focus();
},
updated() {
this.$el.scrollTop = this.$el.scrollHeight;
},
beforeUnmount() {
TerminalService.off('response', this.responseListener);
},
methods: {
onClick() {
this.$refs.input.focus();
},
onKeydown(event) {
if ((event.code === 'Enter' || event.code === 'NumpadEnter') && this.commandText) {
2022-09-14 11:26:01 +00:00
this.commands.push({ text: this.commandText });
2022-09-06 12:03:37 +00:00
TerminalService.emit('command', this.commandText);
this.commandText = '';
}
},
responseListener(response) {
this.commands[this.commands.length - 1].response = response;
}
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>