Custom Nodes
Last updated
Last updated
In order to use Psiagram to the fullest, developers can use the framework to build out a custom Node library. In this section we will touch on the API of Nodes and how to extend the class in order to build custom components.
But first, some links to various Node files:
Provides the base implementation of Node. Every Node must extend this class
(or if not, it must at least be in the prototype chain)
Must be extended in order to be functional
Provides an extended Node that handles placing text within the Node
Must be extended in order to be functional
This is an example implementation. Use this as a reference for any Nodes
you build.
Implements (see above) to create a Rectangle Node in basic grey
colors
Functional as is (does not need to be extended)
Feel free to check the links out to get a basic idea of some of the Nodes provided by Psiagram. They will be referenced later on.
The takes care of a lot of the internal-use scenarios (example: the getElement
method is defined already, so that Paper can call it to get the Node group, which is also pre-defined). However, some of the functionality must be implemented in order for your custom Node to work. The following are the methods and interfaces within the Node API.
The id
, gridSize
, uniqueId
and paper
properties are given by Paper to every Node. As you can see, there isn't much here to define the look and feel of the Node. This only serves as a foundation, and provides definitions for the two properties that are passed in from the Paper instance.
The properties of the Base Class defined in TypeScript are:
The initialize method must be overwritten.
Teardown is called when the Node is being removed from the DOM. You can cause transformations, change appearance, and teardown any listeners or processes. If none of these are needed, you do not need to overwrite this function.
Width get and set must be overwritten.
External methods need to call ThisNode.width
in order to calculate various things, and it's not as straightforward as getting it from props (ex: your component is a circle that takes only radius, so get width must return 2 * radius).
You may also wish to adjust the width of this component, and in doing so, you want those changes to be reflected in the DOM automatically. All of the manipulations must be wrapped in setters so updating the DOM is as easy as ThisNode.width = 200
;
Height get and set must be overwritten.
External methods need to call ThisNode.height
in order to calculate various things, and it's not as straightforward as getting it from props (ex: your component is a circle that takes only radius, so get height must return 2 * radius).
You may also wish to adjust the height of this component, and in doing so, you want those changes to be reflected in the DOM automatically. All of the manipulations must be wrapped in setters so updating the DOM is as easy as ThisNode.height = 200
;
Do not modify or overwrite this function unless you know what you're doing. It is used by the Paper to extract the entire Node group, and breaking the functionality may cause all of Psiagram to work incorrectly.
Do not modify or overwrite this function unless you know what you're doing. It should be used by all extending classes to insert SVG elements into the Node group. Breaking the functionality may lead to incorrect rendering of the Node.
The properties of the defined in TypeScript are:
See the to see how IBaseNodeProperties
is extended to allow for more properties.
P
represents the props given to the or extending class. These props must extend IBaseNodeProperties
- i.e. they must include id
and gridSize
.
Group is kept private, and should not be touched by any classes extending the . However, props
is to be used for storing any passed-in props, and is accessible from all extending classes.
Check out how extends the constructor of to set defaults for certain props.
There are multiple methods within the . Many of these should not be touched, or should only be extended if needed.
Once again, P
represents the props given to the or extending class. The constructor is where you initialize any of the props passed into the extending class. All of the props are already taken care of, so you only need to worry about the new ones. For example, here is the constructor taken from :
Initialize is called when the Node is being mounted into the DOM. You can build visual SVG components and add them to the group using this.addToGroup(element)
. This function is only called once, so any changes in the future must be done through setters. Here's initialize function as an example:
For example, here is the width getter in :
For example, here is the width setter in :
For example, here is the height getter in :
For example, here is the height setter in :
This function will always return the Node group. Any visual components for this Node should be contained within the Node group by adding them using .
This function allows you to easily append elements into the Node group. Generally, it is used inside of to add created elements to the Node group.