Line data Source code
1 3 : <template>
2 : <div class="flex flex-col gap-1">
3 : <div v-if="!isDisabled" class="flex items-center gap-1">
4 : <div class="min-w-0 flex-1">
5 : <Select
6 : :model-value="modelValue.interpolation"
7 : @update:model-value="onInterpolationChange"
8 : >
9 : <SelectTrigger size="md">
10 : <SelectValue />
11 : </SelectTrigger>
12 : <SelectContent>
13 6 : <SelectItem
14 : v-for="interp in CURVE_INTERPOLATIONS"
15 : :key="interp"
16 : :value="interp"
17 : >
18 : {{ $t(`curveWidget.${interp}`) }}
19 : </SelectItem>
20 : </SelectContent>
21 : </Select>
22 : </div>
23 : <WidgetHistoryButtons :can-undo :can-redo @undo="undo" @redo="redo" />
24 : </div>
25 : <CurveEditor
26 : :model-value="effectiveCurve.points"
27 : :disabled="isDisabled"
28 : :interpolation="effectiveCurve.interpolation"
29 : :histogram="histogram"
30 : @update:model-value="onPointsChange"
31 : @edit-end="commitAfterUpdate"
32 : />
33 : </div>
34 : </template>
35 :
36 : <script setup lang="ts">
37 : import { computed, nextTick, watch } from 'vue'
38 :
39 : import {
40 : singleValueExtractor,
41 : useUpstreamValue
42 : } from '@/composables/useUpstreamValue'
43 : import { useWidgetValueHistory } from '@/composables/useWidgetValueHistory'
44 : import { widgetHistoryScope } from '@/composables/widgetHistoryScope'
45 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
46 : import type { SimplifiedWidget } from '@/types/simplifiedWidget'
47 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
48 : import WidgetHistoryButtons from '@/components/widgetHistory/WidgetHistoryButtons.vue'
49 :
50 : import Select from '@comfyorg/design-system/select/Select'
51 : import SelectContent from '@comfyorg/design-system/select/SelectContent'
52 : import SelectItem from '@comfyorg/design-system/select/SelectItem'
53 : import SelectTrigger from '@comfyorg/design-system/select/SelectTrigger'
54 : import SelectValue from '@comfyorg/design-system/select/SelectValue'
55 :
56 : import CurveEditor from './CurveEditor.vue'
57 : import { isCurveData } from './curveUtils'
58 : import { CURVE_INTERPOLATIONS } from '@comfyorg/graph-model/curve'
59 : import type { CurveData, CurveInterpolation } from '@comfyorg/graph-model/curve'
60 : import type { PointTuple as CurvePoint } from '@comfyorg/shared-frontend-utils/geometry'
61 :
62 : const { widget, nodeId } = defineProps<{
63 : widget: SimplifiedWidget
64 : nodeId?: NodeId
65 : }>()
66 :
67 3 : const modelValue = defineModel<CurveData>({
68 : default: () => ({
69 : points: [
70 : [0, 0],
71 : [1, 1]
72 : ],
73 : interpolation: 'monotone_cubic'
74 : })
75 : })
76 :
77 3 : const isDisabled = computed(() => !!widget.options?.disabled)
78 :
79 3 : const nodeOutputStore = useNodeOutputStore()
80 3 : const histogram = computed(() => {
81 4 : const locatorId = widget.nodeLocatorId
82 4 : if (!locatorId) return null
83 : const output = nodeOutputStore.nodeOutputs[locatorId]
84 4 : const data = output?.histogram
85 4 : if (!Array.isArray(data) || data.length === 0) return null
86 0 : return new Uint32Array(data)
87 : })
88 :
89 3 : const upstreamValue = useUpstreamValue(
90 : () => widget.linkedUpstream,
91 : singleValueExtractor(isCurveData)
92 : )
93 :
94 3 : const { canUndo, canRedo, undo, redo, commit, discardHistory } =
95 : useWidgetValueHistory({
96 : scope: () => widgetHistoryScope(widget, nodeId),
97 : read: () => modelValue.value,
98 : apply: (value) => {
99 0 : if (isCurveData(value)) modelValue.value = value
100 : }
101 : })
102 :
103 1 : function commitAfterUpdate() {
104 1 : void nextTick(() => commit())
105 : }
106 :
107 3 : watch(upstreamValue, (upstream) => {
108 0 : if (isDisabled.value && upstream) {
109 0 : modelValue.value = upstream
110 0 : void nextTick(discardHistory)
111 : }
112 : })
113 :
114 3 : const effectiveCurve = computed(() =>
115 : isDisabled.value && upstreamValue.value
116 : ? upstreamValue.value
117 : : modelValue.value
118 : )
119 :
120 1 : function onPointsChange(points: CurvePoint[]) {
121 1 : modelValue.value = { ...modelValue.value, points }
122 : }
123 :
124 0 : function onInterpolationChange(value: unknown) {
125 0 : if (typeof value !== 'string') return
126 0 : modelValue.value = {
127 : ...modelValue.value,
128 : interpolation: value as CurveInterpolation
129 : }
130 0 : commitAfterUpdate()
131 : }
132 : </script>
|