projectstorm / react-diagrams

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

Adding NodeModel to NodeLayer set itself as its parent, but NodeModel's parent must be a Diagram #1023

Open jeevcat opened 6 months ago

jeevcat commented 6 months ago

The type the parent of a NodeModel must be a DiagramModel (which is a CanvasModel):

https://github.com/projectstorm/react-diagrams/blob/a457385a8ce3379e4554f1ba3bf866265b8b0258/packages/react-diagrams-core/src/entities/node/NodeModel.ts#L21

However, when adding a node to a layer, the layer sets the node's parent to itself:

https://github.com/projectstorm/react-diagrams/blob/a457385a8ce3379e4554f1ba3bf866265b8b0258/packages/react-canvas-core/src/entities/layer/LayerModel.ts#L79

As LayerModel doesn't inherit from DiagramModel, a class inheriting from NodeModel cannot correctly define its parent type to be a NodeLayerModel (due to the G extends NodeModelGenerics constraint), which is what the parent will actually be at runtime, if the node has been added to a layer.

The above also applies for links.