javascript 在Sigma.js中拖动节点时意外触发Click事件

cdmah0mi  于 2023-08-02  发布在  Java
关注(0)|答案(1)|浏览(99)

在我的sigma.js webapp中,我主要有两种类型的事件:一个是处理点击事件(打开节点的URL),另一个是处理拖动事件(能够在空间中到处拖动节点)。如果我们单击节点,则会打开节点的页面URL:

const renderer = new Sigma(graph, container);

  renderer.on("clickNode", ({ node }) => {
    if (!graph.getNodeAttribute(node, "hidden")) {
      window.open(graph.getNodeAttribute(node, "pageURL"), "_blank");
    }
  });

字符串
而这段代码负责处理拖动事件。我们可以使用鼠标将节点拖动到任何地方:

let draggedNode: string | null = null;
  let isDragging = false;

  /* On mouse down on a node
    - we enable the drag mode
    - save in the dragged node in the state
    - highlight the node
    - disable the camera so its state is not updated */

  renderer.on("downNode", (e) => {
    isDragging = true;
    draggedNode = e.node;
    graph.setNodeAttribute(draggedNode, "highlighted", true);
  });

  // On mouse move, if the drag mode is enabled, we change the position of the draggedNode
  renderer.getMouseCaptor().on("mousemovebody", (e) => {
    if (!isDragging || !draggedNode) return;

    // Get new position of node
    const pos = renderer.viewportToGraph(e);

    graph.setNodeAttribute(draggedNode, "x", pos.x);
    graph.setNodeAttribute(draggedNode, "y", pos.y);

    // Prevent sigma to move camera:
    e.preventSigmaDefault();
    e.original.preventDefault();
    e.original.stopPropagation();
  });

  // Disable the autoscale at the first down interaction
  renderer.getMouseCaptor().on("mousedown", () => {
    if (!renderer.getCustomBBox()) renderer.setCustomBBox(renderer.getBBox());
  });

  // On mouse up, we reset the autoscaling and the dragging mode
  renderer.getMouseCaptor().on("mouseup", () => {
    if (draggedNode) {
      graph.removeNodeAttribute(draggedNode, "highlighted");
    }
    isDragging = false;
    draggedNode = null;
  });
}


我得到的问题是,每当我拖动节点&释放鼠标按钮; click事件被自动触发,因此pageURL被打开(它应该只有在我单击节点时才打开;而不是拖动节点)。
有什么方法可以在拖动时不触发单击事件吗?我也遵循了一些堆栈溢出的答案来区分点击和拖动,但它们都不起作用。Here is the CodeSandbox link用于尝试它的手

zdwk9cvp

zdwk9cvp1#

我的GSoC导师Michael Hamann解决了这个问题。我欠他这个答案:)

const delta = 10;
  let startX;
  let startY;
  let allowClick = true;

  // Disable the autoscale at the first down interaction
  renderer.getMouseCaptor().on("mousedown", (event) => {
    startX = event.original.pageX;
    startY = event.original.pageY;
    if (!renderer.getCustomBBox()) renderer.setCustomBBox(renderer.getBBox());
  });

  // On mouse up, we reset the autoscaling and the dragging mode
  renderer.getMouseCaptor().on("mouseup", (event) => {
    if (draggedNode) {
      graph.removeNodeAttribute(draggedNode, "highlighted");
      const diffX = Math.abs(event.original.pageX - startX);
      const diffY = Math.abs(event.original.pageY - startY);
      allowClick = diffX < delta && diffY < delta;
      isDragging = false;
      draggedNode = null;
    }
  });

  renderer.on("clickNode", ({ node }) => {
    if (!graph.getNodeAttribute(node, "hidden") && allowClick) {
      window.open(graph.getNodeAttribute(node, "pageURL"), "_blank");
    }
  });

字符串

相关问题