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: add custom icon error #65

Merged
merged 1 commit into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그냥 registerdIcon은 어떨까요? 커스텀은 유저가 등록하는 아이콘이고 그 외에 것은 이미 등록되어 있으니까요~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 좋은것 같습니다.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@private 추가요~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 !

*/
_addWithDrag(canvas) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 메서드에서 하는 일은 이벤트를 등록하는 일뿐이네요. 메서드명에 이벤트 등록에 대한 내용이 들어가면 좋겠습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵!

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