mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 05:10:26 +08:00
Enhance Transformer event handling and add unit test for cleanup of subscriptions. Ensure event listeners are properly managed on transformer destruction. fix #1872
This commit is contained in:
parent
9e790a36ee
commit
9b989b41aa
@ -335,10 +335,12 @@ export class Transformer extends Group {
|
|||||||
this.update();
|
this.update();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const additionalEvents = node._attrsAffectingSize
|
if (node._attrsAffectingSize.length) {
|
||||||
.map((prop) => prop + 'Change.' + this._getEventNamespace())
|
const additionalEvents = node._attrsAffectingSize
|
||||||
.join(' ');
|
.map((prop) => prop + 'Change.' + this._getEventNamespace())
|
||||||
node.on(additionalEvents, onChange);
|
.join(' ');
|
||||||
|
node.on(additionalEvents, onChange);
|
||||||
|
}
|
||||||
node.on(
|
node.on(
|
||||||
TRANSFORM_CHANGE_STR.map(
|
TRANSFORM_CHANGE_STR.map(
|
||||||
(e) => e + `.${this._getEventNamespace()}`
|
(e) => e + `.${this._getEventNamespace()}`
|
||||||
|
@ -5088,4 +5088,49 @@ describe('Transformer', function () {
|
|||||||
}, 100);
|
}, 100);
|
||||||
}, 100);
|
}, 100);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should properly clean up subscriptions on detach/destroy', function () {
|
||||||
|
var stage = addStage();
|
||||||
|
var layer = new Konva.Layer();
|
||||||
|
stage.add(layer);
|
||||||
|
|
||||||
|
var rect = new Konva.Rect({
|
||||||
|
x: 100,
|
||||||
|
y: 60,
|
||||||
|
width: 100,
|
||||||
|
height: 100,
|
||||||
|
fill: 'yellow',
|
||||||
|
});
|
||||||
|
layer.add(rect);
|
||||||
|
// draw to attach all listeners
|
||||||
|
layer.draw();
|
||||||
|
|
||||||
|
// Count initial number of event listeners
|
||||||
|
var initialListeners = Object.keys(rect.eventListeners).length;
|
||||||
|
|
||||||
|
// Create and attach first transformer
|
||||||
|
var tr1 = new Konva.Transformer({
|
||||||
|
nodes: [rect],
|
||||||
|
});
|
||||||
|
layer.add(tr1);
|
||||||
|
|
||||||
|
// Destroy first transformer
|
||||||
|
tr1.destroy();
|
||||||
|
|
||||||
|
// Create and attach second transformer
|
||||||
|
var tr2 = new Konva.Transformer({
|
||||||
|
nodes: [rect],
|
||||||
|
});
|
||||||
|
layer.add(tr2);
|
||||||
|
|
||||||
|
// Destroy second transformer
|
||||||
|
tr2.destroy();
|
||||||
|
|
||||||
|
// Check that we have same number of listeners as initially
|
||||||
|
assert.equal(
|
||||||
|
Object.keys(rect.eventListeners).length,
|
||||||
|
initialListeners,
|
||||||
|
'Event listeners should be cleaned up properly'
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user