From 4edb0331a1f0e80f754c34ce6686873c13506022 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 25 Sep 2017 21:22:28 +0900 Subject: [PATCH 1/6] clean operation definitions --- src/actions/operation.js | 18 +++++++++--------- src/background/keys.js | 28 ++++++++++++++-------------- src/content/index.js | 4 ++-- src/operations/index.js | 31 +++++++++++++++++++------------ 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/src/actions/operation.js b/src/actions/operation.js index 50329f8..5646c1c 100644 --- a/src/actions/operation.js +++ b/src/actions/operation.js @@ -6,15 +6,15 @@ import * as zooms from '../background/zooms'; const exec = (operation, tab) => { switch (operation.type) { - case operations.TABS_CLOSE: + case operations.TAB_CLOSE: return tabs.closeTab(tab.id); - case operations.TABS_REOPEN: + case operations.TAB_REOPEN: return tabs.reopenTab(); - case operations.TABS_PREV: + case operations.TAB_PREV: return tabs.selectPrevTab(tab.index, operation.count); - case operations.TABS_NEXT: + case operations.TAB_NEXT: return tabs.selectNextTab(tab.index, operation.count); - case operations.TABS_RELOAD: + case operations.TAB_RELOAD: return tabs.reload(tab, operation.cache); case operations.ZOOM_IN: return zooms.zoomIn(); @@ -22,21 +22,21 @@ const exec = (operation, tab) => { return zooms.zoomOut(); case operations.ZOOM_NEUTRAL: return zooms.neutral(); - case operations.COMMAND_OPEN: + case operations.COMMAND_SHOW: return consoleActions.showCommand(''); - case operations.COMMAND_TABS_OPEN: + case operations.COMMAND_SHOW_OPEN: if (operation.alter) { // alter url return consoleActions.showCommand('open ' + tab.url); } return consoleActions.showCommand('open '); - case operations.COMMAND_TABS_NEW: + case operations.COMMAND_SHOW_TABOPEN: if (operation.alter) { // alter url return consoleActions.showCommand('tabopen ' + tab.url); } return consoleActions.showCommand('tabopen '); - case operations.COMMAND_BUFFER: + case operations.COMMAND_SHOW_BUFFER: return consoleActions.showCommand('buffer '); default: return browser.tabs.sendMessage(tab.id, { diff --git a/src/background/keys.js b/src/background/keys.js index e4a8b19..5459706 100644 --- a/src/background/keys.js +++ b/src/background/keys.js @@ -1,12 +1,12 @@ import operations from '../operations'; const defaultKeymap = { - ':': { type: operations.COMMAND_OPEN }, - 'o': { type: operations.COMMAND_TABS_OPEN, alter: false }, - 'O': { type: operations.COMMAND_TABS_OPEN, alter: true }, - 't': { type: operations.COMMAND_TABS_NEW, alter: false }, - 'T': { type: operations.COMMAND_TABS_NEW, alter: true }, - 'b': { type: operations.COMMAND_BUFFER }, + ':': { 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 }, @@ -17,14 +17,14 @@ const defaultKeymap = { '': { type: operations.SCROLL_PAGES, count: 1 }, 'gg': { type: operations.SCROLL_TOP }, 'G': { type: operations.SCROLL_BOTTOM }, - '0': { type: operations.SCROLL_LEFT }, - '$': { type: operations.SCROLL_RIGHT }, - 'd': { type: operations.TABS_CLOSE }, - 'u': { type: operations.TABS_REOPEN }, - 'h': { type: operations.TABS_PREV, count: 1 }, - 'l': { type: operations.TABS_NEXT, count: 1 }, - 'r': { type: operations.TABS_RELOAD, cache: false }, - 'R': { type: operations.TABS_RELOAD, cache: true }, + '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 }, diff --git a/src/content/index.js b/src/content/index.js index 80acd2d..812fbc5 100644 --- a/src/content/index.js +++ b/src/content/index.js @@ -85,9 +85,9 @@ const execOperation = (operation) => { return scrolls.scrollTop(window); case operations.SCROLL_BOTTOM: return scrolls.scrollBottom(window); - case operations.SCROLL_LEFT: + case operations.SCROLL_HOME: return scrolls.scrollLeft(window); - case operations.SCROLL_RIGHT: + case operations.SCROLL_END: return scrolls.scrollRight(window); case operations.FOLLOW_START: return startFollows(operation.newTab); diff --git a/src/operations/index.js b/src/operations/index.js index d6ffc42..b68f59d 100644 --- a/src/operations/index.js +++ b/src/operations/index.js @@ -1,17 +1,22 @@ export default { // Command - COMMAND_OPEN: 'cmd.open', - COMMAND_TABS_OPEN: 'cmd.tabs.open', - COMMAND_TABS_NEW: 'cmd.tabs.new', - COMMAND_BUFFER: 'cmd.buffer', + COMMAND_SHOW: 'command.show', + COMMAND_SHOW_OPEN: 'command.show.open', + COMMAND_SHOW_TABOPEN: 'command.show.tabopen', + COMMAND_SHOW_BUFFER: 'command.show.buffer', + // Scrolls SCROLL_LINES: 'scroll.lines', SCROLL_PAGES: 'scroll.pages', SCROLL_TOP: 'scroll.top', SCROLL_BOTTOM: 'scroll.bottom', - SCROLL_LEFT: 'scroll.left', - SCROLL_RIGHT: 'scroll.right', + SCROLL_HOME: 'scroll.home', + SCROLL_END: 'scroll.end', + + // Follows FOLLOW_START: 'follow.start', + + // Navigations NAVIGATE_HISTORY_PREV: 'navigate.history.prev', NAVIGATE_HISTORY_NEXT: 'navigate.history.next', NAVIGATE_LINK_PREV: 'navigate.link.prev', @@ -19,12 +24,14 @@ export default { NAVIGATE_PARENT: 'navigate.parent', NAVIGATE_ROOT: 'navigate.root', - // Background - TABS_CLOSE: 'tabs.close', - TABS_REOPEN: 'tabs.reopen', - TABS_PREV: 'tabs.prev', - TABS_NEXT: 'tabs.next', - TABS_RELOAD: 'tabs.reload', + // Tabs + TAB_CLOSE: 'tabs.close', + TAB_REOPEN: 'tabs.reopen', + TAB_PREV: 'tabs.prev', + TAB_NEXT: 'tabs.next', + TAB_RELOAD: 'tabs.reload', + + // Zooms ZOOM_IN: 'zoom.in', ZOOM_OUT: 'zoom.out', ZOOM_NEUTRAL: 'zoom.neutral', From 8791ed4e5e5f3418c26a1030f3920849090acc06 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 25 Sep 2017 22:15:56 +0900 Subject: [PATCH 2/6] add simple form --- manifest.json | 10 +++++++--- src/settings/index.js | 19 +++++++++++++++++++ src/settings/settings.html | 15 +++++++++++++++ src/settings/settings.scss | 7 +++++++ webpack.config.js | 6 ++++++ 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/settings/index.js create mode 100644 src/settings/settings.html create mode 100644 src/settings/settings.scss diff --git a/manifest.json b/manifest.json index dfb4ae9..7c60cdd 100644 --- a/manifest.json +++ b/manifest.json @@ -15,11 +15,15 @@ ] }, "permissions": [ + "history", "sessions", - "tabs", - "history" + "storage", + "tabs" ], "web_accessible_resources": [ "build/console.html" - ] + ], + "options_ui": { + "page": "build/settings.html" + } } diff --git a/src/settings/index.js b/src/settings/index.js new file mode 100644 index 0000000..f2bba32 --- /dev/null +++ b/src/settings/index.js @@ -0,0 +1,19 @@ +import './settings.scss'; + +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); +}); + +document.addEventListener('DOMContentLoaded', () => { + browser.storage.local.get().then((value) => { + if (value.json) { + form.elements['plain-json'].value = value.json; + } + }, console.error); +}); diff --git a/src/settings/settings.html b/src/settings/settings.html new file mode 100644 index 0000000..1719d6c --- /dev/null +++ b/src/settings/settings.html @@ -0,0 +1,15 @@ + + + + + + +
+ + + +
+ + + diff --git a/src/settings/settings.scss b/src/settings/settings.scss new file mode 100644 index 0000000..5a0f08c --- /dev/null +++ b/src/settings/settings.scss @@ -0,0 +1,7 @@ +.vimvixen-settings-form { + textarea[name=plain-json] { + font-family: monospace; + width: 100%; + resize: vertical; + } +} diff --git a/webpack.config.js b/webpack.config.js index ba08975..bf121d1 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -7,6 +7,7 @@ const dist = path.resolve(__dirname, 'build'); module.exports = { entry: { index: path.join(src, 'content'), + settings: path.join(src, 'settings'), background: path.join(src, 'background'), console: path.join(src, 'console', 'console.js') }, @@ -46,6 +47,11 @@ module.exports = { template: path.join(src, 'console', 'console.html'), filename: path.join(dist, 'console.html'), inject: false + }), + new HtmlWebpackPlugin({ + template: path.join(src, 'settings', 'settings.html'), + filename: path.join(dist, 'settings.html'), + inject: false }) ] }; From 8ba490ea1179fee73407815f69cdcea6e25c9281 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 30 Sep 2017 13:24:24 +0900 Subject: [PATCH 3/6] fix settings form --- src/settings/settings.html | 9 ++++++--- src/settings/settings.scss | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/settings/settings.html b/src/settings/settings.html index 1719d6c..99d6c6b 100644 --- a/src/settings/settings.html +++ b/src/settings/settings.html @@ -4,10 +4,13 @@ +

Configure

+ +

Home page

- - + + +
diff --git a/src/settings/settings.scss b/src/settings/settings.scss index 5a0f08c..5707c8a 100644 --- a/src/settings/settings.scss +++ b/src/settings/settings.scss @@ -2,6 +2,7 @@ textarea[name=plain-json] { font-family: monospace; width: 100%; + min-height: 64ex; resize: vertical; } } From e97ffafea319af56c8e446623f33dc32bebba40e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 27 Sep 2017 21:51:39 +0900 Subject: [PATCH 4/6] 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); }); From e3f95f9ecfd6a824ecc972d86f8f1176fd3c6f8d Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 28 Sep 2017 21:02:47 +0900 Subject: [PATCH 5/6] Initialize default settings on background --- src/background/default-settings.js | 39 ++++++++++++++++++++++++++++++ src/background/index.js | 9 +++++-- src/settings/default-settings.js | 39 ------------------------------ src/settings/index.js | 27 ++++++++------------- 4 files changed, 56 insertions(+), 58 deletions(-) create mode 100644 src/background/default-settings.js delete mode 100644 src/settings/default-settings.js diff --git a/src/background/default-settings.js b/src/background/default-settings.js new file mode 100644 index 0000000..ea6318b --- /dev/null +++ b/src/background/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/background/index.js b/src/background/index.js index 7aa9637..89d519c 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -6,7 +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 DefaultSettings from './default-settings'; import * as store from '../store'; let prevInput = []; @@ -92,10 +92,15 @@ browser.runtime.onMessage.addListener((message, sender) => { const initializeSettings = () => { browser.storage.local.get('settings').then((value) => { let settings = {}; - if (value.settings.json) { + if (value.settings && value.settings.json) { settings = JSON.parse(value.settings.json); } else { settings = DefaultSettings; + browser.storage.local.set({ + settings: { + json: DefaultSettings + } + }); } let action = inputActions.setKeymaps(settings.keymaps); backgroundStore.dispatch(action); diff --git a/src/settings/default-settings.js b/src/settings/default-settings.js deleted file mode 100644 index 5a89f58..0000000 --- a/src/settings/default-settings.js +++ /dev/null @@ -1,39 +0,0 @@ -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 89a9533..e075385 100644 --- a/src/settings/index.js +++ b/src/settings/index.js @@ -1,24 +1,17 @@ import './settings.scss'; -import DefaultSettings from './default-settings'; -let form = document.getElementById('vimvixen-settings-form'); - -form.addEventListener('submit', (e) => { - e.preventDefault(); - browser.storage.local.set({ - settings: { - json: e.target.elements['plain-json'].value - } +document.addEventListener('DOMContentLoaded', () => { + let form = document.getElementById('vimvixen-settings-form'); + form.addEventListener('submit', (e) => { + e.preventDefault(); + browser.storage.local.set({ + settings: { + json: e.target.elements['plain-json'].value + } + }); }); -}); -document.addEventListener('DOMContentLoaded', () => { 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); - } + form.elements['plain-json'].value = value.settings.json; }, console.error); }); From c5efeda78cdf39e3eda1eabc5f89b601b1fcdb6e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 30 Sep 2017 13:42:04 +0900 Subject: [PATCH 6/6] reload settings on settings changed --- src/background/index.js | 27 +++++++++++---------------- src/messages/index.js | 4 +++- src/settings/index.js | 5 +++++ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/background/index.js b/src/background/index.js index 89d519c..5ae967c 100644 --- a/src/background/index.js +++ b/src/background/index.js @@ -6,7 +6,6 @@ import * as consoleActions from '../actions/console'; import * as tabActions from '../actions/tab'; import reducers from '../reducers'; import messages from '../messages'; -import DefaultSettings from './default-settings'; import * as store from '../store'; let prevInput = []; @@ -57,6 +56,14 @@ const keyQueueChanged = (state, sender) => { backgroundStore.dispatch(inputActions.clearKeys(), sender); }; +const reloadSettings = () => { + browser.storage.local.get('settings').then((value) => { + let settings = JSON.parse(value.settings.json); + let action = inputActions.setKeymaps(settings.keymaps); + backgroundStore.dispatch(action); + }, console.error); +}; + const handleMessage = (message, sender) => { switch (message.type) { case messages.KEYDOWN: @@ -78,6 +85,8 @@ const handleMessage = (message, sender) => { case messages.CONSOLE_CHANGEED: return backgroundStore.dispatch( commandActions.complete(message.text), sender); + case messages.SETTINGS_RELOAD: + return reloadSettings(); } }; @@ -90,21 +99,7 @@ browser.runtime.onMessage.addListener((message, sender) => { }); const initializeSettings = () => { - browser.storage.local.get('settings').then((value) => { - let settings = {}; - if (value.settings && value.settings.json) { - settings = JSON.parse(value.settings.json); - } else { - settings = DefaultSettings; - browser.storage.local.set({ - settings: { - json: DefaultSettings - } - }); - } - let action = inputActions.setKeymaps(settings.keymaps); - backgroundStore.dispatch(action); - }, console.error); + reloadSettings(); }; initializeSettings(); diff --git a/src/messages/index.js b/src/messages/index.js index 4e34436..df9fba2 100644 --- a/src/messages/index.js +++ b/src/messages/index.js @@ -8,5 +8,7 @@ export default { KEYDOWN: 'keydown', - OPEN_URL: 'open.url' + OPEN_URL: 'open.url', + + SETTINGS_RELOAD: 'settings.reload', }; diff --git a/src/settings/index.js b/src/settings/index.js index e075385..2ed060c 100644 --- a/src/settings/index.js +++ b/src/settings/index.js @@ -1,4 +1,5 @@ import './settings.scss'; +import messages from '../messages'; document.addEventListener('DOMContentLoaded', () => { let form = document.getElementById('vimvixen-settings-form'); @@ -8,6 +9,10 @@ document.addEventListener('DOMContentLoaded', () => { settings: { json: e.target.elements['plain-json'].value } + }).then(() => { + return browser.runtime.sendMessage({ + type: messages.SETTINGS_RELOAD + }); }); });