refactoring

This commit is contained in:
2026-03-12 17:07:42 +01:00
parent 084863909a
commit e29c5d643c
11 changed files with 136 additions and 87 deletions

View File

@@ -1,3 +1,8 @@
/**
* Per-node menubar (Node, Output, Data, Inputs, Insert). Content can be passed as props
* or resolved from the node type descriptor: when nodeMenuExtraContent is not provided,
* it is taken from getNodeType(nodeType)?.getNodeMenuExtraContent?.(nodeId, data).
*/
import React, { useCallback, useContext, useMemo } from 'react'
import FlowContext from '@/lib/graph/flowContext'
import { getNodeType } from '@/lib/graph/nodeRegistry'

View File

@@ -13,7 +13,7 @@ import {
import { getResolvedContentForConfig } from './renderingLogic'
import type { NodeTypeDescriptor } from '@/lib/graph/nodeRegistry'
import ConfigNode from './ConfigNode'
import { createImageExportHandlers, type OutputMenuContext } from '@/components/nodes/render/outputMenuRegistry'
import { createImageExportHandlers, type OutputMenuContext } from '@/components/nodes/render/outputMenuHandlers'
const ICON_CLASS = 'mr-2 h-4 w-4'

View File

@@ -7,43 +7,11 @@
import nunjucks from 'nunjucks'
import type { ResolvedContentResult, SourceRenderingLogicContext } from '@/lib/graph/rendering'
import { getConfigContent, getConfigType, getConfigTypeId, type ConfigTypeId } from '@/lib/graph/rendering'
import { isReachable, resolveExtendsRef, getTemplateRefs } from '@/lib/graph/templateRefs'
type Node = { id: string; type?: string; data?: unknown }
type Edge = { id: string; source: string; target: string }
function isReachable(edges: Edge[], startId: string, targetId: string): boolean {
const q: string[] = [startId]
const seen = new Set<string>([startId])
while (q.length) {
const cur = q.shift()!
if (cur === targetId) return true
for (const e of edges) {
if (e.source === cur && !seen.has(e.target)) {
seen.add(e.target)
q.push(e.target)
}
}
}
return false
}
function resolveExtendsRef(nodes: Node[], name: string): string {
const refName = name.replace(/\.(puml|html)$/, '').trim()
return nodes.find((n) => n.id === refName || (n.data as Record<string, unknown>)?.title === refName)?.id ?? refName
}
function getTemplateRefs(content: string): string[] {
const refs: string[] = []
const extendMatch = content.match(/\{\%\s*extends\s+["']([^"']+)["']\s*\%\}/)
if (extendMatch) refs.push(extendMatch[1].trim())
const includeRegex = /\{\%\s*include\s+["']([^"']+)["']\s*\%\}/g
let m
while ((m = includeRegex.exec(content)) !== null) refs.push(m[1].trim())
const importRegex = /\{\%\s*import\s+["']([^"']+)["']\s+as\s+\w+\s*\%\}/g
while ((m = importRegex.exec(content)) !== null) refs.push(m[1].trim())
return refs
}
export async function getResolvedContentForConfig(context: SourceRenderingLogicContext): Promise<ResolvedContentResult> {
const { nodes, edges, sourceNodeId, renderNodeId } = context
const srcId = sourceNodeId
@@ -56,11 +24,11 @@ export async function getResolvedContentForConfig(context: SourceRenderingLogicC
const configIdsUsed = new Set<string>()
const addConfigAndRefs = (templateName: string, visited = new Set<string>()) => {
const refId = resolveExtendsRef(nodes, templateName)
const refId = resolveExtendsRef(nodes as { id: string; data?: unknown }[], templateName)
if (visited.has(refId)) throw new Error(`Circular reference detected: ${templateName}`)
const node = nodes.find((n) => n.id === refId && n.type === 'config')
if (!node) throw new Error(`Config not found: ${templateName}`)
if (refId !== srcId && !isReachable(edges, refId, id))
if (refId !== srcId && !isReachable(edges as { source: string; target: string }[], refId, id))
throw new Error(`Referenced config not connected to renderer: ${templateName}`)
visited.add(refId)
configIdsUsed.add(refId)
@@ -72,10 +40,10 @@ export async function getResolvedContentForConfig(context: SourceRenderingLogicC
const configLoader = {
getSource: (name: string): { src: string; path: string } | null => {
const refId = resolveExtendsRef(nodes, name)
const refId = resolveExtendsRef(nodes as { id: string; data?: unknown }[], name)
const node = nodes.find((n) => n.id === refId && n.type === 'config')
if (!node) return null
if (refId !== srcId && !isReachable(edges, refId, id))
if (refId !== srcId && !isReachable(edges as { source: string; target: string }[], refId, id))
throw new Error(`Referenced config not connected to renderer: ${name}`)
return {
src: getConfigContent((node.data ?? undefined) as Record<string, unknown> | undefined),

View File

@@ -1,4 +1,4 @@
export { default as RenderingNode, type RenderingNodeData } from './RenderingNode'
export { getRenderNodeDescriptor } from './descriptor'
export { createImageExportHandlers } from './outputMenuRegistry'
export type { OutputMenuContext } from './outputMenuRegistry'
export { createImageExportHandlers } from './outputMenuHandlers'
export type { OutputMenuContext } from './outputMenuHandlers'

View File

@@ -20,6 +20,7 @@ import {
processSvgDisplay,
stripTemplateSyntax,
} from '@/lib/graph/rendering'
import { isReachable, resolveExtendsRef, getTemplateRefs } from '@/lib/graph/templateRefs'
import type { ConfigTypeId, SourceRenderingLogicContext } from '@/lib/graph/rendering'
export type RenderingNodeData = {
@@ -136,54 +137,22 @@ export function useRenderingNodeState(
: ''
const connectedNodeIds = useMemo(() => {
const edgeList = edges as { source: string; target: string }[]
const nodeList = nodes as { id: string; type?: string; data?: unknown }[]
const out = new Set<string>()
const isReachable = (startId: string, targetId: string) => {
const q: string[] = [startId]
const seen = new Set<string>([startId])
while (q.length) {
const cur = q.shift()!
if (cur === targetId) return true
for (const e of edges as { source: string; target: string }[]) {
if (e.source === cur && !seen.has(e.target)) {
seen.add(e.target)
q.push(e.target)
}
}
}
return false
}
const resolveRef = (name: string) => {
const refName = name.replace(/\.(puml|html)$/, '').trim()
return (nodes as { id: string; data?: { title?: string } }[]).find(
(n) => n.id === refName || n.data?.title === refName
)?.id ?? refName
}
const getTemplateRefs = (content: string): string[] => {
const refs: string[] = []
const extendMatch = content.match(/\{\%\s*extends\s+["']([^"']+)["']\s*\%\}/)
if (extendMatch) refs.push(extendMatch[1].trim())
const includeRegex = /\{\%\s*include\s+["']([^"']+)["']\s*\%\}/g
let m
while ((m = includeRegex.exec(content)) !== null) refs.push(m[1].trim())
const importRegex = /\{\%\s*import\s+["']([^"']+)["']\s+as\s+\w+\s*\%\}/g
while ((m = importRegex.exec(content)) !== null) refs.push(m[1].trim())
return refs
}
const addConfigRefs = (nodeId: string, visited: Set<string>) => {
if (visited.has(nodeId)) return
const node = (nodes as { id: string; type?: string; data?: unknown }[]).find(
(n) => n.id === nodeId && n.type === 'config'
)
const node = nodeList.find((n) => n.id === nodeId && n.type === 'config')
if (!node) return
visited.add(nodeId)
out.add(nodeId)
const content = getConfigContent((node.data ?? undefined) as Record<string, unknown> | undefined)
for (const ref of getTemplateRefs(content)) {
const refId = resolveRef(ref)
const refId = resolveExtendsRef(nodeList, ref)
if (
refId &&
(nodes as { id: string; type?: string }[]).some((n) => n.id === refId && n.type === 'config') &&
isReachable(refId, id)
nodeList.some((n) => n.id === refId && n.type === 'config') &&
isReachable(edgeList, refId, id)
) {
addConfigRefs(refId, visited)
}
@@ -191,11 +160,11 @@ export function useRenderingNodeState(
}
const configVisited = new Set<string>()
for (const nid of incomingIds) {
const node = (nodes as { id: string; type?: string }[]).find((n) => n.id === nid)
const node = nodeList.find((n) => n.id === nid)
if (node?.type === 'config') addConfigRefs(nid, configVisited)
else out.add(nid)
}
for (const e of edges as { source: string; target: string }[]) {
for (const e of edgeList) {
if (out.has(e.target)) out.add(e.source)
}
return out