mirror of
https://github.com/konvajs/konva.git
synced 2025-10-15 12:34:52 +08:00
warning on dublicate ids
This commit is contained in:
@@ -7,13 +7,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
* Show a warning when a stage has too many layers.
|
* Show a warning when a stage has too many layers.
|
||||||
|
* Show a warning on duplicate ids
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
* Fixes inconsistent `layer.setSize()` method. Now it has same arguments as any container.
|
* Fixes inconsistent `layer.setSize()` method. Now it has same arguments as any container.
|
||||||
* Full rewrite to Typescript with tons of refactoring and small optimizations. The public API should be 100% the same
|
* Full rewrite to Typescript with tons of refactoring and small optimizations. The public API should be 100% the same
|
||||||
* Fixed `patternImage` and `radialGradient` for `Konva.Text`
|
* Fixed `patternImage` and `radialGradient` for `Konva.Text`
|
||||||
* `Konva.Util._isObject` is renamed to `Konva.Util._isPlainObject`.
|
* `Konva.Util._isObject` is renamed to `Konva.Util._isPlainObject`.
|
||||||
* TODO: changed behavior of `removeId`.
|
* changed behavior of `removeId`.
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
* `Konva.Util.addMethods`
|
* `Konva.Util.addMethods`
|
||||||
|
53
konva.js
53
konva.js
@@ -20,8 +20,6 @@
|
|||||||
* @namespace Konva
|
* @namespace Konva
|
||||||
*/
|
*/
|
||||||
var version = '@@version';
|
var version = '@@version';
|
||||||
// private
|
|
||||||
var ids = {};
|
|
||||||
var names = {};
|
var names = {};
|
||||||
var shapes = {};
|
var shapes = {};
|
||||||
var isBrowser = typeof window !== 'undefined' &&
|
var isBrowser = typeof window !== 'undefined' &&
|
||||||
@@ -62,31 +60,6 @@
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
var _addId = function (node, id) {
|
|
||||||
if (!id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// do we need this warning?
|
|
||||||
// if (ids[id]) {
|
|
||||||
// console.warn(
|
|
||||||
// 'Duplicate id "' +
|
|
||||||
// id +
|
|
||||||
// '". Please don not use same id several times. It may break find() method look up.'
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
ids[id] = node;
|
|
||||||
};
|
|
||||||
var _removeId = function (id, node) {
|
|
||||||
// node has no id
|
|
||||||
if (!id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// another node is registered (possible for duplicate ids)
|
|
||||||
if (ids[id] !== node) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
delete ids[id];
|
|
||||||
};
|
|
||||||
var _addName = function (node, name) {
|
var _addName = function (node, name) {
|
||||||
if (name) {
|
if (name) {
|
||||||
if (!names[name]) {
|
if (!names[name]) {
|
||||||
@@ -2086,6 +2059,28 @@
|
|||||||
return HitCanvas;
|
return HitCanvas;
|
||||||
}(Canvas));
|
}(Canvas));
|
||||||
|
|
||||||
|
var ids = {};
|
||||||
|
var ID_WARNING = "Duplicate id \"{id}\".\nPlease do not use same id several times, it will break find() method look up.\nIf you have duplicates it is better to use \"name\" property instead.\n";
|
||||||
|
var _addId = function (node, id) {
|
||||||
|
if (!id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ids[id]) {
|
||||||
|
Util.warn(ID_WARNING.replace('{id}', id));
|
||||||
|
}
|
||||||
|
ids[id] = node;
|
||||||
|
};
|
||||||
|
var _removeId = function (id, node) {
|
||||||
|
// node has no id
|
||||||
|
if (!id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// another node is registered (possible for duplicate ids)
|
||||||
|
if (ids[id] !== node) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
delete ids[id];
|
||||||
|
};
|
||||||
// CONSTANTS
|
// CONSTANTS
|
||||||
var ABSOLUTE_OPACITY = 'absoluteOpacity', ABSOLUTE_TRANSFORM = 'absoluteTransform', ABSOLUTE_SCALE = 'absoluteScale', CHANGE = 'Change', CHILDREN = 'children', KONVA = 'konva', LISTENING = 'listening', MOUSEENTER = 'mouseenter', MOUSELEAVE = 'mouseleave', NAME = 'name', SET$1 = 'set', SHAPE = 'Shape', SPACE = ' ', STAGE = 'stage', TRANSFORM = 'transform', UPPER_STAGE = 'Stage', VISIBLE = 'visible', CLONE_BLACK_LIST = ['id'], TRANSFORM_CHANGE_STR = [
|
var ABSOLUTE_OPACITY = 'absoluteOpacity', ABSOLUTE_TRANSFORM = 'absoluteTransform', ABSOLUTE_SCALE = 'absoluteScale', CHANGE = 'Change', CHILDREN = 'children', KONVA = 'konva', LISTENING = 'listening', MOUSEENTER = 'mouseenter', MOUSELEAVE = 'mouseleave', NAME = 'name', SET$1 = 'set', SHAPE = 'Shape', SPACE = ' ', STAGE = 'stage', TRANSFORM = 'transform', UPPER_STAGE = 'Stage', VISIBLE = 'visible', CLONE_BLACK_LIST = ['id'], TRANSFORM_CHANGE_STR = [
|
||||||
'xChange.konva',
|
'xChange.konva',
|
||||||
@@ -15650,6 +15645,7 @@
|
|||||||
Collection: Collection,
|
Collection: Collection,
|
||||||
Util: Util,
|
Util: Util,
|
||||||
Node: Node,
|
Node: Node,
|
||||||
|
ids: ids,
|
||||||
Container: Container,
|
Container: Container,
|
||||||
Stage: Stage,
|
Stage: Stage,
|
||||||
stages: stages,
|
stages: stages,
|
||||||
@@ -15680,7 +15676,6 @@
|
|||||||
Transformer: Transformer,
|
Transformer: Transformer,
|
||||||
Wedge: Wedge,
|
Wedge: Wedge,
|
||||||
version: version,
|
version: version,
|
||||||
ids: ids,
|
|
||||||
names: names,
|
names: names,
|
||||||
shapes: shapes,
|
shapes: shapes,
|
||||||
isBrowser: isBrowser,
|
isBrowser: isBrowser,
|
||||||
@@ -15688,8 +15683,6 @@
|
|||||||
dblClickWindow: dblClickWindow,
|
dblClickWindow: dblClickWindow,
|
||||||
isDragging: isDragging,
|
isDragging: isDragging,
|
||||||
isDragReady: isDragReady,
|
isDragReady: isDragReady,
|
||||||
_addId: _addId,
|
|
||||||
_removeId: _removeId,
|
|
||||||
_addName: _addName,
|
_addName: _addName,
|
||||||
_removeName: _removeName,
|
_removeName: _removeName,
|
||||||
getAngle: getAngle,
|
getAngle: getAngle,
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
import { Util, Collection } from './Util';
|
import { Util, Collection } from './Util';
|
||||||
import { Factory, Validators } from './Factory';
|
import { Factory, Validators } from './Factory';
|
||||||
import { Node } from './Node';
|
import { Node, ids } from './Node';
|
||||||
import { getGlobalKonva, ids, names } from './Global';
|
import { getGlobalKonva, names } from './Global';
|
||||||
|
|
||||||
import { GetSet, Vector2d, IRect } from './types';
|
import { GetSet, IRect } from './types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Container constructor. Containers are used to contain nodes or other containers
|
* Container constructor. Containers are used to contain nodes or other containers
|
||||||
|
@@ -15,8 +15,6 @@ var PI_OVER_180 = Math.PI / 180;
|
|||||||
*/
|
*/
|
||||||
export const version = '@@version';
|
export const version = '@@version';
|
||||||
|
|
||||||
// private
|
|
||||||
export const ids = {};
|
|
||||||
export const names = {};
|
export const names = {};
|
||||||
export const shapes = {};
|
export const shapes = {};
|
||||||
|
|
||||||
@@ -66,32 +64,6 @@ export const isDragReady = function() {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
export const _addId = function(node: any, id) {
|
|
||||||
if (!id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// do we need this warning?
|
|
||||||
// if (ids[id]) {
|
|
||||||
// console.warn(
|
|
||||||
// 'Duplicate id "' +
|
|
||||||
// id +
|
|
||||||
// '". Please don not use same id several times. It may break find() method look up.'
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
ids[id] = node;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const _removeId = function(id: string, node: any) {
|
|
||||||
// node has no id
|
|
||||||
if (!id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// another node is registered (possible for duplicate ids)
|
|
||||||
if (ids[id] !== node) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
delete ids[id];
|
|
||||||
};
|
|
||||||
|
|
||||||
export const _addName = function(node: any, name) {
|
export const _addName = function(node: any, name) {
|
||||||
if (name) {
|
if (name) {
|
||||||
|
37
src/Node.ts
37
src/Node.ts
@@ -1,16 +1,39 @@
|
|||||||
import { Util, Collection, Transform, RectConf, Point } from './Util';
|
import { Util, Collection, Transform, RectConf, Point } from './Util';
|
||||||
import { Factory, Validators } from './Factory';
|
import { Factory, Validators } from './Factory';
|
||||||
import { SceneCanvas, HitCanvas } from './Canvas';
|
import { SceneCanvas, HitCanvas } from './Canvas';
|
||||||
import {
|
import { _removeName, _addName, getGlobalKonva } from './Global';
|
||||||
_removeId,
|
|
||||||
_removeName,
|
|
||||||
_addId,
|
|
||||||
_addName,
|
|
||||||
getGlobalKonva
|
|
||||||
} from './Global';
|
|
||||||
import { Container } from './Container';
|
import { Container } from './Container';
|
||||||
import { GetSet, Vector2d } from './types';
|
import { GetSet, Vector2d } from './types';
|
||||||
|
|
||||||
|
export const ids = {};
|
||||||
|
|
||||||
|
const ID_WARNING = `Duplicate id "{id}".
|
||||||
|
Please do not use same id several times, it will break find() method look up.
|
||||||
|
If you have duplicates it is better to use "name" property instead.
|
||||||
|
`;
|
||||||
|
|
||||||
|
const _addId = function(node: Node, id) {
|
||||||
|
if (!id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (ids[id]) {
|
||||||
|
Util.warn(ID_WARNING.replace('{id}', id));
|
||||||
|
}
|
||||||
|
ids[id] = node;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const _removeId = function(id: string, node: any) {
|
||||||
|
// node has no id
|
||||||
|
if (!id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// another node is registered (possible for duplicate ids)
|
||||||
|
if (ids[id] !== node) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
delete ids[id];
|
||||||
|
};
|
||||||
|
|
||||||
export type Filter = (this: Node, imageData: ImageData) => void;
|
export type Filter = (this: Node, imageData: ImageData) => void;
|
||||||
|
|
||||||
type globalCompositeOperationType =
|
type globalCompositeOperationType =
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
export * from './Global';
|
export * from './Global';
|
||||||
|
|
||||||
export { Collection, Util } from './Util';
|
export { Collection, Util } from './Util';
|
||||||
export { Node } from './Node';
|
export { Node, ids } from './Node';
|
||||||
export { Container } from './Container';
|
export { Container } from './Container';
|
||||||
|
|
||||||
export { Stage, stages } from './Stage';
|
export { Stage, stages } from './Stage';
|
||||||
|
@@ -118,20 +118,23 @@ suite('Node', function() {
|
|||||||
assert.equal(layer.getAbsoluteOpacity(), 0.5);
|
assert.equal(layer.getAbsoluteOpacity(), 0.5);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('warn on duplicate id', function() {
|
test.only('warn on duplicate id', function() {
|
||||||
var oldWarn = Konva.Util.warn;
|
var oldWarn = Konva.Util.warn;
|
||||||
var called = false;
|
var called = false;
|
||||||
Konva.Util.warn = function() {
|
Konva.Util.warn = function() {
|
||||||
called = true;
|
called = true;
|
||||||
};
|
};
|
||||||
var circle = new Konva.Circle({
|
var circle1 = new Konva.Circle({
|
||||||
id: 'circle'
|
id: 'circle'
|
||||||
});
|
});
|
||||||
var circle = new Konva.Circle({
|
var circle2 = new Konva.Circle({
|
||||||
id: 'circle'
|
id: 'circle'
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(called, true);
|
assert.equal(called, true);
|
||||||
Konva.Util.warn = oldWarn;
|
Konva.Util.warn = oldWarn;
|
||||||
|
circle1.destroy();
|
||||||
|
circle2.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
// ======================================================
|
// ======================================================
|
||||||
@@ -1153,8 +1156,6 @@ suite('Node', function() {
|
|||||||
layer.add(circle);
|
layer.add(circle);
|
||||||
stage.add(layer);
|
stage.add(layer);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* add custom attr that points to self. The setAttrs method should
|
* add custom attr that points to self. The setAttrs method should
|
||||||
* not inifinitely recurse causing a stack overflow
|
* not inifinitely recurse causing a stack overflow
|
||||||
@@ -3085,7 +3086,7 @@ suite('Node', function() {
|
|||||||
|
|
||||||
// last shape is registered
|
// last shape is registered
|
||||||
assert.equal(Konva.ids.shape, rect);
|
assert.equal(Konva.ids.shape, rect);
|
||||||
|
|
||||||
// destroying circle should not remove rect from regiter
|
// destroying circle should not remove rect from regiter
|
||||||
circle.destroy();
|
circle.destroy();
|
||||||
|
|
||||||
|
@@ -1231,12 +1231,11 @@ suite('Stage', function() {
|
|||||||
image.src = url;
|
image.src = url;
|
||||||
});
|
});
|
||||||
|
|
||||||
test.only('show a warning if the stage has too many layers', function() {
|
test('show a warning if the stage has too many layers', function() {
|
||||||
var stage = addStage();
|
var stage = addStage();
|
||||||
var oldWarn = Konva.Util.warn;
|
var oldWarn = Konva.Util.warn;
|
||||||
var called = false;
|
var called = false;
|
||||||
Konva.Util.warn = function() {
|
Konva.Util.warn = function() {
|
||||||
oldWarn.apply(null, arguments);
|
|
||||||
called = true;
|
called = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user