Skip to content

Commit dd14144

Browse files
[fix] Update Search & Replace to support nodes in subgraphs (#4576)
1 parent 00cd9fa commit dd14144

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

src/extensions/core/saveImageExtraOutput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ app.registerExtension({
3535
// @ts-expect-error fixme ts strict error
3636
widget.serializeValue = () => {
3737
// @ts-expect-error fixme ts strict error
38-
return applyTextReplacements(app.graph.nodes, widget.value)
38+
return applyTextReplacements(app.graph, widget.value)
3939
}
4040

4141
return r

src/extensions/core/widgetInputs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class PrimitiveNode extends LGraphNode {
4646
]
4747
let v = this.widgets?.[0].value
4848
if (v && this.properties[replacePropertyName]) {
49-
v = applyTextReplacements(app.graph.nodes, v as string)
49+
v = applyTextReplacements(app.graph, v as string)
5050
}
5151

5252
// For each output link copy our value over the original widget value

src/scripts/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function clone<T>(obj: T): T {
2121
* There are external callers to this function, so we need to keep it for now
2222
*/
2323
export function applyTextReplacements(app: ComfyApp, value: string): string {
24-
return _applyTextReplacements(app.graph.nodes, value)
24+
return _applyTextReplacements(app.graph, value)
2525
}
2626

2727
export async function addStylesheet(

src/utils/searchAndReplace.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import type { LGraphNode } from '@comfyorg/litegraph'
1+
import type { LGraph, Subgraph } from '@comfyorg/litegraph'
22

33
import { formatDate } from '@/utils/formatUtil'
4+
import { collectAllNodes } from '@/utils/graphTraversalUtil'
45

56
export function applyTextReplacements(
6-
allNodes: LGraphNode[],
7+
graph: LGraph | Subgraph,
78
value: string
89
): string {
10+
const allNodes = collectAllNodes(graph)
11+
912
return value.replace(/%([^%]+)%/g, function (match, text) {
1013
const split = text.split('.')
1114
if (split.length !== 2) {

tests-ui/tests/utils/serachAndReplace.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LGraph } from '@comfyorg/litegraph'
12
import type { LGraphNode } from '@comfyorg/litegraph'
23
import { describe, expect, it } from 'vitest'
34

@@ -21,7 +22,11 @@ describe('applyTextReplacements', () => {
2122
} as LGraphNode
2223
]
2324

24-
const result = applyTextReplacements(mockNodes, '%TestNode.testWidget%')
25+
const mockGraph = new LGraph()
26+
for (const node of mockNodes) {
27+
mockGraph.add(node)
28+
}
29+
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
2530

2631
// The expected result should have all invalid characters replaced with underscores
2732
expect(result).toBe('file_name_with_invalid_chars_____control_chars__')
@@ -51,7 +56,11 @@ describe('applyTextReplacements', () => {
5156
} as LGraphNode
5257
]
5358

54-
const result = applyTextReplacements(mockNodes, '%TestNode.testWidget%')
59+
const mockGraph = new LGraph()
60+
for (const node of mockNodes) {
61+
mockGraph.add(node)
62+
}
63+
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
5564
expect(result).toBe(expected)
5665
}
5766
})
@@ -66,7 +75,11 @@ describe('applyTextReplacements', () => {
6675
} as LGraphNode
6776
]
6877

69-
const result = applyTextReplacements(mockNodes, '%TestNode.testWidget%')
78+
const mockGraph = new LGraph()
79+
for (const node of mockNodes) {
80+
mockGraph.add(node)
81+
}
82+
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
7083
expect(result).toBe(validChars)
7184
})
7285
})

0 commit comments

Comments
 (0)