projectstorm / react-diagrams

a super simple, no-nonsense diagramming library written in react that just works
https://projectstorm.cloud/react-diagrams
MIT License
8.51k stars 1.17k forks source link

Selection box cause an error #867

Closed rougsig closed 2 years ago

rougsig commented 3 years ago

Steps to reproduce:

  1. Go to demo
  2. Try to select with box (shift + mouse click)
  3. See error in the console: Firefox: Uncaught ReferenceError: TouchEvent is not defined Chrome: Uncaught TypeError: Cannot read properties of undefined (reading 'x')

Expected result: Selection box is visible and select nodes.

rougsig commented 3 years ago

Bug is here.

vadimshvetsov commented 2 years ago

It comes from #828 . I'm ready to PR something like this:

getBoxDimensions(event: AbstractDisplacementStateEvent): ClientRect {
        let rel: Point;
            if (event.event.touches) {
          const touch = event.event.touches[0];
          rel = this.engine.getRelativePoint(touch.clientX, touch.clientY);
        } else {
                  rel = this.engine.getRelativePoint(event.event.clientX, event.event.clientY)
                }
                ...
    }

So I'm suggesting to use second path by default instead of assuming that it is MouseEvent. Do you have better idea for fast fix @renato-bohler ?

It looks like React.MouseEvent is a TS type. I'm not sure that it can be used with instanceof. We can use event.event.nativeEvent and instanceof native event as second option.

renato-bohler commented 2 years ago

It comes from #828 . I'm ready to PR something like this:

getBoxDimensions(event: AbstractDisplacementStateEvent): ClientRect {
      let rel: Point;
          if (event.event.touches) {
        const touch = event.event.touches[0];
        rel = this.engine.getRelativePoint(touch.clientX, touch.clientY);
      } else {
                  rel = this.engine.getRelativePoint(event.event.clientX, event.event.clientY)
                }
                ...
  }

So I'm suggesting to use second path by default instead of assuming that it is MouseEvent. Do you have better idea for fast fix @renato-bohler ?

It looks like React.MouseEvent is a TS type. I'm not sure that it can be used with instanceof. We can use event.event.nativeEvent and instanceof native event as second option.

Hmm, it's weird that this same code works on my project... but well, it seems to me that your change will address that issue.