Cytoscape.js - 检测是否在 boxend 上选择了任何节点

Cytoscape.js - detecting if any nodes have been selected on boxend

我正在我的 cytoscape 实例上订阅正确触发的 boxend 事件,但我希望能够确定事件触发时是否有任何节点在框中。看起来 Event target 属性 只是 cytoscape 实例,而不是被选中的元素。

这就是我希望做的事情。

this.cy.on('boxend', event => {

  if (event.nodesSelected()) {
    // fire off some action
  } else {
    console.log('no nodes selected');
  }

});

我可以想到一个解决方法,使用 boxstart 设置标志 nodesSelected = false,然后在 box/boxselect 事件上设置 nodesSelected = true,但这似乎并不理想。

如果你想了解整个手势流程,你还需要听box。来自文档:

box : 在 boxend

的框内时在元素上触发

http://js.cytoscape.org/#events/user-input-device-events

通常对于 UI,对 box.

等事件使用去抖动就足够了
let boxedNodes = cy.collection();

let handleElements = debounce(function(){
  // ...

  boxedNodes = cy.collection();
});

cy.on('box', function( e ){
  let node = e.target;

  boxedNodes = boxedNodes.union( node );

  handleElements();
});