From 574692551a27ea56660bf2061daeaa0d34beaff4 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Sat, 5 Oct 2019 02:06:02 +0000 Subject: Make Properties class --- src/settings/components/form/PropertiesForm.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/settings/components/form') diff --git a/src/settings/components/form/PropertiesForm.tsx b/src/settings/components/form/PropertiesForm.tsx index ee98b7e..db8c8e5 100644 --- a/src/settings/components/form/PropertiesForm.tsx +++ b/src/settings/components/form/PropertiesForm.tsx @@ -18,7 +18,7 @@ class PropertiesForm extends React.Component { render() { let types = this.props.types; - let value = this.props.value; + let values = this.props.value; return
{ @@ -46,10 +46,10 @@ class PropertiesForm extends React.Component { {name}
; -- cgit v1.2.3 From 8428671a0acf47a4a90b6b1f39cf94401f1e5520 Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Sat, 5 Oct 2019 13:56:21 +0000 Subject: Fix form options --- src/settings/components/form/BlacklistForm.tsx | 38 +++++++++++++++----------- src/settings/components/index.tsx | 11 ++++---- src/settings/reducers/setting.ts | 2 ++ src/shared/SettingData.ts | 10 +++---- src/shared/settings/Properties.ts | 2 +- 5 files changed, 35 insertions(+), 28 deletions(-) (limited to 'src/settings/components/form') diff --git a/src/settings/components/form/BlacklistForm.tsx b/src/settings/components/form/BlacklistForm.tsx index 637bc1e..f352e41 100644 --- a/src/settings/components/form/BlacklistForm.tsx +++ b/src/settings/components/form/BlacklistForm.tsx @@ -2,10 +2,11 @@ import './BlacklistForm.scss'; import AddButton from '../ui/AddButton'; import DeleteButton from '../ui/DeleteButton'; import React from 'react'; +import { BlacklistJSON } from '../../../shared/settings/Blacklist'; interface Props { - value: string[]; - onChange: (value: string[]) => void; + value: BlacklistJSON; + onChange: (value: BlacklistJSON) => void; onBlur: () => void; } @@ -19,19 +20,24 @@ class BlacklistForm extends React.Component { render() { return
{ - this.props.value.map((url, index) => { - return
- - -
; - }) + this.props.value + .map((item, index) => { + if (typeof item !== 'string') { + // TODO support partial blacklist; + return null; + } + return
+ + +
; + }) } @@ -41,7 +47,7 @@ class BlacklistForm extends React.Component { bindValue(e: any) { let name = e.target.name; let index = e.target.getAttribute('data-index'); - let next = this.props.value ? this.props.value.slice() : []; + let next = this.props.value.slice(); if (name === 'url') { next[index] = e.target.value; diff --git a/src/settings/components/index.tsx b/src/settings/components/index.tsx index bd99d52..586f0f0 100644 --- a/src/settings/components/index.tsx +++ b/src/settings/components/index.tsx @@ -32,7 +32,7 @@ class SettingsComponent extends React.Component { this.props.dispatch(settingActions.load()); } - renderFormFields(form: any) { + renderFormFields(form: FormSettings) { return
Keybindings @@ -53,7 +53,7 @@ class SettingsComponent extends React.Component {
Blacklist @@ -62,7 +62,7 @@ class SettingsComponent extends React.Component { Properties @@ -89,10 +89,9 @@ class SettingsComponent extends React.Component { let fields = null; let disabled = this.props.error.length > 0; if (this.props.source === 'form') { - fields = this.renderFormFields(this.props.form); + fields = this.renderFormFields(this.props.form!!); } else if (this.props.source === 'json') { - fields = this.renderJsonFields( - this.props.json as JSONTextSettings, this.props.error); + fields = this.renderJsonFields(this.props.json!!, this.props.error); } return (
diff --git a/src/settings/reducers/setting.ts b/src/settings/reducers/setting.ts index 89bf1cb..804853f 100644 --- a/src/settings/reducers/setting.ts +++ b/src/settings/reducers/setting.ts @@ -2,6 +2,7 @@ import * as actions from '../actions'; import { JSONTextSettings, FormSettings, SettingSource, } from '../../shared/SettingData'; +import { DefaultSetting } from '../../shared/settings/Settings'; export interface State { source: SettingSource; @@ -13,6 +14,7 @@ export interface State { const defaultState: State = { source: SettingSource.JSON, json: JSONTextSettings.fromText(''), + form: FormSettings.fromSettings(DefaultSetting), error: '', }; diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts index 2dedfef..c28a5dd 100644 --- a/src/shared/SettingData.ts +++ b/src/shared/SettingData.ts @@ -141,13 +141,13 @@ export class JSONTextSettings { } export class FormSettings { - private keymaps: FormKeymaps; + public readonly keymaps: FormKeymaps; - private search: FormSearch; + public readonly search: FormSearch; - private properties: Properties; + public readonly properties: Properties; - private blacklist: Blacklist; + public readonly blacklist: Blacklist; constructor( keymaps: FormKeymaps, @@ -210,7 +210,7 @@ export class FormSettings { keymaps: ReturnType; search: ReturnType; properties: ReturnType; - blacklist: string[]; + blacklist: ReturnType; } { return { keymaps: this.keymaps.toJSON(), diff --git a/src/shared/settings/Properties.ts b/src/shared/settings/Properties.ts index 1bc4c7f..63ff991 100644 --- a/src/shared/settings/Properties.ts +++ b/src/shared/settings/Properties.ts @@ -59,7 +59,7 @@ export default class Properties { hintchars?: string; smoothscroll?: boolean; complete?: string; - }) { + } = {}) { this.hintchars = hintchars || defaultValues.hintchars; this.smoothscroll = smoothscroll || defaultValues.smoothscroll; this.complete = complete || defaultValues.complete; -- cgit v1.2.3 From 532eeb5a1d9712676a9b3730bb7b27134c57831b Mon Sep 17 00:00:00 2001 From: Shin'ya UEOKA Date: Mon, 7 Oct 2019 12:02:02 +0000 Subject: Rename valueOf to fromJSON --- .../repositories/PersistentSettingRepository.ts | 2 +- src/settings/components/form/KeymapsForm.tsx | 2 +- src/settings/components/form/SearchForm.tsx | 4 ++-- src/settings/components/index.tsx | 2 +- src/settings/storage.ts | 2 +- src/shared/SettingData.ts | 24 +++++++++++----------- test/settings/components/form/KeymapsForm.test.tsx | 4 ++-- .../components/form/SearchEngineForm.test.tsx | 8 ++++---- test/shared/SettingData.test.ts | 12 +++++------ 9 files changed, 30 insertions(+), 30 deletions(-) (limited to 'src/settings/components/form') diff --git a/src/background/repositories/PersistentSettingRepository.ts b/src/background/repositories/PersistentSettingRepository.ts index 927bce9..e3b78b3 100644 --- a/src/background/repositories/PersistentSettingRepository.ts +++ b/src/background/repositories/PersistentSettingRepository.ts @@ -8,7 +8,7 @@ export default class SettingRepository { if (!settings) { return null; } - return SettingData.valueOf(settings as any); + return SettingData.fromJSON(settings as any); } } diff --git a/src/settings/components/form/KeymapsForm.tsx b/src/settings/components/form/KeymapsForm.tsx index 3cba0c0..94934ae 100644 --- a/src/settings/components/form/KeymapsForm.tsx +++ b/src/settings/components/form/KeymapsForm.tsx @@ -12,7 +12,7 @@ interface Props { class KeymapsForm extends React.Component { public static defaultProps: Props = { - value: FormKeymaps.valueOf({}), + value: FormKeymaps.fromJSON({}), onChange: () => {}, onBlur: () => {}, }; diff --git a/src/settings/components/form/SearchForm.tsx b/src/settings/components/form/SearchForm.tsx index 6ba6cfb..0aaf6fd 100644 --- a/src/settings/components/form/SearchForm.tsx +++ b/src/settings/components/form/SearchForm.tsx @@ -12,7 +12,7 @@ interface Props { class SearchForm extends React.Component { public static defaultProps: Props = { - value: FormSearch.valueOf({ default: '', engines: []}), + value: FormSearch.fromJSON({ default: '', engines: []}), onChange: () => {}, onBlur: () => {}, }; @@ -81,7 +81,7 @@ class SearchForm extends React.Component { } } - this.props.onChange(FormSearch.valueOf(next)); + this.props.onChange(FormSearch.fromJSON(next)); if (name === 'delete' || name === 'default') { this.props.onBlur(); } diff --git a/src/settings/components/index.tsx b/src/settings/components/index.tsx index 586f0f0..160dd9c 100644 --- a/src/settings/components/index.tsx +++ b/src/settings/components/index.tsx @@ -133,7 +133,7 @@ class SettingsComponent extends React.Component { let data = new SettingData({ source: this.props.source, form: (this.props.form as FormSettings).buildWithSearch( - FormSearch.valueOf(value)), + FormSearch.fromJSON(value)), }); this.props.dispatch(settingActions.set(data)); } diff --git a/src/settings/storage.ts b/src/settings/storage.ts index 32b6351..2a983df 100644 --- a/src/settings/storage.ts +++ b/src/settings/storage.ts @@ -6,7 +6,7 @@ export const load = async(): Promise => { return DefaultSettingData; } try { - return SettingData.valueOf(settings as any); + return SettingData.fromJSON(settings as any); } catch (e) { console.error('unable to load settings', e); return DefaultSettingData; diff --git a/src/shared/SettingData.ts b/src/shared/SettingData.ts index b19a756..532570e 100644 --- a/src/shared/SettingData.ts +++ b/src/shared/SettingData.ts @@ -6,9 +6,9 @@ import Properties from './settings/Properties'; import Blacklist from './settings/Blacklist'; export class FormKeymaps { - private data: {[op: string]: string}; + private readonly data: {[op: string]: string}; - constructor(data: {[op: string]: string}) { + private constructor(data: {[op: string]: string}) { this.data = data; } @@ -38,7 +38,7 @@ export class FormKeymaps { return new FormKeymaps(newData); } - static valueOf(o: ReturnType): FormKeymaps { + static fromJSON(o: ReturnType): FormKeymaps { let data: {[op: string]: string} = {}; for (let op of Object.keys(o)) { data[op] = o[op] as string; @@ -65,9 +65,9 @@ export class FormKeymaps { } export class FormSearch { - private default: string; + private readonly default: string; - private engines: string[][]; + private readonly engines: string[][]; constructor(defaultEngine: string, engines: string[][]) { this.default = defaultEngine; @@ -92,7 +92,7 @@ export class FormSearch { }; } - static valueOf(o: ReturnType): FormSearch { + static fromJSON(o: ReturnType): FormSearch { if (!Object.prototype.hasOwnProperty.call(o, 'default')) { throw new TypeError(`"default" field not set`); } @@ -220,15 +220,15 @@ export class FormSettings { }; } - static valueOf(o: ReturnType): FormSettings { + static fromJSON(o: ReturnType): FormSettings { for (let name of ['keymaps', 'search', 'properties', 'blacklist']) { if (!Object.prototype.hasOwnProperty.call(o, name)) { throw new Error(`"${name}" field not set`); } } return new FormSettings( - FormKeymaps.valueOf(o.keymaps), - FormSearch.valueOf(o.search), + FormKeymaps.fromJSON(o.keymaps), + FormSearch.fromJSON(o.search), Properties.fromJSON(o.properties), Blacklist.fromJSON(o.blacklist), ); @@ -311,7 +311,7 @@ export default class SettingData { throw new Error(`unknown settings source: ${this.source}`); } - static valueOf(o: { + static fromJSON(o: { source: string; json?: string; form?: ReturnType; @@ -326,7 +326,7 @@ export default class SettingData { case SettingSource.Form: return new SettingData({ source: o.source, - form: FormSettings.valueOf( + form: FormSettings.fromJSON( o.form as ReturnType), }); } @@ -334,7 +334,7 @@ export default class SettingData { } } -export const DefaultSettingData: SettingData = SettingData.valueOf({ +export const DefaultSettingData: SettingData = SettingData.fromJSON({ source: 'json', json: DefaultSettingJSONText, }); diff --git a/test/settings/components/form/KeymapsForm.test.tsx b/test/settings/components/form/KeymapsForm.test.tsx index dc2322b..1d1e77c 100644 --- a/test/settings/components/form/KeymapsForm.test.tsx +++ b/test/settings/components/form/KeymapsForm.test.tsx @@ -9,7 +9,7 @@ import { expect } from 'chai'; describe("settings/form/KeymapsForm", () => { describe('render', () => { it('renders keymap fields', () => { - let root = ReactTestRenderer.create().root @@ -48,7 +48,7 @@ describe("settings/form/KeymapsForm", () => { it('invokes onChange event on edit', (done) => { ReactTestUtils.act(() => { ReactDOM.render( { describe('render', () => { it('renders SearchForm', () => { - let root = ReactTestRenderer.create().root; @@ -41,7 +41,7 @@ describe("settings/form/SearchForm", () => { it('invokes onChange event on edit', (done) => { ReactTestUtils.act(() => { ReactDOM.render( { it('invokes onChange event on delete', (done) => { ReactTestUtils.act(() => { - ReactDOM.render( { it('invokes onChange event on add', (done) => { ReactTestUtils.act(() => { - ReactDOM.render( { 'scroll.home': '0', }; - let keymaps = FormKeymaps.valueOf(data).toKeymaps().toJSON(); + let keymaps = FormKeymaps.fromJSON(data).toKeymaps().toJSON(); expect(keymaps).to.deep.equal({ 'j': { type: 'scroll.vertically', count: 1 }, '0': { type: 'scroll.home' }, @@ -118,7 +118,7 @@ describe('shared/SettingData', () => { blacklist: [] }; - let settings = FormSettings.valueOf(data).toSettings(); + let settings = FormSettings.fromJSON(data).toSettings(); expect({ keymaps: settings.keymaps.toJSON(), search: settings.search.toJSON(), @@ -211,7 +211,7 @@ describe('shared/SettingData', () => { }`, }; - let j = SettingData.valueOf(data).toJSON(); + let j = SettingData.fromJSON(data).toJSON(); expect(j.source).to.equal('json'); expect(j.json).to.be.a('string'); }); @@ -236,7 +236,7 @@ describe('shared/SettingData', () => { }, }; - let j = SettingData.valueOf(data).toJSON(); + let j = SettingData.fromJSON(data).toJSON(); expect(j.source).to.equal('form'); expect(j.form).to.deep.equal({ keymaps: {}, @@ -277,7 +277,7 @@ describe('shared/SettingData', () => { }`, }; - let settings = SettingData.valueOf(data).toSettings(); + let settings = SettingData.fromJSON(data).toSettings(); expect(settings.search.defaultEngine).to.equal('google'); }); @@ -301,7 +301,7 @@ describe('shared/SettingData', () => { }, }; - let settings = SettingData.valueOf(data).toSettings(); + let settings = SettingData.fromJSON(data).toSettings(); expect(settings.search.defaultEngine).to.equal('yahoo'); }); }); -- cgit v1.2.3