[fix] Update Search & Replace to support nodes in subgraphs (#4576)

This commit is contained in:
Christian Byrne
2025-07-29 00:10:56 -07:00
committed by GitHub
parent 00cd9fadec
commit dd14144f47
5 changed files with 24 additions and 8 deletions

View File

@@ -35,7 +35,7 @@ app.registerExtension({
// @ts-expect-error fixme ts strict error // @ts-expect-error fixme ts strict error
widget.serializeValue = () => { widget.serializeValue = () => {
// @ts-expect-error fixme ts strict error // @ts-expect-error fixme ts strict error
return applyTextReplacements(app.graph.nodes, widget.value) return applyTextReplacements(app.graph, widget.value)
} }
return r return r

View File

@@ -46,7 +46,7 @@ export class PrimitiveNode extends LGraphNode {
] ]
let v = this.widgets?.[0].value let v = this.widgets?.[0].value
if (v && this.properties[replacePropertyName]) { if (v && this.properties[replacePropertyName]) {
v = applyTextReplacements(app.graph.nodes, v as string) v = applyTextReplacements(app.graph, v as string)
} }
// For each output link copy our value over the original widget value // For each output link copy our value over the original widget value

View File

@@ -21,7 +21,7 @@ export function clone<T>(obj: T): T {
* There are external callers to this function, so we need to keep it for now * There are external callers to this function, so we need to keep it for now
*/ */
export function applyTextReplacements(app: ComfyApp, value: string): string { export function applyTextReplacements(app: ComfyApp, value: string): string {
return _applyTextReplacements(app.graph.nodes, value) return _applyTextReplacements(app.graph, value)
} }
export async function addStylesheet( export async function addStylesheet(

View File

@@ -1,11 +1,14 @@
import type { LGraphNode } from '@comfyorg/litegraph' import type { LGraph, Subgraph } from '@comfyorg/litegraph'
import { formatDate } from '@/utils/formatUtil' import { formatDate } from '@/utils/formatUtil'
import { collectAllNodes } from '@/utils/graphTraversalUtil'
export function applyTextReplacements( export function applyTextReplacements(
allNodes: LGraphNode[], graph: LGraph | Subgraph,
value: string value: string
): string { ): string {
const allNodes = collectAllNodes(graph)
return value.replace(/%([^%]+)%/g, function (match, text) { return value.replace(/%([^%]+)%/g, function (match, text) {
const split = text.split('.') const split = text.split('.')
if (split.length !== 2) { if (split.length !== 2) {

View File

@@ -1,3 +1,4 @@
import { LGraph } from '@comfyorg/litegraph'
import type { LGraphNode } from '@comfyorg/litegraph' import type { LGraphNode } from '@comfyorg/litegraph'
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
@@ -21,7 +22,11 @@ describe('applyTextReplacements', () => {
} as LGraphNode } as LGraphNode
] ]
const result = applyTextReplacements(mockNodes, '%TestNode.testWidget%') const mockGraph = new LGraph()
for (const node of mockNodes) {
mockGraph.add(node)
}
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
// The expected result should have all invalid characters replaced with underscores // The expected result should have all invalid characters replaced with underscores
expect(result).toBe('file_name_with_invalid_chars_____control_chars__') expect(result).toBe('file_name_with_invalid_chars_____control_chars__')
@@ -51,7 +56,11 @@ describe('applyTextReplacements', () => {
} as LGraphNode } as LGraphNode
] ]
const result = applyTextReplacements(mockNodes, '%TestNode.testWidget%') const mockGraph = new LGraph()
for (const node of mockNodes) {
mockGraph.add(node)
}
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
expect(result).toBe(expected) expect(result).toBe(expected)
} }
}) })
@@ -66,7 +75,11 @@ describe('applyTextReplacements', () => {
} as LGraphNode } as LGraphNode
] ]
const result = applyTextReplacements(mockNodes, '%TestNode.testWidget%') const mockGraph = new LGraph()
for (const node of mockNodes) {
mockGraph.add(node)
}
const result = applyTextReplacements(mockGraph, '%TestNode.testWidget%')
expect(result).toBe(validChars) expect(result).toBe(validChars)
}) })
}) })