From f90f77c18e4c5cc26eddc6c69809f87abdd0e28e Mon Sep 17 00:00:00 2001 From: zombieyang Date: Fri, 25 Apr 2025 07:48:28 +0800 Subject: [PATCH] support default widgetable node --- javascript/sdppp-custom.js | 36 +++++++++++++----- plugins/photoshop/css/styles.css | 3 +- typescripts/_build/esbuild.ts | 37 +++++++++++-------- .../modules/comfy/src/graph-to-form.mts | 29 ++++++++++++--- .../comfy/src/popup/tsx/WorkflowEditWeb.tsx | 37 ++++++++++++------- .../comfy/src/popup/tsx/widgets/combo.tsx | 34 ++++++++++------- .../comfy/src/popup/tsx/widgets/number.tsx | 26 ++++++++----- typescripts/modules/photoshop-internal | 2 +- 8 files changed, 135 insertions(+), 69 deletions(-) diff --git a/javascript/sdppp-custom.js b/javascript/sdppp-custom.js index e35ba02..07e8fb6 100644 --- a/javascript/sdppp-custom.js +++ b/javascript/sdppp-custom.js @@ -97,20 +97,20 @@ export default function (sdppp) { } let widgets = node.widgets.slice(0, sliceNum) .map((widget, index) => { - let uiWeight = 12; - if (widget.type == "number" || widget.type == "combo") { - uiWeight = index == 0 ? (sliceNum == 2 ? 8 : 6) : 4 - } - if (widget.type == "toggle") { - uiWeight = 4; - } - return { + const ret = { value: widget.value, - name: widget.label || widget.name, outputType: widget.type || "string", options: widget.options, - uiWeight: uiWeight + uiWeight: 12 } + if (widget.type == "number" || widget.type == "combo") { + ret.uiWeight = index == 0 ? (sliceNum == 2 ? 8 : 6) : 4 + } + if (widget.type == "toggle") { + ret.uiWeight = 4; + ret.name = widget.label || widget.name + } + return ret }) .filter(Boolean) return { @@ -249,6 +249,22 @@ export default function (sdppp) { } } }); + + sdppp.widgetable.add('__DEFAULT__', { + formatter: (node) => { + return { + id: node.id, + title: getTitle(node), + widgets: node.widgets.map((widget) => ({ + name: widget.label || widget.name, + outputType: widget.type || "string", + value: widget.value, + options: widget.options, + uiWeight: widget.uiWeight || 12 + })) + }; + } + }) } diff --git a/plugins/photoshop/css/styles.css b/plugins/photoshop/css/styles.css index 0675e32..6399951 100644 --- a/plugins/photoshop/css/styles.css +++ b/plugins/photoshop/css/styles.css @@ -482,8 +482,7 @@ sp-progressbar { } .workflow-edit-button sp-action-button.highlight { - background-color: var(--uxp-host-text-color-secondary); - color: var(--uxp-host-background-color); + box-shadow: inset 5px 5px var(--uxp-host-text-color); } .client-list { diff --git a/typescripts/_build/esbuild.ts b/typescripts/_build/esbuild.ts index 3cd4301..12c83f2 100644 --- a/typescripts/_build/esbuild.ts +++ b/typescripts/_build/esbuild.ts @@ -13,19 +13,22 @@ import WebSocket, { WebSocketServer } from 'ws'; import { join } from 'path'; import { typescriptSrcRoot } from './lib.ts'; import { globSync } from 'glob'; -// 创建WebSocket服务器 -const wss = new WebSocketServer({ port: 8787 }); -const clients = new Set(); +let wss: WebSocketServer | null = null; +let clients = new Set(); +if (process.env.NODE_ENV === 'development') { + // 创建WebSocket服务器 + wss = new WebSocketServer({ port: 8787 }); -wss.on('connection', (ws) => { - clients.add(ws); - console.log('Client connected, total clients:', clients.size); + wss.on('connection', (ws) => { + clients.add(ws); + console.log('Client connected, total clients:', clients.size); - ws.on('close', () => { - clients.delete(ws); - console.log('Client disconnected, total clients:', clients.size); + ws.on('close', () => { + clients.delete(ws); + console.log('Client disconnected, total clients:', clients.size); + }); }); -}); +} // 向所有客户端发送编译完成通知 function notifyClients(result: { @@ -39,9 +42,11 @@ function notifyClients(result: { warnings: result.warnings.length }); - for (const client of clients) { - if (client.readyState === 1) { // OPEN - client.send(message); + if (process.env.NODE_ENV === 'development') { + for (const client of clients) { + if (client.readyState === 1) { // OPEN + client.send(message); + } } } } @@ -50,7 +55,7 @@ async function createBuildContexts() { const contexts = []; const startTime = Date.now(); const isWatchMode = process.argv.includes('--watch'); - + const buildConfigs = await Promise.all( globSync(join(typescriptSrcRoot, 'modules/*')) .map(async (path: string) => { @@ -110,7 +115,9 @@ async function main() { for (const { context } of contexts) { await context.dispose(); } - wss.close(); + if (process.env.NODE_ENV === 'development') { + wss?.close(); + } console.log('Build completed successfully.'); } } catch (error) { diff --git a/typescripts/modules/comfy/src/graph-to-form.mts b/typescripts/modules/comfy/src/graph-to-form.mts index 16ad309..2a40d97 100644 --- a/typescripts/modules/comfy/src/graph-to-form.mts +++ b/typescripts/modules/comfy/src/graph-to-form.mts @@ -29,9 +29,12 @@ sdpppX.widgetable.add = function (name: string, fn: NodeConverter | { export function setWidgetValue(node: any, widgetIndex: any, value: any) { + const defaultConverter = customNodeConvertersByWildcard.find(([wildcard]) => { + return wildcard == '__DEFAULT__' + }) const converter = customNodeConvertersByWildcard.find(([wildcard]) => { return wildcardMatch(wildcard, node.type); - }); + }) || defaultConverter; let setted = false; if (converter) { const setter = converter[1].setter; @@ -49,15 +52,17 @@ export function setWidgetValue(node: any, widgetIndex: any, value: any) { export function getWidgetTableValue(graph: any): WidgetTableValue { const ret: WidgetTableValue = {}; + const defaultConverter = customNodeConvertersByWildcard.find(([wildcard]) => { + return wildcard == '__DEFAULT__' + }) graph .nodes .forEach((node: any) => { if (!node.widgets || node.widgets.length == 0) return; // no widgets - const converter = customNodeConvertersByWildcard.find(([wildcard]) => { return wildcardMatch(wildcard, node.type); - }); + }) || defaultConverter; if (converter) { const converted = converter[1].formatter(node); if (converted) { @@ -129,16 +134,30 @@ export function makeWidgetTableStructure(graph: any, activeWorkflow: any): Widge } let widgets = node.widgets; - return { + const defaultConverter = customNodeConvertersByWildcard.find(([wildcard]) => { + return wildcard == '__DEFAULT__' + }) + if (defaultConverter) { + const converted = defaultConverter[1].formatter(node); + if (converted) { + return Object.assign(converted, { + uiWeightSum: converted.widgets.reduce((sum: number, widget: any) => sum + (widget.uiWeight || 12), 0), + }); + } + } + const converted = { id: node.id, title: title, - uiWeightSum: widgets.reduce((sum: number, widget: any) => sum + (widget.uiWeight || 12), 0), widgets: widgets.map((widget: any) => ({ name: widget.label || widget.name, outputType: widget.type || "string", + value: widget.value, options: widget.options })) }; + return Object.assign(converted, { + uiWeightSum: converted.widgets.reduce((sum: number, widget: any) => sum + (widget.uiWeight || 12), 0), + }); }) .filter(Boolean) .sort((a: WidgetTableStructureNode, b: WidgetTableStructureNode) => { diff --git a/typescripts/modules/comfy/src/popup/tsx/WorkflowEditWeb.tsx b/typescripts/modules/comfy/src/popup/tsx/WorkflowEditWeb.tsx index c0e005b..ea29503 100644 --- a/typescripts/modules/comfy/src/popup/tsx/WorkflowEditWeb.tsx +++ b/typescripts/modules/comfy/src/popup/tsx/WorkflowEditWeb.tsx @@ -37,20 +37,13 @@ export class WorkflowEditWrap extends React.Component<{ lastError: '', executingNodeTitle: '', }, - widgetTableStructure: { - nodes: {}, - groups: {}, - nodeIndexes: [], - widgetTableID: '', - widgetTableName: '', - widgetTablePath: '', - widgetTablePersisted: false, - } as WidgetTableStructure, - widgetTableValue: {} as WidgetTableValue, - widgetTableErrors: {} as Record, + widgetTableStructure: pageStore.data.widgetTableStructure, + widgetTableValue: pageStore.data.widgetTableValue, + widgetTableErrors: pageStore.data.widgetTableErrors, } private eventListeners: { event: string; handler: (e: any) => void }[] = []; + private unsubscribes: (() => void)[] = []; constructor(props: any) { super(props) @@ -134,15 +127,28 @@ export class WorkflowEditWrap extends React.Component<{ }); }); - pageStore.subscribe('widgetTableStructure', (wtStructure: WidgetTableStructure) => { + const structureCallback = (wtStructure: WidgetTableStructure) => { this.setState({ widgetTableStructure: wtStructure }) - }) - pageStore.subscribe('widgetTableValue', (wtValue: WidgetTableValue) => { + } + pageStore.subscribe('/widgetTableStructure', structureCallback) + const valueCallback = (wtValue: WidgetTableValue) => { this.setState({ widgetTableValue: wtValue }) + } + pageStore.subscribe('/widgetTableValue', valueCallback) + const errorsCallback = (wtErrors: Record) => { + this.setState({ + widgetTableErrors: wtErrors + }) + } + pageStore.subscribe('/widgetTableErrors', errorsCallback) + this.unsubscribes.push(() => { + pageStore.unsubscribe(structureCallback) + pageStore.unsubscribe(valueCallback) + pageStore.unsubscribe(errorsCallback) }) } @@ -150,6 +156,7 @@ export class WorkflowEditWrap extends React.Component<{ this.eventListeners.forEach(({ event, handler }) => { api.removeEventListener(event, handler); }); + this.unsubscribes.forEach((unsubscribe) => unsubscribe()) } render() { @@ -196,6 +203,7 @@ export class WorkflowEditWrap extends React.Component<{ inputMin={min} inputMax={max} inputStep={step} + name={widget.name} value={parseFloat(this.state.widgetTableValue[fieldInfo.id][widgetIndex])} onValueChange={(v) => { editProps.onWidgetChange(fieldInfo.id, widgetIndex, v, fieldInfo); @@ -208,6 +216,7 @@ export class WorkflowEditWrap extends React.Component<{ uiWeight={widget.uiWeight || 12} key={widgetIndex} options={widget.options?.values || []} + name={widget.name} onSelectUpdate={(v) => { editProps.onWidgetChange(fieldInfo.id, widgetIndex, v, fieldInfo); }} diff --git a/typescripts/modules/comfy/src/popup/tsx/widgets/combo.tsx b/typescripts/modules/comfy/src/popup/tsx/widgets/combo.tsx index e03b73f..ae6f5e6 100644 --- a/typescripts/modules/comfy/src/popup/tsx/widgets/combo.tsx +++ b/typescripts/modules/comfy/src/popup/tsx/widgets/combo.tsx @@ -1,4 +1,4 @@ -import { Select } from 'antd'; +import { Flex, Select, Typography } from 'antd'; import React from 'react'; import i18n from '../../../../../../src/common/i18n.mts'; import { useUIWeightCSS } from './hooks.mts'; @@ -8,12 +8,14 @@ export interface DropdownWidgetProps extends BaseWidgetProps { onSelectUpdate: (identify: string, index: number) => void; options: string[]; value: string; -} + name?: string; +} export const ComboWidget: React.FC = ({ onSelectUpdate, options, value, + name, uiWeight }) => { // 处理选择变化 @@ -24,17 +26,23 @@ export const ComboWidget: React.FC = ({ const uiWeightCSS = useUIWeightCSS(uiWeight || 12); return ( - ({ value: option, label: option }))} + style={{ flex: 2 }} + onSelect={handleSelect} + placeholder={i18n("select...")} + showSearch + filterOption={(input, option) => + (option?.label ?? '').toLowerCase().includes(input.toLowerCase()) + } + /> + ); }; diff --git a/typescripts/modules/comfy/src/popup/tsx/widgets/number.tsx b/typescripts/modules/comfy/src/popup/tsx/widgets/number.tsx index 22ac237..784170a 100644 --- a/typescripts/modules/comfy/src/popup/tsx/widgets/number.tsx +++ b/typescripts/modules/comfy/src/popup/tsx/widgets/number.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Flex, InputNumber, Slider, Space } from 'antd'; +import { Flex, InputNumber, Slider, Space, Typography } from 'antd'; import { useUIWeightCSS } from './hooks.mts'; import { BaseWidgetProps } from './_base'; @@ -8,6 +8,7 @@ interface NumberWidgetProps extends BaseWidgetProps { inputMax: number; inputStep: number; value?: number; + name?: string; onValueChange: (value: number) => void; } @@ -15,6 +16,7 @@ export const NumberWidget: React.FC = ({ inputMin, inputMax, inputStep, + name, value = 0, uiWeight = 1, onValueChange @@ -58,14 +60,20 @@ export const NumberWidget: React.FC = ({ // } return ( - + align='center' + > + {name && {name}} + + ); }; diff --git a/typescripts/modules/photoshop-internal b/typescripts/modules/photoshop-internal index 67ab9be..1a240fb 160000 --- a/typescripts/modules/photoshop-internal +++ b/typescripts/modules/photoshop-internal @@ -1 +1 @@ -Subproject commit 67ab9be25893088876f77fb095af4407e0a41c36 +Subproject commit 1a240fbc7312afd0897e69e47ce1d64939262b37