string and fill properties validation can accept CanvasGradient as valid value. close #965

This commit is contained in:
Anton Lavrenov
2020-08-26 09:54:03 -05:00
parent 6fc2e42906
commit 02ec2c4e61
7 changed files with 104 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ import {
getNumberOrAutoValidator,
getStringValidator,
getBooleanValidator,
getStringOrGradientValidator,
} from './Validators';
import { Context, SceneContext } from './Context';
@@ -849,7 +850,12 @@ Shape.prototype.on.call(
);
// add getters and setters
Factory.addGetterSetter(Shape, 'stroke', undefined, getStringValidator());
Factory.addGetterSetter(
Shape,
'stroke',
undefined,
getStringOrGradientValidator()
);
/**
* get/set stroke color
@@ -1220,7 +1226,12 @@ Factory.addGetterSetter(Shape, 'fillPatternImage');
* imageObj.src = 'path/to/image/jpg';
*/
Factory.addGetterSetter(Shape, 'fill', undefined, getStringValidator());
Factory.addGetterSetter(
Shape,
'fill',
undefined,
getStringOrGradientValidator()
);
/**
* get/set fill color

View File

@@ -66,6 +66,7 @@ export function getNumberOrAutoValidator() {
};
}
}
export function getStringValidator() {
if (Konva.isUnminified) {
return function (val: any, attr: string) {
@@ -81,6 +82,26 @@ export function getStringValidator() {
};
}
}
export function getStringOrGradientValidator() {
if (Konva.isUnminified) {
return function (val: any, attr: string) {
const isString = Util._isString(val);
const isGradient =
Object.prototype.toString.call(val) === '[object CanvasGradient]';
if (!(isString || isGradient)) {
Util.warn(
_formatValue(val) +
' is a not valid value for "' +
attr +
'" attribute. The value should be a string or a native gradient.'
);
}
return val;
};
}
}
export function getFunctionValidator() {
if (Konva.isUnminified) {
return function (val: any, attr: string) {

View File

@@ -84,6 +84,6 @@ export const Konva = Core.Util._assign(Core, {
RGBA,
Sepia,
Solarize,
Threshold
}
Threshold,
},
});