clientIO / joint

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

feat(dia.Cell): new toJSON options #2728

Closed MartinKanera closed 3 months ago

MartinKanera commented 3 months ago

Description

This PR includes changes to the dia.Cell.toJSON(), now accepting two new options.

These two options are also applicable to dia.Graph.toJSON() method.

This PR also includes a new util.objectDifference function used to determine the difference between two provided objects.

Documentation

https://docs.jointjs.com/api/dia/Cell/#tojson

toJSON()

cell.toJSON([opt])

Return a copy of the cell's attributes for JSON serialization. This can be used for persistance or serialization. Note that this method doesn't return a JSON string but rather an object that can be then serialized to JSON with JSON.stringify().

By default, opt.ignoreDefaults is set to ['attrs']. This setting ensures that only non-default attributes in the attrs object are returned, as it compares the attrs object to its defaults. To include all attributes of dia.Cell without filtering out defaults, set opt.ignoreDefaults to false. Alternatively, setting opt.ignoreDefaults to true will compare all attributes to their defaults, returning only those that differ. You can also provide an array of strings for opt.ignoreDefaults, specifying a list of attributes to compare against their defaults.

Additionally, opt.ignoreEmptyAttributes defaults to false, which may include empty objects in the JSON output. To exclude empty objects, set opt.ignoreEmptyAttributes to true.

const rectangle = new shapes.standard.Rectangle();

rectangle.toJSON();
// {
//     "type": "standard.Rectangle",
//     "attrs": {},
//     "position": {
//         "x": 0,
//         "y": 0
//     },
//     "size": {
//         "width": 1,
//         "height": 1
//     },
//     "angle": 0,
//     "id": "..."
// }

rectangle.toJSON({ ignoreDefaults: true });
// {
//     "type": "standard.Rectangle",
//     "attrs": {},
//     "position": {},
//     "size": {},
//     "id": "..."
// }

rectangle.toJSON({ ignoreDefaults: true, ignoreEmptyAttributes: true });
// {
//     "type": "standard.Rectangle",
//     "id": "..."
// }

rectangle.toJSON({ ignoreDefaults: ['attrs', 'size'] });
// {
//     "type": "standard.Rectangle",
//     "attrs": {},
//     "position": {
//         "x": 0,
//         "y": 0
//     },
//     "size": {},
//     "angle": 0,
//     "id": "..."
// }

https://docs.jointjs.com/api/dia/Graph/#tojson

toJSON()

graph.toJSON([opt])

This method accepts an optional opt object, the opt.cellAttributes accepts same options as the dia.Cell.toJSON() method, which will impact the result of the toJSON() method.


https://docs.jointjs.com/api/util/

objectDifference()

util.objectDifference(object, base, [opt])

Return an object that represents the difference between object and base.

object Object The object to compare
base Object The object to compare with
[opt.maxDepth] number The maximum depth to compare