From e97ffafea319af56c8e446623f33dc32bebba40e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 27 Sep 2017 21:51:39 +0900 Subject: [PATCH] load keymaps from storage --- src/actions/index.js | 1 + src/actions/input.js | 9 ++++++- src/background/index.js | 20 +++++++++++++-- src/background/keys.js | 42 +------------------------------- src/reducers/input.js | 6 +++++ src/settings/default-settings.js | 39 +++++++++++++++++++++++++++++ src/settings/index.js | 19 +++++++++------ 7 files changed, 85 insertions(+), 51 deletions(-) create mode 100644 src/settings/default-settings.js diff --git a/src/actions/index.js b/src/actions/index.js index 977b3c2..7b79864 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -8,4 +8,5 @@ export default { // User input INPUT_KEY_PRESS: 'input.key,press', INPUT_CLEAR_KEYS: 'input.clear.keys', + INPUT_SET_KEYMAPS: 'input.set,keymaps', }; diff --git a/src/actions/input.js b/src/actions/input.js index 07948a1..de6de4e 100644 --- a/src/actions/input.js +++ b/src/actions/input.js @@ -14,4 +14,11 @@ const clearKeys = () => { }; }; -export { keyPress, clearKeys }; +const setKeymaps = (keymaps) => { + return { + type: actions.INPUT_SET_KEYMAPS, + keymaps: keymaps + }; +}; + +export { keyPress, clearKeys, setKeymaps }; diff --git a/src/background/index.js b/src/background/index.js index 9df22fd..7aa9637 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -6,6 +6,7 @@ import * as consoleActions from '../actions/console'; import * as tabActions from '../actions/tab'; import reducers from '../reducers'; import messages from '../messages'; +import DefaultSettings from '../settings/default-settings'; import * as store from '../store'; let prevInput = []; @@ -41,7 +42,7 @@ backgroundStore.subscribe((sender) => { const keyQueueChanged = (state, sender) => { let prefix = keys.asKeymapChars(state.input.keys); - let matched = Object.keys(keys.defaultKeymap).filter((keyStr) => { + let matched = Object.keys(state.input.keymaps).filter((keyStr) => { return keyStr.startsWith(prefix); }); if (matched.length === 0) { @@ -51,7 +52,7 @@ const keyQueueChanged = (state, sender) => { matched.length === 1 && prefix !== matched[0]) { return Promise.resolve(); } - let action = keys.defaultKeymap[matched]; + let action = state.input.keymaps[matched]; backgroundStore.dispatch(operationActions.exec(action, sender.tab), sender); backgroundStore.dispatch(inputActions.clearKeys(), sender); }; @@ -87,3 +88,18 @@ browser.runtime.onMessage.addListener((message, sender) => { backgroundStore.dispatch(consoleActions.showError(e.message), sender); } }); + +const initializeSettings = () => { + browser.storage.local.get('settings').then((value) => { + let settings = {}; + if (value.settings.json) { + settings = JSON.parse(value.settings.json); + } else { + settings = DefaultSettings; + } + let action = inputActions.setKeymaps(settings.keymaps); + backgroundStore.dispatch(action); + }, console.error); +}; + +initializeSettings(); diff --git a/src/background/keys.js b/src/background/keys.js index 5459706..aca050e 100644 --- a/src/background/keys.js +++ b/src/background/keys.js @@ -1,43 +1,3 @@ -import operations from '../operations'; - -const defaultKeymap = { - ':': { type: operations.COMMAND_SHOW }, - 'o': { type: operations.COMMAND_SHOW_OPEN, alter: false }, - 'O': { type: operations.COMMAND_SHOW_OPEN, alter: true }, - 't': { type: operations.COMMAND_SHOW_TABOPEN, alter: false }, - 'T': { type: operations.COMMAND_SHOW_TABOPEN, alter: true }, - 'b': { type: operations.COMMAND_SHOW_BUFFER }, - 'k': { type: operations.SCROLL_LINES, count: -1 }, - 'j': { type: operations.SCROLL_LINES, count: 1 }, - '': { type: operations.SCROLL_LINES, count: -1 }, - '': { type: operations.SCROLL_LINES, count: 1 }, - '': { type: operations.SCROLL_PAGES, count: -0.5 }, - '': { type: operations.SCROLL_PAGES, count: 0.5 }, - '': { type: operations.SCROLL_PAGES, count: -1 }, - '': { type: operations.SCROLL_PAGES, count: 1 }, - 'gg': { type: operations.SCROLL_TOP }, - 'G': { type: operations.SCROLL_BOTTOM }, - '0': { type: operations.SCROLL_HOME }, - '$': { type: operations.SCROLL_END }, - 'd': { type: operations.TAB_CLOSE }, - 'u': { type: operations.TAB_REOPEN }, - 'h': { type: operations.TAB_PREV, count: 1 }, - 'l': { type: operations.TAB_NEXT, count: 1 }, - 'r': { type: operations.TAB_RELOAD, cache: false }, - 'R': { type: operations.TAB_RELOAD, cache: true }, - 'zi': { type: operations.ZOOM_IN }, - 'zo': { type: operations.ZOOM_OUT }, - 'zz': { type: operations.ZOOM_NEUTRAL }, - 'f': { type: operations.FOLLOW_START, newTab: false }, - 'F': { type: operations.FOLLOW_START, newTab: true }, - 'H': { type: operations.NAVIGATE_HISTORY_PREV }, - 'L': { type: operations.NAVIGATE_HISTORY_NEXT }, - '[[': { type: operations.NAVIGATE_LINK_PREV }, - ']]': { type: operations.NAVIGATE_LINK_NEXT }, - 'gu': { type: operations.NAVIGATE_PARENT }, - 'gU': { type: operations.NAVIGATE_ROOT }, -}; - const asKeymapChars = (keys) => { return keys.map((k) => { let c = String.fromCharCode(k.code); @@ -58,4 +18,4 @@ const asCaretChars = (keys) => { }).join(''); }; -export { defaultKeymap, asKeymapChars, asCaretChars }; +export { asKeymapChars, asCaretChars }; diff --git a/src/reducers/input.js b/src/reducers/input.js index eb7ff24..dca26e2 100644 --- a/src/reducers/input.js +++ b/src/reducers/input.js @@ -2,6 +2,7 @@ import actions from '../actions'; const defaultState = { keys: [], + keymaps: {} }; export default function reducer(state = defaultState, action = {}) { @@ -19,6 +20,11 @@ export default function reducer(state = defaultState, action = {}) { return Object.assign({}, state, { keys: [], }); + case actions.INPUT_SET_KEYMAPS: + return Object.assign({}, state, { + keymaps: action.keymaps, + keys: [], + }); default: return state; } diff --git a/src/settings/default-settings.js b/src/settings/default-settings.js new file mode 100644 index 0000000..5a89f58 --- /dev/null +++ b/src/settings/default-settings.js @@ -0,0 +1,39 @@ +export default { + 'keymaps': { + '0': { 'type': 'scroll.home' }, + ':': { 'type': 'command.show' }, + 'o': { 'type': 'command.show.open', 'alter': false }, + 'O': { 'type': 'command.show.open', 'alter': true }, + 't': { 'type': 'command.show.tabopen', 'alter': false }, + 'T': { 'type': 'command.show.tabopen', 'alter': true }, + 'b': { 'type': 'command.show.buffer' }, + 'k': { 'type': 'scroll.lines', 'count': -1 }, + 'j': { 'type': 'scroll.lines', 'count': 1 }, + '': { 'type': 'scroll.lines', 'count': -1 }, + '': { 'type': 'scroll.lines', 'count': 1 }, + '': { 'type': 'scroll.pages', 'count': -0.5 }, + '': { 'type': 'scroll.pages', 'count': 0.5 }, + '': { 'type': 'scroll.pages', 'count': -1 }, + '': { 'type': 'scroll.pages', 'count': 1 }, + 'gg': { 'type': 'scroll.top' }, + 'G': { 'type': 'scroll.bottom' }, + '$': { 'type': 'scroll.end' }, + 'd': { 'type': 'tabs.close' }, + 'u': { 'type': 'tabs.reopen' }, + 'h': { 'type': 'tabs.prev', 'count': 1 }, + 'l': { 'type': 'tabs.next', 'count': 1 }, + 'r': { 'type': 'tabs.reload', 'cache': false }, + 'R': { 'type': 'tabs.reload', 'cache': true }, + 'zi': { 'type': 'zoom.in' }, + 'zo': { 'type': 'zoom.out' }, + 'zz': { 'type': 'zoom.neutral' }, + 'f': { 'type': 'follow.start', 'newTab': false }, + 'F': { 'type': 'follow.start', 'newTab': true }, + 'H': { 'type': 'navigate.history.prev' }, + 'L': { 'type': 'navigate.history.next' }, + '[[': { 'type': 'navigate.link.prev' }, + ']]': { 'type': 'navigate.link.next' }, + 'gu': { 'type': 'navigate.parent' }, + 'gU': { 'type': 'navigate.root' } + } +}; diff --git a/src/settings/index.js b/src/settings/index.js index f2bba32..89a9533 100644 --- a/src/settings/index.js +++ b/src/settings/index.js @@ -1,19 +1,24 @@ import './settings.scss'; +import DefaultSettings from './default-settings'; let form = document.getElementById('vimvixen-settings-form'); form.addEventListener('submit', (e) => { - let value = { - json: e.target.elements['plain-json'].value - }; e.preventDefault(); - browser.storage.local.set(value); + browser.storage.local.set({ + settings: { + json: e.target.elements['plain-json'].value + } + }); }); document.addEventListener('DOMContentLoaded', () => { - browser.storage.local.get().then((value) => { - if (value.json) { - form.elements['plain-json'].value = value.json; + browser.storage.local.get('settings').then((value) => { + if (value.settings.json) { + form.elements['plain-json'].value = value.settings.json; + } else { + form.elements['plain-json'].value = + JSON.stringify(DefaultSettings, null, 2); } }, console.error); });