Custom Nodes

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 Base Nodearrow-up-right class in order to build custom components.

But first, some links to various Node files:

  1. Base Nodearrow-up-right

    • 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

  2. Text Nodearrow-up-right

    • Provides an extended Node that handles placing text within the Node

    • Must be extended in order to be functional

  3. Rectanglearrow-up-right

    • This is an example implementation. Use this as a reference for any Nodes

      you build.

    • Implements Text Nodearrow-up-right (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 Base Nodearrow-up-right 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.

Base Node Initialization Properties

The properties of the Base Nodearrow-up-right defined in TypeScript are:

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.

See the Text Nodearrow-up-right to see how IBaseNodeProperties is extended to allow for more properties.

Base Node Class Properties

The properties of the Base Class defined in TypeScript are:

P represents the props given to the Base Nodearrow-up-right 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 Base Nodearrow-up-right. However, props is to be used for storing any passed-in props, and is accessible from all extending classes.

Check out how Text Nodearrow-up-right extends the constructor of Base Nodearrow-up-right to set defaults for certain props.

Base Node Class Methods

There are multiple methods within the Base Nodearrow-up-right. Many of these should not be touched, or should only be extended if needed.

constructor

Once again, P represents the props given to the Base Nodearrow-up-right or extending class. The constructor is where you initialize any of the props passed into the extending class. All of the Base Nodearrow-up-right props are already taken care of, so you only need to worry about the new ones. For example, here is the constructor taken from Text Nodearrow-up-right:

initialize

circle-exclamation

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 Text Node'sarrow-up-right initialize function as an example:

teardown

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

circle-exclamation

get

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).

For example, here is the width getter in Rectanglearrow-up-right:

set

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;

For example, here is the width setter in Rectanglearrow-up-right:

height

circle-exclamation

get

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).

For example, here is the height getter in Rectanglearrow-up-right:

set

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;

For example, here is the height setter in Rectanglearrow-up-right:

getElement

triangle-exclamation

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 addToGroup.

addToGroup

triangle-exclamation

This function allows you to easily append elements into the Node group. Generally, it is used inside of initialize to add created elements to the Node group.

Last updated