aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/layout.js
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2022-01-13 17:29:38 +0000
committers-ol <s+removethis@s-ol.nu>2022-01-13 17:29:38 +0000
commit31159ddff5bc489ab6216a72304e1f7c7ce4a432 (patch)
treee0c6efe46d4590c77ceabb4094c8ecd7c4289e00 /client/src/layout.js
parentmove everything into server/ (diff)
parentmove everything into client/ (diff)
downloadfedidag-31159ddff5bc489ab6216a72304e1f7c7ce4a432.tar.gz
fedidag-31159ddff5bc489ab6216a72304e1f7c7ce4a432.zip
Merge server and client repositories
Diffstat (limited to 'client/src/layout.js')
-rw-r--r--client/src/layout.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/client/src/layout.js b/client/src/layout.js
new file mode 100644
index 0000000..c9c2a4d
--- /dev/null
+++ b/client/src/layout.js
@@ -0,0 +1,48 @@
+import Viz from './viz.es';
+
+const toNumber = (i) => Number(i);
+const gv = new Viz({ workerURL: 'lib/lite.render.js' });
+
+export default class Layout {
+ constructor() {
+ this.src = 'digraph { node [shape="rectangle"];\n';
+ }
+
+ addNode(id, width, height) {
+ this.src += ` "${id}" [fixedsize=true; width=${width / 72}; height=${height / 72}];\n`;
+ }
+
+ addLink(from, to) {
+ this.src += ` "${from}" -> "${to}";\n`;
+ }
+
+ async render() {
+ this.src += '}';
+
+ const info = await gv.renderJSONObject(this.src, { format: 'json0' });
+ info.objects ||= [];
+ info.edges ||= [];
+ const [width, height] = info.bb.split(',').map(toNumber).slice(2);
+
+ const positions = {};
+ for (const object of info.objects) {
+ const [x, y] = object.pos.split(',').map(toNumber);
+ positions[object.name] = [ x, height - y ];
+ }
+
+ const links = info.edges.map((edge) => {
+ const pos = edge.pos.slice(2);
+ return pos.split(' ').map(p => {
+ const [x, y] = p.split(',');
+ return [toNumber(x), height - toNumber(y)];
+ });
+
+ });
+
+ return {
+ width, height,
+ positions,
+ links,
+ };
+ }
+}