Line data Source code
1 : <template>
2 7792 : <div
3 : v-show="widgetState.visible"
4 : ref="widgetElement"
5 : class="dom-widget size-full"
6 : :title="tooltip"
7 : :style="style"
8 : >
9 : <component
10 : :is="widget.component"
11 : v-if="isComponentWidget(widget)"
12 : class="size-full"
13 : :model-value="widget.value"
14 : :widget="widget"
15 : :node-id="widget.node.id"
16 : v-bind="widget.props"
17 : @update:model-value="emit('update:widgetValue', $event)"
18 : />
19 : </div>
20 : </template>
21 :
22 : <script setup lang="ts">
23 : import { useElementBounding, useEventListener, whenever } from '@vueuse/core'
24 : import type { CSSProperties } from 'vue'
25 : import { computed, nextTick, onMounted, ref, watch } from 'vue'
26 :
27 : import { useAbsolutePosition } from '@/composables/element/useAbsolutePosition'
28 : import { useDomClipping } from '@/composables/element/useDomClipping'
29 : import { findFirstNode } from '@/lib/litegraph/src/utils/collections'
30 : import { useSettingStore } from '@/platform/settings/settingStore'
31 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
32 : import { isComponentWidget, isDOMWidget } from '@/scripts/domWidget'
33 : import type { DomWidgetState } from '@/stores/domWidgetStore'
34 :
35 : import { reportDomWidgetMountFailure } from './domWidgetMountReporting'
36 :
37 : const { widgetState } = defineProps<{
38 : widgetState: DomWidgetState
39 : }>()
40 4558 : const widget = widgetState.widget
41 :
42 4558 : const emit = defineEmits<{
43 : 'update:widgetValue': [value: string | object]
44 : }>()
45 :
46 4558 : const widgetElement = ref<HTMLElement | undefined>()
47 4558 : const { style: positionStyle, updatePosition } = useAbsolutePosition({
48 : useTransform: true
49 : })
50 4558 : const { style: clippingStyle, updateClipPath } = useDomClipping()
51 :
52 4558 : const canvasStore = useCanvasStore()
53 4558 : const settingStore = useSettingStore()
54 4558 : const enableDomClipping = computed(() =>
55 : settingStore.get('Comfy.DOMClippingEnabled')
56 : )
57 4558 : const style = computed<CSSProperties>(() => {
58 7788 : const isDisabled = widget.computedDisabled
59 :
60 7788 : return {
61 : ...positionStyle.value,
62 : ...(enableDomClipping.value ? clippingStyle.value : {}),
63 : zIndex: widgetState.zIndex,
64 : pointerEvents:
65 : !widgetState.visible || widgetState.readonly || isDisabled
66 : ? 'none'
67 : : 'auto',
68 : opacity: isDisabled ? 0.5 : 1
69 : }
70 : })
71 :
72 4558 : const updateDomClipping = () => {
73 6362 : const lgCanvas = canvasStore.canvas
74 6362 : if (!lgCanvas || !widgetElement.value) return
75 :
76 1804 : const selectedNode = findFirstNode(lgCanvas.selectedItems)
77 6299 : if (!selectedNode) {
78 : // Clear clipping when no node is selected
79 730 : updateClipPath(widgetElement.value, lgCanvas.canvas, false, undefined)
80 730 : return
81 : }
82 :
83 1074 : const isSelected = selectedNode === widgetState.widget.node
84 6362 : const renderArea = selectedNode?.renderArea
85 6362 : const offset = lgCanvas.ds.offset
86 6362 : const scale = lgCanvas.ds.scale
87 6362 : const selectedAreaConfig = renderArea
88 : ? {
89 : x: renderArea[0],
90 : y: renderArea[1],
91 : width: renderArea[2],
92 : height: renderArea[3],
93 : scale,
94 : offset: [offset[0], offset[1]] as [number, number]
95 : }
96 : : undefined
97 :
98 6362 : updateClipPath(
99 : widgetElement.value,
100 : lgCanvas.canvas,
101 : isSelected,
102 : selectedAreaConfig
103 : )
104 : }
105 :
106 : /**
107 : * @note mapping between canvas position and client position depends on the
108 : * canvas element's position, so we need to watch the canvas element's position
109 : * and update the position of the widget accordingly.
110 : */
111 4558 : const { left, top } = useElementBounding(canvasStore.getCanvas().canvas)
112 :
113 4558 : watch(
114 : [
115 : () => widgetState.pos,
116 : () => widgetState.size,
117 : // Visibility transitions (e.g. LOD low_quality flipping) must refresh
118 : // style: while invisible, DomWidgets.vue does not update widgetState
119 : // and ds.offset/ds.scale are non-reactive, so updatePosition must be
120 : // re-run against the current viewport when the widget reappears.
121 : () => widgetState.visible,
122 : left,
123 : top,
124 : enableDomClipping
125 : ],
126 : () => {
127 6362 : updatePosition(widgetState)
128 6362 : if (enableDomClipping.value) {
129 6362 : updateDomClipping()
130 : }
131 : },
132 : { immediate: true }
133 : )
134 :
135 4558 : watch(
136 : () => widgetState.visible,
137 : (newVisible, oldVisible) => {
138 153 : if (!newVisible && oldVisible) {
139 103 : widget.options.onHide?.(widget)
140 : }
141 : }
142 : )
143 4558 : useEventListener(document, 'mousedown', (event) => {
144 4506 : if (!isDOMWidget(widget) || !widgetState.visible || !widget.element.blur) {
145 231 : return
146 : }
147 4275 : if (!widget.element.contains(event.target as HTMLElement)) {
148 4264 : widget.element.blur()
149 : }
150 : })
151 :
152 4558 : onMounted(() => {
153 4558 : if (!isDOMWidget(widget)) {
154 25 : return
155 : }
156 4558 : useEventListener(
157 : widget.element,
158 : widget.options.selectOn ?? ['focus', 'click'],
159 : () => {
160 23 : const lgCanvas = canvasStore.canvas
161 23 : if (!lgCanvas) return
162 :
163 23 : const ownerNode = widgetState.widget.node
164 23 : lgCanvas.selectNode(ownerNode)
165 23 : lgCanvas.bringToFront(ownerNode)
166 : }
167 : )
168 : })
169 :
170 : const inputSpec = widget.node.constructor.nodeData
171 4558 : const tooltip = inputSpec?.inputs?.[widget.name]?.tooltip
172 :
173 : // Mount DOM element when widget is or becomes visible
174 4558 : const mountElementIfVisible = () => {
175 4721 : if (!(widgetState.visible && isDOMWidget(widget) && widgetElement.value)) {
176 473 : return
177 : }
178 : // Only append if not already a child
179 4719 : if (widgetElement.value.contains(widget.element)) {
180 42 : return
181 : }
182 :
183 4719 : try {
184 4206 : widget.element.classList.add('h-full', 'w-full')
185 4206 : widgetElement.value.appendChild(widget.element)
186 : } catch (error) {
187 0 : reportDomWidgetMountFailure(error, {
188 : nodeId: widget.node.id,
189 : nodeType: widget.node.type,
190 : widgetName: widget.name
191 : })
192 : }
193 : }
194 :
195 : // Check on mount - but only after next tick to ensure visibility is calculated
196 4558 : onMounted(() => {
197 4558 : void nextTick(mountElementIfVisible)
198 : })
199 :
200 : // And watch for visibility changes
201 4558 : watch(
202 : () => widgetState.visible,
203 : () => {
204 153 : mountElementIfVisible()
205 : }
206 : )
207 :
208 4558 : whenever(() => !canvasStore.linearMode, mountElementIfVisible)
209 : </script>
|