A node contains the node content itself and its ports. Check NodeModel source code, if you want to see what class methods can be extended.
Extending the NodeModel
If you want to create a custom node that looks entirely different, then you need to create a component that renders using its default or customized data mode. In the example below, it uses a customized data model DiamondNodeModel to render DiamondNodeWidget, and both of them are being created in the model factory DiamondNodeFactory.
Because our Diamond node always has four ports, we add four default port models into the DiamondNodeModel. Depending on the type of node you are creating, this is basically where you store your vertex data in the graph theory sense.
// DiamondNodeModel.tsimport { NodeModel, NodeModelGenerics, PortModelAlignment } from'@projectstorm/react-diagrams';import { DiamondPortModel } from'./DiamondPortModel';exportinterfaceDiamondNodeModelGenerics { PORT:DiamondPortModel;}// this can be further extended for more complicated node typesexportclassDiamondNodeModelextendsNodeModel<NodeModelGenerics&DiamondNodeModelGenerics> {constructor() {super({ type:'diamond' });this.addPort(newDiamondPortModel(PortModelAlignment.TOP));this.addPort(newDiamondPortModel(PortModelAlignment.LEFT));this.addPort(newDiamondPortModel(PortModelAlignment.BOTTOM));this.addPort(newDiamondPortModel(PortModelAlignment.RIGHT)); }}
This is where we create our customized component. This component can be any customized react component as long as they respect the node and engine props. Ports also need to be rendered inside the node component.
Now we need to create a new node factory to tell the system how our new node model fits into the core system. We specifically are going to extend the DefaultLinkFactory because we want to render a DiamondNodeWidget with data model being DiamondNodeModel. To accomplish that, we simply extend generateReactWidget(event) to return a DiamondNodeWidget and extend generateModel to return a DiamondNodeModel instance.