Line data Source code
1 : import type {
2 : LGraph,
3 : LGraphNode,
4 : Subgraph
5 : } from '@/lib/litegraph/src/litegraph'
6 : import { parseAnnotatedPath } from '@/utils/createAnnotatedPath'
7 : import { collectAllNodes } from '@/utils/graphTraversalUtil'
8 :
9 : const LONGEST_ANNOTATION = '[output]'
10 :
11 0 : function readAnnotationRoot(suffix: string): string | undefined {
12 0 : const annotation = suffix.trim()
13 0 : if (!annotation || annotation.length > LONGEST_ANNOTATION.length) return
14 0 : const { filepath, rootFolder } = parseAnnotatedPath(annotation)
15 0 : return filepath ? undefined : rootFolder
16 : }
17 :
18 0 : function indexRootsByPath(deletedValues: ReadonlySet<string>) {
19 0 : const rootsByPath = new Map<string, Set<string>>()
20 0 : for (const value of deletedValues) {
21 0 : const { filepath, rootFolder } = parseAnnotatedPath(value)
22 0 : if (!filepath) continue
23 0 : const roots = rootsByPath.get(filepath) ?? new Set<string>()
24 0 : rootsByPath.set(filepath, roots.add(rootFolder))
25 : }
26 0 : return rootsByPath
27 : }
28 :
29 : /** Matches widget values naming the same root folder and path as a deleted value. */
30 0 : export function createDeletedValueMatcher(
31 : deletedValues: ReadonlySet<string>
32 : ): (value: unknown) => boolean {
33 0 : const rootsByPath = indexRootsByPath(deletedValues)
34 0 : const hasRoot = (filepath: string, rootFolder: string | undefined) =>
35 : rootFolder !== undefined && !!rootsByPath.get(filepath)?.has(rootFolder)
36 :
37 0 : return (value) => {
38 0 : if (typeof value !== 'string') return false
39 0 : if (rootsByPath.has(value)) {
40 0 : const parsed = parseAnnotatedPath(value)
41 0 : if (hasRoot(parsed.filepath, parsed.rootFolder)) return true
42 : }
43 0 : const annotationStart = value.trimEnd().lastIndexOf('[')
44 0 : if (annotationStart < 0) return false
45 0 : const filepath = value.slice(0, annotationStart).trimEnd()
46 0 : return hasRoot(filepath, readAnnotationRoot(value.slice(filepath.length)))
47 : }
48 : }
49 :
50 : /** Clears the canvas and Vue preview state of every node referencing a deleted value. */
51 0 : export function clearNodePreviewCacheForValues(
52 : rootGraph: LGraph | Subgraph,
53 : deletedValues: ReadonlySet<string>,
54 : removeOutputsForNode: (node: LGraphNode) => void
55 : ): void {
56 0 : if (deletedValues.size === 0) return
57 0 : for (const node of findNodesReferencingValues(rootGraph, deletedValues)) {
58 0 : removeOutputsForNode(node)
59 0 : node.imgs = undefined
60 0 : node.videoContainer = undefined
61 0 : node.graph?.setDirtyCanvas(true)
62 : }
63 : }
64 :
65 : /** Nodes, including subgraph interiors, whose widgets reference a deleted value. */
66 0 : export function findNodesReferencingValues(
67 : rootGraph: LGraph | Subgraph,
68 : deletedValues: ReadonlySet<string>,
69 : { includeSubgraphNodes = false }: { includeSubgraphNodes?: boolean } = {}
70 : ): LGraphNode[] {
71 0 : if (deletedValues.size === 0) return []
72 0 : const isDeleted = createDeletedValueMatcher(deletedValues)
73 0 : const matches: LGraphNode[] = []
74 0 : for (const node of collectAllNodes(rootGraph)) {
75 0 : if (!node.widgets?.length) continue
76 0 : if (
77 : !includeSubgraphNodes &&
78 : typeof node.isSubgraphNode === 'function' &&
79 : node.isSubgraphNode()
80 : )
81 0 : continue
82 0 : if (node.widgets.some((w) => isDeleted(w.value))) matches.push(node)
83 : }
84 0 : return matches
85 : }
|