如何锁定 cytoscape.js 节点在其父节点中的位置

How to lock the position of a cytoscape.js node within its parent node

我想锁定一个节点相对于其父复合节点的位置,这样如果我抓取并拖动父节点,子节点会随之移动,但子节点不能单独抓取。如果我将子项设置为不可抓取 and/or 锁定,那么它不会随其父项一起移动,但如果我不这样做,则可以单独拖动它,这是我不想要的。这能做到吗?

否则,有没有办法以编程方式 grab/ungrab 一个节点,以便我可以监听抓取事件,然后抓取父节点 instead/as 好?

复合grabbing/locking子逻辑可能总体上需要改进,但你现在的版本应该可以达到你今天想要的效果:

cy.nodes().nonorphans()
  .on('grab', function(){ this.ungrabify(); })
  .on('free', function(){ this.grabify(); })
;

这使得所有 nonorphan/child 节点不可直接抓取,但仍可与其父节点一起移动。

改进参考:https://github.com/cytoscape/cytoscape.js/issues/1074

为了后代,将在 cytoscape-automove

中执行此操作的代码
let nodeList = cy.nodes().nonorphans();
for (let i=0; i < nodeList.length; i++) {
        let n = nodeList[i];
        let parent = n.parent()[0];
        let family = parent.children();
        family.add(parent);

        this.cy.automove({
            nodesMatching: family,
            reposition: 'drag',
            dragWith: n
        });
    }