|
|
@ -1,13 +1,16 @@ |
|
|
|
|
|
|
|
import Ajv from 'ajv'; |
|
|
|
|
|
|
|
|
|
|
|
import Keymaps, { KeymapsJSON } from './Keymaps'; |
|
|
|
import Keymaps, { KeymapsJSON } from './Keymaps'; |
|
|
|
import Search, { SearchJSON } from './Search'; |
|
|
|
import Search, { SearchJSON } from './Search'; |
|
|
|
import Properties, { PropertiesJSON } from './Properties'; |
|
|
|
import Properties, { PropertiesJSON } from './Properties'; |
|
|
|
import Blacklist, { BlacklistJSON } from './Blacklist'; |
|
|
|
import Blacklist, { BlacklistJSON } from './Blacklist'; |
|
|
|
|
|
|
|
import validate from './validate'; |
|
|
|
|
|
|
|
|
|
|
|
export type SettingsJSON = { |
|
|
|
export type SettingsJSON = { |
|
|
|
keymaps: KeymapsJSON, |
|
|
|
keymaps?: KeymapsJSON, |
|
|
|
search: SearchJSON, |
|
|
|
search?: SearchJSON, |
|
|
|
properties: PropertiesJSON, |
|
|
|
properties?: PropertiesJSON, |
|
|
|
blacklist: BlacklistJSON, |
|
|
|
blacklist?: BlacklistJSON, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export default class Settings { |
|
|
|
export default class Settings { |
|
|
@ -36,25 +39,30 @@ export default class Settings { |
|
|
|
this.blacklist = blacklist; |
|
|
|
this.blacklist = blacklist; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static fromJSON(json: any): Settings { |
|
|
|
static fromJSON(json: unknown): Settings { |
|
|
|
|
|
|
|
let valid = validate(json); |
|
|
|
|
|
|
|
if (!valid) { |
|
|
|
|
|
|
|
let message = (validate as any).errors!! |
|
|
|
|
|
|
|
.map((err: Ajv.ErrorObject) => { |
|
|
|
|
|
|
|
return `'${err.dataPath}' of ${err.keyword} ${err.message}`; |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.join('; '); |
|
|
|
|
|
|
|
throw new TypeError(message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let obj = json as SettingsJSON; |
|
|
|
let settings = { ...DefaultSetting }; |
|
|
|
let settings = { ...DefaultSetting }; |
|
|
|
for (let key of Object.keys(json)) { |
|
|
|
if (obj.keymaps) { |
|
|
|
switch (key) { |
|
|
|
settings.keymaps = Keymaps.fromJSON(obj.keymaps); |
|
|
|
case 'keymaps': |
|
|
|
} |
|
|
|
settings.keymaps = Keymaps.fromJSON(json.keymaps); |
|
|
|
if (obj.search) { |
|
|
|
break; |
|
|
|
settings.search = Search.fromJSON(obj.search); |
|
|
|
case 'search': |
|
|
|
} |
|
|
|
settings.search = Search.fromJSON(json.search); |
|
|
|
if (obj.properties) { |
|
|
|
break; |
|
|
|
settings.properties = Properties.fromJSON(obj.properties); |
|
|
|
case 'properties': |
|
|
|
|
|
|
|
settings.properties = Properties.fromJSON(json.properties); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case 'blacklist': |
|
|
|
|
|
|
|
settings.blacklist = Blacklist.fromJSON(json.blacklist); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
throw new TypeError('unknown setting: ' + key); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (obj.blacklist) { |
|
|
|
|
|
|
|
settings.blacklist = Blacklist.fromJSON(obj.blacklist); |
|
|
|
} |
|
|
|
} |
|
|
|
return new Settings(settings); |
|
|
|
return new Settings(settings); |
|
|
|
} |
|
|
|
} |
|
|
|