Skip to content
Snippets Groups Projects
editor.ts 2.66 KiB
Newer Older
  • Learn to ignore specific revisions
  • import { State } from "./state";
    
    import { loadGraphJson } from "../../datasets";
    
    import ForceGraph from "force-graph";
    
    import * as Interactions from "./interactions";
    
    import { setSpace, SPACE } from "../../config";
    
    import { Graph } from "./structures/graph/graph";
    
    export let state: any = undefined;
    export let graph: Graph = undefined;
    export let renderer: any;
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
    window.onload = function () {
    
        // Only execute, if corresponding dom is present
        if (document.getElementById("ks-editor") === null) {
            return;
        }
    
        document.onkeydown = (e) => state.onKeyDown(e);
        document.onkeyup = (e) => state.onKeyUp(e);
    
    
        Interactions.initInteractions();
    
    
        loadSpace(SPACE);
    };
    
    
    export function loadSpace(spaceId: string) {
    
        if (state !== undefined && spaceId === SPACE) {
            return;
        }
        setSpace(spaceId);
    
    
        return loadGraphJson(SPACE).then((graphConfig) => {
            state = new State();
    
            graph = Graph.parse(graphConfig);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
    
            // graph.restartSimulation();
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
    
    function extractPositions(event: any) {
    
            graph: renderer.screen2GraphCoords(event.layerX, event.layerY),
    
            window: { x: event.clientX, y: event.clientY },
        };
    }
    
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    function load() {
        const graphContainer = document.getElementById("2d-graph");
        const width = graphContainer.offsetWidth;
    
    
        renderer = ForceGraph()(graphContainer)
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .height(600)
            .width(width)
            .graphData(graph.data)
    
            .nodeLabel("label")
    
            .linkColor((link) => state.linkColor(link))
            .nodeColor((node) => state.nodeColor(node))
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .onNodeClick((node) => state.onNodeClick(node))
    
            .onNodeDragEnd((node, translate) =>
                state.onNodeDragEnd(node, translate)
            )
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .autoPauseRedraw(false) // keep redrawing after engine has stopped
            .linkWidth((link) => state.linkWidth(link))
            .linkDirectionalParticles(state.linkDirectionalParticles())
            .linkDirectionalParticleWidth((link) =>
                state.linkDirectionalParticleWidth(link)
            )
    
            .onBackgroundClick((event) =>
                state.onBackgroundClick(event, extractPositions(event))
            )
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .nodeCanvasObjectMode((node) => state.nodeCanvasObjectMode(node))
    
            .nodeCanvasObject((node, ctx, globalScale) =>
                state.nodeCanvasObject(node, ctx, globalScale)
            )
    
            .linkCanvasObjectMode((link) => state.linkCanvasObjectMode(link))
    
            .linkCanvasObject((link, ctx, globalScale) =>
                state.linkCanvasObject(link, ctx, globalScale)
            )
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            .onLinkClick((link) => state.onLinkClick(link));
    
    Maximilian Giller's avatar
    Maximilian Giller committed
        graph.onChangeCallbacks.push((data) => {
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    }