JavaScript SVG diagram editor 3.9 KB (open source library)
Tags: javascript, webdev, opensource, github
Published: 2022-02-04 19:47:05

![JavaScript SVG diagram editor 3.9 KB](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/io93zlwis42svb4qsmkc.gif)

[Demo](https://alexeyboiko.github.io/DgrmJS/) | [GitHub](https://github.com/AlexeyBoiko/DgrmJS)

Experiments with creating of diagram editor with Blazor Webassembly ([Blazor Webassembly SVG Drag And Drop](https://medium.com/codex/blazor-webassembly-svg-drag-and-drop-e680769ac682), [Blazor WebAssembly: Connecting Lines in SVG](https://alexey-boyko.medium.com/blazor-webassembly-connecting-lines-in-svg-c319f93c1475)) have shown that Blazor is not suitable for intensive DOM manipulation.

It was known in advance that there would be a subsidence: WebAssembly has no direct access to DOM, WebAssembly uses JavaScript interop to change the DOM. The delays were so high that dragging on mobile slowed down after the third shape was added.

## Rejection of frameworks (for this task)

Suspicion that Blazor’s virtual DOM incorrectly tracks changes (maybe Blazor is trying to update more DOM objects than needed) is not justified. Event throttling and other Microsoft recommendations ([ASP.NET Core Blazor performance best practices](https://docs.microsoft.com/en-us/aspnet/core/blazor/performance?view=aspnetcore-6.0)) did not help.

There are so many nuances to just update one attribute:

- virtual DOM, trees, subtrees,
- "loops" of changes (changes in one place lead to changes in another, changes there lead to a change in the first),
- nuances of passing parameters to components and tracking their changes, subscription / unsubscription.

A trivial JavaScript task just got too complicated with Blazor. The framework only gets in the way here. Excessive complexity is manifested not only in Blazor, but also in other frameworks. If you haven’t seen it yet, check out talk of creator of [Svelte](https://svelte.dev/) "[Rich Harris — Rethinking reactivity](https://www.youtube.com/watch?v=AdNJ3fydeao)". In the video there is an example with the brakes of the React application: there the DOM is rebuilt on the fly when entering into the text field. Here, the DOM is rebuilt on the fly as the mouse moves (dragging the shape).

Vanilla-JavaScript prototype showed no signs of slowing down at 1000 shapes.

After years of using Angular, doing something in vanilla JavaScript seemed like a regression. All right: manually to read HTML attributes and to hang up handlers. But how to develop without components, without IoC, without templates? Most importantly — without “reactivity”? However, withdrawal. passed quickly enough. It turned out that there is life beyond the borders of frameworks, and in some ways more complete.

## Rejection of TypeScript (of TypeScript compiler)

Type Checking, intellisense and other tooling — that’s why TypeScript is loved. TypeScript has interfaces, literals, and even generics. TypeScript is so addictive, that it’s easy to forget that TypeScript is just a way to describe types for JavaScript. Yes, it says on the main page of [typescriptlang.org](https://www.typescriptlang.org/): "TypeScript is JavaScript with syntax for types".

JSDoc provides all the same features (type checks, intellisense, etc.).

An example of “typing” with JSDoc:
```js
/**
 * @param {SVGGraphicsElement} svgEl
 * @param {number} transform
 * @param {SVGSVGElement=} svg pass if svgEl not yet in DOM
 * @returns {SVGTransform}
 */
 function ensureTransform(svgEl, transform, svg) {
    ...
    return ...;
}
```
You can even describe types in TypeScript and use them in js files:
```js
// ts-file
interface IDiagram {
    on(evtType: DiagramEventType,
        listener: EventListenerOrEventListenerObject): this;
    shapeAdd(param: PresenterShapeAppendParam): IDiagramShape;
    shapeDel(shape: IDiagramShape): void;
    shapeConnect(param: DiagramShapeConnectParam): void;
}
// js-file - Diagram implements IDiagram
/** @implements {IDiagram} */
export class Diagram {
    …
}
```
In this case, both "find all references" and "renaming" and "checking that the object implements the interface" will work (at least in Visual Studio Code everything worked out of the box).

Pros of not using the TypeScript compiler:

- JS code is exactly what you wrote,
- speeds up development — no need to wait for compilation,
- no need for map files, easier to debug.

JSDoc is not as concise as TypeScript, has unusual syntax, has worse IDE support.
A mixed approach turned out to be convenient:

- type descriptions in ts-files in TypeScript
- real code in JavaScript with JSDoc.

## DgrmJS

The result is a vanilla-JavaScript library.

[DgrmJS](https://github.com/AlexeyBoiko/DgrmJS) is a JavaScript library for creating SVG diagrams.
The main goal of the library is to set up workflows in BPM (Business Process Management) systems.

- works on desktop and mobile
- has no dependency
- small size
- shapes are created declaratively

Main idea

- Allow developers to use standard SVG objects and features to declaratively create shapes that will be used in the diagram.
To create shape, developers should add special data- attributes to standard SVG markup. So any svg images can be used as a shape in a diagram.
- DgrmJS dispatch events, like ‘shape is selected’ or ‘shape is connecting to another shape’.
Developers can use these events to implement their own logic, for example, make a JSON description of the workflow.

An example of a declarative description of the "circle" shape template:

```html
<g data-templ="circle">
    <circle ... />
    <text data-key="text"></text>
 
    <!--
        out connector
        data-connect-point - point into shape where connector line starts
        data-connect-dir - direction of connector line
    -->
    <circle
        data-connect="out"
        data-connect-point="60,0"
        data-connect-dir="right" ...>
    </circle>
 
    <!--
        in connector
    -->
    <circle
        data-connect="in"
        data-connect-point="-60,0"
        data-connect-dir="left" ...>
    </circle>
</g>
```
The figure below shows two shapes (two circles) created using the “circle” template. By clicking on the figure — output connectors are displayed, from where you can pull out the connecting line. When you hover the end of the connecting line over the figure, the input connectors are displayed.

![Connection of shapes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owe8pgjkksgyt7b1f6qg.gif)

Code for adding a shape to a diagram:
```js
import { svgDiagramCreate } from './diagram/svg-presenter/svg-diagram-factory.js';
const diagram =
    svgDiagramCreate(document.getElementById('diagram'));
diagram.shapeAdd({
    templateKey: 'circle',
    position: { x: 120, y: 120 }
});
```
More examples on [GitHub](https://github.com/AlexeyBoiko/DgrmJS).

## Conclusion
The article does not call for abandoning frameworks or TypeScript. Long-term adherence to the same paradigms, approaches, frameworks, as a result, can “blind”, narrow the scope of vision. Often we don’t even make a choice — try to find a vacancy for Blazor WebAssembly or Svelte developer, you can only choose between React and Angular (also Vue).

It’s good to have the opportunity to experiment. It was interesting to emerge from the “reactive approach” bubble.

## Other articles about dgrm.net
 - [JavaScript diagram editor that renders diagrams from PNG images (open source)](https://dev.to/alexboyko/javascript-diagram-editor-that-renders-diagrams-from-png-images-open-source-a63)
 - [JavaScript text editor for SVG](https://dev.to/alexboyko/javascript-text-editor-for-svg-13aa)
 - [Flowchart editor UI](https://dev.to/alexboyko/flowchart-editor-ui-58a1)
 - [JavaScript: Zoom like in maps for SVG/HTML](https://dev.to/alexboyko/javascript-zoom-like-in-maps-for-svghtml-2m3b)