Browse Source

added new parameter to add method; running keymaps only when the editor is not on edit mode; improved test setup

pull/1800/head
Arthur Almeida 7 years ago
parent
commit
80b19c220e
  1. 20
      src/keymaps/index.js
  2. 65
      test/specs/keymaps/index.js

20
src/keymaps/index.js

@ -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 });
}
},
@ -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];
@ -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;

65
test/specs/keymaps/index.js

@ -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', () => {
@ -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();
});
});
});
});

Loading…
Cancel
Save