load keymaps from storage

jh-changes
Shin'ya Ueoka 7 years ago
parent 8ba490ea11
commit e97ffafea3
  1. 1
      src/actions/index.js
  2. 9
      src/actions/input.js
  3. 20
      src/background/index.js
  4. 42
      src/background/keys.js
  5. 6
      src/reducers/input.js
  6. 39
      src/settings/default-settings.js
  7. 19
      src/settings/index.js

@ -8,4 +8,5 @@ export default {
// User input // User input
INPUT_KEY_PRESS: 'input.key,press', INPUT_KEY_PRESS: 'input.key,press',
INPUT_CLEAR_KEYS: 'input.clear.keys', INPUT_CLEAR_KEYS: 'input.clear.keys',
INPUT_SET_KEYMAPS: 'input.set,keymaps',
}; };

@ -14,4 +14,11 @@ const clearKeys = () => {
}; };
}; };
export { keyPress, clearKeys }; const setKeymaps = (keymaps) => {
return {
type: actions.INPUT_SET_KEYMAPS,
keymaps: keymaps
};
};
export { keyPress, clearKeys, setKeymaps };

@ -6,6 +6,7 @@ import * as consoleActions from '../actions/console';
import * as tabActions from '../actions/tab'; import * as tabActions from '../actions/tab';
import reducers from '../reducers'; import reducers from '../reducers';
import messages from '../messages'; import messages from '../messages';
import DefaultSettings from '../settings/default-settings';
import * as store from '../store'; import * as store from '../store';
let prevInput = []; let prevInput = [];
@ -41,7 +42,7 @@ backgroundStore.subscribe((sender) => {
const keyQueueChanged = (state, sender) => { const keyQueueChanged = (state, sender) => {
let prefix = keys.asKeymapChars(state.input.keys); 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); return keyStr.startsWith(prefix);
}); });
if (matched.length === 0) { if (matched.length === 0) {
@ -51,7 +52,7 @@ const keyQueueChanged = (state, sender) => {
matched.length === 1 && prefix !== matched[0]) { matched.length === 1 && prefix !== matched[0]) {
return Promise.resolve(); return Promise.resolve();
} }
let action = keys.defaultKeymap[matched]; let action = state.input.keymaps[matched];
backgroundStore.dispatch(operationActions.exec(action, sender.tab), sender); backgroundStore.dispatch(operationActions.exec(action, sender.tab), sender);
backgroundStore.dispatch(inputActions.clearKeys(), sender); backgroundStore.dispatch(inputActions.clearKeys(), sender);
}; };
@ -87,3 +88,18 @@ browser.runtime.onMessage.addListener((message, sender) => {
backgroundStore.dispatch(consoleActions.showError(e.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();

@ -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 },
'<C-E>': { type: operations.SCROLL_LINES, count: -1 },
'<C-Y>': { type: operations.SCROLL_LINES, count: 1 },
'<C-U>': { type: operations.SCROLL_PAGES, count: -0.5 },
'<C-D>': { type: operations.SCROLL_PAGES, count: 0.5 },
'<C-B>': { type: operations.SCROLL_PAGES, count: -1 },
'<C-F>': { 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) => { const asKeymapChars = (keys) => {
return keys.map((k) => { return keys.map((k) => {
let c = String.fromCharCode(k.code); let c = String.fromCharCode(k.code);
@ -58,4 +18,4 @@ const asCaretChars = (keys) => {
}).join(''); }).join('');
}; };
export { defaultKeymap, asKeymapChars, asCaretChars }; export { asKeymapChars, asCaretChars };

@ -2,6 +2,7 @@ import actions from '../actions';
const defaultState = { const defaultState = {
keys: [], keys: [],
keymaps: {}
}; };
export default function reducer(state = defaultState, action = {}) { export default function reducer(state = defaultState, action = {}) {
@ -19,6 +20,11 @@ export default function reducer(state = defaultState, action = {}) {
return Object.assign({}, state, { return Object.assign({}, state, {
keys: [], keys: [],
}); });
case actions.INPUT_SET_KEYMAPS:
return Object.assign({}, state, {
keymaps: action.keymaps,
keys: [],
});
default: default:
return state; return state;
} }

@ -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 },
'<C-E>': { 'type': 'scroll.lines', 'count': -1 },
'<C-Y>': { 'type': 'scroll.lines', 'count': 1 },
'<C-U>': { 'type': 'scroll.pages', 'count': -0.5 },
'<C-D>': { 'type': 'scroll.pages', 'count': 0.5 },
'<C-B>': { 'type': 'scroll.pages', 'count': -1 },
'<C-F>': { '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' }
}
};

@ -1,19 +1,24 @@
import './settings.scss'; import './settings.scss';
import DefaultSettings from './default-settings';
let form = document.getElementById('vimvixen-settings-form'); let form = document.getElementById('vimvixen-settings-form');
form.addEventListener('submit', (e) => { form.addEventListener('submit', (e) => {
let value = {
json: e.target.elements['plain-json'].value
};
e.preventDefault(); e.preventDefault();
browser.storage.local.set(value); browser.storage.local.set({
settings: {
json: e.target.elements['plain-json'].value
}
});
}); });
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
browser.storage.local.get().then((value) => { browser.storage.local.get('settings').then((value) => {
if (value.json) { if (value.settings.json) {
form.elements['plain-json'].value = value.json; form.elements['plain-json'].value = value.settings.json;
} else {
form.elements['plain-json'].value =
JSON.stringify(DefaultSettings, null, 2);
} }
}, console.error); }, console.error);
}); });