clientIO / joint

A proven SVG-based JavaScript diagramming library powering exceptional UIs
https://jointjs.com
Mozilla Public License 2.0
4.51k stars 842 forks source link

refactor(dia.Graph)!: throw exception when cell constructor not found #2491

Closed kumilingus closed 5 months ago

kumilingus commented 5 months ago

Description

Throws an exception when a new cell is to be created from JSON if a type does not refer to a constructor.

This is to solve the recurring issue with dia.ElementView: markup required.

const MyElement = dia.Element.define('MyElement');
const graph = new dia.Graph({}, { cellNamespace: { MyElement }});
graph.addCell({ type: 'MyElement' });

const paper = new dia.Paper({ model: graph, frozen: true });
paper.unfreeze(); // throws `dia.ElementView: markup required`
const MyElement = dia.Element.define('MyElement');
const graph = new dia.Graph({}, { cellNamespace: { /* MyElement is not defined here */ }});
graph.addCell({ type: 'MyElement' }); // throws `dia.Graph: Could not find cell constructor...`

Migration guide

The PR also removes the default link type from the abstract class dia.Link to be aligned with dia.Element class model.

Before:

graph.addCell(new dia.Element); // throws `dia.Graph: cell type must be a string.`
graph.addCell(new dia.Link); // the link was successfully added to the graph

Now:

graph.addCell(new dia.Element); // throws `dia.Graph: cell type must be a string.`
graph.addCell(new dia.Link); // throws `dia.Graph: cell type must be a string.`

If you only work with models and don't intend to draw links or use a custom link view, you can add a type to the link.

graph.addCell(new dia.Link({ type: 'link' }));  // the link was successfully added to the graph

In other scenarios, please create a custom link (dia.Link.define('MyLink', /* ... */)) or use built-in links such as standard.Link.

zbynekstara commented 5 months ago

to fix layout-directed-graph tests do the following changes in joint-layout-directed-graph/test/index.js: