diff --git a/web/src/utils/index.js b/web/src/utils/index.js index 5818d24..56d6e7f 100644 --- a/web/src/utils/index.js +++ b/web/src/utils/index.js @@ -17,6 +17,15 @@ EventBus.$on = (event, callback) => { EventBus[event].push(callback) } +EventBus.$off = (event, callback) => { + if (!EventBus[event]) return + if (!callback) { + delete EventBus[event] + } else { + EventBus[event] = EventBus[event].filter(cb => cb !== callback) + } +} + export const randomStr = (e) =>{ e = e || 16 let str = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678', diff --git a/web/src/views/terminal/components/sftp-v2.vue b/web/src/views/terminal/components/sftp-v2.vue index 8a1ffe3..a59e784 100644 --- a/web/src/views/terminal/components/sftp-v2.vue +++ b/web/src/views/terminal/components/sftp-v2.vue @@ -541,6 +541,7 @@ import unknowIcon from '@/assets/image/system/unknow.png' import { useContextMenu } from '@/composables/useContextMenu' import TextEditor from '@/components/text-editor/index.vue' import ImagePreview from '@/components/image-preview/index.vue' +import { EventBus } from '@/utils' const props = defineProps({ hostId: { @@ -1586,6 +1587,17 @@ const onRowContextMenu = (row, _column, event) => { } }) + // 发送cd指令到终端 + if (row.type === 'd') { + const cdCommand = `cd ${ currentPath.value }/${ row.name }`.replace(/\/+/g, '/') + items.push({ + label: '发送cd指令到终端', + onClick: () => { + EventBus.$emit('exec_external_command', cdCommand) + } + }) + } + showMenu(event, items) } diff --git a/web/src/views/terminal/components/terminal-tab.vue b/web/src/views/terminal/components/terminal-tab.vue index d65a458..79b7d7c 100644 --- a/web/src/views/terminal/components/terminal-tab.vue +++ b/web/src/views/terminal/components/terminal-tab.vue @@ -671,14 +671,14 @@ const inputCommand = (command, isSyncAllSession = false) => { socket.value.emit('input', command) } -EventBus.$on('exec_external_command', (command) => { +const execExternalCommand = (command) => { if (!socket.value || !socket.value.connected || curStatus.value !== CONNECT_SUCCESS) { $message.error('终端连接已断开,无法执行指令') return } socket.value.emit('input', command + '\n') term.value?.focus() -}) +} onMounted(async () => { createLocalTerminal() @@ -686,9 +686,11 @@ onMounted(async () => { connectIO() await nextTick() onData() + EventBus.$on('exec_external_command', execExternalCommand) }) onBeforeUnmount(() => { + EventBus.$off('exec_external_command', execExternalCommand) socket.value?.close() window.removeEventListener('resize', handleResize) clearInterval(pingTimer.value)