Straighten Links between nodes

Hi There,

aligning links to be straight has been a bugbear for a while, so finally inspiration has hit:

align-link-to-source-or-target

I created some JS code to straighten links. This works by computing the differ between the start of a link and the end of a link. Then that differ is either added or subtract from the target/source node location. Since I don't use grids, this a bugbear with me hence this functionality.

This works with multi-output nodes as will as single output nodes. Hence this extra functionality: align middle/top/bottom does not work with multi-output nodes and single links.

I added shortcuts for this:

And I will add it to the presenter-mode node where I collect together various extra functionality.

Functionality:

(function(){
    RED.actions.remove("presenter-mode:align-link-to-destination")
    RED.actions.add("presenter-mode:align-link-to-destination", () => {
        if (RED.view.selection().links?.length > 0) {
            RED.view.selection().links.forEach( lnk => {
                let diff = Math.abs(lnk.y1 - lnk.y2)
                let hst = {
                    dirty: false,
                    t: "move",
                    nodes: [{
                        n: lnk.source,
                        ox: lnk.source.x,
                        oy: lnk.source.y,
                        moved: false
                    }]
                }

                // here we move the source to match the target, which means
                // adding the diff or subtracting the diff from the source 
                // y value
                if ( lnk.y1 > lnk.y2) { // source is higher than target
                    lnk.source.y -= diff
                } else {
                    lnk.source.y += diff
                }
                lnk.source.dirty = true
                RED.history.push(hst)
            })
        }
        RED.nodes.dirty(true);
        RED.view.redraw();
    })

    RED.actions.remove("presenter-mode:align-link-to-source")
    RED.actions.add("presenter-mode:align-link-to-source", () => {
        if (RED.view.selection().links?.length > 0) {
            RED.view.selection().links.forEach(lnk => {
                let diff = Math.abs(lnk.y1 - lnk.y2)
                let hst = {
                    dirty: false,
                    t: "move",
                    nodes: [{
                        n: lnk.target,
                        ox: lnk.target.x,
                        oy: lnk.target.y,
                        moved: false
                    }]
                }

                if (lnk.y1 > lnk.y2) { // source is higher than target
                    lnk.target.y += diff
                } else {
                    lnk.target.y -= diff
                }

                lnk.target.dirty = true
                RED.history.push(hst)
            })
        }
        RED.nodes.dirty(true);
        RED.view.redraw();
    })

})();

Related:

1 Like