Line data Source code
1 16 : <template>
2 : <div class="widget-text-preview group relative w-full">
3 : <div
4 : class="invisible absolute top-1.5 right-1.5 z-10 flex gap-1 group-focus-within:visible group-hover:visible"
5 : >
6 : <Button
7 : variant="textonly"
8 : size="icon"
9 : class="hover:bg-foreground/10"
10 : :title="$t('g.copyToClipboard')"
11 : :aria-label="$t('g.copyToClipboard')"
12 : @click="handleCopy"
13 : @pointerdown.capture.stop
14 : >
15 : <i class="icon-[lucide--copy] text-foreground" />
16 : </Button>
17 : <Button
18 : v-if="downloadUrl"
19 : variant="textonly"
20 : size="icon"
21 : class="hover:bg-foreground/10"
22 : :title="$t('g.download')"
23 : :aria-label="$t('g.download')"
24 : @click="handleDownload"
25 : @pointerdown.capture.stop
26 : >
27 : <i class="icon-[lucide--download] text-foreground" />
28 : </Button>
29 : </div>
30 :
31 : <SanitizedHtml
32 : v-if="showMarkdown"
33 : class="comfy-markdown-content size-full min-h-[60px] overflow-y-auto rounded-lg text-sm"
34 : data-capture-wheel="true"
35 : role="textarea"
36 : :aria-label="widget.name"
37 : aria-readonly="true"
38 : :html="renderedMarkdown"
39 : />
40 : <Textarea
41 : v-else
42 : :model-value="modelValue"
43 : readonly
44 : :aria-label="widget.name"
45 : :class="
46 : cn(
47 : WidgetInputBaseClass,
48 : 'size-full resize-none font-mono text-(length:--comfy-textarea-font-size) leading-normal',
49 : 'overflow-y-auto'
50 : )
51 : "
52 : data-capture-wheel="true"
53 : @pointerdown.capture.stop
54 : @pointermove.capture.stop
55 : @pointerup.capture.stop
56 : />
57 : </div>
58 : </template>
59 :
60 : <script setup lang="ts">
61 : import { computed } from 'vue'
62 : import { useI18n } from 'vue-i18n'
63 :
64 : import SanitizedHtml from '@comfyorg/design-system/common/SanitizedHtml'
65 : import Button from '@comfyorg/design-system/button/Button'
66 : import Textarea from '@comfyorg/design-system/textarea/Textarea'
67 : import { useCopyToClipboard } from '@/composables/useCopyToClipboard'
68 : import { useToast } from '@/components/ui/toast/toastStore'
69 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
70 : import { api } from '@/scripts/api'
71 : import { downloadFile } from '@/base/common/downloadUtil'
72 : import type {
73 : NodeOutputWith,
74 : ResultItem
75 : } from '@comfyorg/api-client/executionTypes'
76 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
77 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
78 : import {
79 : stripGraphPrefix,
80 : useWidgetValueStore
81 : } from '@/stores/widgetValueStore'
82 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
83 : import type { SimplifiedWidget } from '@/types/simplifiedWidget'
84 : import { widgetId } from '@comfyorg/graph-model/widgetId'
85 : import { resolveNode } from '@/utils/litegraphUtil'
86 : import { renderMarkdownToHtml } from '@/utils/markdownRendererUtil'
87 : import { cn } from '@comfyorg/tailwind-utils'
88 :
89 : import { WidgetInputBaseClass } from './layout'
90 :
91 : type SaveTextOutput = NodeOutputWith<{
92 : files?: ResultItem[]
93 : }>
94 :
95 2355 : const MODE_WIDGET_NAME = 'preview_mode'
96 :
97 : const { widget, nodeId } = defineProps<{
98 : widget: SimplifiedWidget<string>
99 : nodeId?: NodeId
100 : }>()
101 :
102 77 : const modelValue = defineModel<string>({ default: '' })
103 :
104 77 : const { t } = useI18n()
105 77 : const { copyToClipboard } = useCopyToClipboard()
106 77 : const canvasStore = useCanvasStore()
107 77 : const widgetValueStore = useWidgetValueStore()
108 77 : const nodeOutputStore = useNodeOutputStore()
109 77 : const { nodeToNodeLocatorId } = useWorkflowStore()
110 :
111 77 : const localNodeId = computed(() =>
112 : nodeId === undefined ? null : stripGraphPrefix(nodeId)
113 : )
114 :
115 77 : const showMarkdown = computed<boolean>(() => {
116 82 : const graphId = canvasStore.canvas?.graph?.rootGraph.id
117 82 : if (!graphId || localNodeId.value === null) return false
118 82 : return (
119 : widgetValueStore.getWidget(
120 : widgetId(graphId, localNodeId.value, MODE_WIDGET_NAME)
121 : )?.value === true
122 : )
123 : })
124 :
125 77 : const renderedMarkdown = computed(() =>
126 : showMarkdown.value ? renderMarkdownToHtml(modelValue.value || '') : ''
127 : )
128 :
129 77 : const savedFile = computed(() => {
130 121 : if (nodeId === undefined) return undefined
131 121 : const node = resolveNode(nodeId)
132 121 : if (!node) return undefined
133 : const outputs = nodeOutputStore.nodeOutputs[nodeToNodeLocatorId(node)] as
134 : | SaveTextOutput
135 : | undefined
136 121 : return outputs?.files?.[0]
137 : })
138 :
139 77 : const downloadUrl = computed(() => {
140 77 : const file = savedFile.value
141 77 : if (!file?.filename) return undefined
142 77 : const params = new URLSearchParams({
143 : filename: file.filename,
144 : subfolder: file.subfolder ?? '',
145 : type: file.type ?? 'output'
146 : })
147 77 : return api.apiURL(`/view?${params}`)
148 : })
149 :
150 0 : function handleCopy() {
151 0 : void copyToClipboard(modelValue.value)
152 : }
153 :
154 0 : function handleDownload() {
155 0 : if (!downloadUrl.value) return
156 0 : try {
157 0 : downloadFile(downloadUrl.value, savedFile.value?.filename)
158 : } catch {
159 0 : useToast().error(t('g.error'), { description: t('g.failedToDownloadFile') })
160 : }
161 : }
162 : </script>
|