Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: CanvasWrapper's onDrop event assumes canvas is positioned in the top left of the document. #33

Closed
phlickey opened this issue Aug 17, 2019 · 3 comments

Comments

@phlickey
Copy link

phlickey commented Aug 17, 2019

Right now the onDrop event handler assumes determines the position to place a node when the on drop event fires based on the clientX and clientY properties of the drop event, correcting for the Chart's offset.

This works when the canvas is in the top left hand corner of the document. However, when the Canvas is positioned to the right, or down, the clientX and clientY values do not correspond to the position of a users mouse when the drop event occurred.

Proposing making the following change to the onDrop method that gets passed to ComponentInner in Canvas.wrapper.tsx

onDrop={ (e) => {
  const data = JSON.parse(e.dataTransfer.getData(REACT_FLOW_CHART))
  let elementOffset;
  if (this.ref.current){
    elementOffset = {
      x: this.ref.current.offsetLeft,
      y: this.ref.current.offsetTop
    }
  }else{
    elementOffset = {x: 0, y: 0}
  }
  onCanvasDrop({ data, position: {
    x: e.clientX - (position.x + elementOffset.x),
    y: e.clientY - (position.y + elementOffset.y),
  }})
} }

this would get the actual offset of the HTML element on the page and include this when calculating where the node should drop.

Does anyone see any issues with this? Should I make a PR?

@phlickey
Copy link
Author

On further testing, offsetLeft and offsetTop probably aren't sufficient.

Would probably need to be something like:

let elementOffset;
  if (this.ref.current){
    let {x,y} = this.ref.getBoundingClientRect()
    elementOffset = { x, y }
  }else{
    elementOffset = {x: 0, y: 0}
  }

Is there a better place that this offset could be calculated and stored somewhere stateful? Does it make sense to add it to the resize observer?

@mayansalama
Copy link

This has been bugging me as well - would be great to get this sorted.

@phlickey
Copy link
Author

Looks like the offset is already being stored in state, and updated through the resize observer, just not being used when calculating the position property of the new node created on the onDrop event.

Made a PR that adds it in, since it seems pretty cut and dry and unlikely to affect anything else
#34

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants