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

93 lines
2.6 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-04-24 08:33:32 +00:00
<div class="p-terminal p-component" @click="onClick" v-bind="ptm('root')">
<div v-if="welcomeMessage" v-bind="ptm('welcomeMessage')">{{ welcomeMessage }}</div>
<div class="p-terminal-content" v-bind="ptm('content')">
<div v-for="(command, i) of commands" :key="command.text + i.toString()" v-bind="ptm('commands')">
<span class="p-terminal-prompt" v-bind="ptm('prompt')">{{ prompt }}</span>
<span class="p-terminal-command" v-bind="ptm('command')">{{ command.text }}</span>
<div class="p-terminal-response" aria-live="polite" v-bind="ptm('response')">{{ command.response }}</div>
2022-09-06 12:03:37 +00:00
</div>
</div>
2023-04-24 08:33:32 +00:00
<div class="p-terminal-prompt-container" v-bind="ptm('container')">
<span class="p-terminal-prompt" v-bind="ptm('prompt')">{{ prompt }}</span>
<input ref="input" v-model="commandText" type="text" class="p-terminal-input" autocomplete="off" @keydown="onKeydown" v-bind="ptm('commandText')" />
2022-09-06 12:03:37 +00:00
</div>
</div>
</template>
<script>
2023-04-24 08:08:08 +00:00
import BaseComponent from 'primevue/basecomponent';
2022-09-06 12:03:37 +00:00
import TerminalService from 'primevue/terminalservice';
export default {
name: 'Terminal',
2023-04-24 08:08:08 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
props: {
welcomeMessage: {
type: String,
default: null
},
prompt: {
type: String,
default: null
}
},
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) {
2022-12-08 11:04:25 +00:00
if (event.code === 'Enter' && 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>
<style>
.p-terminal {
height: 18rem;
overflow: auto;
}
.p-terminal-prompt-container {
display: flex;
align-items: center;
}
.p-terminal-input {
flex: 1 1 auto;
border: 0 none;
background-color: transparent;
color: inherit;
padding: 0;
outline: 0 none;
}
.p-terminal-input::-ms-clear {
display: none;
}
</style>