Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
2 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
3 : import { resolveNodeRootGraphId } from '@/lib/litegraph/src/utils/widget'
4 : import { defineDeprecatedProperty } from '@/lib/litegraph/src/utils/feedback'
5 : import {
6 : bindMultilineTextareaWidget,
7 : createMultilineInputElement
8 : } from '@/renderer/extensions/vueNodes/widgets/utils/multilineTextarea'
9 : import { isStringInputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
10 : import type { InputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
11 : import { useApp } from '@/scripts/appInstance'
12 : import { isDOMWidget } from '@/scripts/domWidget'
13 : import type { DOMWidgetOptions } from '@/types/domWidget'
14 : import type { ComfyWidgetConstructorV2 } from '@/types/widgetConstructor'
15 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
16 : import { widgetId } from '@comfyorg/graph-model/widgetId'
17 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
18 : import type { WidgetState } from '@comfyorg/graph-model/widgetState'
19 :
20 6548 : function addMultilineWidget(
21 : node: LGraphNode,
22 : name: string,
23 : opts: { defaultVal: string; placeholder?: string }
24 : ) {
25 6548 : const widgetStore = useWidgetValueStore()
26 6548 : const inputEl = createMultilineInputElement(
27 : opts.defaultVal,
28 : opts.placeholder || name
29 : )
30 :
31 6548 : let owner: IBaseWidget | undefined
32 6548 : let ownState: WidgetState | undefined
33 : /**
34 : * Resolves the store entry under the widget's current name and adopts it as
35 : * its own, or returns undefined once the widget has left its node and the
36 : * entry belongs to another widget. The widget is found by its element
37 : * because `addDOMWidget` can invoke `setValue` synchronously, while restoring
38 : * a saved value, before the `widget` binding it returns is initialized.
39 : */
40 40998 : function adoptOwnEntry():
41 : | { id: WidgetId; state: WidgetState | undefined }
42 : | undefined {
43 40998 : owner ??= node.widgets?.find(
44 : (widget) => isDOMWidget(widget) && widget.element === inputEl
45 : )
46 40998 : const graphId = resolveNodeRootGraphId(node, useApp().rootGraph.id)
47 40998 : const id = widgetId(graphId, node.id, owner?.name ?? name)
48 40998 : const state = widgetStore.getWidget(id)
49 : const listed = owner !== undefined && node.widgets?.includes(owner) === true
50 : const takenByAnother =
51 : !listed && ownState !== undefined && state !== ownState
52 40998 : if (takenByAnother) return undefined
53 40998 : ownState = state
54 40998 : return { id, state }
55 : }
56 :
57 6548 : const widgetOptions: DOMWidgetOptions<string> = {
58 : getValue(): string {
59 34387 : const value = adoptOwnEntry()?.state?.value
60 34387 : return typeof value === 'string' ? value : inputEl.value
61 : },
62 : setValue(v: string) {
63 6611 : inputEl.value = v
64 6611 : const entry = adoptOwnEntry()
65 6611 : if (entry === undefined) return
66 6611 : if (entry.state) {
67 6609 : entry.state.value = v
68 6609 : return
69 : }
70 2 : ownState = widgetStore.registerWidget(entry.id, {
71 : type: 'customtext',
72 : value: v,
73 : options: widgetOptions
74 : })
75 : }
76 : }
77 :
78 6548 : const widget = node.addDOMWidget(name, 'customtext', inputEl, widgetOptions)
79 :
80 6548 : widget.element = inputEl
81 :
82 : /** @deprecated Use {@link widget.element} instead (renamed in PR #8594). */
83 6548 : defineDeprecatedProperty(
84 : widget,
85 : 'inputEl',
86 : 'element',
87 : 'widget.inputEl is deprecated. Use widget.element instead.'
88 : )
89 6548 : widget.options.minNodeSize = [400, 200]
90 :
91 6548 : bindMultilineTextareaWidget(widget, inputEl)
92 :
93 6548 : return widget
94 : }
95 :
96 2356 : export const useStringWidget = () => {
97 4711 : const widgetConstructor: ComfyWidgetConstructorV2 = (
98 : node: LGraphNode,
99 : inputSpec: InputSpec
100 : ) => {
101 9778 : if (!isStringInputSpec(inputSpec)) {
102 0 : throw new Error(`Invalid input data: ${inputSpec}`)
103 : }
104 :
105 9778 : const defaultVal = inputSpec.default ?? ''
106 : const multiline = inputSpec.multiline
107 :
108 9778 : const widget = multiline
109 : ? addMultilineWidget(node, inputSpec.name, {
110 : defaultVal,
111 : placeholder: inputSpec.placeholder
112 : })
113 : : node.addWidget('text', inputSpec.name, defaultVal, () => {}, {})
114 :
115 9778 : if (typeof inputSpec.dynamicPrompts === 'boolean') {
116 5969 : widget.dynamicPrompts = inputSpec.dynamicPrompts
117 : }
118 :
119 9778 : return widget
120 : }
121 :
122 4711 : return widgetConstructor
123 : }
|