Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
load(imageName, img) {
let promise;
if (!imageName && !img) { // Back to the initial state, not error.
const canvas = this.getCanvas();
canvas.backgroundImage = null;
canvas.renderAll();
promise = new Promise(resolve => {
this.setCanvasImage('', null);
resolve();
});
} else {
promise = this._setBackgroundImage(img).then(oImage => {
this.setCanvasImage(imageName, oImage);
this.adjustCanvasDimension();
return oImage;
});
}
return promise;
}
it('removeObjectStream () must be executed as many times as the length of the Object array.', done => {
const promise = new Promise(resolve => {
resolve();
});
spyOn(imageEditor, '_removeObjectStream').and.callThrough();
spyOn(imageEditor, 'removeObject').and.returnValue(promise);
const removeJobsSequens = [1, 2, 3, 4];
const expected = removeJobsSequens.length + 1;
const removeObjectStremPromise = imageEditor._removeObjectStream(removeJobsSequens);
removeObjectStremPromise.then(() => {
expect(imageEditor._removeObjectStream.calls.count()).toBe(expected);
done();
});
});
it('add once event mousedown should be executed When the addIcon action occurs', () => {
const promise = new Promise(resolve => {
resolve(300);
});
spyOn(imageEditorMock, 'changeCursor');
spyOn(imageEditorMock, 'addIcon').and.returnValue(promise);
iconAction.addIcon('iconTypeA');
expect(imageEditorMock.changeCursor).toHaveBeenCalled();
imageEditorMock.fire('mousedown', null, {
x: 10,
y: 10
});
expected = imageEditorMock.addIcon.calls.mostRecent().args[0];
expect(expected).toBe('iconTypeA');
});
add(type, options) {
return new Promise((resolve, reject) => {
const sourceImg = this._getSourceImage();
const canvas = this.getCanvas();
let imgFilter = this._getFilter(sourceImg, type);
if (!imgFilter) {
imgFilter = this._createFilter(sourceImg, type, options);
}
if (!imgFilter) {
reject(rejectMessages.invalidParameters);
}
this._changeFilterValues(imgFilter, options);
this._apply(sourceImg, () => {
canvas.renderAll();
resolve({
add(type, options) {
return new Promise(resolve => {
const canvas = this.getCanvas();
options = this._extendOptions(options);
const shapeObj = this._createInstance(type, options);
this._bindEventOnShape(shapeObj);
canvas.add(shapeObj).setActiveObject(shapeObj);
const objectProperties = this.graphics.createObjectProperties(shapeObj);
resolve(objectProperties);
});
}
change(shapeObj, options) {
return new Promise((resolve, reject) => {
if (inArray(shapeObj.get('type'), shapeType) < 0) {
reject(rejectMessages.unsupportedType);
}
shapeObj.set(options);
this.getCanvas().renderAll();
resolve();
});
}
setStyle(activeObj, styleObj) {
return new Promise(resolve => {
snippet.forEach(styleObj, (val, key) => {
if (activeObj[key] === val) {
styleObj[key] = resetStyles[key] || '';
}
}, this);
activeObj.set(styleObj);
this.getCanvas().renderAll();
resolve();
});
}
addImageObject(imgUrl) {
const callback = bind(this._callbackAfterLoadingImageObject, this);
return new Promise(resolve => {
fabric.Image.fromURL(imgUrl, image => {
callback(image);
resolve();
}, {
crossOrigin: 'Anonymous'
}
);
});
}
_setBackgroundImage(img) {
if (!img) {
return Promise.reject(rejectMessages.loadImage);
}
return new Promise((resolve, reject) => {
const canvas = this.getCanvas();
canvas.setBackgroundImage(img, () => {
const oImage = canvas.backgroundImage;
if (oImage && oImage.getElement()) {
resolve(oImage);
} else {
reject(rejectMessages.loadingImageFailed);
}
}, imageOption);
});
}
}