Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/custom icon error #67

Merged
merged 3 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: add custom icon error
  • Loading branch information
jinwoo-kim-nhn committed Oct 1, 2018
commit 45c7c588d2ddc1036d98cb4be1c8234f57c67a64
60 changes: 34 additions & 26 deletions src/js/component/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,47 +61,55 @@ class Icon extends Component {
const canvas = this.getCanvas();
const path = this._pathMap[type];
const selectionStyle = consts.fObjectOptions.SELECTION_STYLE;
const isDefaultIconPath = Object.keys(consts.defaultIconPath).indexOf(type) >= 0;
const useDragAddIcon = this.useDragAddIcon && isDefaultIconPath;
const icon = path ? this._createIcon(path) : null;

if (!path) {
if (!icon) {
reject(rejectMessages.invalidParameters);
}

const icon = this._createIcon(path);

icon.set(snippet.extend({
type: 'icon',
fill: this._oColor
}, selectionStyle, options, this.graphics.controlStyle));

if (this.useDragAddIcon) {
canvas.add(icon).setActiveObject(icon);
canvas.on({
'mouse:move': fEvent => {
canvas.selection = false;

this.fire(events.ICON_CREATE_RESIZE, {
moveOriginPointer: canvas.getPointer(fEvent.e)
});
},
'mouse:up': fEvent => {
this.fire(events.ICON_CREATE_END, {
moveOriginPointer: canvas.getPointer(fEvent.e)
});

canvas.defaultCursor = 'default';
canvas.off('mouse:up');
canvas.off('mouse:move');
canvas.selection = true;
}
});
} else {
canvas.add(icon).setActiveObject(icon);
canvas.add(icon).setActiveObject(icon);

if (useDragAddIcon) {
this._addWithDrag(canvas);
}

resolve(this.graphics.createObjectProperties(icon));
});
}

/**
* Added icon drag
* @param {fabric.Canvas} canvas - Canvas instance
*/
_addWithDrag(canvas) {
canvas.on({
'mouse:move': fEvent => {
canvas.selection = false;

this.fire(events.ICON_CREATE_RESIZE, {
moveOriginPointer: canvas.getPointer(fEvent.e)
});
},
'mouse:up': fEvent => {
this.fire(events.ICON_CREATE_END, {
moveOriginPointer: canvas.getPointer(fEvent.e)
});

canvas.defaultCursor = 'default';
canvas.off('mouse:up');
canvas.off('mouse:move');
canvas.selection = true;
}
});
}

/**
* Register icon paths
* @param {{key: string, value: string}} pathInfos - Path infos
Expand Down
25 changes: 25 additions & 0 deletions test/icon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ describe('Icon', () => {
expect(icon._createIcon).toHaveBeenCalledWith(path);
});

it('`_addWithDrag()` should be executed when `useDragAddIcon` is true.', () => {
const defaultIconKey = 'icon-arrow';
icon._pathMap[defaultIconKey] = true;

spyOn(icon, '_createIcon').and.returnValue(new fabric.Object({}));
spyOn(icon, '_addWithDrag');
spyOn(icon, 'useDragAddIcon').and.returnValue(true);

icon.add(defaultIconKey);

expect(icon._addWithDrag).toHaveBeenCalled();
});

it('`_addWithDrag()` should be not executed when target icon is not default icon.', () => {
const nonDefaultIconKey = 'non-default-icon';

spyOn(icon, '_createIcon').and.returnValue(new fabric.Object({}));
spyOn(icon, '_addWithDrag');
spyOn(icon, 'useDragAddIcon').and.returnValue(true);

icon.add(nonDefaultIconKey);

expect(icon._addWithDrag).not.toHaveBeenCalled();
});

it('setColor() should change color of next inserted icon.', () => {
let activeObj;
const color = '#ffffff';
Expand Down