projectstorm / react-diagrams

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

crash in MoveItemsState (when click empty canvas space after popup) #665

Closed flieks closed 4 years ago

flieks commented 4 years ago

Hi

after clicking on a node which does open my dialog, and then closing it and clicking on the canvas (not on a node bug on empty space canvas)

 node1.registerListener({
            eventDidFire: (e) => {
                try {
                    if(e && e.function == "selectionChanged" && e.isSelected) {
                        setOpenDialog(true)
                    }
                } catch (error) {
                }
            }
        });
Uncaught TypeError: Cannot read property 'isSelected' of null
    at Object.fire (MoveItemsState.ts:26)
    at ActionEventBus.fireAction (ActionEventBus.ts:73)
    at onMouseDown (CanvasWidget.tsx:86)
    at HTMLUnknownElement.callCallback (react-dom.development.js:188)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
    at invokeGuardedCallback (react-dom.development.js:292)

It only happens after the dialog has opened and closed. Otherwise i can click the empty space and drag it fine.

image

It crashes because in here element is null after const element = this.engine.getActionEventBus().getModelForEvent(event);

    constructor() {
        super({
            name: 'move-items'
        });
        this.registerAction(
            new Action({
                type: InputType.MOUSE_DOWN,
                fire: (event: ActionEvent<React.MouseEvent>) => {
                    const element = this.engine.getActionEventBus().getModelForEvent(event);
                    if (!element.isSelected()) {
                        this.engine.getModel().clearSelection();
                    }
                    element.setSelected(true);
                    this.engine.repaintCanvas();
                }
            })
        );
    }
flieks commented 4 years ago

@tkolanko thanks this is great. When will this be merged you think ? Or i can in meantime insert my own MoveItemState ?

flieks commented 4 years ago

Ok i also implemented my own MoveItemState and inserted it through engine.getStateMachine().pushState(new States()); Thanks for the help

paulocfjunior commented 4 years ago

+1 I also tried the same approach to add a custom state handler to fix it on my side but had no success. I'm not really sure how to do that, though. (I'd love to know how to achieve that)

DuyBui1993 commented 3 years ago

Hi,

This also happens when:

  1. mouse right-clicking on the locked node (which will open the context menu)
  2. mouse left on the canvas to close the context menu
  3. and then dragging the canvas

Can reproduce in the demo of storybook