From e3f95f9ecfd6a824ecc972d86f8f1176fd3c6f8d Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Thu, 28 Sep 2017 21:02:47 +0900 Subject: [PATCH] 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); });