Line data Source code
1 16 : <template>
2 49 : <div
3 : ref="rootEl"
4 : data-testid="terminal-root"
5 : class="relative size-full overflow-hidden bg-neutral-900"
6 : >
7 : <div
8 : class="size-full rounded-none p-2 [&_.xterm]:overflow-x-auto [&_.xterm]:overflow-y-hidden [&_.xterm-screen]:overflow-hidden [&_.xterm-screen]:bg-neutral-900"
9 : >
10 : <div ref="terminalEl" data-testid="terminal-host" class="h-full" />
11 : </div>
12 : <Button
13 : :tooltip="tooltipText"
14 : tooltip-side="left"
15 : data-testid="terminal-copy-button"
16 : variant="secondary"
17 : size="sm"
18 : :class="
19 : cn('absolute top-2 right-8 transition-opacity', {
20 : 'pointer-events-none opacity-0 select-none': !isHovered
21 : })
22 : "
23 : :aria-label="tooltipText"
24 : @click="handleCopy"
25 : >
26 : <i class="icon-[lucide--copy]" />
27 : </Button>
28 : </div>
29 : </template>
30 :
31 : <script setup lang="ts">
32 : import { useElementHover, useEventListener } from '@vueuse/core'
33 : import type { IDisposable } from '@xterm/xterm'
34 : import type { Ref } from 'vue'
35 : import { computed, onMounted, onUnmounted, ref } from 'vue'
36 : import { useI18n } from 'vue-i18n'
37 :
38 : import Button from '@comfyorg/design-system/button/Button'
39 : import { useTerminal } from '@/composables/bottomPanelTabs/useTerminal'
40 : import { electronAPI } from '@/utils/envUtil'
41 : import { isDesktop } from '@/platform/distribution/types'
42 : import { cn } from '@comfyorg/tailwind-utils'
43 :
44 24 : const { t } = useI18n()
45 :
46 24 : const emit = defineEmits<{
47 : created: [ReturnType<typeof useTerminal>, Ref<HTMLElement | undefined>]
48 : unmounted: []
49 : }>()
50 24 : const terminalEl = ref<HTMLElement | undefined>()
51 24 : const rootEl = ref<HTMLElement | undefined>()
52 24 : const hasSelection = ref(false)
53 :
54 24 : const isHovered = useElementHover(rootEl)
55 :
56 24 : const terminalData = useTerminal(terminalEl)
57 24 : emit('created', terminalData, ref(rootEl))
58 :
59 24 : const { terminal } = terminalData
60 24 : let selectionDisposable: IDisposable | undefined
61 :
62 24 : const tooltipText = computed(() => {
63 27 : return hasSelection.value
64 : ? t('serverStart.copySelectionTooltip')
65 : : t('serverStart.copyAllTooltip')
66 : })
67 :
68 24 : const handleCopy = async () => {
69 1 : const existingSelection = terminal.getSelection()
70 1 : const shouldSelectAll = !existingSelection
71 1 : if (shouldSelectAll) terminal.selectAll()
72 :
73 1 : const selectedText = shouldSelectAll
74 : ? terminal.getSelection()
75 : : existingSelection
76 :
77 1 : if (selectedText) {
78 1 : await navigator.clipboard.writeText(selectedText)
79 :
80 1 : if (shouldSelectAll) {
81 1 : terminal.clearSelection()
82 : }
83 : }
84 : }
85 :
86 : const showContextMenu = (event: MouseEvent) => {
87 : event.preventDefault()
88 : electronAPI()?.showContextMenu({ type: 'text' })
89 : }
90 :
91 : if (isDesktop) {
92 : useEventListener(terminalEl, 'contextmenu', showContextMenu)
93 : }
94 :
95 24 : onMounted(() => {
96 24 : selectionDisposable = terminal.onSelectionChange(() => {
97 6 : hasSelection.value = terminal.hasSelection()
98 : })
99 : })
100 :
101 24 : onUnmounted(() => {
102 6 : selectionDisposable?.dispose()
103 6 : emit('unmounted')
104 : })
105 : </script>
|