From c2a663a64d23816821d333c0d1c7c7c968f494b0 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 21 Oct 2017 10:13:20 +0900 Subject: clean --- src/content/components/common/input.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/content/components/common') diff --git a/src/content/components/common/input.js b/src/content/components/common/input.js index df09894..2de0d7d 100644 --- a/src/content/components/common/input.js +++ b/src/content/components/common/input.js @@ -47,17 +47,14 @@ export default class InputComponent { return; } - let stop = false; for (let listener of this.onKeyListeners) { - stop = stop || listener(e.key, e.ctrlKey); + let stop = listener(e.key, e.ctrlKey); if (stop) { + e.preventDefault(); + e.stopPropagation(); break; } } - if (stop) { - e.preventDefault(); - e.stopPropagation(); - } } fromInput(e) { -- cgit v1.2.3 From 214a5103f3e2914028206a13ba115c69a7ee07f1 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 21 Oct 2017 11:06:58 +0900 Subject: emit mapped keys from input-component --- src/content/actions/input.js | 11 +--- src/content/components/common/index.js | 4 +- src/content/components/common/input.js | 11 +++- src/content/components/common/keymapper.js | 4 +- test/content/actions/input.test.js | 8 +-- test/content/components/common/input.test.js | 78 ++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 21 deletions(-) create mode 100644 test/content/components/common/input.test.js (limited to 'src/content/components/common') diff --git a/src/content/actions/input.js b/src/content/actions/input.js index 10ff835..31cfee3 100644 --- a/src/content/actions/input.js +++ b/src/content/actions/input.js @@ -1,16 +1,9 @@ import actions from 'content/actions'; -const asKeymapChars = (key, ctrl) => { - if (ctrl) { - return ''; - } - return key; -}; - -const keyPress = (key, ctrl) => { +const keyPress = (key) => { return { type: actions.INPUT_KEY_PRESS, - key: asKeymapChars(key, ctrl), + key, }; }; diff --git a/src/content/components/common/index.js b/src/content/components/common/index.js index 98a2337..db0ac43 100644 --- a/src/content/components/common/index.js +++ b/src/content/components/common/index.js @@ -10,8 +10,8 @@ export default class Common { const input = new InputComponent(win.document.body, store); const keymapper = new KeymapperComponent(store); - input.onKey((key, ctrl) => follow.key(key, ctrl)); - input.onKey((key, ctrl) => keymapper.key(key, ctrl)); + input.onKey(key => follow.key(key)); + input.onKey(key => keymapper.key(key)); this.store = store; this.children = [ diff --git a/src/content/components/common/input.js b/src/content/components/common/input.js index 2de0d7d..9af9fbe 100644 --- a/src/content/components/common/input.js +++ b/src/content/components/common/input.js @@ -1,3 +1,10 @@ +const mapKey = (e) => { + if (e.ctrlKey) { + return ''; + } + return e.key; +}; + export default class InputComponent { constructor(target) { this.pressed = {}; @@ -47,8 +54,10 @@ export default class InputComponent { return; } + let key = mapKey(e); + for (let listener of this.onKeyListeners) { - let stop = listener(e.key, e.ctrlKey); + let stop = listener(key); if (stop) { e.preventDefault(); e.stopPropagation(); diff --git a/src/content/components/common/keymapper.js b/src/content/components/common/keymapper.js index 655c3f2..8c0ddb1 100644 --- a/src/content/components/common/keymapper.js +++ b/src/content/components/common/keymapper.js @@ -9,8 +9,8 @@ export default class KeymapperComponent { update() { } - key(key, ctrl) { - this.store.dispatch(inputActions.keyPress(key, ctrl)); + key(key) { + this.store.dispatch(inputActions.keyPress(key)); let input = this.store.getState().input; let matched = Object.keys(input.keymaps).filter((keyStr) => { diff --git a/test/content/actions/input.test.js b/test/content/actions/input.test.js index 6031829..30705d2 100644 --- a/test/content/actions/input.test.js +++ b/test/content/actions/input.test.js @@ -5,16 +5,10 @@ import * as inputActions from 'content/actions/input'; describe("input actions", () => { describe("keyPress", () => { it('create INPUT_KEY_PRESS action', () => { - let action = inputActions.keyPress('a', false); + let action = inputActions.keyPress('a'); expect(action.type).to.equal(actions.INPUT_KEY_PRESS); expect(action.key).to.equal('a'); }); - - it('create INPUT_KEY_PRESS action from key with ctrl', () => { - let action = inputActions.keyPress('b', true); - expect(action.type).to.equal(actions.INPUT_KEY_PRESS); - expect(action.key).to.equal(''); - }); }); describe("clearKeys", () => { diff --git a/test/content/components/common/input.test.js b/test/content/components/common/input.test.js new file mode 100644 index 0000000..d4f554a --- /dev/null +++ b/test/content/components/common/input.test.js @@ -0,0 +1,78 @@ +import InputComponent from 'content/components/common/input'; +import { expect } from "chai"; + +describe('InputComponent', () => { + it('register callbacks', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect(key).is.equals('a'); + }); + component.onKeyDown({ key: 'a' }); + }); + + it('invoke callback once', () => { + let component = new InputComponent(window.document); + let a = 0, b = 0; + component.onKey((key) => { + if (key == 'a') { + ++a; + } else { + key == 'b' + ++b; + } + }); + component.onKeyDown({ key: 'a' }); + component.onKeyDown({ key: 'b' }); + component.onKeyPress({ key: 'a' }); + component.onKeyUp({ key: 'a' }); + component.onKeyPress({ key: 'b' }); + component.onKeyUp({ key: 'b' }); + + expect(a).is.equals(1); + expect(b).is.equals(1); + }) + + it('add prefix when ctrl pressed', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect(key).is.equals(''); + }); + component.onKeyDown({ key: 'a', ctrlKey: true }); + }) + + it('does not invoke only meta keys', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect.fail(); + }); + component.onKeyDown({ key: 'Shift' }); + component.onKeyDown({ key: 'Control' }); + component.onKeyDown({ key: 'Alt' }); + component.onKeyDown({ key: 'OS' }); + }) + + it('ignores events from input elements', () => { + ['input', 'textarea', 'select'].forEach((name) => { + let target = window.document.createElement(name); + let component = new InputComponent(target); + component.onKey((key) => { + expect.fail(); + }); + component.onKeyDown({ key: 'x', target }); + }); + }); + + it('ignores events from contenteditable elements', () => { + let target = window.document.createElement('div'); + let component = new InputComponent(target); + component.onKey((key) => { + expect.fail(); + }); + + target.setAttribute('contenteditable', ''); + component.onKeyDown({ key: 'x', target }); + + target.setAttribute('contenteditable', 'true'); + component.onKeyDown({ key: 'x', target }); + }) +}); -- cgit v1.2.3 From cd6f7e77887f535e88690b84ad081f7cc630306f Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 21 Oct 2017 12:42:30 +0900 Subject: support shift modifier --- src/content/components/common/input.js | 13 ++++++++++++- test/content/components/common/input.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) (limited to 'src/content/components/common') diff --git a/src/content/components/common/input.js b/src/content/components/common/input.js index 9af9fbe..f285b0c 100644 --- a/src/content/components/common/input.js +++ b/src/content/components/common/input.js @@ -1,6 +1,17 @@ +const modifierdKeyName = (name) => { + if (name.length === 1) { + return name.toUpperCase(); + } else if (name === 'Escape') { + return 'Esc'; + } + return name; +}; + const mapKey = (e) => { if (e.ctrlKey) { - return ''; + return ''; + } else if (e.shiftKey && e.key.length !== 1) { + return ''; } return e.key; }; diff --git a/test/content/components/common/input.test.js b/test/content/components/common/input.test.js index d4f554a..912ac34 100644 --- a/test/content/components/common/input.test.js +++ b/test/content/components/common/input.test.js @@ -40,6 +40,30 @@ describe('InputComponent', () => { component.onKeyDown({ key: 'a', ctrlKey: true }); }) + it('press X', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect(key).is.equals('X'); + }); + component.onKeyDown({ key: 'X', shiftKey: true }); + }) + + it('press + ', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect(key).is.equals(''); + }); + component.onKeyDown({ key: 'Escape', shiftKey: true }); + }) + + it('press + ', () => { + let component = new InputComponent(window.document); + component.onKey((key) => { + expect(key).is.equals(''); + }); + component.onKeyDown({ key: 'Escape', ctrlKey: true }); + }) + it('does not invoke only meta keys', () => { let component = new InputComponent(window.document); component.onKey((key) => { -- cgit v1.2.3 From dc6d93c1da28ed17fe1bc5be07bdfab30a40dd66 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 21 Oct 2017 19:40:22 +0900 Subject: enable/disable addon by keys --- src/content/actions/operation.js | 7 +++++++ src/content/components/common/keymapper.js | 12 ++++++++++++ src/content/reducers/index.js | 3 +++ src/shared/operations.js | 5 +++++ 4 files changed, 27 insertions(+) (limited to 'src/content/components/common') diff --git a/src/content/actions/operation.js b/src/content/actions/operation.js index 81bcc2f..897f361 100644 --- a/src/content/actions/operation.js +++ b/src/content/actions/operation.js @@ -4,9 +4,16 @@ import * as scrolls from 'content/scrolls'; import * as navigates from 'content/navigates'; import * as urls from 'content/urls'; import * as consoleFrames from 'content/console-frames'; +import * as addonActions from './addon'; const exec = (operation) => { switch (operation.type) { + case operations.ADDON_ENABLE: + return addonActions.enable(); + case operations.ADDON_DISABLE: + return addonActions.disable(); + case operations.ADDON_TOGGLE_ENABLED: + return addonActions.toggleEnabled(); case operations.SCROLL_VERTICALLY: return scrolls.scrollVertically(window, operation.count); case operations.SCROLL_HORIZONALLY: diff --git a/src/content/components/common/keymapper.js b/src/content/components/common/keymapper.js index 8c0ddb1..5070cd8 100644 --- a/src/content/components/common/keymapper.js +++ b/src/content/components/common/keymapper.js @@ -1,5 +1,6 @@ import * as inputActions from 'content/actions/input'; import * as operationActions from 'content/actions/operation'; +import operations from 'shared/operations'; export default class KeymapperComponent { constructor(store) { @@ -10,12 +11,23 @@ export default class KeymapperComponent { } key(key) { + let enabled = this.store.getState().addon.enabled; + this.store.dispatch(inputActions.keyPress(key)); let input = this.store.getState().input; let matched = Object.keys(input.keymaps).filter((keyStr) => { return keyStr.startsWith(input.keys); }); + if (!enabled) { + // available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if + // the addon disabled + matched = matched.filter((keys) => { + let type = input.keymaps[keys].type; + return type === operations.ADDON_ENABLE || + type === operations.ADDON_TOGGLE_ENABLED; + }); + } if (matched.length === 0) { this.store.dispatch(inputActions.clearKeys()); return false; diff --git a/src/content/reducers/index.js b/src/content/reducers/index.js index c026a19..7cf318e 100644 --- a/src/content/reducers/index.js +++ b/src/content/reducers/index.js @@ -1,14 +1,17 @@ +import addonReducer from './addon'; import inputReducer from './input'; import followReducer from './follow'; // Make setting reducer instead of re-use const defaultState = { + addon: addonReducer(undefined, {}), input: inputReducer(undefined, {}), follow: followReducer(undefined, {}), }; export default function reducer(state = defaultState, action = {}) { return Object.assign({}, state, { + addon: addonReducer(state.addon, action), input: inputReducer(state.input, action), follow: followReducer(state.follow, action), }); diff --git a/src/shared/operations.js b/src/shared/operations.js index 0d2a381..d5c2985 100644 --- a/src/shared/operations.js +++ b/src/shared/operations.js @@ -1,4 +1,9 @@ export default { + // Addons + ADDON_ENABLE: 'addon.enable', + ADDON_DISABLE: 'addon.disable', + ADDON_TOGGLE_ENABLED: 'addon.toggle.enabled', + // Command COMMAND_SHOW: 'command.show', COMMAND_SHOW_OPEN: 'command.show.open', -- cgit v1.2.3