Drawflow icon indicating copy to clipboard operation
Drawflow copied to clipboard

during panning, editor does not follow mouse cursor when it's outside of the element

Open m-t-chang opened this issue 2 years ago • 8 comments

See the screen recording below. Perhaps this could be fixed by using setPointerCapture() when dragging begins?

https://developer.mozilla.org/en-US/docs/Web/API/Element/setPointerCapture

https://user-images.githubusercontent.com/93272954/171086151-e9661fbb-13ad-4cc6-9af1-fba5669c63e4.mov

m-t-chang avatar May 31 '22 03:05 m-t-chang

Other solutions stop de Drag on outside.

  • https://github.com/jerosoler/Drawflow/issues/237

jerosoler avatar May 31 '22 06:05 jerosoler

Wow! Work correct! ;)

I did not know this method. It is very simple to implement:

    editor.pointerdown = (e) => {
        editor.container.onpointermove = editor.position(e);
        editor.container.setPointerCapture(e.pointerId);
    } 
    editor.pointerup = (e) => {
        editor.container.onpointermove = null;
        editor.container.releasePointerCapture(e.pointerId);
    } 

    editor.container.addEventListener('pointerdown', editor.pointerdown);
    editor.container.addEventListener('pointerup', editor.pointerup); 

jerosoler avatar May 31 '22 07:05 jerosoler

Awesome, thanks!

m-t-chang avatar May 31 '22 08:05 m-t-chang

I added a line to check for event target, since setPointerCapture can interfere with other custom events. (this is CoffeeScript syntax)

        editor.container.addEventListener('pointerdown', (e) ->
            if e.target.matches('.parent-drawflow, .drawflow')
                editor.container.onpointermove = editor.position(e)
                editor.container.setPointerCapture(e.pointerId)
        )
        editor.container.addEventListener('pointerup', (e) ->
            editor.container.onpointermove = null
            editor.container.releasePointerCapture(e.pointerId)
        )

m-t-chang avatar Jun 01 '22 08:06 m-t-chang

@m-t-chang m-t-chang

Do you have a plain Javascript version of that code above that checks for event targets?

thanks.

gpack avatar Jun 30 '22 00:06 gpack

@gpack Try this:

editor.container.addEventListener('pointerdown', function(e) {
  if (e.target.matches('.parent-drawflow, .drawflow')) {
    editor.container.onpointermove = editor.position(e);
    return editor.container.setPointerCapture(e.pointerId);
  }
});

editor.container.addEventListener('pointerup', function(e) {
  editor.container.onpointermove = null;
  return editor.container.releasePointerCapture(e.pointerId);
});

m-t-chang avatar Jun 30 '22 00:06 m-t-chang

@m-t-chang Thank you so much for that kind sir. And for your very quick reply too 👍

gpack avatar Jun 30 '22 00:06 gpack

This way it only affects the canvas.

In the previous way it also works for nodes and canvas.

Since it runs in editor.container it shouldn't affect other applications.

jerosoler avatar Jun 30 '22 06:06 jerosoler

Hey, what's actually going on here? editor.container.onpointermove = editor.position(e); the editor.position function has a return type of void? so we are effectively setting editor.container.onpointermove to undefined? if this is intended it would be clearer to do it over 2 lines, and set it to null like the lines below.

    editor.pointerdown = (e) => {
        editor.position(e);
        editor.container.onpointermove = null;
        editor.container.setPointerCapture(e.pointerId);
    } 
    editor.pointerup = (e) => {
        editor.container.onpointermove = null;
        editor.container.releasePointerCapture(e.pointerId);
    } 

    editor.container.addEventListener('pointerdown', editor.pointerdown);
    editor.container.addEventListener('pointerup', editor.pointerup); 

like this? it seems to work but so does

    editor.pointerdown = (e) => {
        editor.position(e);
        editor.container.setPointerCapture(e.pointerId);
    } 
    editor.pointerup = (e) => {
        editor.container.releasePointerCapture(e.pointerId);
    } 

    editor.container.addEventListener('pointerdown', editor.pointerdown);
    editor.container.addEventListener('pointerup', editor.pointerup); 

so why are we nulling out editor.container.onpointermove?

MatthewAlner avatar Oct 24 '22 15:10 MatthewAlner

so why are we nulling out editor.container.onpointermove?

It is passed to null. Because once the mouse is up there is no reason to pass the position function.

You can see more at https://developer.mozilla.org/en-US/docs/Web/API/Element/setPointerCapture

jerosoler avatar Oct 24 '22 16:10 jerosoler