Make Keymap class
This commit is contained in:
parent
b496cea582
commit
410ffbb037
15 changed files with 223 additions and 129 deletions
|
@ -8,6 +8,7 @@ import AddonEnabledController from '../controllers/AddonEnabledController';
|
|||
import LinkController from '../controllers/LinkController';
|
||||
import OperationController from '../controllers/OperationController';
|
||||
import MarkController from '../controllers/MarkController';
|
||||
import { toJSON } from '../../shared/Settings';
|
||||
|
||||
@injectable()
|
||||
export default class ContentMessageListener {
|
||||
|
@ -101,8 +102,8 @@ export default class ContentMessageListener {
|
|||
return this.commandController.exec(text);
|
||||
}
|
||||
|
||||
onSettingsQuery(): Promise<any> {
|
||||
return this.settingController.getSetting();
|
||||
async onSettingsQuery(): Promise<any> {
|
||||
return toJSON(await this.settingController.getSetting());
|
||||
}
|
||||
|
||||
onFindGetKeyword(): Promise<string> {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { injectable } from 'tsyringe';
|
||||
import MemoryStorage from '../infrastructures/MemoryStorage';
|
||||
import Settings from '../../shared/Settings';
|
||||
import Settings, { valueOf, toJSON } from '../../shared/Settings';
|
||||
import * as PropertyDefs from '../../shared/property-defs';
|
||||
|
||||
const CACHED_SETTING_KEY = 'setting';
|
||||
|
@ -14,11 +14,13 @@ export default class SettingRepository {
|
|||
}
|
||||
|
||||
get(): Promise<Settings> {
|
||||
return Promise.resolve(this.cache.get(CACHED_SETTING_KEY));
|
||||
let data = this.cache.get(CACHED_SETTING_KEY);
|
||||
return Promise.resolve(valueOf(data));
|
||||
}
|
||||
|
||||
update(value: Settings): void {
|
||||
return this.cache.set(CACHED_SETTING_KEY, value);
|
||||
let data = toJSON(value);
|
||||
return this.cache.set(CACHED_SETTING_KEY, data);
|
||||
}
|
||||
|
||||
async setProperty(
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import Settings from '../../shared/Settings';
|
||||
import Settings, { valueOf } from '../../shared/Settings';
|
||||
import * as messages from '../../shared/messages';
|
||||
|
||||
export default interface SettingClient {
|
||||
|
@ -10,6 +10,6 @@ export class SettingClientImpl {
|
|||
let settings = await browser.runtime.sendMessage({
|
||||
type: messages.SETTINGS_QUERY,
|
||||
});
|
||||
return settings as Settings;
|
||||
return valueOf(settings);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,16 +3,16 @@ import KeymapRepository from '../repositories/KeymapRepository';
|
|||
import SettingRepository from '../repositories/SettingRepository';
|
||||
import AddonEnabledRepository from '../repositories/AddonEnabledRepository';
|
||||
import * as operations from '../../shared/operations';
|
||||
import { Keymaps } from '../../shared/Settings';
|
||||
import Key from '../domains/Key';
|
||||
import KeySequence from '../domains/KeySequence';
|
||||
import Keymaps from '../../shared/settings/Keymaps';
|
||||
|
||||
type KeymapEntityMap = Map<KeySequence, operations.Operation>;
|
||||
|
||||
const reservedKeymaps: Keymaps = {
|
||||
const reservedKeymaps = Keymaps.fromJSON({
|
||||
'<Esc>': { type: operations.CANCEL },
|
||||
'<C-[>': { type: operations.CANCEL },
|
||||
};
|
||||
});
|
||||
|
||||
@injectable()
|
||||
export default class KeymapUseCase {
|
||||
|
@ -65,16 +65,11 @@ export default class KeymapUseCase {
|
|||
}
|
||||
|
||||
private keymapEntityMap(): KeymapEntityMap {
|
||||
let keymaps = {
|
||||
...this.settingRepository.get().keymaps,
|
||||
...reservedKeymaps,
|
||||
};
|
||||
let entries = Object.entries(keymaps).map((entry) => {
|
||||
return [
|
||||
KeySequence.fromMapKeys(entry[0]),
|
||||
entry[1],
|
||||
];
|
||||
}) as [KeySequence, operations.Operation][];
|
||||
let keymaps = this.settingRepository.get().keymaps.combine(reservedKeymaps);
|
||||
let entries = keymaps.entries().map(entry => [
|
||||
KeySequence.fromMapKeys(entry[0]),
|
||||
entry[1],
|
||||
]) as [KeySequence, operations.Operation][];
|
||||
return new Map<KeySequence, operations.Operation>(entries);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import {
|
||||
JSONSettings, FormSettings, SettingSource,
|
||||
JSONTextSettings, FormSettings, SettingSource,
|
||||
} from '../../shared/SettingData';
|
||||
|
||||
// Settings
|
||||
|
@ -11,14 +11,14 @@ export const SETTING_SWITCH_TO_JSON = 'setting.switch.to.json';
|
|||
interface SettingSetSettingsAcion {
|
||||
type: typeof SETTING_SET_SETTINGS;
|
||||
source: SettingSource;
|
||||
json?: JSONSettings;
|
||||
json?: JSONTextSettings;
|
||||
form?: FormSettings;
|
||||
}
|
||||
|
||||
interface SettingShowErrorAction {
|
||||
type: typeof SETTING_SHOW_ERROR;
|
||||
error: string;
|
||||
json: JSONSettings;
|
||||
json: JSONTextSettings;
|
||||
}
|
||||
|
||||
interface SettingSwitchToFormAction {
|
||||
|
@ -28,7 +28,7 @@ interface SettingSwitchToFormAction {
|
|||
|
||||
interface SettingSwitchToJsonAction {
|
||||
type: typeof SETTING_SWITCH_TO_JSON;
|
||||
json: JSONSettings,
|
||||
json: JSONTextSettings,
|
||||
}
|
||||
|
||||
export type SettingAction =
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import * as actions from './index';
|
||||
import * as storages from '../storage';
|
||||
import SettingData, {
|
||||
JSONSettings, FormSettings, SettingSource,
|
||||
JSONTextSettings, FormSettings, SettingSource,
|
||||
} from '../../shared/SettingData';
|
||||
|
||||
const load = async(): Promise<actions.SettingAction> => {
|
||||
|
@ -26,7 +26,7 @@ const save = async(data: SettingData): Promise<actions.SettingAction> => {
|
|||
return set(data);
|
||||
};
|
||||
|
||||
const switchToForm = (json: JSONSettings): actions.SettingAction => {
|
||||
const switchToForm = (json: JSONTextSettings): actions.SettingAction => {
|
||||
try {
|
||||
// toSettings exercise validation
|
||||
let form = FormSettings.fromSettings(json.toSettings());
|
||||
|
@ -44,7 +44,7 @@ const switchToForm = (json: JSONSettings): actions.SettingAction => {
|
|||
};
|
||||
|
||||
const switchToJson = (form: FormSettings): actions.SettingAction => {
|
||||
let json = JSONSettings.fromSettings(form.toSettings());
|
||||
let json = JSONTextSettings.fromSettings(form.toSettings());
|
||||
return {
|
||||
type: actions.SETTING_SWITCH_TO_JSON,
|
||||
json,
|
||||
|
|
|
@ -8,7 +8,7 @@ import BlacklistForm from './form/BlacklistForm';
|
|||
import PropertiesForm from './form/PropertiesForm';
|
||||
import * as settingActions from '../../settings/actions/setting';
|
||||
import SettingData, {
|
||||
JSONSettings, FormKeymaps, FormSearch, FormSettings,
|
||||
JSONTextSettings, FormKeymaps, FormSearch, FormSettings,
|
||||
} from '../../shared/SettingData';
|
||||
import { State as AppState } from '../reducers/setting';
|
||||
import * as settings from '../../shared/Settings';
|
||||
|
@ -75,7 +75,7 @@ class SettingsComponent extends React.Component<Props> {
|
|||
</div>;
|
||||
}
|
||||
|
||||
renderJsonFields(json: JSONSettings, error: string) {
|
||||
renderJsonFields(json: JSONTextSettings, error: string) {
|
||||
return <div>
|
||||
<Input
|
||||
type='textarea'
|
||||
|
@ -85,7 +85,7 @@ class SettingsComponent extends React.Component<Props> {
|
|||
error={error}
|
||||
onValueChange={this.bindJson.bind(this)}
|
||||
onBlur={this.save.bind(this)}
|
||||
value={json.toJSON()}
|
||||
value={json.toJSONText()}
|
||||
/>
|
||||
</div>;
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ class SettingsComponent extends React.Component<Props> {
|
|||
fields = this.renderFormFields(this.props.form);
|
||||
} else if (this.props.source === 'json') {
|
||||
fields = this.renderJsonFields(
|
||||
this.props.json as JSONSettings, this.props.error);
|
||||
this.props.json as JSONTextSettings, this.props.error);
|
||||
}
|
||||
return (
|
||||
<div>
|
||||
|
@ -165,7 +165,7 @@ class SettingsComponent extends React.Component<Props> {
|
|||
bindJson(_name: string, value: string) {
|
||||
let data = new SettingData({
|
||||
source: this.props.source,
|
||||
json: JSONSettings.valueOf(value),
|
||||
json: JSONTextSettings.fromText(value),
|
||||
});
|
||||
this.props.dispatch(settingActions.set(data));
|
||||
}
|
||||
|
@ -183,7 +183,7 @@ class SettingsComponent extends React.Component<Props> {
|
|||
return;
|
||||
}
|
||||
this.props.dispatch(
|
||||
settingActions.switchToForm(this.props.json as JSONSettings));
|
||||
settingActions.switchToForm(this.props.json as JSONTextSettings));
|
||||
this.save();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
import * as actions from '../actions';
|
||||
import {
|
||||
JSONSettings, FormSettings, SettingSource,
|
||||
JSONTextSettings, FormSettings, SettingSource,
|
||||
} from '../../shared/SettingData';
|
||||
|
||||
export interface State {
|
||||
source: SettingSource;
|
||||
json?: JSONSettings;
|
||||
json?: JSONTextSettings;
|
||||
form?: FormSettings;
|
||||
error: string;
|
||||
}
|
||||
|
||||
const defaultState: State = {
|
||||
source: SettingSource.JSON,
|
||||
json: JSONSettings.valueOf(''),
|
||||
json: JSONTextSettings.fromText(''),
|
||||
error: '',
|
||||
};
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import * as operations from './operations';
|
||||
import Settings, * as settings from './Settings';
|
||||
import Keymaps from './settings/Keymaps';
|
||||
|
||||
export class FormKeymaps {
|
||||
private data: {[op: string]: string};
|
||||
|
@ -8,8 +9,8 @@ export class FormKeymaps {
|
|||
this.data = data;
|
||||
}
|
||||
|
||||
toKeymaps(): settings.Keymaps {
|
||||
let keymaps: settings.Keymaps = {};
|
||||
toKeymaps(): Keymaps {
|
||||
let keymaps: { [key: string]: operations.Operation } = {};
|
||||
for (let name of Object.keys(this.data)) {
|
||||
let [type, argStr] = name.split('?');
|
||||
let args = {};
|
||||
|
@ -19,7 +20,7 @@ export class FormKeymaps {
|
|||
let key = this.data[name];
|
||||
keymaps[key] = operations.valueOf({ type, ...args });
|
||||
}
|
||||
return keymaps;
|
||||
return Keymaps.fromJSON(keymaps);
|
||||
}
|
||||
|
||||
toJSON(): {[op: string]: string} {
|
||||
|
@ -42,10 +43,11 @@ export class FormKeymaps {
|
|||
return new FormKeymaps(data);
|
||||
}
|
||||
|
||||
static fromKeymaps(keymaps: settings.Keymaps): FormKeymaps {
|
||||
static fromKeymaps(keymaps: Keymaps): FormKeymaps {
|
||||
let json = keymaps.toJSON();
|
||||
let data: {[op: string]: string} = {};
|
||||
for (let key of Object.keys(keymaps)) {
|
||||
let op = keymaps[key];
|
||||
for (let key of Object.keys(json)) {
|
||||
let op = json[key];
|
||||
let args = { ...op };
|
||||
delete args.type;
|
||||
|
||||
|
@ -109,27 +111,32 @@ export class FormSearch {
|
|||
}
|
||||
}
|
||||
|
||||
export class JSONSettings {
|
||||
private json: string;
|
||||
|
||||
constructor(json: any) {
|
||||
this.json = json;
|
||||
export class JSONTextSettings {
|
||||
constructor(
|
||||
private json: string,
|
||||
) {
|
||||
}
|
||||
|
||||
toSettings(): Settings {
|
||||
return settings.valueOf(JSON.parse(this.json));
|
||||
}
|
||||
|
||||
toJSON(): string {
|
||||
toJSONText(): string {
|
||||
return this.json;
|
||||
}
|
||||
|
||||
static valueOf(o: ReturnType<JSONSettings['toJSON']>): JSONSettings {
|
||||
return new JSONSettings(o);
|
||||
static fromText(o: string): JSONTextSettings {
|
||||
return new JSONTextSettings(o);
|
||||
}
|
||||
|
||||
static fromSettings(data: Settings): JSONSettings {
|
||||
return new JSONSettings(JSON.stringify(data, undefined, 2));
|
||||
static fromSettings(data: Settings): JSONTextSettings {
|
||||
let json = {
|
||||
keymaps: data.keymaps.toJSON(),
|
||||
search: data.search,
|
||||
properties: data.properties,
|
||||
blacklist: data.blacklist,
|
||||
};
|
||||
return new JSONTextSettings(JSON.stringify(json, undefined, 2));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -192,7 +199,7 @@ export class FormSettings {
|
|||
|
||||
toSettings(): Settings {
|
||||
return settings.valueOf({
|
||||
keymaps: this.keymaps.toKeymaps(),
|
||||
keymaps: this.keymaps.toKeymaps().toJSON(),
|
||||
search: this.search.toSearchSettings(),
|
||||
properties: this.properties,
|
||||
blacklist: this.blacklist,
|
||||
|
@ -244,7 +251,7 @@ export enum SettingSource {
|
|||
export default class SettingData {
|
||||
private source: SettingSource;
|
||||
|
||||
private json?: JSONSettings;
|
||||
private json?: JSONTextSettings;
|
||||
|
||||
private form?: FormSettings;
|
||||
|
||||
|
@ -252,7 +259,7 @@ export default class SettingData {
|
|||
source, json, form
|
||||
}: {
|
||||
source: SettingSource,
|
||||
json?: JSONSettings,
|
||||
json?: JSONTextSettings,
|
||||
form?: FormSettings,
|
||||
}) {
|
||||
this.source = source;
|
||||
|
@ -264,7 +271,7 @@ export default class SettingData {
|
|||
return this.source;
|
||||
}
|
||||
|
||||
getJSON(): JSONSettings {
|
||||
getJSON(): JSONTextSettings {
|
||||
if (!this.json) {
|
||||
throw new TypeError('json settings not set');
|
||||
}
|
||||
|
@ -283,7 +290,7 @@ export default class SettingData {
|
|||
case SettingSource.JSON:
|
||||
return {
|
||||
source: this.source,
|
||||
json: (this.json as JSONSettings).toJSON(),
|
||||
json: (this.json as JSONTextSettings).toJSONText(),
|
||||
};
|
||||
case SettingSource.Form:
|
||||
return {
|
||||
|
@ -313,8 +320,8 @@ export default class SettingData {
|
|||
case SettingSource.JSON:
|
||||
return new SettingData({
|
||||
source: o.source,
|
||||
json: JSONSettings.valueOf(
|
||||
o.json as ReturnType<JSONSettings['toJSON']>),
|
||||
json: JSONTextSettings.fromText(
|
||||
o.json as ReturnType<JSONTextSettings['toJSONText']>),
|
||||
});
|
||||
case SettingSource.Form:
|
||||
return new SettingData({
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import * as operations from './operations';
|
||||
import * as PropertyDefs from './property-defs';
|
||||
|
||||
export type Keymaps = {[key: string]: operations.Operation};
|
||||
import Keymaps from './settings/Keymaps';
|
||||
|
||||
export interface Search {
|
||||
default: string;
|
||||
|
@ -21,14 +19,6 @@ export default interface Settings {
|
|||
blacklist: string[];
|
||||
}
|
||||
|
||||
export const keymapsValueOf = (o: any): Keymaps => {
|
||||
return Object.keys(o).reduce((keymaps: Keymaps, key: string): Keymaps => {
|
||||
let op = operations.valueOf(o[key]);
|
||||
keymaps[key] = op;
|
||||
return keymaps;
|
||||
}, {});
|
||||
};
|
||||
|
||||
export const searchValueOf = (o: any): Search => {
|
||||
if (typeof o.default !== 'string') {
|
||||
throw new TypeError('string field "default" not set"');
|
||||
|
@ -97,7 +87,7 @@ export const valueOf = (o: any): Settings => {
|
|||
for (let key of Object.keys(o)) {
|
||||
switch (key) {
|
||||
case 'keymaps':
|
||||
settings.keymaps = keymapsValueOf(o.keymaps);
|
||||
settings.keymaps = Keymaps.fromJSON(o.keymaps);
|
||||
break;
|
||||
case 'search':
|
||||
settings.search = searchValueOf(o.search);
|
||||
|
@ -115,8 +105,17 @@ export const valueOf = (o: any): Settings => {
|
|||
return settings;
|
||||
};
|
||||
|
||||
export const toJSON = (settings: Settings): any => {
|
||||
return {
|
||||
keymaps: settings.keymaps.toJSON(),
|
||||
search: settings.search,
|
||||
properties: settings.properties,
|
||||
blacklist: settings.blacklist,
|
||||
};
|
||||
};
|
||||
|
||||
export const DefaultSetting: Settings = {
|
||||
keymaps: {
|
||||
keymaps: Keymaps.fromJSON({
|
||||
'0': { 'type': 'scroll.home' },
|
||||
':': { 'type': 'command.show' },
|
||||
'o': { 'type': 'command.show.open', 'alter': false },
|
||||
|
@ -179,7 +178,7 @@ export const DefaultSetting: Settings = {
|
|||
'N': { 'type': 'find.prev' },
|
||||
'.': { 'type': 'repeat.last' },
|
||||
'<S-Esc>': { 'type': 'addon.toggle.enabled' }
|
||||
},
|
||||
}),
|
||||
search: {
|
||||
default: 'google',
|
||||
engines: {
|
||||
|
|
37
src/shared/settings/Keymaps.ts
Normal file
37
src/shared/settings/Keymaps.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import * as operations from '../operations';
|
||||
|
||||
export type KeymapsJSON = { [key: string]: operations.Operation };
|
||||
|
||||
export default class Keymaps {
|
||||
constructor(
|
||||
private readonly data: KeymapsJSON,
|
||||
) {
|
||||
}
|
||||
|
||||
static fromJSON(json: any): Keymaps {
|
||||
if (typeof json !== 'object' || json === null) {
|
||||
throw new TypeError('invalid keymaps type: ' + JSON.stringify(json));
|
||||
}
|
||||
|
||||
let data: KeymapsJSON = {};
|
||||
for (let key of Object.keys(json)) {
|
||||
data[key] = operations.valueOf(json[key]);
|
||||
}
|
||||
return new Keymaps(data);
|
||||
}
|
||||
|
||||
combine(other: Keymaps): Keymaps {
|
||||
return new Keymaps({
|
||||
...this.data,
|
||||
...other.data,
|
||||
});
|
||||
}
|
||||
|
||||
toJSON(): KeymapsJSON {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
entries(): [string, operations.Operation][] {
|
||||
return Object.entries(this.data);
|
||||
}
|
||||
}
|
Reference in a new issue