Declare setting types

This commit is contained in:
Shin'ya Ueoka 2019-05-05 08:03:29 +09:00
parent d01db82c0d
commit a0882bbceb
48 changed files with 1618 additions and 903 deletions

View file

@ -1,4 +1,5 @@
import Redux from 'redux';
import Settings from '../../shared/Settings';
// Enable/disable
export const ADDON_SET_ENABLED = 'addon.set.enabled';
@ -45,7 +46,7 @@ export interface FindSetKeywordAction extends Redux.Action {
export interface SettingSetAction extends Redux.Action {
type: typeof SETTING_SET;
value: any;
settings: Settings,
}
export interface InputKeyPressAction extends Redux.Action {

View file

@ -8,7 +8,6 @@ import * as urls from '../urls';
import * as consoleFrames from '../console-frames';
import * as addonActions from './addon';
import * as markActions from './mark';
import * as properties from '../../shared/settings/properties';
// eslint-disable-next-line complexity, max-lines-per-function
const exec = (
@ -16,8 +15,7 @@ const exec = (
settings: any,
addonEnabled: boolean,
): Promise<actions.Action> | actions.Action => {
let smoothscroll = settings.properties.smoothscroll ||
properties.defaults.smoothscroll;
let smoothscroll = settings.properties.smoothscroll;
switch (operation.type) {
case operations.ADDON_ENABLE:
return addonActions.enable();

View file

@ -1,29 +1,20 @@
import * as actions from './index';
import * as keyUtils from '../../shared/utils/keys';
import * as operations from '../../shared/operations';
import * as messages from '../../shared/messages';
import Settings, { Keymaps } from '../../shared/Settings';
const reservedKeymaps = {
const reservedKeymaps: Keymaps = {
'<Esc>': { type: operations.CANCEL },
'<C-[>': { type: operations.CANCEL },
};
const set = (value: any): actions.SettingAction => {
let entries: any[] = [];
if (value.keymaps) {
let keymaps = { ...value.keymaps, ...reservedKeymaps };
entries = Object.entries(keymaps).map((entry) => {
return [
keyUtils.fromMapKeys(entry[0]),
entry[1],
];
});
}
const set = (settings: Settings): actions.SettingAction => {
return {
type: actions.SETTING_SET,
value: { ...value,
keymaps: entries, }
settings: {
...settings,
keymaps: { ...settings.keymaps, ...reservedKeymaps },
}
};
};

View file

@ -8,6 +8,7 @@ import MessageListener from '../../MessageListener';
import * as addonActions from '../../actions/addon';
import * as blacklists from '../../../shared/blacklists';
import * as keys from '../../../shared/utils/keys';
import * as actions from '../../actions';
export default class Common {
private win: Window;
@ -45,9 +46,9 @@ export default class Common {
reloadSettings() {
try {
this.store.dispatch(settingActions.load())
.then(({ value: settings }: any) => {
.then((action: actions.SettingAction) => {
let enabled = !blacklists.includes(
settings.blacklist, this.win.location.href
action.settings.blacklist, this.win.location.href
);
this.store.dispatch(addonActions.setEnabled(enabled));
});

View file

@ -61,7 +61,6 @@ export default class InputComponent {
}
let key = keys.fromKeyboardEvent(e);
for (let listener of this.onKeyListeners) {
let stop = listener(key);
if (stop) {

View file

@ -3,7 +3,10 @@ import * as operationActions from '../../actions/operation';
import * as operations from '../../../shared/operations';
import * as keyUtils from '../../../shared/utils/keys';
const mapStartsWith = (mapping, keys) => {
const mapStartsWith = (
mapping: keyUtils.Key[],
keys: keyUtils.Key[],
): boolean => {
if (mapping.length < keys.length) {
return false;
}
@ -16,26 +19,33 @@ const mapStartsWith = (mapping, keys) => {
};
export default class KeymapperComponent {
constructor(store) {
private store: any;
constructor(store: any) {
this.store = store;
}
// eslint-disable-next-line max-statements
key(key) {
key(key: keyUtils.Key): boolean {
this.store.dispatch(inputActions.keyPress(key));
let state = this.store.getState();
let input = state.input;
let keymaps = new Map(state.setting.keymaps);
let keymaps = new Map<keyUtils.Key[], operations.Operation>(
state.setting.keymaps.map(
(e: {key: keyUtils.Key[], op: operations.Operation}) => [e.key, e.op],
)
);
let matched = Array.from(keymaps.keys()).filter((mapping) => {
return mapStartsWith(mapping, input.keys);
});
let matched = Array.from(keymaps.keys()).filter(
(mapping: keyUtils.Key[]) => {
return mapStartsWith(mapping, input.keys);
});
if (!state.addon.enabled) {
// available keymaps are only ADDON_ENABLE and ADDON_TOGGLE_ENABLED if
// the addon disabled
matched = matched.filter((keys) => {
let type = keymaps.get(keys).type;
let type = (keymaps.get(keys) as operations.Operation).type;
return type === operations.ADDON_ENABLE ||
type === operations.ADDON_TOGGLE_ENABLED;
});
@ -47,7 +57,7 @@ export default class KeymapperComponent {
matched.length === 1 && input.keys.length < matched[0].length) {
return true;
}
let operation = keymaps.get(matched[0]);
let operation = keymaps.get(matched[0]) as operations.Operation;
let act = operationActions.exec(
operation, state.setting, state.addon.enabled
);

View file

@ -1,7 +1,6 @@
import * as markActions from 'content/actions/mark';
import * as scrolls from 'content/scrolls';
import * as consoleFrames from 'content/console-frames';
import * as properties from 'shared/settings/properties';
import * as markActions from '../../actions/mark';
import * as scrolls from '../..//scrolls';
import * as consoleFrames from '../..//console-frames';
const cancelKey = (key): boolean => {
return key.key === 'Esc' || key.key === '[' && key.ctrlKey;
@ -20,8 +19,7 @@ export default class MarkComponent {
// eslint-disable-next-line max-statements
key(key) {
let { mark: markStage, setting } = this.store.getState();
let smoothscroll = setting.properties.smoothscroll ||
properties.defaults.smoothscroll;
let smoothscroll = setting.properties.smoothscroll;
if (!markStage.setMode && !markStage.jumpMode) {
return false;

View file

@ -2,7 +2,6 @@ import * as followControllerActions from '../../actions/follow-controller';
import * as messages from '../../../shared/messages';
import MessageListener, { WebMessageSender } from '../../MessageListener';
import HintKeyProducer from '../../hint-key-producer';
import * as properties from '../../../shared/settings/properties';
const broadcastMessage = (win: Window, message: messages.Message): void => {
let json = JSON.stringify(message);
@ -162,7 +161,6 @@ export default class FollowController {
}
hintchars() {
return this.store.getState().setting.properties.hintchars ||
properties.defaults.hintchars;
return this.store.getState().setting.properties.hintchars;
}
}

View file

@ -1,12 +1,20 @@
import * as actions from '../actions';
import * as keyUtils from '../../shared/utils/keys';
import * as operations from '../../shared/operations';
import { Properties } from '../../shared/Settings';
export interface State {
keymaps: any[];
keymaps: { key: keyUtils.Key[], op: operations.Operation }[];
properties: Properties;
}
const defaultState = {
// keymaps is and arrays of key-binding pairs, which is entries of Map
const defaultState: State = {
keymaps: [],
properties: {
complete: '',
smoothscroll: false,
hintchars: '',
},
};
export default function reducer(
@ -15,7 +23,15 @@ export default function reducer(
): State {
switch (action.type) {
case actions.SETTING_SET:
return { ...action.value };
return {
keymaps: Object.entries(action.settings.keymaps).map((entry) => {
return {
key: keyUtils.fromMapKeys(entry[0]),
op: entry[1],
};
}),
properties: action.settings.properties,
};
default:
return state;
}