From 086a9c738901a5d17cda4c1e8e3a146dd4f07b96 Mon Sep 17 00:00:00 2001 From: Craig Harshbarger Date: Fri, 3 Nov 2023 09:22:45 -0500 Subject: [PATCH] Simplify and organize node highlighting --- src/factories/nodes.ts | 51 +++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/factories/nodes.ts b/src/factories/nodes.ts index f8924568..f9b9a585 100644 --- a/src/factories/nodes.ts +++ b/src/factories/nodes.ts @@ -47,19 +47,17 @@ export async function nodesContainerFactory() { container.name = DEFAULT_NODES_CONTAINER_NAME emitter.on('layoutSettingsUpdated', async () => { - const selected = getSelected() - if (Boolean(runData) && Boolean(container.parent)) { rows.clear() columns.clear() await render(runData!) } - handleSelection(selected?.id ?? null) + highlightSelectedNode() }) - emitter.on('nodeSelected', (selected) => { - handleSelection(selected?.id ?? null) + emitter.on('nodeSelected', () => { + highlightSelectedNode() }) async function render(data: RunGraphData): Promise { @@ -312,38 +310,49 @@ export async function nodesContainerFactory() { setPositions() } - async function handleSelection(selectedNodeId: string | null): Promise { + async function highlightSelectedNode(): Promise { const settings = await waitForSettings() + const selected = getSelected() - const path = selectedNodeId && !settings.disableEdges ? getDependencyPathIds(selectedNodeId) : [] - - if (selectedNodeId) { - path.push(selectedNodeId) + if (!selected || settings.disableEdges) { + highlightPath([]) + return } + const path = getDependencyPathIds(selected.id) + + highlightPath(path) + } + + function highlightPath(path: string[]): void { + highlightNodes(path) + highlightEdges(path) + } + + function highlightNodes(path: string[]): void { for (const [nodeId, { element }] of nodes) { - const dim = selectedNodeId && !path.includes(nodeId) + const highlight = path.length === 0 || path.includes(nodeId) - if (dim) { - element.alpha = config.styles.nodeUnselectedAlpha + if (highlight) { + element.alpha = 1 continue } - element.alpha = 1 + element.alpha = config.styles.nodeUnselectedAlpha } + } + function highlightEdges(path: string[]): void { for (const [edgeId, { element }] of edges) { const [parentId, childId] = edgeId.split('_') - const inPath = path.includes(parentId) && path.includes(childId) - - const dim = selectedNodeId && !inPath + const highlighted = path.length === 0 || path.includes(parentId) && path.includes(childId) - if (dim) { - element.alpha = config.styles.nodeUnselectedAlpha + if (highlighted) { + element.alpha = 1 continue } - element.alpha = 1 + element.alpha = config.styles.nodeUnselectedAlpha } } @@ -351,7 +360,7 @@ export async function nodesContainerFactory() { const parents = getAllSiblingIds(nodeId, 'parents') const children = getAllSiblingIds(nodeId, 'children') - return [...parents, ...children] + return [nodeId, ...parents, ...children] } function getAllSiblingIds(nodeId: string, direction: 'parents' | 'children'): string[] {