add setting actions in content
This commit is contained in:
parent
7639e99b75
commit
c6eb5553d0
10 changed files with 74 additions and 25 deletions
|
@ -1,12 +1,15 @@
|
||||||
export default {
|
export default {
|
||||||
// User input
|
// Enable/disable
|
||||||
ADDON_ENABLE: 'addon.enable',
|
ADDON_ENABLE: 'addon.enable',
|
||||||
ADDON_DISABLE: 'addon.disable',
|
ADDON_DISABLE: 'addon.disable',
|
||||||
ADDON_TOGGLE_ENABLED: 'addon.toggle.enabled',
|
ADDON_TOGGLE_ENABLED: 'addon.toggle.enabled',
|
||||||
|
|
||||||
|
// Settings
|
||||||
|
SETTING_SET: 'setting.set',
|
||||||
|
|
||||||
|
// 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',
|
|
||||||
|
|
||||||
// Completion
|
// Completion
|
||||||
COMPLETION_SET_ITEMS: 'completion.set.items',
|
COMPLETION_SET_ITEMS: 'completion.set.items',
|
||||||
|
|
|
@ -13,11 +13,4 @@ const clearKeys = () => {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const setKeymaps = (keymaps) => {
|
export { keyPress, clearKeys };
|
||||||
return {
|
|
||||||
type: actions.INPUT_SET_KEYMAPS,
|
|
||||||
keymaps,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export { keyPress, clearKeys, setKeymaps };
|
|
||||||
|
|
10
src/content/actions/setting.js
Normal file
10
src/content/actions/setting.js
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import actions from 'content/actions';
|
||||||
|
|
||||||
|
const set = (value) => {
|
||||||
|
return {
|
||||||
|
type: actions.SETTING_SET,
|
||||||
|
value,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export { set };
|
|
@ -1,7 +1,7 @@
|
||||||
import InputComponent from './input';
|
import InputComponent from './input';
|
||||||
import KeymapperComponent from './keymapper';
|
import KeymapperComponent from './keymapper';
|
||||||
import FollowComponent from './follow';
|
import FollowComponent from './follow';
|
||||||
import * as inputActions from 'content/actions/input';
|
import * as settingActions from 'content/actions/setting';
|
||||||
import messages from 'shared/messages';
|
import messages from 'shared/messages';
|
||||||
|
|
||||||
export default class Common {
|
export default class Common {
|
||||||
|
@ -40,7 +40,7 @@ export default class Common {
|
||||||
browser.runtime.sendMessage({
|
browser.runtime.sendMessage({
|
||||||
type: messages.SETTINGS_QUERY,
|
type: messages.SETTINGS_QUERY,
|
||||||
}).then((settings) => {
|
}).then((settings) => {
|
||||||
this.store.dispatch(inputActions.setKeymaps(settings.keymaps));
|
this.store.dispatch(settingActions.set(settings));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,19 +11,20 @@ export default class KeymapperComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
key(key) {
|
key(key) {
|
||||||
let enabled = this.store.getState().addon.enabled;
|
|
||||||
|
|
||||||
this.store.dispatch(inputActions.keyPress(key));
|
this.store.dispatch(inputActions.keyPress(key));
|
||||||
|
|
||||||
let input = this.store.getState().input;
|
let state = this.store.getState();
|
||||||
let matched = Object.keys(input.keymaps).filter((keyStr) => {
|
let input = state.input;
|
||||||
|
let keymaps = state.setting.keymaps;
|
||||||
|
|
||||||
|
let matched = Object.keys(keymaps).filter((keyStr) => {
|
||||||
return keyStr.startsWith(input.keys);
|
return keyStr.startsWith(input.keys);
|
||||||
});
|
});
|
||||||
if (!enabled) {
|
if (!state.addon.enabled) {
|
||||||
// available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if
|
// available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if
|
||||||
// the addon disabled
|
// the addon disabled
|
||||||
matched = matched.filter((keys) => {
|
matched = matched.filter((keys) => {
|
||||||
let type = input.keymaps[keys].type;
|
let type = keymaps[keys].type;
|
||||||
return type === operations.ADDON_ENABLE ||
|
return type === operations.ADDON_ENABLE ||
|
||||||
type === operations.ADDON_TOGGLE_ENABLED;
|
type === operations.ADDON_TOGGLE_ENABLED;
|
||||||
});
|
});
|
||||||
|
@ -35,7 +36,7 @@ export default class KeymapperComponent {
|
||||||
matched.length === 1 && input.keys !== matched[0]) {
|
matched.length === 1 && input.keys !== matched[0]) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
let operation = input.keymaps[matched];
|
let operation = keymaps[matched];
|
||||||
this.store.dispatch(operationActions.exec(operation));
|
this.store.dispatch(operationActions.exec(operation));
|
||||||
this.store.dispatch(inputActions.clearKeys());
|
this.store.dispatch(inputActions.clearKeys());
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
import addonReducer from './addon';
|
import addonReducer from './addon';
|
||||||
|
import settingReducer from './setting';
|
||||||
import inputReducer from './input';
|
import inputReducer from './input';
|
||||||
import followReducer from './follow';
|
import followReducer from './follow';
|
||||||
|
|
||||||
// Make setting reducer instead of re-use
|
// Make setting reducer instead of re-use
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
addon: addonReducer(undefined, {}),
|
addon: addonReducer(undefined, {}),
|
||||||
|
setting: settingReducer(undefined, {}),
|
||||||
input: inputReducer(undefined, {}),
|
input: inputReducer(undefined, {}),
|
||||||
follow: followReducer(undefined, {}),
|
follow: followReducer(undefined, {}),
|
||||||
};
|
};
|
||||||
|
@ -12,6 +14,7 @@ const defaultState = {
|
||||||
export default function reducer(state = defaultState, action = {}) {
|
export default function reducer(state = defaultState, action = {}) {
|
||||||
return Object.assign({}, state, {
|
return Object.assign({}, state, {
|
||||||
addon: addonReducer(state.addon, action),
|
addon: addonReducer(state.addon, action),
|
||||||
|
setting: settingReducer(state.setting, action),
|
||||||
input: inputReducer(state.input, action),
|
input: inputReducer(state.input, action),
|
||||||
follow: followReducer(state.follow, action),
|
follow: followReducer(state.follow, action),
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
import actions from 'content/actions';
|
import actions from 'content/actions';
|
||||||
|
|
||||||
const defaultState = {
|
const defaultState = {
|
||||||
keys: '',
|
keys: ''
|
||||||
keymaps: {},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function reducer(state = defaultState, action = {}) {
|
export default function reducer(state = defaultState, action = {}) {
|
||||||
|
@ -15,10 +14,6 @@ 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,
|
|
||||||
});
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
13
src/content/reducers/setting.js
Normal file
13
src/content/reducers/setting.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import actions from 'content/actions';
|
||||||
|
|
||||||
|
const defaultState = {};
|
||||||
|
|
||||||
|
export default function reducer(state = defaultState, action = {}) {
|
||||||
|
switch (action.type) {
|
||||||
|
case actions.SETTING_SET:
|
||||||
|
return Object.assign({}, action.value);
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
13
test/content/actions/setting.test.js
Normal file
13
test/content/actions/setting.test.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
import { expect } from "chai";
|
||||||
|
import actions from 'content/actions';
|
||||||
|
import * as settingActions from 'content/actions/setting';
|
||||||
|
|
||||||
|
describe("setting actions", () => {
|
||||||
|
describe("set", () => {
|
||||||
|
it('create SETTING_SET action', () => {
|
||||||
|
let action = settingActions.set({ red: 'apple', yellow: 'banana' });
|
||||||
|
expect(action.type).to.equal(actions.SETTING_SET);
|
||||||
|
expect(action.value).to.deep.equal({ red: 'apple', yellow: 'banana' });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
18
test/content/reducers/setting.test.js
Normal file
18
test/content/reducers/setting.test.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { expect } from "chai";
|
||||||
|
import actions from 'content/actions';
|
||||||
|
import settingReducer from 'content/reducers/setting';
|
||||||
|
|
||||||
|
describe("content setting reducer", () => {
|
||||||
|
it('return the initial state', () => {
|
||||||
|
let state = settingReducer(undefined, {});
|
||||||
|
expect(state).to.deep.equal({});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('return next state for SETTING_SET', () => {
|
||||||
|
let newSettings = { red: 'apple', yellow: 'banana' };
|
||||||
|
let action = { type: actions.SETTING_SET, value: newSettings };
|
||||||
|
let state = settingReducer(undefined, action);
|
||||||
|
expect(state).to.deep.equal(newSettings);
|
||||||
|
expect(state).not.to.equal(newSettings); // assert deep copy
|
||||||
|
});
|
||||||
|
});
|
Reference in a new issue