react-diagrams icon indicating copy to clipboard operation
react-diagrams copied to clipboard

Recording nodes being moved

Open bknill opened this issue 8 years ago • 8 comments

Like a few other people I'm trying to store node positions. I've done a hack using the MouseUp, but this affecting the performance of a few other things and I'd really like to try and use events.

I've found lots of posts that suggest:

You can subscribe to actionStartedFiring/actionStillFiring, and it will fire MoveItemsAction where you can access moved items from action.selectionModels

But no code to show how this works

i.e. in this code, all of these events work

    this.diagramModel = new DiagramModel()
    this.diagramModel.addListener({
      linksUpdated:   this.onUpdateLinks.bind(this),
      offsetUpdated:  _.debounce(this.updateOffset.bind(this),500),
      zoomUpdated:    this.updateZoom.bind(this),
      actionStillFiring: (a) => console.log(a),
    })

But actionStartedFiring/actionStillFiring are never called, MoveItemsAction is never called.. I just can't find any way of listening for nodes being moved.

Please can someone give me some example code to show how this works?

bknill avatar Sep 18 '18 23:09 bknill

Hi, thanks for this research and you are almost there!

The thing is that actionStartedFiring/actionStillFiring are widget events, not model, so apropriate usage will be like this

<DiagramWidget
    {...props}
    actionStartedFiring={this.onDiagramActionStarted}
    actionStoppedFiring={this.onDiagramActionStopped}
/>

JokerNN avatar Sep 20 '18 16:09 JokerNN

Ahh right. Cool thanks!

bknill avatar Sep 24 '18 08:09 bknill

Hmm if I add

          actionStartedFiring={this.onDiagramActionStarted.bind(this)}
        actionStoppedFiring={this.onDiagramActionStopped.bind(this)}
  onDiagramActionStarted(event){
    console.log(event)
  }

  onDiagramActionStopped(event){
    console.log(event)
  }

I can no longer drag the diagram? Am I supposed to deal with the events myself if I use this?

bknill avatar Oct 02 '18 07:10 bknill

Hey, that's curious, I will take a look today

JokerNN avatar Oct 02 '18 09:10 JokerNN

Ah ok, I looked into implementation and you need to return true from actionStartedFiring listener.

https://github.com/projectstorm/react-diagrams/blob/fa34f5c98b42eb4b6770a64d9d06373cc153e4c6/src/widgets/DiagramWidget.tsx#L200

JokerNN avatar Oct 02 '18 09:10 JokerNN

I think returning falsy value is basically a way to stop event from firing. And that's what happening in your case

JokerNN avatar Oct 02 '18 09:10 JokerNN

^ as per https://github.com/projectstorm/react-diagrams/issues/267 this listeners is now available on all classes which extend BasePositionModel (NodeModel, PointModel etc..)

dylanvorster avatar Aug 11 '19 14:08 dylanvorster

thank @JokerNN , it need to return true to works

TranVanKung avatar Nov 20 '19 10:11 TranVanKung