Skip to content
Snippets Groups Projects
connecttool.js 1.54 KiB
Newer Older
  • Learn to ignore specific revisions
  • import Tool from "./tool";
    import * as Graph from "../graph";
    
    import ConnectIcon from "../../images/tools/connect.png";
    
    import { Editor } from "../components/editor";
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    const KEEP_SOURCE_KEY_ID = 17;
    
    
    export default class ConnectTool extends Tool {
    
    Maximilian Giller's avatar
    Maximilian Giller committed
        constructor(key) {
    
            super("Connect two nodes", ConnectIcon, key);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            this.keepSource = false;
        }
    
        onNodeClick(node) {
            // Is a first node selected?
    
                Editor.globalState.selectedItem === undefined ||
                Editor.globalState.selectedItem.node === false
    
                Editor.globalState.setSelectedItem(node);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
                return;
            }
    
            // Add new link
    
            var link = Editor.globalGraph.addLink(
                Editor.globalState.selectedItem[Graph.NODE_ID],
    
                node[Graph.NODE_ID],
                details
    
    Maximilian Giller's avatar
    Maximilian Giller committed
    
            if (link === undefined) {
                console.error("Could not create new link");
                return;
            }
    
            if (this.keepSource === false) {
                // Deselect the current first node
                // TODO: Returned object not yet converted to normal one
    
                Editor.globalState.setSelectedItem(link);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
            }
        }
    
        onBackgroundClick(event, positions) {
    
            Editor.globalState.setSelectedItem(undefined);
    
    Maximilian Giller's avatar
    Maximilian Giller committed
        }
    
        onKeyDown(key) {
            if (key.keyCode === KEEP_SOURCE_KEY_ID) {
                this.keepSource = true;
            }
        }
    
        onKeyUp(key) {
            if (key.keyCode === KEEP_SOURCE_KEY_ID) {
                this.keepSource = false;
            }
        }