Skip to content

Commit

Permalink
feat(frontend): icon for error edges
Browse files Browse the repository at this point in the history
Make error edges more visible even in black&white printouts.
  • Loading branch information
kris7t committed Apr 27, 2024
1 parent c934d31 commit 9849856
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
27 changes: 24 additions & 3 deletions subprojects/frontend/src/graph/dotSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ function binarySerach(
return undefined;
}

function getEdgeLabel(
name: string,
containment: boolean,
value: string,
): string {
if (value !== 'ERROR') {
return containment ? `<<b>${name}</b>>` : `"${name}"`;
}
// No need to set an id for the image for animation,
// because it will be the only `<use>` element in its group.
return `<<table fixedsize="TRUE" align="left" border="0" cellborder="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="#ERROR"/></td>
<td width="3.9375"></td>
<td align="left">${containment ? `<b>${name}</b>` : name}</td>
</tr>
</table>>`;
}

function createRelationEdges(
graph: GraphStore,
nodeData: NodeData[],
Expand All @@ -243,10 +262,10 @@ function createRelationEdges(
let weight = EDGE_WEIGHT;
let penwidth = 1;
const name = graph.getName(relation);
let label = `"${name}"`;
let containment = false;
if (detail.type === 'reference' && detail.containment) {
weight = CONTAINMENT_WEIGHT;
label = `<<b>${name}</b>>`;
containment = true;
penwidth = 2;
} else if (
detail.type === 'opposite' &&
Expand Down Expand Up @@ -311,8 +330,10 @@ function createRelationEdges(
edgeWeight *= UNKNOWN_WEIGHT_FACTOR;
}

const id = `${fromNode.name},${toNode.name},${relation.name}`;
const label = getEdgeLabel(name, containment, value);
lines.push(`n${from} -> n${to} [
id="${fromNode.name},${toNode.name},${relation.name}",
id="${id}",
dir="${dir}",
constraint=${edgeConstraint},
weight=${edgeWeight},
Expand Down
25 changes: 15 additions & 10 deletions subprojects/frontend/src/graph/postProcessSVG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,20 +135,28 @@ function hrefToClass(node: SVGGElement) {
});
}

function parseCoordinate(element: SVGElement, attribute: string): number {
return Number(element.getAttribute(attribute)?.replace('px', '') ?? '0');
}

function replaceImages(node: SVGGElement) {
node.querySelectorAll<SVGImageElement>('image').forEach((image) => {
const href =
image.getAttribute('href') ?? image.getAttributeNS(XLINK_NS, 'href');
if (href === 'undefined' || !href?.startsWith('#')) {
return;
}
const width = image.getAttribute('width')?.replace('px', '') ?? '';
const height = image.getAttribute('height')?.replace('px', '') ?? '';
const width = parseCoordinate(image, 'width');
const height = parseCoordinate(image, 'height');
const x = parseCoordinate(image, 'x');
const y = parseCoordinate(image, 'y');
const size = Math.min(width, height);
const sizeString = String(size);
const use = document.createElementNS(SVG_NS, 'use');
use.setAttribute('x', image.getAttribute('x') ?? '');
use.setAttribute('y', image.getAttribute('y') ?? '');
use.setAttribute('width', width);
use.setAttribute('height', height);
use.setAttribute('x', String(x + (width - size) / 2));
use.setAttribute('y', String(y + (height - size) / 2));
use.setAttribute('width', sizeString);
use.setAttribute('height', sizeString);
const iconName = `icon-${href.replace('#', '')}`;
use.setAttribute('href', `#refinery-${iconName}`);
use.classList.add('icon', iconName);
Expand Down Expand Up @@ -193,14 +201,11 @@ function markerColorToClass(svg: SVGSVGElement) {
}

export default function postProcessSvg(svg: SVGSVGElement) {
// svg
// .querySelectorAll<SVGTitleElement>('title')
// .forEach((title) => title.parentElement?.removeChild(title));
svg.querySelectorAll<SVGGElement>('g.node').forEach((node) => {
optimizeNodeShapes(node);
hrefToClass(node);
replaceImages(node);
});
replaceImages(svg);
// Increase padding to fit box shadows for multi-objects.
const viewBox = [
svg.viewBox.baseVal.x - 6,
Expand Down

0 comments on commit 9849856

Please sign in to comment.