Add graph bits

This commit is contained in:
space-nuko
2023-06-02 13:07:59 -05:00
parent e3f98374f0
commit 2197994317
4 changed files with 144 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
<script context="module" lang="ts">
export const GRAPH_STATE = {};
export type GraphContext = {
getCyInstance: () => cytoscape.Core
}
</script>
<script lang="ts">
import { onMount, setContext } from "svelte"
import cytoscape from "cytoscape"
import dagre from "cytoscape-dagre"
import GraphStyles from "./GraphStyles"
const graphContext: GraphContext = {
getCyInstance: () => cyInstance
}
setContext(GRAPH_STATE, graphContext)
let refElement = null
let cyInstance: cytoscape.Core = null
onMount(() => {
cytoscape.use(dagre)
cyInstance = cytoscape({
container: refElement,
style: GraphStyles,
})
cyInstance.on("add", () => {
cyInstance
.makeLayout({
name: "dagre",
rankDir: "TB",
nodeSep: 150
})
.run()
})
})
</script>
<div class="graph" bind:this={refElement}>
{#if cyInstance}
<slot></slot>
{/if}
</div>
<style lang="scss">
.graph {
background: white;
width: 100%;
height: 100%;
}
</style>

View File

@@ -0,0 +1,15 @@
<script lang="ts">
import { onMount, getContext } from 'svelte'
import { GRAPH_STATE, type GraphContext } from './Graph.svelte';
export let edge
const { getCyInstance } = getContext(GRAPH_STATE) as GraphContext;
const cyInstance = getCyInstance()
cyInstance.add({
group: 'edges',
id: edge.id,
data: { ...edge }
})
</script>

View File

@@ -0,0 +1,15 @@
<script lang="ts">
import { onMount, getContext } from 'svelte'
import { GRAPH_STATE, type GraphContext } from './Graph.svelte';
export let node
const { getCyInstance } = getContext(GRAPH_STATE) as GraphContext
const cyInstance = getCyInstance()
cyInstance.add({
group: 'nodes',
id: node.id,
data: { ...node }
})
</script>

View File

@@ -0,0 +1,57 @@
export default [
{
selector: 'node',
style: {
'width': '50',
'height': '50',
'font-size': '18',
'font-weight': 'bold',
'content': `data(label)`,
'text-valign': 'center',
'text-wrap': 'wrap',
'text-max-width': '140',
'background-color': 'gold',
'border-color': 'orange',
'border-width': '3',
'color': 'darkred'
}
},
{
selector: 'node:selected',
style: {
'background-color': 'darkred',
color: 'white',
'border-color': 'darkred',
'line-color': '#0e76ba',
'target-arrow-color': '#0e76ba'
}
},
{
selector: 'edge',
style: {
'curve-style': 'bezier',
'color': 'darkred',
'text-background-color': '#ffffff',
'text-background-opacity': '1',
'text-background-padding': '3',
'width': '3',
'target-arrow-shape': 'triangle',
'line-color': 'darkred',
'target-arrow-color': 'darkred',
'font-weight': 'bold'
}
},
{
selector: 'edge[label]',
style: {
'content': `data(label)`,
}
},
{
selector: 'edge.label',
style: {
'line-color': 'orange',
'target-arrow-color': 'orange'
}
}
]