allow reset component attributes via overloader

This commit is contained in:
Anton Lavrenov 2022-08-19 17:48:03 -05:00
parent 94ac6f3684
commit 63d2aceaa7
4 changed files with 67 additions and 35 deletions

View File

@ -44,7 +44,13 @@ export const Factory = {
return this;
};
},
addComponentsGetterSetter(constructor, attr, components, validator?, after?) {
addComponentsGetterSetter(
constructor,
attr: string,
components: Array<string>,
validator?: Function,
after?: Function
) {
var len = components.length,
capitalize = Util._capitalize,
getter = GET + capitalize(attr),
@ -85,6 +91,11 @@ export const Factory = {
}
this._setAttr(attr + capitalize(key), val[key]);
}
if (!val) {
components.forEach((component) => {
this._setAttr(attr + capitalize(component), undefined);
});
}
this._fireChangeEvent(attr, oldVal, val);

View File

@ -2024,8 +2024,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
return new Promise((resolve, reject) => {
try {
const callback = config?.callback;
if (callback)
delete config.callback;
if (callback) delete config.callback;
Util._urlToImage(this.toDataURL(config as any), function (img) {
resolve(img);
callback?.(img);
@ -2069,9 +2068,8 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
return new Promise((resolve, reject) => {
try {
const callback = config?.callback;
if (callback)
delete config.callback;
this.toCanvas(config).toBlob(blob => {
if (callback) delete config.callback;
this.toCanvas(config).toBlob((blob) => {
resolve(blob);
callback?.(blob);
});
@ -2615,7 +2613,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
rotation: GetSet<number, this>;
zIndex: GetSet<number, this>;
scale: GetSet<Vector2d, this>;
scale: GetSet<Vector2d | undefined, this>;
scaleX: GetSet<number, this>;
scaleY: GetSet<number, this>;
skew: GetSet<Vector2d, this>;

View File

@ -146,7 +146,7 @@ export function getNumberArrayValidator() {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#description
const TypedArray = Int8Array ? Object.getPrototypeOf(Int8Array) : null;
if (TypedArray && val instanceof TypedArray) {
return val
return val;
}
if (!Util._isArray(val)) {
Util.warn(
@ -191,6 +191,10 @@ export function getBooleanValidator() {
export function getComponentValidator(components: any) {
if (Konva.isUnminified) {
return function (val: any, attr: string) {
// ignore validation on undefined value, because it will reset to defalt
if (val === undefined || val === null) {
return val;
}
if (!Util.isObject(val)) {
Util.warn(
_formatValue(val) +

View File

@ -3274,6 +3274,25 @@ describe('Node', function () {
assert.equal(circle.size().height, 11);
});
it('overloaders reset', function () {
var stage = addStage();
var layer = new Konva.Layer();
var circle = new Konva.Circle({
radius: 70,
fill: 'green',
});
layer.add(circle);
stage.add(layer);
circle.scale({ x: 2, y: 2 });
circle.scale(undefined);
assert.equal(circle.scaleX(), 1);
assert.equal(circle.scaleY(), 1);
});
it('cache shape', function () {
var stage = addStage();
var layer = new Konva.Layer();