simlify code a bit

This commit is contained in:
Anton Lavrenov 2023-03-23 16:54:44 -05:00
parent 78c0bddafa
commit bfb6be6e6f
2 changed files with 14 additions and 46 deletions

View File

@ -289,35 +289,18 @@ export class Transformer extends Group {
this.detach(); this.detach();
} }
const ancestors = this.getAncestors(); const filteredNodes = nodes.filter((node) => {
const filteredNodes = nodes.filter(node => {
// check if ancestor of the transformer // check if ancestor of the transformer
if (ancestors.includes(node)) if (node.isAncestorOf(this)) {
Util.error(
'Konva.Transformer cannot be an a child of the node you are trying to attach'
);
return false; return false;
let pointer = node.parent;
// check if descendant of any transformer
while (pointer) {
const type = pointer.getType();
if (type != 'Group' && type != 'Shape')
break;
if (pointer.className == Transformer.prototype.className)
return false;
pointer = pointer.parent;
} }
return true; return true;
}); });
if (filteredNodes.length != nodes.length) {
Util.error('nodes should not be descendants of a transformer, or ancestors of this transformer.');
return;
}
this._nodes = nodes = filteredNodes; this._nodes = nodes = filteredNodes;
if (nodes.length === 1 && this.useSingleNodeRotation()) { if (nodes.length === 1 && this.useSingleNodeRotation()) {
this.rotation(nodes[0].getAbsoluteRotation()); this.rotation(nodes[0].getAbsoluteRotation());

View File

@ -36,7 +36,7 @@ function simulateMouseUp(tr: Transformer, pos = { x: 0, y: 0 }) {
su(tr.getStage(), pos || { x: 1, y: 1 }); su(tr.getStage(), pos || { x: 1, y: 1 });
} }
describe('Transformer', function () { describe.only('Transformer', function () {
// ====================================================== // ======================================================
it('init transformer on simple rectangle', function () { it('init transformer on simple rectangle', function () {
var stage = addStage(); var stage = addStage();
@ -4770,20 +4770,6 @@ describe('Transformer', function () {
assert.equal(clone.nodes().length, 0); assert.equal(clone.nodes().length, 0);
}); });
describe('`transformer.nodes( )` should filter invalid nodes if they are descendants of a transformer or parent of the transformer', function () {
it('should filter children of a transformer', function () {
const stage = addStage()!;
const layer = new Konva.Layer();
stage.add(layer);
const tr = new Konva.Transformer();
layer.add(tr);
tr.nodes([tr.children![0]]);
assert.equal(tr.nodes().length, 0);
});
it('should filter parent of the transformer', function () { it('should filter parent of the transformer', function () {
const stage = addStage(); const stage = addStage();
@ -4796,5 +4782,4 @@ describe('Transformer', function () {
tr.nodes([layer]); tr.nodes([layer]);
assert.equal(tr.nodes().length, 0); assert.equal(tr.nodes().length, 0);
}); });
});
}); });