diff options
Diffstat (limited to 'src/content')
-rw-r--r-- | src/content/actions/index.ts | 3 | ||||
-rw-r--r-- | src/content/actions/operation.ts | 4 | ||||
-rw-r--r-- | src/content/actions/setting.ts | 23 | ||||
-rw-r--r-- | src/content/components/common/index.ts | 5 | ||||
-rw-r--r-- | src/content/components/common/input.ts | 1 | ||||
-rw-r--r-- | src/content/components/common/keymapper.ts | 28 | ||||
-rw-r--r-- | src/content/components/common/mark.ts | 10 | ||||
-rw-r--r-- | src/content/components/top-content/follow-controller.ts | 4 | ||||
-rw-r--r-- | src/content/reducers/setting.ts | 24 |
9 files changed, 57 insertions, 45 deletions
diff --git a/src/content/actions/index.ts b/src/content/actions/index.ts index 18d0a69..a259211 100644 --- a/src/content/actions/index.ts +++ b/src/content/actions/index.ts @@ -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 { diff --git a/src/content/actions/operation.ts b/src/content/actions/operation.ts index 6acb407..41e080b 100644 --- a/src/content/actions/operation.ts +++ b/src/content/actions/operation.ts @@ -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(); diff --git a/src/content/actions/setting.ts b/src/content/actions/setting.ts index a8f049a..92f8559 100644 --- a/src/content/actions/setting.ts +++ b/src/content/actions/setting.ts @@ -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 }, + } }; }; diff --git a/src/content/components/common/index.ts b/src/content/components/common/index.ts index 9b5164e..8bd697f 100644 --- a/src/content/components/common/index.ts +++ b/src/content/components/common/index.ts @@ -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)); }); diff --git a/src/content/components/common/input.ts b/src/content/components/common/input.ts index 64eb5f3..1fe34c9 100644 --- a/src/content/components/common/input.ts +++ b/src/content/components/common/input.ts @@ -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) { diff --git a/src/content/components/common/keymapper.ts b/src/content/components/common/keymapper.ts index d9c9834..c94bae0 100644 --- a/src/content/components/common/keymapper.ts +++ b/src/content/components/common/keymapper.ts @@ -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 ); diff --git a/src/content/components/common/mark.ts b/src/content/components/common/mark.ts index 500d03b..686116c 100644 --- a/src/content/components/common/mark.ts +++ b/src/content/components/common/mark.ts @@ -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; diff --git a/src/content/components/top-content/follow-controller.ts b/src/content/components/top-content/follow-controller.ts index be71f6e..d49b22a 100644 --- a/src/content/components/top-content/follow-controller.ts +++ b/src/content/components/top-content/follow-controller.ts @@ -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; } } diff --git a/src/content/reducers/setting.ts b/src/content/reducers/setting.ts index fa8e8ee..a3dc3aa 100644 --- a/src/content/reducers/setting.ts +++ b/src/content/reducers/setting.ts @@ -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; } |