jerosoler / Drawflow

Simple flow library 🖥️🖱️
https://jerosoler.github.io/Drawflow/
MIT License
4.78k stars 741 forks source link

Is there a way to change the input? #483

Closed fajardokarl closed 2 years ago

fajardokarl commented 2 years ago

Hi, been using this for a project and it's been good. But a requirement was added on our side of the project. Is there a way to change the input? In our case, we need to make the connection detached when we draw the input on a node that has a connection.

jerosoler commented 2 years ago

Hi @fajardokarl

You mean this?

Using the connectionCreated event we remove the previous connection. And we leave the new one.

fajardokarl commented 2 years ago

Thank you for the reply @jerosoler. What I meant was like resetting the connection when you drag an input with the connection, It will detach. image

Then the output connection will remain and other end will make the mouse pointer its end position again, like this. image

jerosoler commented 2 years ago

I think this could work.

    editor.on("clickEnd", (e) => {
        if(e.target.classList[0] === "input") {
            const nodeId = e.target.closest(".drawflow-node").id.slice(5);
            const nodeInfo = editor.getNodeFromId(nodeId);
            if(nodeInfo.inputs[e.target.classList[1]].connections.length > 0) {
                const removeConnectionInfo = nodeInfo.inputs[e.target.classList[1]].connections[0];

                editor.removeSingleConnection(removeConnectionInfo.node, nodeInfo.id, removeConnectionInfo.input, e.target.classList[1]);

                var evt = new MouseEvent("mousedown", {
                    view: window,
                    bubbles: true,
                    cancelable: false,
                });
                editor.container.querySelector(`#node-${removeConnectionInfo.node} .outputs .output.${removeConnectionInfo.input}`).dispatchEvent(evt);
            }
        }
    });
fajardokarl commented 2 years ago

It works, thank you so much. I think this could be a good additional feature for the library in the future version. Cheers.