Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/artf/grapesjs into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
artf committed Feb 18, 2019
2 parents 6016b60 + ec735ef commit efc6387
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/commands/view/ComponentDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isArray } from 'underscore';

module.exports = {
run(ed, sender, opts = {}) {
if (ed.getModel().isEditing() || ed.Canvas.isInputFocused()) return;
let components = opts.component || ed.getSelectedAll();
components = isArray(components) ? [...components] : [components];

Expand Down
7 changes: 1 addition & 6 deletions src/commands/view/ComponentEnter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
module.exports = {
run(ed) {
if (
!ed.Canvas.hasFocus() ||
ed.getModel().isEditing() ||
ed.Canvas.isInputFocused()
)
return;
if (!ed.Canvas.hasFocus()) return;
const toSelect = [];

ed.getSelectedAll().forEach(component => {
Expand Down
7 changes: 1 addition & 6 deletions src/commands/view/ComponentExit.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
module.exports = {
run(ed) {
if (
!ed.Canvas.hasFocus() ||
ed.getModel().isEditing() ||
ed.Canvas.isInputFocused()
)
return;
if (!ed.Canvas.hasFocus()) return;
const toSelect = [];

ed.getSelectedAll().forEach(component => {
Expand Down
7 changes: 1 addition & 6 deletions src/commands/view/ComponentNext.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
module.exports = {
run(ed) {
if (
!ed.Canvas.hasFocus() ||
ed.getModel().isEditing() ||
ed.Canvas.isInputFocused()
)
return;
if (!ed.Canvas.hasFocus()) return;
const toSelect = [];

ed.getSelectedAll().forEach(component => {
Expand Down
7 changes: 1 addition & 6 deletions src/commands/view/ComponentPrev.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
module.exports = {
run(ed) {
if (
!ed.Canvas.hasFocus() ||
ed.getModel().isEditing() ||
ed.Canvas.isInputFocused()
)
return;
if (!ed.Canvas.hasFocus()) return;
const toSelect = [];

ed.getSelectedAll().forEach(component => {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/view/CopyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
const em = ed.getModel();
const models = [...ed.getSelectedAll()];

if (models.length && !em.isEditing() && !ed.Canvas.isInputFocused()) {
if (models.length) {
em.set('clipboard', models);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/view/PasteComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
const clp = em.get('clipboard');
const selected = ed.getSelected();

if (clp && selected && !em.isEditing() && !ed.Canvas.isInputFocused()) {
if (clp && selected) {
ed.getSelectedAll().forEach(comp => {
if (!comp) return;
const coll = comp.collection;
Expand Down
20 changes: 12 additions & 8 deletions src/keymaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module.exports = () => {

for (let id in defKeys) {
const value = defKeys[id];
this.add(id, value.keys, value.handler);
this.add(id, value.keys, value.handler, { force: false });
}
},

Expand All @@ -137,8 +137,9 @@ module.exports = () => {
*/
add(id, keys, handler, opts = {}) {
const em = this.em;
const editor = em.getEditor();
const force = opts.force || false;
const cmd = em.get('Commands');
const editor = em.getEditor();
const canvas = em.get('Canvas');
const keymap = { id, keys, handler };
const pk = keymaps[id];
Expand All @@ -149,12 +150,15 @@ module.exports = () => {
const opt = { event: e, h };
handler = isString(handler) ? cmd.get(handler) : handler;
opts.prevent && canvas.getCanvasView().preventDefault(e);
typeof handler == 'object'
? handler.run(editor, 0, opt)
: handler(editor, 0, opt);
const args = [id, h.shortcut, e];
em.trigger('keymap:emit', ...args);
em.trigger(`keymap:emit:${id}`, ...args);
const ableTorun = !em.isEditing() && !editor.Canvas.isInputFocused();
if (ableTorun || force) {
typeof handler == 'object'
? handler.run(editor, 0, opt)
: handler(editor, 0, opt);
const args = [id, h.shortcut, e];
em.trigger('keymap:emit', ...args);
em.trigger(`keymap:emit:${id}`, ...args);
}
});
em.trigger('keymap:add', keymap);
return keymap;
Expand Down
65 changes: 62 additions & 3 deletions test/specs/keymaps/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const Editor = require('editor/model/Editor');
import Editor from 'editor/index';
const Keymaps = require('keymaps');

describe('Keymaps', () => {
describe('Main', () => {
let em;
let obj;
let editor;

beforeEach(() => {
em = new Editor();
obj = new Keymaps().init({ em });
editor = Editor().init();
em = editor.getModel();
obj = editor.Keymaps;
});

test('Object exists', () => {
Expand Down Expand Up @@ -53,5 +55,62 @@ describe('Keymaps', () => {
const removed = obj.remove('tes');
expect(called).toEqual(1);
});

describe('Given the edit is not on edit mode', () => {
beforeEach(() => {
em.setEditing(0);
});

it('Should run the handler', () => {
const handler = {
run: jest.fn()
};
obj.add('test', 'ctrl+a', handler);
const keyboardEvent = new KeyboardEvent('keydown', {
keyCode: 65,
which: 65,
ctrlKey: true
});
document.dispatchEvent(keyboardEvent);

expect(handler.run).toBeCalled();
});
});

describe('Given the edit is on edit mode', () => {
beforeEach(() => {
em.setEditing(1);
});

it('Should not run the handler', () => {
const handler = {
run: jest.fn()
};
obj.add('test', 'ctrl+a', handler);
const keyboardEvent = new KeyboardEvent('keydown', {
keyCode: 65,
which: 65,
ctrlKey: true
});
document.dispatchEvent(keyboardEvent);

expect(handler.run).toBeCalledTimes(0);
});

it('Should run the handler if checked as force', () => {
const handler = {
run: jest.fn()
};
obj.add('test', 'ctrl+a', handler, { force: true });
const keyboardEvent = new KeyboardEvent('keydown', {
keyCode: 65,
which: 65,
ctrlKey: true
});
document.dispatchEvent(keyboardEvent);

expect(handler.run).toBeCalled();
});
});
});
});

0 comments on commit efc6387

Please sign in to comment.