Skip to content

Commit

Permalink
fix(dia.Paper): ensure grid pattern ids are unique (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumilingus authored May 9, 2024
1 parent 00a6756 commit 68c5be4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/joint-core/src/dia/layers/GridLayer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const GridLayer = PaperLayer.extend({

gridSettings.forEach((gridLayerSetting, index) => {

const id = 'pattern_' + index;
const id = this._getPatternId(index);
const options = merge({}, gridLayerSetting);
const { scaleFactor = 1 } = options;
options.width = gridSize * scaleFactor || 1;
Expand Down Expand Up @@ -107,12 +107,16 @@ export const GridLayer = PaperLayer.extend({
}
gridSettings.forEach((options, index) => {
if (isFunction(options.update)) {
const vPattern = patterns['pattern_' + index];
const vPattern = patterns[this._getPatternId(index)];
options.update(vPattern.node.firstChild, options, paper);
}
});
},

_getPatternId(index) {
return `pattern_${this.options.paper.cid}_${index}`;
},

_getGridRefs() {
let { _gridCache: grid } = this;
if (grid) return grid;
Expand Down
26 changes: 26 additions & 0 deletions packages/joint-core/test/jointjs/paper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,32 @@ QUnit.module('paper', function(hooks) {
return paper;
};

QUnit.test('Unique pattern id', function(assert) {

const paper1 = new joint.dia.Paper({
drawGrid: true,
gridSize: 10
});

const paper2 = new joint.dia.Paper({
drawGrid: true,
gridSize: 10
});

const svg1 = getGridVel(paper1);
const pattern1 = svg1.findOne('pattern');
assert.ok(pattern1.id);

const svg2 = getGridVel(paper2);
const pattern2 = svg2.findOne('pattern');
assert.ok(pattern2.id);

assert.notEqual(pattern1.id, pattern2.id);

paper1.remove();
paper2.remove();
});

QUnit.module('drawGridSize option', function(hooks) {

QUnit.test('is used to draw grid', function(assert) {
Expand Down

0 comments on commit 68c5be4

Please sign in to comment.