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: Backspace does not work at text editing #66

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
fix: Backspace does not work at text editing
  • Loading branch information
jinwoo-kim-nhn committed Oct 2, 2018
commit f6c78345bea8b0c84925d333f13c1286523a5ad3
13 changes: 10 additions & 3 deletions src/js/graphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,18 @@ class Graphics {

/**
* To data url from canvas
* @param {string} type - A DOMString indicating the image format. The default type is image/png.
* @param {Object} options - options for toDataURL
* @param {String} [options.format=png] The format of the output image. Either "jpeg" or "png"
* @param {Number} [options.quality=1] Quality level (0..1). Only used for jpeg.
* @param {Number} [options.multiplier=1] Multiplier to scale by
* @param {Number} [options.left] Cropping left offset. Introduced in fabric v1.2.14
* @param {Number} [options.top] Cropping top offset. Introduced in fabric v1.2.14
* @param {Number} [options.width] Cropping width. Introduced in fabric v1.2.14
* @param {Number} [options.height] Cropping height. Introduced in fabric v1.2.14
* @returns {string} A DOMString containing the requested data URI.
*/
toDataURL(type) {
return this._canvas && this._canvas.toDataURL(type);
toDataURL(options) {
Copy link
Member

Choose a reason for hiding this comment

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

혹시 이 메서드 퍼블릭 API인가요?
만약 그렇다면.. 파라미터 타입이 변경되면 버저닝 이슈가 있겠네요 ;ㅅ;

Copy link
Member

Choose a reason for hiding this comment

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

http:https://fabricjs.com/docs/fabric.Canvas.html#toDataURL
처음 개발할 때 패브릭 버전 확인해보면 좋을 것 같습니다~ 일단 픽스 버전으로 올리면 될듯요

return this._canvas && this._canvas.toDataURL(options);
}

/**
Expand Down
19 changes: 15 additions & 4 deletions src/js/imageEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ class ImageEditor {
*/
/* eslint-disable complexity */
_onKeyDown(e) {
const activeObject = this._graphics.getActiveObject();
const activeObjectGroup = this._graphics.getActiveGroupObject();
const existRemoveObject = activeObject || activeObjectGroup;

if ((e.ctrlKey || e.metaKey) && e.keyCode === keyCodes.Z) {
// There is no error message on shortcut when it's empty
this.undo()['catch'](() => {});
Expand All @@ -305,7 +309,7 @@ class ImageEditor {
this.redo()['catch'](() => {});
}

if ((e.keyCode === keyCodes.BACKSPACE || e.keyCode === keyCodes.DEL)) {
if (((e.keyCode === keyCodes.BACKSPACE || e.keyCode === keyCodes.DEL) && existRemoveObject)) {
e.preventDefault();
this.removeActiveObject();
}
Expand Down Expand Up @@ -1226,7 +1230,14 @@ class ImageEditor {

/**
* Get data url
* @param {string} type - A DOMString indicating the image format. The default type is image/png.
* @param {Object} options - options for toDataURL
* @param {String} [options.format=png] The format of the output image. Either "jpeg" or "png"
* @param {Number} [options.quality=1] Quality level (0..1). Only used for jpeg.
* @param {Number} [options.multiplier=1] Multiplier to scale by
* @param {Number} [options.left] Cropping left offset. Introduced in fabric v1.2.14
* @param {Number} [options.top] Cropping top offset. Introduced in fabric v1.2.14
* @param {Number} [options.width] Cropping width. Introduced in fabric v1.2.14
* @param {Number} [options.height] Cropping height. Introduced in fabric v1.2.14
* @returns {string} A DOMString containing the requested data URI
* @example
* imgEl.src = imageEditor.toDataURL();
Expand All @@ -1235,8 +1246,8 @@ class ImageEditor {
* imageEditor.addImageObject(imgUrl);
* });
*/
toDataURL(type) {
return this._graphics.toDataURL(type);
toDataURL(options) {
return this._graphics.toDataURL(options);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/imageEditor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import snippet from 'tui-code-snippet';
import Promise from 'core-js/library/es6/promise';
import ImageEditor from '../src/js/imageEditor';
import consts from '../src/js/consts';

describe('ImageEditor', () => {
// hostnameSent module scope variable can not be reset.
Expand Down Expand Up @@ -58,6 +59,20 @@ describe('ImageEditor', () => {
});
});

it('`preventDefault` of BACKSPACE key events should not be executed when object is selected state.', () => {
const spyCallback = jasmine.createSpy();

spyOn(imageEditor._graphics, 'getActiveObject').and.returnValue(null);
spyOn(imageEditor._graphics, 'getActiveGroupObject').and.returnValue(null);

imageEditor._onKeyDown({
keyCode: consts.keyCodes.BACKSPACE,
preventDefault: spyCallback
});

expect(spyCallback).not.toHaveBeenCalled();
});

describe('removeActiveObject()', () => {
it('_removeObjectStream should be executed when group exists.', () => {
spyOn(imageEditor._graphics, 'getActiveObject');
Expand Down