remove Konva.names and Konva.ids. close #1073

This commit is contained in:
Anton Lavrenov
2021-05-06 15:25:12 -05:00
parent ef6fbf2e14
commit 386287eebe
7 changed files with 14 additions and 358 deletions

View File

@@ -709,71 +709,6 @@ describe('Container', function () {
assert.equal(a, 2, 'listener should have fired for rect');
});
// ======================================================
it('test ids and names hashes', function () {
var stage = addStage();
var layer = new Konva.Layer();
var circle = new Konva.Circle({
x: stage.width() / 2,
y: stage.height() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
id: 'myCircle3',
});
var rect = new Konva.Rect({
x: 300,
y: 100,
width: 100,
height: 50,
fill: 'purple',
stroke: 'black',
strokeWidth: 4,
name: 'myRect3',
});
layer.add(circle);
layer.add(rect);
stage.add(layer);
assert.equal(
Konva.ids['myCircle3'].getId(),
'myCircle3',
'circle id not in ids hash'
);
assert.equal(
Konva.names['myRect3'][0].getName(),
'myRect3',
'rect name not in names hash'
);
circle.setId('newCircleId');
assert.notEqual(
Konva.ids['newCircleId'],
undefined,
'circle not in ids hash'
);
assert.equal(
Konva.ids['myCircle3'],
undefined,
'old circle id key is still in ids hash'
);
rect.setName('newRectName');
assert.notEqual(
Konva.names['newRectName'][0],
undefined,
'new rect name not in names hash'
);
assert.equal(
Konva.names['myRect3'],
undefined,
'old rect name is still in names hash'
);
});
// ======================================================
it('remove all children from layer', function () {
var stage = addStage();
@@ -816,68 +751,6 @@ describe('Container', function () {
);
});
// ======================================================
it('destroy all children from layer', function () {
var stage = addStage();
var layer = new Konva.Layer({
name: 'layerName',
id: 'layerId',
});
var group = new Konva.Group();
var circle1 = new Konva.Circle({
x: 100,
y: stage.height() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'circleName',
id: 'circleId',
});
var circle2 = new Konva.Circle({
x: 300,
y: stage.height() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
});
group.add(circle1);
group.add(circle2);
layer.add(group);
stage.add(layer);
assert.equal(layer.children.length, 1, 'layer should have 1 children');
assert.equal(group.children.length, 2, 'group should have 2 children');
assert(
Konva.names.circleName.length > 0,
'circleName should be in names hash'
);
assert.equal(
Konva.ids.circleId.getId(),
'circleId',
'layerId should be in ids hash'
);
layer.destroyChildren();
layer.draw();
assert.equal(layer.children.length, 0, 'layer should have 0 children');
assert.equal(group.children.length, 0, 'group should have 0 children');
assert.equal(
Konva.names.circleName,
undefined,
'circleName should not be in names hash'
);
assert.equal(
Konva.ids.circleId,
undefined,
'layerId should not be in ids hash'
);
});
// ======================================================
it('add group', function () {
var stage = addStage();

View File

@@ -2745,32 +2745,6 @@ describe('Node', function () {
assert.equal(circle.getParent(), undefined);
});
// ======================================================
it('memory leak test for destroy and a shape with several names', function () {
var stage = addStage();
var layer = new Konva.Layer();
var circle = new Konva.Circle({
x: stage.width() / 2,
y: stage.height() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
name: 'my-new-shape my-new-circle',
});
layer.add(circle);
stage.add(layer);
assert.equal(Konva.names['my-new-shape'].length, 1);
assert.equal(Konva.names['my-new-circle'].length, 1);
circle.destroy();
assert.equal(Konva.names['my-new-shape'], undefined);
assert.equal(Konva.names['my-new-circle'], undefined);
});
// ======================================================
it('destroy shape without adding its parent to stage', function () {
var stage = addStage();
@@ -3003,64 +2977,19 @@ describe('Node', function () {
layer.add(rect);
stage.add(layer);
assert.equal(Konva.ids.myCircle2._id, circle._id);
assert.equal(Konva.names.myRect2[0]._id, rect._id);
assert.equal(Konva.shapes[circleColorKey]._id, circle._id);
assert.equal(Konva.shapes[rectColorKey]._id, rect._id);
circle.destroy();
assert.equal(Konva.ids.myCircle2, undefined);
assert.equal(Konva.names.myRect2[0]._id, rect._id);
assert.equal(Konva.shapes[circleColorKey], undefined);
assert.equal(Konva.shapes[rectColorKey]._id, rect._id);
rect.destroy();
assert.equal(Konva.ids.myCircle2, undefined);
assert.equal(Konva.names.myRect2, undefined);
assert.equal(Konva.shapes[circleColorKey], undefined);
assert.equal(Konva.shapes[rectColorKey], undefined);
});
// ======================================================
it('destroy should remove only required shape from ids regestry', function () {
var stage = addStage();
var layer = new Konva.Layer();
var circle = new Konva.Circle({
x: stage.width() / 2,
y: stage.height() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
id: 'shape',
});
var rect = new Konva.Rect({
x: 300,
y: 100,
width: 100,
height: 50,
fill: 'purple',
stroke: 'black',
strokeWidth: 4,
id: 'shape',
});
layer.add(circle);
layer.add(rect);
stage.add(layer);
// last shape is registered
assert.equal(Konva.ids.shape, rect);
// destroying circle should not remove rect from regiter
circle.destroy();
assert.equal(Konva.ids.shape, rect);
});
// ======================================================
it('hide stage', function () {
var stage = addStage({

View File

@@ -709,49 +709,8 @@ describe('Stage', function () {
layer.add(circle);
stage.add(layer);
assert.equal(
Konva.ids.stageFalconId._id,
stage._id,
'stage id should be in global ids map'
);
assert.equal(
Konva.names.stageFalconName[0]._id,
stage._id,
'stage name should be in global names map'
);
assert.equal(
Konva.ids.circleFalconId._id,
circle._id,
'circle id should be in global ids map'
);
assert.equal(
Konva.names.circleFalconName[0]._id,
circle._id,
'circle name should be in global names map'
);
stage.destroy();
assert.equal(
Konva.ids.stageFalconId,
undefined,
'stage should no longer be in ids map'
);
assert.equal(
Konva.names.stageFalconName,
undefined,
'stage should no longer be in names map'
);
assert.equal(
Konva.ids.circleFalconId,
undefined,
'circle should no longer be in ids map'
);
assert.equal(
Konva.names.circleFalconName,
undefined,
'circle should no longer be in names map'
);
assert.equal(
Konva.stages.indexOf(stage) === -1,
true,

View File

@@ -73,31 +73,17 @@ describe('Text', function () {
var stage = addStage();
var layer = new Konva.Layer();
var rect = new Konva.Rect({
x: stage.width() / 2,
y: stage.height() / 2,
stroke: '#555',
strokeWidth: 5,
fill: '#ddd',
width: 400,
height: 100,
shadowColor: 'black',
shadowBlur: 1,
shadowOffset: { x: 10, y: 10 },
shadowOpacity: 0.2,
cornerRadius: 10,
});
var text = new Konva.Text({
x: stage.width() / 2,
y: stage.height() / 2,
x: 40,
y: 40,
text: 'Hello World!',
fontSize: 50,
fontFamily: 'Calibri',
fontFamily: 'Arial',
fontStyle: 'normal',
fill: '#888',
stroke: '#333',
align: 'right',
shadowForStrokeEnabled: false,
lineHeight: 1.2,
width: 400,
height: 100,
@@ -112,14 +98,15 @@ describe('Text', function () {
draggable: true,
});
// center text box
rect.offsetX(text.getWidth() / 2);
group.add(rect);
group.add(text);
layer.add(group);
stage.add(layer);
assert.equal(
layer.getContext().getTrace(false, true),
'clearRect(0,0,578,200);save();transform(1,0,0,1,40,40);shadowColor=rgba(255,0,0,0.2);shadowBlur=1;shadowOffsetX=10;shadowOffsetY=10;font=normal normal 50px Arial;textBaseline=middle;textAlign=left;translate(10,10);save();fillStyle=#888;fillText(Hello World!,108,30);lineWidth=2;shadowColor=rgba(0,0,0,0);strokeStyle=#333;strokeText(Hello World!,108,30);restore();restore();'
);
assert.equal(text.getClassName(), 'Text', 'getClassName should be Text');
});