Skip to content

Commit 36f7ac8

Browse files
Update to account for other class data
egraphs-good/egraph-serialize#23
1 parent 278a263 commit 36f7ac8

File tree

5 files changed

+57
-18
lines changed

5 files changed

+57
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ First install [Yarn](https://yarnpkg.com/getting-started/install), then run:
7474

7575
```sh
7676
yarn install
77-
yarn run [build|start|lint]
77+
yarn run [build|dev|lint]
7878
```
7979

8080
## Releasing

examples/manual/homepage.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,19 @@
3737
"eclass": "right",
3838
"children": ["2", "2"]
3939
}
40+
},
41+
"class_data": {
42+
"top": {
43+
"type": "int",
44+
"": "baz, bar",
45+
"other thing": "another label",
46+
"third thing": "baz, bar",
47+
"fourth thing": "another label"
48+
},
49+
"middle": {
50+
"type": "int",
51+
"": "baz, bar",
52+
"other thing": "another label"
53+
}
4054
}
4155
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "egraph-visualizer",
3-
"version": "2.1.2",
3+
"version": "2.2.0",
44
"repository": {
55
"type": "git",
66
"url": "git+https://github.com/egraphs-good/egraph-visualizer.git"

src/Visualizer.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,27 @@ import { Loading } from "./Loading";
5252
import { Slider, SliderOutput, SliderTack } from "./react-aria-components-tailwind-starter/src/slider";
5353

5454
export function EClassNode({ data, selected }: NodeProps<FlowClass>) {
55+
// Create one div per extra node
56+
const extra_nodes = Object.entries(data.extra || {}).map(([key, value]) => (
57+
<div
58+
key={key}
59+
className="truncate"
60+
title={`${key}: ${value}`}
61+
style={{ fontSize: 6 }}
62+
>
63+
{key != "" && <span className="pr-1 font-bold">{key}</span>}
64+
<span>{value}</span>
65+
</div>
66+
));
5567
return (
5668
<div
5769
className={`rounded-md border-dotted border-black h-full w-full ${selected ? "border-2" : "border"}`}
5870
style={{ backgroundColor: data.color! || "white" }}
5971
title={data.id}
6072
>
73+
<div className="p-1 font-mono">
74+
{...extra_nodes}
75+
</div>
6176
<MyNodeToolbar type="class" id={data.id} />
6277
<Handle type="target" position={Position.Top} className="invisible" />
6378
<Handle type="source" position={Position.Bottom} className="invisible" />
@@ -84,7 +99,7 @@ export function ENode(
8499
{props?.outerRef ? <></> : <MyNodeToolbar type="node" id={props!.data!.id} />}
85100

86101
<div
87-
className={`font-mono text-xs truncate max-w-96 min-w-4 text-center ${subsumed ? "text-gray-300" : ""}`}
102+
className={`font-mono text-base truncate max-w-96 min-w-6 text-center ${subsumed ? "text-gray-300" : ""}`}
88103
title={`${props?.data?.id}\n${props?.data?.label}`}
89104
ref={props?.innerRef}
90105
>
@@ -103,7 +118,7 @@ export function MyNodeToolbar(node: { type: "class" | "node"; id: string }) {
103118
<NodeToolbar position={Position.Top}>
104119
<button
105120
onClick={onClick}
106-
className="rounded bg-white px-2 py-1 text-xs font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
121+
className="rounded bg-white px-2 py-1 text-base font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
107122
>
108123
Filter
109124
</button>

src/layout.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,26 @@ const rootLayoutOptions = {
2828
// the number of pixels of padding between nodes and between nodes and their parents
2929
const nodePadding = 5;
3030

31-
const classLayoutOptions = {
32-
"elk.algorithm": "layered",
33-
"elk.direction": "DOWN",
34-
"elk.spacing.componentComponent": nodePadding.toString(),
35-
"elk.spacing.nodeNode": nodePadding.toString(),
36-
"elk.padding": `[top=${nodePadding},left=${nodePadding},bottom=${nodePadding},right=${nodePadding}]`,
37-
"elk.spacing.portPort": "0",
38-
// allow ports on e-class to be anywhere
39-
// TODO: they only seem to appear on top side of nodes, figure out if there is a way to allow them
40-
// to be on all sides if it would result in a better layout
41-
portConstraints: "FREE",
42-
};
31+
/// amount of padding per extra class item
32+
const extra_padding = 8;
33+
function classLayoutOptions(n_extra: number) {
34+
let top = nodePadding + n_extra * extra_padding;
35+
if (n_extra > 0) {
36+
top += 2;
37+
}
38+
return {
39+
"elk.algorithm": "layered",
40+
"elk.direction": "DOWN",
41+
"elk.spacing.componentComponent": nodePadding.toString(),
42+
"elk.spacing.nodeNode": nodePadding.toString(),
43+
"elk.padding": `[top=${top},left=${nodePadding},bottom=${nodePadding},right=${nodePadding}]`,
44+
"elk.spacing.portPort": "0",
45+
// allow ports on e-class to be anywhere
46+
// TODO: they only seem to appear on top side of nodes, figure out if there is a way to allow them
47+
// to be on all sides if it would result in a better layout
48+
portConstraints: "FREE",
49+
};
50+
}
4351

4452
// https://github.com/eclipse/elk/issues/1037#issuecomment-2122136560
4553
const interactiveOptions = {
@@ -83,6 +91,7 @@ export type FlowClass = Node<
8391
{
8492
color: string | null;
8593
id: string;
94+
extra: { [key: string]: string };
8695
// selected?: boolean
8796
},
8897
"class"
@@ -275,10 +284,11 @@ function toELKNode(
275284
elkRoot.layoutOptions!["elk.aspectRatio"] = aspectRatio as unknown as string;
276285
for (const [classID, nodes] of classToNodes.entries()) {
277286
const elkClassID = `class-${classID}`;
287+
const extra = class_data[classID] ? Object.fromEntries(Object.entries(class_data[classID]!).filter(([key]) => key !== "type")) : {};
278288
const elkClass: MyELKNode["children"][0] = {
279289
id: elkClassID,
280-
data: { color: colors.get(class_data[classID]?.type)!, id: classID },
281-
layoutOptions: classLayoutOptions,
290+
data: { color: colors.get(class_data[classID]?.type)!, id: classID, extra },
291+
layoutOptions: classLayoutOptions(Object.keys(extra).length),
282292
children: [],
283293
ports: mergeEdges
284294
? []

0 commit comments

Comments
 (0)