From 4e5ddc1d57ba4f42314eb49ae57a9b67950be596 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 22 Nov 2017 22:38:34 +0900 Subject: Add keybindings form --- src/settings/actions/setting.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/settings/actions') diff --git a/src/settings/actions/setting.js b/src/settings/actions/setting.js index c1b27c8..fa158a3 100644 --- a/src/settings/actions/setting.js +++ b/src/settings/actions/setting.js @@ -4,10 +4,10 @@ import DefaultSettings from 'shared/default-settings'; const load = () => { return browser.storage.local.get('settings').then(({ settings }) => { - if (settings) { - return set(settings); + if (!settings) { + return set(DefaultSettings); } - return set(DefaultSettings); + return set(Object.assign({}, DefaultSettings, settings)); }, console.error); }; @@ -28,6 +28,7 @@ const set = (settings) => { type: actions.SETTING_SET_SETTINGS, source: settings.source, json: settings.json, + form: settings.form, value: JSON.parse(settings.json), }; }; -- cgit v1.2.3 From f27d21908a2b402e3274288d4411a48160f38e4c Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 25 Nov 2017 18:03:15 +0900 Subject: load settiongs from form --- src/settings/actions/setting.js | 12 ++- src/settings/components/form/keymaps-form.jsx | 38 ++++---- src/shared/default-settings.js | 131 -------------------------- src/shared/settings/default.js | 131 ++++++++++++++++++++++++++ src/shared/settings/values.js | 35 +++++++ 5 files changed, 195 insertions(+), 152 deletions(-) delete mode 100644 src/shared/default-settings.js create mode 100644 src/shared/settings/default.js create mode 100644 src/shared/settings/values.js (limited to 'src/settings/actions') diff --git a/src/settings/actions/setting.js b/src/settings/actions/setting.js index fa158a3..1d4ef34 100644 --- a/src/settings/actions/setting.js +++ b/src/settings/actions/setting.js @@ -1,6 +1,7 @@ import actions from 'settings/actions'; import messages from 'shared/messages'; -import DefaultSettings from 'shared/default-settings'; +import DefaultSettings from 'shared/settings/default'; +import * as settingsValues from 'shared/settings/values'; const load = () => { return browser.storage.local.get('settings').then(({ settings }) => { @@ -24,12 +25,19 @@ const save = (settings) => { }; const set = (settings) => { + let value = JSON.parse(DefaultSettings.json); + if (settings.source === 'json') { + value = settingsValues.fromJson(settings.json); + } else if (settings.source === 'form') { + value = settingsValues.fromForm(settings.form); + } + return { type: actions.SETTING_SET_SETTINGS, source: settings.source, json: settings.json, form: settings.form, - value: JSON.parse(settings.json), + value, }; }; diff --git a/src/settings/components/form/keymaps-form.jsx b/src/settings/components/form/keymaps-form.jsx index a112979..a827e4e 100644 --- a/src/settings/components/form/keymaps-form.jsx +++ b/src/settings/components/form/keymaps-form.jsx @@ -4,29 +4,29 @@ import Input from '../ui/input'; const KeyMapFields = [ [ - ['scroll.vertically?{count:-1}', 'Scroll down'], - ['scroll.vertically?{count:1}', 'Scroll up'], - ['scroll.horizonally?{count:-1}', 'Scroll left'], - ['scroll.horizonally?{count:1}', 'Scroll right'], + ['scroll.vertically?{"count":1}', 'Scroll down'], + ['scroll.vertically?{"count":-1}', 'Scroll up'], + ['scroll.horizonally?{"count":-1}', 'Scroll left'], + ['scroll.horizonally?{"count":1}', 'Scroll right'], ['scroll.home', 'Scroll leftmost'], ['scroll.end', 'Scroll last'], - ['scroll.pages?{count:-0.5}', 'Scroll up by half of screen'], - ['scroll.pages?{count:0.5}', 'Scroll up by half of screen'], - ['scroll.pages?{count:-1}', 'Scroll up by a screen'], - ['scroll.pages?{count:1}', 'Scroll up by a screen'], + ['scroll.pages?{"count":-0.5}', 'Scroll up by half of screen'], + ['scroll.pages?{"count":0.5}', 'Scroll up by half of screen'], + ['scroll.pages?{"count":-1}', 'Scroll up by a screen'], + ['scroll.pages?{"count":1}', 'Scroll up by a screen'], ], [ ['tabs.close', 'Close a tab'], ['tabs.reopen', 'Reopen closed tab'], - ['tabs.next?{count:1}', 'Select next Tab'], - ['tabs.prev?{count:1}', 'Select prev Tab'], + ['tabs.next?{"count":1}', 'Select next Tab'], + ['tabs.prev?{"count":1}', 'Select prev Tab'], ['tabs.first', 'Select first tab'], ['tabs.last', 'Select last tab'], - ['tabs.reload?{cache:true}', 'Reload current tab'], + ['tabs.reload?{"cache":true}', 'Reload current tab'], ['tabs.pin.toggle', 'Toggle pinned state'], ['tabs.duplicate', 'Dupplicate a tab'], ], [ - ['follow.start?{newTab:false}', 'Follow a link'], - ['follow.start?{newTab:true}', 'Follow a link in new tab'], + ['follow.start?{"newTab":false}', 'Follow a link'], + ['follow.start?{"newTab":true}', 'Follow a link in new tab'], ['navigate.histories.prev', 'Go back in histories'], ['navigate.histories.next', 'Go forward in histories'], ['navigate.link.next', 'Open next link'], @@ -39,12 +39,12 @@ const KeyMapFields = [ ['find.prev', 'Find previous word'], ], [ ['command.show', 'Open console'], - ['command.show.open?{alter:false}', 'Open URL'], - ['command.show.open?{alter:true}', 'Alter URL'], - ['command.show.tabopen?{alter:false}', 'Open URL in new Tab'], - ['command.show.tabopen?{alter:true}', 'Alter URL in new Tab'], - ['command.show.winopen?{alter:false}', 'Open URL in new window'], - ['command.show.winopen?{alter:true}', 'Alter URL in new window'], + ['command.show.open?{"alter":false}', 'Open URL'], + ['command.show.open?{"alter":true}', 'Alter URL'], + ['command.show.tabopen?{"alter":false}', 'Open URL in new Tab'], + ['command.show.tabopen?{"alter":true}', 'Alter URL in new Tab'], + ['command.show.winopen?{"alter":false}', 'Open URL in new window'], + ['command.show.winopen?{"alter":true}', 'Alter URL in new window'], ['command.show.buffer', 'Open buffer command'], ], [ ['addon.toggle.enabled', 'Enable or disable'], diff --git a/src/shared/default-settings.js b/src/shared/default-settings.js deleted file mode 100644 index 218390b..0000000 --- a/src/shared/default-settings.js +++ /dev/null @@ -1,131 +0,0 @@ -export default { - source: 'json', - json: `{ - "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 }, - "w": { "type": "command.show.winopen", "alter": false }, - "W": { "type": "command.show.winopen", "alter": true }, - "b": { "type": "command.show.buffer" }, - "k": { "type": "scroll.vertically", "count": -1 }, - "j": { "type": "scroll.vertically", "count": 1 }, - "h": { "type": "scroll.horizonally", "count": -1 }, - "l": { "type": "scroll.horizonally", "count": 1 }, - "": { "type": "scroll.vertically", "count": -1 }, - "": { "type": "scroll.vertically", "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" }, - "K": { "type": "tabs.prev", "count": 1 }, - "J": { "type": "tabs.next", "count": 1 }, - "g0": { "type": "tabs.first" }, - "g$": { "type": "tabs.last" }, - "r": { "type": "tabs.reload", "cache": false }, - "R": { "type": "tabs.reload", "cache": true }, - "zp": { "type": "tabs.pin.toggle" }, - "zd": { "type": "tabs.duplicate" }, - "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" }, - "y": { "type": "urls.yank" }, - "/": { "type": "find.start" }, - "n": { "type": "find.next" }, - "N": { "type": "find.prev" }, - "": { "type": "addon.toggle.enabled" } - }, - "search": { - "default": "google", - "engines": { - "google": "https://google.com/search?q={}", - "yahoo": "https://search.yahoo.com/search?p={}", - "bing": "https://www.bing.com/search?q={}", - "duckduckgo": "https://duckduckgo.com/?q={}", - "twitter": "https://twitter.com/search?q={}", - "wikipedia": "https://en.wikipedia.org/w/index.php?search={}" - } - } -}`, - - 'form': { - 'keymaps': { - 'scroll.vertically?{count:-1}': 'j', - 'scroll.vertically?{count:1}': 'k', - 'scroll.horizonally?{count:-1}': 'h', - 'scroll.horizonally?{count:1}': 'l', - 'scroll.home': '0', - 'scroll.end': '$', - 'scroll.pages?{count:-0.5}': '', - 'scroll.pages?{count:0.5}': '', - 'scroll.pages?{count:-1}': '', - 'scroll.pages?{count:1}': '', - - 'tabs.close': 'd', - 'tabs.reopen': 'u', - 'tabs.next?{count:1}': 'J', - 'tabs.prev?{count:1}': 'K', - 'tabs.first': 'g0', - 'tabs.last': 'g$', - 'tabs.reload?{cache:true}': 'r', - 'tabs.pin.toggle': 'zp', - 'tabs.duplicate': 'zd', - - 'follow.start?{newTab:false}': 'f', - 'follow.start?{newTab:true}': 'F', - 'navigate.histories.prev': 'H', - 'navigate.histories.next': 'L', - 'navigate.link.next': ']]', - 'navigate.link.prev': '[[', - 'navigate.parent': 'gu', - 'navigate.root': 'gU', - - 'find.start': '/', - 'find.next': 'n', - 'find.prev': 'N', - - 'command.show': ':', - 'command.show.open?{alter:false}': 'o', - 'command.show.open?{alter:true}': 'O', - 'command.show.tabopen?{alter:false}': 't', - 'command.show.tabopen?{alter:true}': 'T', - 'command.show.winopen?{alter:false}': 'w', - 'command.show.winopen?{alter:true}': 'W', - 'command.show.buffer': 'b', - - 'addon.toggle.enabled': '', - 'urls.yank': 'y', - 'zoom.in': 'zi', - 'zoom.out': 'zo', - 'zoom.neutral': 'zz', - }, - 'search': { - 'default': 'google', - 'engines': [ - ['google', 'https,//google.com/search?q={}'], - ['yahoo', 'https,//search.yahoo.com/search?p={}'], - ['bing', 'https,//www.bing.com/search?q={}'], - ['duckduckgo', 'https,//duckduckgo.com/?q={}'], - ['twitter', 'https,//twitter.com/search?q={}'], - ['wikipedia', 'https,//en.wikipedia.org/w/index.php?search={}'], - ] - }, - 'blacklist': [], - } -}; diff --git a/src/shared/settings/default.js b/src/shared/settings/default.js new file mode 100644 index 0000000..fc259b8 --- /dev/null +++ b/src/shared/settings/default.js @@ -0,0 +1,131 @@ +export default { + source: 'json', + json: `{ + "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 }, + "w": { "type": "command.show.winopen", "alter": false }, + "W": { "type": "command.show.winopen", "alter": true }, + "b": { "type": "command.show.buffer" }, + "k": { "type": "scroll.vertically", "count": -1 }, + "j": { "type": "scroll.vertically", "count": 1 }, + "h": { "type": "scroll.horizonally", "count": -1 }, + "l": { "type": "scroll.horizonally", "count": 1 }, + "": { "type": "scroll.vertically", "count": -1 }, + "": { "type": "scroll.vertically", "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" }, + "K": { "type": "tabs.prev", "count": 1 }, + "J": { "type": "tabs.next", "count": 1 }, + "g0": { "type": "tabs.first" }, + "g$": { "type": "tabs.last" }, + "r": { "type": "tabs.reload", "cache": false }, + "R": { "type": "tabs.reload", "cache": true }, + "zp": { "type": "tabs.pin.toggle" }, + "zd": { "type": "tabs.duplicate" }, + "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" }, + "y": { "type": "urls.yank" }, + "/": { "type": "find.start" }, + "n": { "type": "find.next" }, + "N": { "type": "find.prev" }, + "": { "type": "addon.toggle.enabled" } + }, + "search": { + "default": "google", + "engines": { + "google": "https://google.com/search?q={}", + "yahoo": "https://search.yahoo.com/search?p={}", + "bing": "https://www.bing.com/search?q={}", + "duckduckgo": "https://duckduckgo.com/?q={}", + "twitter": "https://twitter.com/search?q={}", + "wikipedia": "https://en.wikipedia.org/w/index.php?search={}" + } + } +}`, + + 'form': { + 'keymaps': { + 'scroll.vertically?{"count":1}': 'j', + 'scroll.vertically?{"count":-1}': 'k', + 'scroll.horizonally?{"count":-1}': 'h', + 'scroll.horizonally?{"count":1}': 'l', + 'scroll.home': '0', + 'scroll.end': '$', + 'scroll.pages?{"count":-0.5}': '', + 'scroll.pages?{"count":0.5}': '', + 'scroll.pages?{"count":-1}': '', + 'scroll.pages?{"count":1}': '', + + 'tabs.close': 'd', + 'tabs.reopen': 'u', + 'tabs.next?{"count":1}': 'J', + 'tabs.prev?{"count":1}': 'K', + 'tabs.first': 'g0', + 'tabs.last': 'g$', + 'tabs.reload?{"cache":true}': 'r', + 'tabs.pin.toggle': 'zp', + 'tabs.duplicate': 'zd', + + 'follow.start?{"newTab":false}': 'f', + 'follow.start?{"newTab":true}': 'F', + 'navigate.histories.prev': 'H', + 'navigate.histories.next': 'L', + 'navigate.link.next': ']]', + 'navigate.link.prev': '[[', + 'navigate.parent': 'gu', + 'navigate.root': 'gU', + + 'find.start': '/', + 'find.next': 'n', + 'find.prev': 'N', + + 'command.show': ':', + 'command.show.open?{"alter":false}': 'o', + 'command.show.open?{"alter":true}': 'O', + 'command.show.tabopen?{"alter":false}': 't', + 'command.show.tabopen?{"alter":true}': 'T', + 'command.show.winopen?{"alter":false}': 'w', + 'command.show.winopen?{"alter":true}': 'W', + 'command.show.buffer': 'b', + + 'addon.toggle.enabled': '', + 'urls.yank': 'y', + 'zoom.in': 'zi', + 'zoom.out': 'zo', + 'zoom.neutral': 'zz', + }, + 'search': { + 'default': 'google', + 'engines': [ + ['google', 'https,//google.com/search?q={}'], + ['yahoo', 'https,//search.yahoo.com/search?p={}'], + ['bing', 'https,//www.bing.com/search?q={}'], + ['duckduckgo', 'https,//duckduckgo.com/?q={}'], + ['twitter', 'https,//twitter.com/search?q={}'], + ['wikipedia', 'https,//en.wikipedia.org/w/index.php?search={}'], + ] + }, + 'blacklist': [], + } +}; diff --git a/src/shared/settings/values.js b/src/shared/settings/values.js new file mode 100644 index 0000000..d86cfdc --- /dev/null +++ b/src/shared/settings/values.js @@ -0,0 +1,35 @@ +const operationFromName = (name) => { + let [type, argStr] = name.split('?'); + let args = {}; + if (argStr) { + args = JSON.parse(argStr); + } + return Object.assign({ type }, args); +}; + +const fromJson = (json) => { + return JSON.parse(json); +}; + +const fromForm = (form) => { + let keymaps = {}; + for (let name of Object.keys(form.keymaps)) { + let keys = form.keymaps[name]; + keymaps[keys] = operationFromName(name); + } + + let engines = {}; + for (let { name, url } of form.search.engines) { + engines[name] = url; + } + let search = { + default: form.search.default, + engines, + }; + + let blacklist = form.blacklist; + + return { keymaps, search, blacklist }; +}; + +export { fromJson, fromForm }; -- cgit v1.2.3 From d15de42a75ee0f722e8779af1ecff8b51e59c56d Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 25 Nov 2017 22:14:28 +0900 Subject: settings helpers --- .eslintrc | 1 + src/settings/actions/setting.js | 4 +- src/shared/settings/values.js | 95 +++++++++++++++++++++++++----- test/shared/settings/values.test.js | 112 ++++++++++++++++++++++++++++++++++++ 4 files changed, 195 insertions(+), 17 deletions(-) create mode 100644 test/shared/settings/values.test.js (limited to 'src/settings/actions') diff --git a/.eslintrc b/.eslintrc index 6717889..ab61dc7 100644 --- a/.eslintrc +++ b/.eslintrc @@ -44,6 +44,7 @@ "no-plusplus": "off", "no-ternary": "off", "no-undefined": "off", + "no-undef-init": "off", "no-unused-vars": ["error", { "varsIgnorePattern": "h" }], "no-use-before-define": "off", "no-warning-comments": "off", diff --git a/src/settings/actions/setting.js b/src/settings/actions/setting.js index 1d4ef34..1d01fda 100644 --- a/src/settings/actions/setting.js +++ b/src/settings/actions/setting.js @@ -27,9 +27,9 @@ const save = (settings) => { const set = (settings) => { let value = JSON.parse(DefaultSettings.json); if (settings.source === 'json') { - value = settingsValues.fromJson(settings.json); + value = settingsValues.valueFromJson(settings.json); } else if (settings.source === 'form') { - value = settingsValues.fromForm(settings.form); + value = settingsValues.valueFromForm(settings.form); } return { diff --git a/src/shared/settings/values.js b/src/shared/settings/values.js index d86cfdc..4482fbb 100644 --- a/src/shared/settings/values.js +++ b/src/shared/settings/values.js @@ -1,4 +1,6 @@ -const operationFromName = (name) => { +import DefaultSettings from './default'; + +const operationFromFormName = (name) => { let [type, argStr] = name.split('?'); let args = {}; if (argStr) { @@ -7,29 +9,92 @@ const operationFromName = (name) => { return Object.assign({ type }, args); }; -const fromJson = (json) => { +const operationToFormName = (op) => { + let type = op.type; + let args = Object.assign({}, op); + delete args.type; + + if (Object.keys(args).length === 0) { + return type; + } + return op.type + '?' + JSON.stringify(args); +}; + +const valueFromJson = (json) => { return JSON.parse(json); }; -const fromForm = (form) => { - let keymaps = {}; - for (let name of Object.keys(form.keymaps)) { - let keys = form.keymaps[name]; - keymaps[keys] = operationFromName(name); +const valueFromForm = (form) => { + let keymaps = undefined; + if (form.keymaps) { + keymaps = {}; + for (let name of Object.keys(form.keymaps)) { + let keys = form.keymaps[name]; + keymaps[keys] = operationFromFormName(name); + } } - let engines = {}; - for (let { name, url } of form.search.engines) { - engines[name] = url; + let search = undefined; + if (form.search) { + search = { default: form.search.default }; + + if (form.search.engines) { + search.engines = {}; + for (let [name, url] of form.search.engines) { + search.engines[name] = url; + } + } } - let search = { - default: form.search.default, - engines, - }; let blacklist = form.blacklist; return { keymaps, search, blacklist }; }; -export { fromJson, fromForm }; +const jsonFromValue = (value) => { + return JSON.stringify(value, undefined, 2); +}; + +const formFromValue = (value) => { + + let keymaps = undefined; + if (value.keymaps) { + let allowedOps = new Set(Object.keys(DefaultSettings.form.keymaps)); + + keymaps = {}; + for (let keys of Object.keys(value.keymaps)) { + let op = operationToFormName(value.keymaps[keys]); + if (allowedOps.has(op)) { + keymaps[op] = keys; + } + } + } + + let search = undefined; + if (value.search) { + search = { default: value.search.default }; + if (value.search.engines) { + search.engines = Object.keys(value.search.engines).map((name) => { + return [name, value.search.engines[name]]; + }); + } + } + + let blacklist = value.blacklist; + + return { keymaps, search, blacklist }; +}; + +const jsonFromForm = (form) => { + return jsonFromValue(valueFromForm(form)); +}; + +const formFromJson = (json) => { + let value = valueFromJson(json); + return formFromValue(value); +}; + +export { + valueFromJson, valueFromForm, jsonFromValue, formFromValue, + jsonFromForm, formFromJson +}; diff --git a/test/shared/settings/values.test.js b/test/shared/settings/values.test.js new file mode 100644 index 0000000..2c222b6 --- /dev/null +++ b/test/shared/settings/values.test.js @@ -0,0 +1,112 @@ +import { expect } from 'chai'; +import * as values from 'shared/settings/values'; + +describe("settings values", () => { + describe('valueFromJson', () => { + it('return object from json string', () => { + let json = `{ + "keymaps": { "0": {"type": "scroll.home"}}, + "search": { "default": "google", "engines": { "google": "https://google.com/search?q={}" }}, + "blacklist": [ "*.slack.com"] + }`; + let value = values.valueFromJson(json); + + expect(value.keymaps).to.deep.equal({ 0: {type: "scroll.home"}}); + expect(value.search).to.deep.equal({ default: "google", engines: { google: "https://google.com/search?q={}"} }); + expect(value.blacklist).to.deep.equal(["*.slack.com"]); + }); + }); + + describe('valueFromForm', () => { + it('returns value from form', () => { + let form = { + keymaps: { + 'scroll.vertically?{"count":1}': 'j', + 'scroll.home': '0', + }, + search: { + default: 'google', + engines: [['google', 'https://google.com/search?q={}']], + }, + blacklist: ['*.slack.com'], + }; + let value = values.valueFromForm(form); + + expect(value.keymaps).to.have.deep.property('j', { type: "scroll.vertically", count: 1 }); + expect(value.keymaps).to.have.deep.property('0', { type: "scroll.home" }); + expect(JSON.stringify(value.search)).to.deep.equal(JSON.stringify({ default: "google", engines: { google: "https://google.com/search?q={}"} })); + expect(value.search).to.deep.equal({ default: "google", engines: { google: "https://google.com/search?q={}"} }); + expect(value.blacklist).to.deep.equal(["*.slack.com"]); + }); + + it('convert from empty form', () => { + let form = {}; + let value = values.valueFromForm(form); + expect(value).to.not.have.key('keymaps'); + expect(value).to.not.have.key('search'); + expect(value).to.not.have.key('blacklist'); + }); + + it('override keymaps', () => { + let form = { + keymaps: { + 'scroll.vertically?{"count":1}': 'j', + 'scroll.vertically?{"count":-1}': 'j', + } + }; + let value = values.valueFromForm(form); + + expect(value.keymaps).to.have.key('j'); + }); + + it('override search engine', () => { + let form = { + search: { + default: 'google', + engines: [ + ['google', 'https://google.com/search?q={}'], + ['google', 'https://google.co.jp/search?q={}'], + ] + } + }; + let value = values.valueFromForm(form); + + expect(value.search.engines).to.have.property('google', 'https://google.co.jp/search?q={}'); + }); + }); + + describe('jsonFromValue', () => { + }); + + describe('formFromValue', () => { + it('convert empty value to form', () => { + let value = {}; + let form = values.formFromValue(value); + + expect(value).to.not.have.key('keymaps'); + expect(value).to.not.have.key('search'); + expect(value).to.not.have.key('blacklist'); + }); + + it('convert value to form', () => { + let value = { + keymaps: { + j: { type: 'scroll.vertically', count: 1 }, + JJ: { type: 'scroll.vertically', count: 100 }, + 0: { type: 'scroll.home' }, + }, + search: { default: 'google', engines: { google: 'https://google.com/search?q={}' }}, + blacklist: [ '*.slack.com'] + }; + let form = values.formFromValue(value); + + expect(form.keymaps).to.have.property('scroll.vertically?{"count":1}', 'j'); + expect(form.keymaps).to.have.property('scroll.home', '0'); + expect(Object.keys(form.keymaps)).to.have.lengthOf(2); + expect(form.search).to.have.property('default', 'google'); + expect(form.search).to.have.deep.property('engines', [['google', 'https://google.com/search?q={}']]); + expect(form.blacklist).to.have.lengthOf(1); + expect(form.blacklist).to.include('*.slack.com'); + }); + }); +}); -- cgit v1.2.3