Skip to content

Commit d69c548

Browse files
authored
Subgraph widget promotion fixes (#5911)
- Fixes automatic promotion of image previews by ~~more correctly handling a usage of `requestAnimationFrame` and~~ introducing a means to perform actions upon successful load of preview. - When workflows contain an old proxyWidget property in string form, parse it instead of throwing an error. - Do not overlay the `label` of promoted widgets. Further consideration is needed, but this resolves the primary annoyance and prevents untranslated widget names See #5914 ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-5911-Subgraph-widget-promotion-fixes-2826d73d365081838ffeeea4b8d7068c) by [Unito](https://www.unito.io)
1 parent 3eedff3 commit d69c548

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

src/composables/node/useNodeImage.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const useNodePreview = <T extends MediaElement>(
9898
/**
9999
* Attaches a preview image to a node.
100100
*/
101-
export const useNodeImage = (node: LGraphNode) => {
101+
export const useNodeImage = (node: LGraphNode, callback?: () => void) => {
102102
node.previewMediaType = 'image'
103103

104104
const loadElement = (url: string): Promise<HTMLImageElement | null> =>
@@ -112,6 +112,7 @@ export const useNodeImage = (node: LGraphNode) => {
112112
const onLoaded = (elements: HTMLImageElement[]) => {
113113
node.imageIndex = null
114114
node.imgs = elements
115+
callback?.()
115116
}
116117

117118
return useNodePreview(node, {
@@ -126,7 +127,7 @@ export const useNodeImage = (node: LGraphNode) => {
126127
/**
127128
* Attaches a preview video to a node.
128129
*/
129-
export const useNodeVideo = (node: LGraphNode) => {
130+
export const useNodeVideo = (node: LGraphNode, callback?: () => void) => {
130131
node.previewMediaType = 'video'
131132
let minHeight = DEFAULT_VIDEO_SIZE
132133
let minWidth = DEFAULT_VIDEO_SIZE
@@ -187,6 +188,7 @@ export const useNodeVideo = (node: LGraphNode) => {
187188
}
188189

189190
node.videoContainer.replaceChildren(videoElement)
191+
callback?.()
190192
}
191193

192194
return useNodePreview(node, {

src/core/graph/subgraph/proxyWidget.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ function addProxyWidget(
121121
afterQueued: undefined,
122122
computedHeight: undefined,
123123
isProxyWidget: true,
124-
label: name,
125124
last_y: undefined,
126125
name,
127126
node: subgraphNode,

src/core/graph/subgraph/proxyWidgetUtils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,15 @@ export function promoteRecommendedWidgets(subgraphNode: SubgraphNode) {
119119
const interiorNodes = subgraphNode.subgraph.nodes
120120
for (const node of interiorNodes) {
121121
node.updateComputedDisabled()
122-
//NOTE: Since this operation is async, previews still don't exist after the single frame
123-
//Add an onLoad callback to updatePreviews?
124-
updatePreviews(node)
122+
function checkWidgets() {
123+
updatePreviews(node)
124+
const widget = node.widgets?.find((w) => w.name.startsWith('$$'))
125+
if (!widget) return
126+
const pw = getProxyWidgets(subgraphNode)
127+
if (pw.some(matchesPropertyItem([node, widget]))) return
128+
promoteWidget(node, widget, [subgraphNode])
129+
}
130+
requestAnimationFrame(() => updatePreviews(node, checkWidgets))
125131
}
126132
const filteredWidgets: WidgetItem[] = interiorNodes
127133
.flatMap(nodeWidgets)

src/core/schemas/proxyWidget.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ export type ProxyWidgetsProperty = z.infer<typeof proxyWidgetsPropertySchema>
99
export function parseProxyWidgets(
1010
property: NodeProperty | undefined
1111
): ProxyWidgetsProperty {
12-
const result = proxyWidgetsPropertySchema.safeParse(property)
12+
if (typeof property === 'string') property = JSON.parse(property)
13+
const result = proxyWidgetsPropertySchema.safeParse(
14+
typeof property === 'string' ? JSON.parse(property) : property
15+
)
1316
if (result.success) return result.data
1417

1518
const error = fromZodError(result.error)

src/services/litegraphService.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -853,14 +853,14 @@ export const useLitegraphService = () => {
853853
return []
854854
}
855855
}
856-
function updatePreviews(node: LGraphNode) {
856+
function updatePreviews(node: LGraphNode, callback?: () => void) {
857857
try {
858-
unsafeUpdatePreviews.call(node)
858+
unsafeUpdatePreviews.call(node, callback)
859859
} catch (error) {
860860
console.error('Error drawing node background', error)
861861
}
862862
}
863-
function unsafeUpdatePreviews(this: LGraphNode) {
863+
function unsafeUpdatePreviews(this: LGraphNode, callback?: () => void) {
864864
if (this.flags.collapsed) return
865865

866866
const nodeOutputStore = useNodeOutputStore()
@@ -891,9 +891,9 @@ export const useLitegraphService = () => {
891891
(this.animatedImages && !isAnimatedWebp && !isAnimatedPng) ||
892892
isVideoNode(this)
893893
if (isVideo) {
894-
useNodeVideo(this).showPreview()
894+
useNodeVideo(this, callback).showPreview()
895895
} else {
896-
useNodeImage(this).showPreview()
896+
useNodeImage(this, callback).showPreview()
897897
}
898898
}
899899

0 commit comments

Comments
 (0)