From e0cf97acf08f02b4950239987a31d51d2246c0ce Mon Sep 17 00:00:00 2001 From: Roman Bruckner Date: Thu, 28 Dec 2023 14:38:19 +0100 Subject: [PATCH] feat(dia.Cell)!: add mergeArrays options to constructor --- .../src/joint/api/dia/Cell/constructor.html | 13 ++++++++++ packages/joint-core/src/dia/Cell.mjs | 9 ++++++- packages/joint-core/test/jointjs/cell.js | 24 +++++++++++++++++++ packages/joint-core/types/joint.d.ts | 6 ++++- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 packages/joint-core/docs/src/joint/api/dia/Cell/constructor.html diff --git a/packages/joint-core/docs/src/joint/api/dia/Cell/constructor.html b/packages/joint-core/docs/src/joint/api/dia/Cell/constructor.html new file mode 100644 index 000000000..a1a5c18c4 --- /dev/null +++ b/packages/joint-core/docs/src/joint/api/dia/Cell/constructor.html @@ -0,0 +1,13 @@ +
new Cell([attributes], [options])
+ +

When creating an instance of a cell, you can pass in the initial values of the attributes, which will be set on the model.

+ +

If you pass a { mergeArrays: true } as the options, all the arrays defined as class defaults will be merged instead of overridden.

+ +
const MyRect = joint.shapes.standard.Rectangle.define('Rect', { array: [1,2] });
+
+const rect1 = new MyRect({ array: [3] });
+console.log(rect1.get('array')); // [3] array was overridden
+
+const rect2 = new MyRect({ array: [3] }, { mergeArrays: true });
+console.log(rect2.get('array')); // [3,2] array was merged 
diff --git a/packages/joint-core/src/dia/Cell.mjs b/packages/joint-core/src/dia/Cell.mjs index b9683d39e..83b085f95 100644 --- a/packages/joint-core/src/dia/Cell.mjs +++ b/packages/joint-core/src/dia/Cell.mjs @@ -36,6 +36,12 @@ import * as g from '../g/index.mjs'; // Cell base model. // -------------------------- +const attributesMerger = function(a, b) { + if (Array.isArray(a)) { + return b; + } +}; + export const Cell = Model.extend({ // This is the same as mvc.Model with the only difference that is uses util.merge @@ -55,7 +61,8 @@ export const Cell = Model.extend({ if ((defaults = result(this, 'defaults'))) { // // Replaced the call to _.defaults with util.merge. - attrs = merge({}, defaults, attrs); + const customizer = (options && options.mergeArrays === true) ? false : attributesMerger; + attrs = merge({}, defaults, attrs, customizer); // } this.set(attrs, options); diff --git a/packages/joint-core/test/jointjs/cell.js b/packages/joint-core/test/jointjs/cell.js index 7081b70e5..bb558f5ba 100644 --- a/packages/joint-core/test/jointjs/cell.js +++ b/packages/joint-core/test/jointjs/cell.js @@ -112,6 +112,30 @@ QUnit.module('cell', function(hooks) { }); }); + QUnit.module('defaults', function() { + + QUnit.test('arrays', function(assert) { + + const Rect = joint.shapes.standard.Rectangle.define('Rect', { + array: [1,2] + }); + + const rect1 = new Rect({}); + assert.deepEqual(rect1.get('array'), [1,2]); + const rect2 = new Rect({ array: [] }); + assert.deepEqual(rect2.get('array'), []); + const rect3 = new Rect({ array: [3] }); + assert.deepEqual(rect3.get('array'), [3]); + + const rect4 = new Rect({}, { mergeArrays: true }); + assert.deepEqual(rect4.get('array'), [1,2]); + const rect5 = new Rect({ array: [] }, { mergeArrays: true }); + assert.deepEqual(rect5.get('array'), [1,2]); + const rect6 = new Rect({ array: [3] }, { mergeArrays: true }); + assert.deepEqual(rect6.get('array'), [3,2]); + }); + }); + QUnit.module('parent', function(hooks) { QUnit.test('parent', function(assert) { diff --git a/packages/joint-core/types/joint.d.ts b/packages/joint-core/types/joint.d.ts index 33fbc3f05..ddfc446ee 100644 --- a/packages/joint-core/types/joint.d.ts +++ b/packages/joint-core/types/joint.d.ts @@ -320,11 +320,15 @@ export namespace dia { timingFunction?: util.timing.TimingFunction; valueFunction?: util.interpolate.InterpolateFunction; } + + interface ConstructorOptions extends Graph.Options { + mergeArrays?: boolean; + } } class Cell extends mvc.Model { - constructor(attributes?: A, opt?: Graph.Options); + constructor(attributes?: A, opt?: Cell.ConstructorOptions); id: Cell.ID; graph: Graph;