Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
2 : import { resolveNodeRootGraphId } from '@/lib/litegraph/src/litegraph'
3 : import WidgetTextPreview from '@/renderer/extensions/vueNodes/widgets/components/WidgetTextPreview.vue'
4 : import type { CustomInputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
5 : import { app } from '@/scripts/app'
6 : import { ComponentWidgetImpl, addWidget } from '@/scripts/domWidget'
7 : import { ComfyWidgets } from '@/scripts/widgets'
8 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
9 : import { widgetId } from '@comfyorg/graph-model/widgetId'
10 :
11 2355 : const PREVIEW_WIDGET_NAME = 'preview_text'
12 2355 : const MODE_WIDGET_NAME = 'preview_mode'
13 :
14 2355 : const inputSpecTextPreview: CustomInputSpec = {
15 : name: PREVIEW_WIDGET_NAME,
16 : type: 'TEXT_PREVIEW',
17 : isPreview: true
18 : }
19 :
20 70 : export function addTextPreviewWidgets(node: LGraphNode) {
21 70 : const widgetStore = useWidgetValueStore()
22 70 : let fallbackValue = ''
23 :
24 70 : const previewWidgetId = () =>
25 : widgetId(
26 : resolveNodeRootGraphId(node, app.rootGraph.id),
27 : node.id,
28 : PREVIEW_WIDGET_NAME
29 : )
30 :
31 70 : const preview = new ComponentWidgetImpl<string | object>({
32 : node,
33 : name: PREVIEW_WIDGET_NAME,
34 : component: WidgetTextPreview,
35 : inputSpec: inputSpecTextPreview,
36 : type: 'textPreview',
37 : options: {
38 : serialize: false,
39 : hideInPanel: true,
40 : getMinHeight: () => 60,
41 : getValue: () => {
42 264 : const stored = widgetStore.getWidget(previewWidgetId())?.value
43 264 : return typeof stored === 'string' ? stored : fallbackValue
44 : },
45 : setValue: (value: string | object) => {
46 40 : fallbackValue = typeof value === 'string' ? value : ''
47 40 : const state = widgetStore.getWidget(previewWidgetId())
48 40 : if (state) state.value = fallbackValue
49 : }
50 : }
51 : })
52 70 : preview.serialize = false
53 70 : addWidget(node, preview)
54 :
55 70 : const modeWidget = ComfyWidgets['BOOLEAN'](
56 : node,
57 : MODE_WIDGET_NAME,
58 : [
59 : 'BOOLEAN',
60 : { label_on: 'Markdown', label_off: 'Plain text', default: false }
61 : ],
62 : app
63 : ).widget
64 :
65 70 : modeWidget.options.serialize = false
66 70 : modeWidget.serialize = false
67 : }
68 :
69 121 : export function toPreviewText(text: unknown): string {
70 121 : if (text == null) return ''
71 44 : if (Array.isArray(text))
72 34 : return text.filter((part) => part != null).join('\n\n')
73 3 : return String(text)
74 : }
|