mirror of
https://github.com/konvajs/konva.git
synced 2025-10-15 12:34:52 +08:00
fix transformer + clone bug. fix #706
This commit is contained in:
39
konva.js
39
konva.js
@@ -14384,6 +14384,7 @@
|
|||||||
Factory.addGetterSetter(TextPath, 'kerningFunc', null);
|
Factory.addGetterSetter(TextPath, 'kerningFunc', null);
|
||||||
Collection.mapMethods(TextPath);
|
Collection.mapMethods(TextPath);
|
||||||
|
|
||||||
|
var EVENTS_NAME = 'tr-konva';
|
||||||
var ATTR_CHANGE_LIST$2 = [
|
var ATTR_CHANGE_LIST$2 = [
|
||||||
'resizeEnabledChange',
|
'resizeEnabledChange',
|
||||||
'rotateAnchorOffsetChange',
|
'rotateAnchorOffsetChange',
|
||||||
@@ -14399,21 +14400,25 @@
|
|||||||
'anchorFillChange',
|
'anchorFillChange',
|
||||||
'anchorCornerRadiusChange',
|
'anchorCornerRadiusChange',
|
||||||
'ignoreStrokeChange'
|
'ignoreStrokeChange'
|
||||||
].join(' ');
|
]
|
||||||
|
.map(function (e) { return e + ("." + EVENTS_NAME); })
|
||||||
|
.join(' ');
|
||||||
var NODE_RECT = 'nodeRect';
|
var NODE_RECT = 'nodeRect';
|
||||||
var TRANSFORM_CHANGE_STR$1 = [
|
var TRANSFORM_CHANGE_STR$1 = [
|
||||||
'widthChange.tr',
|
'widthChange',
|
||||||
'heightChange.tr',
|
'heightChange',
|
||||||
'scaleXChange.tr',
|
'scaleXChange',
|
||||||
'scaleYChange.tr',
|
'scaleYChange',
|
||||||
'skewXChange.tr',
|
'skewXChange',
|
||||||
'skewYChange.tr',
|
'skewYChange',
|
||||||
'rotationChange.tr',
|
'rotationChange',
|
||||||
'offsetXChange.tr',
|
'offsetXChange',
|
||||||
'offsetYChange.tr',
|
'offsetYChange',
|
||||||
'transformsEnabledChange.tr',
|
'transformsEnabledChange',
|
||||||
'strokeWidthChange.tr'
|
'strokeWidthChange'
|
||||||
].join(' ');
|
]
|
||||||
|
.map(function (e) { return e + ("." + EVENTS_NAME); })
|
||||||
|
.join(' ');
|
||||||
var ANGLES = {
|
var ANGLES = {
|
||||||
'top-left': -45,
|
'top-left': -45,
|
||||||
'top-center': 0,
|
'top-center': 0,
|
||||||
@@ -14558,7 +14563,7 @@
|
|||||||
this._node = node;
|
this._node = node;
|
||||||
this._resetTransformCache();
|
this._resetTransformCache();
|
||||||
var additionalEvents = node._attrsAffectingSize
|
var additionalEvents = node._attrsAffectingSize
|
||||||
.map(function (prop) { return prop + 'Change.tr'; })
|
.map(function (prop) { return prop + 'Change.' + EVENTS_NAME; })
|
||||||
.join(' ');
|
.join(' ');
|
||||||
var onChange = function () {
|
var onChange = function () {
|
||||||
_this._resetTransformCache();
|
_this._resetTransformCache();
|
||||||
@@ -14568,7 +14573,9 @@
|
|||||||
};
|
};
|
||||||
node.on(additionalEvents, onChange);
|
node.on(additionalEvents, onChange);
|
||||||
node.on(TRANSFORM_CHANGE_STR$1, onChange);
|
node.on(TRANSFORM_CHANGE_STR$1, onChange);
|
||||||
node.on('xChange.tr yChange.tr', function () { return _this._resetTransformCache(); });
|
node.on("xChange." + EVENTS_NAME + " yChange." + EVENTS_NAME, function () {
|
||||||
|
return _this._resetTransformCache();
|
||||||
|
});
|
||||||
// we may need it if we set node in initial props
|
// we may need it if we set node in initial props
|
||||||
// so elements are not defined yet
|
// so elements are not defined yet
|
||||||
var elementsCreated = !!this.findOne('.top-left');
|
var elementsCreated = !!this.findOne('.top-left');
|
||||||
@@ -14590,7 +14597,7 @@
|
|||||||
*/
|
*/
|
||||||
Transformer.prototype.detach = function () {
|
Transformer.prototype.detach = function () {
|
||||||
if (this.getNode()) {
|
if (this.getNode()) {
|
||||||
this.getNode().off('.tr');
|
this.getNode().off('.' + EVENTS_NAME);
|
||||||
this._node = undefined;
|
this._node = undefined;
|
||||||
}
|
}
|
||||||
this._resetTransformCache();
|
this._resetTransformCache();
|
||||||
|
2
konva.min.js
vendored
2
konva.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -35,6 +35,8 @@ export interface TransformerConfig extends ContainerConfig {
|
|||||||
boundBoxFunc?: (oldBox: Box, newBox: Box) => Box;
|
boundBoxFunc?: (oldBox: Box, newBox: Box) => Box;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var EVENTS_NAME = 'tr-konva';
|
||||||
|
|
||||||
var ATTR_CHANGE_LIST = [
|
var ATTR_CHANGE_LIST = [
|
||||||
'resizeEnabledChange',
|
'resizeEnabledChange',
|
||||||
'rotateAnchorOffsetChange',
|
'rotateAnchorOffsetChange',
|
||||||
@@ -50,23 +52,27 @@ var ATTR_CHANGE_LIST = [
|
|||||||
'anchorFillChange',
|
'anchorFillChange',
|
||||||
'anchorCornerRadiusChange',
|
'anchorCornerRadiusChange',
|
||||||
'ignoreStrokeChange'
|
'ignoreStrokeChange'
|
||||||
].join(' ');
|
]
|
||||||
|
.map(e => e + `.${EVENTS_NAME}`)
|
||||||
|
.join(' ');
|
||||||
|
|
||||||
var NODE_RECT = 'nodeRect';
|
var NODE_RECT = 'nodeRect';
|
||||||
|
|
||||||
var TRANSFORM_CHANGE_STR = [
|
var TRANSFORM_CHANGE_STR = [
|
||||||
'widthChange.tr',
|
'widthChange',
|
||||||
'heightChange.tr',
|
'heightChange',
|
||||||
'scaleXChange.tr',
|
'scaleXChange',
|
||||||
'scaleYChange.tr',
|
'scaleYChange',
|
||||||
'skewXChange.tr',
|
'skewXChange',
|
||||||
'skewYChange.tr',
|
'skewYChange',
|
||||||
'rotationChange.tr',
|
'rotationChange',
|
||||||
'offsetXChange.tr',
|
'offsetXChange',
|
||||||
'offsetYChange.tr',
|
'offsetYChange',
|
||||||
'transformsEnabledChange.tr',
|
'transformsEnabledChange',
|
||||||
'strokeWidthChange.tr'
|
'strokeWidthChange'
|
||||||
].join(' ');
|
]
|
||||||
|
.map(e => e + `.${EVENTS_NAME}`)
|
||||||
|
.join(' ');
|
||||||
|
|
||||||
var ANGLES = {
|
var ANGLES = {
|
||||||
'top-left': -45,
|
'top-left': -45,
|
||||||
@@ -217,7 +223,7 @@ export class Transformer extends Group {
|
|||||||
this._resetTransformCache();
|
this._resetTransformCache();
|
||||||
|
|
||||||
const additionalEvents = node._attrsAffectingSize
|
const additionalEvents = node._attrsAffectingSize
|
||||||
.map(prop => prop + 'Change.tr')
|
.map(prop => prop + 'Change.' + EVENTS_NAME)
|
||||||
.join(' ');
|
.join(' ');
|
||||||
|
|
||||||
const onChange = () => {
|
const onChange = () => {
|
||||||
@@ -228,7 +234,9 @@ export class Transformer extends Group {
|
|||||||
};
|
};
|
||||||
node.on(additionalEvents, onChange);
|
node.on(additionalEvents, onChange);
|
||||||
node.on(TRANSFORM_CHANGE_STR, onChange);
|
node.on(TRANSFORM_CHANGE_STR, onChange);
|
||||||
node.on('xChange.tr yChange.tr', () => this._resetTransformCache());
|
node.on(`xChange.${EVENTS_NAME} yChange.${EVENTS_NAME}`, () =>
|
||||||
|
this._resetTransformCache()
|
||||||
|
);
|
||||||
// we may need it if we set node in initial props
|
// we may need it if we set node in initial props
|
||||||
// so elements are not defined yet
|
// so elements are not defined yet
|
||||||
var elementsCreated = !!this.findOne('.top-left');
|
var elementsCreated = !!this.findOne('.top-left');
|
||||||
@@ -250,7 +258,7 @@ export class Transformer extends Group {
|
|||||||
*/
|
*/
|
||||||
detach() {
|
detach() {
|
||||||
if (this.getNode()) {
|
if (this.getNode()) {
|
||||||
this.getNode().off('.tr');
|
this.getNode().off('.' + EVENTS_NAME);
|
||||||
this._node = undefined;
|
this._node = undefined;
|
||||||
}
|
}
|
||||||
this._resetTransformCache();
|
this._resetTransformCache();
|
||||||
|
@@ -2332,4 +2332,45 @@ suite('Transformer', function() {
|
|||||||
delete rect.rotation;
|
delete rect.rotation;
|
||||||
assert.deepEqual(shape.getClientRect(), rect), 'change data';
|
assert.deepEqual(shape.getClientRect(), rect), 'change data';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('make sure transformer events are not cloned', function() {
|
||||||
|
var stage = addStage();
|
||||||
|
var layer = new Konva.Layer();
|
||||||
|
stage.add(layer);
|
||||||
|
|
||||||
|
const rect1 = new Konva.Rect({
|
||||||
|
x: stage.width() / 5,
|
||||||
|
y: stage.height() / 5,
|
||||||
|
width: 50,
|
||||||
|
height: 50,
|
||||||
|
fill: 'green',
|
||||||
|
draggable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
layer.add(rect1);
|
||||||
|
|
||||||
|
const tr1 = new Konva.Transformer({
|
||||||
|
node: rect1
|
||||||
|
});
|
||||||
|
layer.add(tr1);
|
||||||
|
|
||||||
|
const rect2 = rect1.clone({
|
||||||
|
fill: 'red',
|
||||||
|
x: stage.width() / 3,
|
||||||
|
y: stage.height() / 3
|
||||||
|
});
|
||||||
|
layer.add(rect2);
|
||||||
|
|
||||||
|
tr1.destroy();
|
||||||
|
|
||||||
|
let tr2 = new Konva.Transformer({
|
||||||
|
node: rect2
|
||||||
|
});
|
||||||
|
layer.add(tr2);
|
||||||
|
|
||||||
|
// should not throw error
|
||||||
|
rect2.width(100);
|
||||||
|
|
||||||
|
stage.draw();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user