-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add vis.js rendering (the default one)
- Loading branch information
1 parent
a5b87e7
commit 662d125
Showing
14 changed files
with
491 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.min.css | ||
*.min.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { flow } from "lodash"; | ||
|
||
import { Path, generateGraphFromSassGraph } from "./generation/sass-graph"; | ||
import { renderGraphToVizGraph } from "./rendering/viz"; | ||
import { renderGraphToVisGraph } from "./rendering/vis"; | ||
|
||
export function generateVisualGraph(pathToFolder: Path): void { | ||
flow( | ||
generateGraphFromSassGraph, | ||
renderGraphToVizGraph, | ||
renderGraphToVisGraph, | ||
)(pathToFolder); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as express from "express"; | ||
// @ts-ignore (missing typings) | ||
import * as opn from "opn"; | ||
|
||
import { Graph } from "../../graph"; | ||
import { graphToVisGraph } from "./translator"; | ||
|
||
export function renderGraphToVisGraph(graph: Graph): void { | ||
const visGraph = graphToVisGraph(graph); | ||
|
||
const app = express(); | ||
app.set("view engine", "pug"); | ||
app.set("views", `${__dirname}/views`); | ||
app.use(express.static(`${__dirname}/assets`)); | ||
|
||
app.get("/", (_req, res) => | ||
res.render("index", { | ||
nodes: JSON.stringify(visGraph.nodes), | ||
edges: JSON.stringify(visGraph.edges), | ||
}), | ||
); | ||
app.listen("3000"); | ||
|
||
// If `wait: true`, opn won't resolve until we quit the browser. | ||
// => don't wait and exit the process after page is open. | ||
opn("http://localhost:3000", { wait: false }) | ||
// Wait for the page to be open & resources to be loaded before we exit. | ||
.then(() => setTimeout(() => process.exit(0), 2000)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Graph } from "../../graph"; | ||
import { graphToVisGraph } from "./translator"; | ||
|
||
it("translate an empty graph", () => { | ||
const graph = new Graph(); | ||
|
||
const visGraph = graphToVisGraph(graph); | ||
|
||
const emptyVisGraph = { nodes: [], edges: [] }; | ||
expect(visGraph).toEqual(emptyVisGraph); | ||
}); | ||
|
||
it("translate a graph with some vertices", () => { | ||
const graph = new Graph(); | ||
graph.addVertice("main", "_header"); | ||
graph.addVertice("main", "_footer"); | ||
|
||
const visGraph = graphToVisGraph(graph); | ||
|
||
const expectedVisGraph = { | ||
nodes: [ | ||
{ id: "main", label: "main" }, | ||
{ id: "_header", label: "_header" }, | ||
{ id: "_footer", label: "_footer" }, | ||
], | ||
edges: [{ from: "main", to: "_header" }, { from: "main", to: "_footer" }], | ||
}; | ||
expect(visGraph).toEqual(expectedVisGraph); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Node, Edge } from "vis"; | ||
|
||
import { Graph } from "../../graph"; | ||
|
||
type VisGraph = { nodes: Node[]; edges: Edge[] }; | ||
|
||
export function graphToVisGraph(graph: Graph): VisGraph { | ||
const nodes: Node[] = []; | ||
const edges: Edge[] = []; | ||
|
||
graph.vertices.forEach((vertice) => { | ||
const [parent, child] = vertice.nodes; | ||
const nodeIds = nodes.map(({ id }) => id); | ||
|
||
if (!nodeIds.includes(parent)) { | ||
nodes.push({ id: parent, label: parent }); | ||
} | ||
|
||
if (!nodeIds.includes(child)) { | ||
nodes.push({ id: child, label: child }); | ||
} | ||
|
||
edges.push({ from: parent, to: child }); | ||
}); | ||
|
||
return { nodes, edges }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
doctype html | ||
head | ||
title Sass Graph Viz | ||
style. | ||
html, body { | ||
height: 100%; | ||
overflow: hidden; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
#graph { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
link(href='vis.4.21.0.min.css', rel='stylesheet', type='text/css') | ||
script(src='vis.4.21.0.min.js') | ||
|
||
script. | ||
function renderVisGraph() { | ||
var container = document.getElementById('graph'); | ||
|
||
var data = { | ||
nodes: new vis.DataSet(JSON.parse('!{nodes}')), | ||
edges: new vis.DataSet(JSON.parse('!{edges}')) | ||
}; | ||
|
||
var options = { | ||
autoResize: true, | ||
height: '100%', | ||
width: '100%', | ||
edges: { | ||
arrows: 'to' | ||
} | ||
}; | ||
|
||
var network = new vis.Network(container, data, options); | ||
} | ||
|
||
body(onload='renderVisGraph()') | ||
div#graph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/rendering/viz.test.ts → src/rendering/viz.js/translator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Graph } from "../../graph"; | ||
|
||
type VizGraph = string; | ||
|
||
export function graphToVizGraph(graph: Graph): VizGraph { | ||
return Array.from(graph.vertices) | ||
.map((vertice) => { | ||
const [parent, child] = vertice.nodes.map(escapeUnsupportedCharacter); | ||
return `${parent} -> ${child}`; | ||
}) | ||
.join("\n"); | ||
} | ||
|
||
function escapeUnsupportedCharacter(s: string): string { | ||
return s.match(/[-\/]/g) ? `"${s}"` : s; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.