|
| 1 | +<template> |
| 2 | + <div class="terminal"> |
| 3 | + <div class="hide" @click="$emit('hide')"> |
| 4 | + <SvgIcon style="width: 20px; height: 20px" name="chevron-down" /> |
| 5 | + </div> |
| 6 | + <x-term ref="refXterm" /> |
| 7 | + </div> |
| 8 | +</template> |
| 9 | + |
| 10 | +<script lang="ts"> |
| 11 | +import { defineComponent, nextTick, onBeforeMount, onBeforeUnmount, ref } from 'vue' |
| 12 | +import { getLogger, sleep } from '@fe/utils' |
| 13 | +import { registerAction, removeAction } from '@fe/core/action' |
| 14 | +import { FLAG_DISABLE_XTERM } from '@fe/support/args' |
| 15 | +import { toggleXterm } from '@fe/services/layout' |
| 16 | +import type { Components } from '@fe/types' |
| 17 | +import SvgIcon from './SvgIcon.vue' |
| 18 | +import XTerm from './Xterm.vue' |
| 19 | +import store from '@fe/support/store' |
| 20 | +
|
| 21 | +const logger = getLogger('component-terminal') |
| 22 | +
|
| 23 | +export default defineComponent({ |
| 24 | + name: 'terminal', |
| 25 | + components: { SvgIcon, XTerm }, |
| 26 | + setup () { |
| 27 | + const refXterm = ref<Components.XTerm.Ref | null>(null) |
| 28 | +
|
| 29 | + function init (opts?: Components.XTerm.InitOpts) { |
| 30 | + refXterm.value?.init({ |
| 31 | + ...opts, |
| 32 | + cwd: opts?.cwd || store.state.currentRepo?.path || '', |
| 33 | + onDisconnect: () => { |
| 34 | + toggleXterm(false) |
| 35 | + }, |
| 36 | + }) |
| 37 | + } |
| 38 | +
|
| 39 | + function input (data: string, addNewLine?: boolean) { |
| 40 | + refXterm.value?.input(data, addNewLine) |
| 41 | + } |
| 42 | +
|
| 43 | + async function runInXterm (cmd: { code: string, start?: string, exit?: string } | string) { |
| 44 | + if (FLAG_DISABLE_XTERM) { |
| 45 | + logger.warn('terminal disabled') |
| 46 | + return |
| 47 | + } |
| 48 | +
|
| 49 | + toggleXterm(true) |
| 50 | +
|
| 51 | + await nextTick() |
| 52 | +
|
| 53 | + init() |
| 54 | +
|
| 55 | + if (typeof cmd === 'string') { |
| 56 | + cmd = { code: cmd } |
| 57 | + } |
| 58 | +
|
| 59 | + if (cmd.start) { |
| 60 | + input(cmd.start, true) |
| 61 | + // wait for child process ready. |
| 62 | + await sleep(400) |
| 63 | + } |
| 64 | +
|
| 65 | + cmd.code.split('\n').forEach(x => { |
| 66 | + input(x, true) |
| 67 | + }) |
| 68 | +
|
| 69 | + if (cmd.exit) { |
| 70 | + input(cmd.exit, true) |
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | + onBeforeMount(() => { |
| 75 | + registerAction({ name: 'xterm.run', handler: runInXterm }) |
| 76 | + registerAction({ name: 'xterm.init', handler: init }) |
| 77 | + }) |
| 78 | +
|
| 79 | + onBeforeUnmount(() => { |
| 80 | + removeAction('xterm.run') |
| 81 | + removeAction('xterm.init') |
| 82 | + }) |
| 83 | +
|
| 84 | + return { |
| 85 | + refXterm, |
| 86 | + } |
| 87 | + }, |
| 88 | +}) |
| 89 | +</script> |
| 90 | + |
| 91 | +<style scoped> |
| 92 | +.terminal { |
| 93 | + box-sizing: border-box; |
| 94 | + padding: 5px; |
| 95 | + background: var(--g-color-98); |
| 96 | + border: 1px solid var(--g-color-88); |
| 97 | + flex: 0 0 auto; |
| 98 | + width: 100%; |
| 99 | + height: 100%; |
| 100 | + padding-right: 0; |
| 101 | + padding-top: 0; |
| 102 | + border-right: 0; |
| 103 | +} |
| 104 | +
|
| 105 | +.hide { |
| 106 | + position: absolute; |
| 107 | + top: -6px; |
| 108 | + right: 0; |
| 109 | + z-index: 10; |
| 110 | + background: var(--g-color-75); |
| 111 | + border-bottom-left-radius: var(--g-border-radius); |
| 112 | + border-bottom-right-radius: var(--g-border-radius); |
| 113 | + width: 30px; |
| 114 | + height: 30px; |
| 115 | + display: flex; |
| 116 | + align-items: center; |
| 117 | + justify-content: center; |
| 118 | + cursor: pointer; |
| 119 | + transform: scaleY(0.6); |
| 120 | + border: 8px solid transparent; |
| 121 | + box-sizing: border-box; |
| 122 | + color: var(--g-color-20); |
| 123 | + opacity: 0; |
| 124 | + transition: opacity .2s; |
| 125 | +} |
| 126 | +
|
| 127 | +.terminal:hover .hide { |
| 128 | + opacity: 1; |
| 129 | +} |
| 130 | +
|
| 131 | +.hide:hover { |
| 132 | + background: var(--g-color-65); |
| 133 | +} |
| 134 | +</style> |
0 commit comments