1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
import * as operations from "./operations";
import Settings, { DefaultSettingJSONText } from "./settings/Settings";
import Keymaps from "./settings/Keymaps";
import Search from "./settings/Search";
import Properties from "./settings/Properties";
import Blacklist from "./settings/Blacklist";
export class FormKeymaps {
private readonly data: { [op: string]: string };
private constructor(data: { [op: string]: string }) {
this.data = data;
}
toKeymaps(): Keymaps {
const keymaps: { [key: string]: operations.Operation } = {};
for (const name of Object.keys(this.data)) {
const [type, argStr] = name.split("?");
let args = {};
if (argStr) {
args = JSON.parse(argStr);
}
const key = this.data[name];
keymaps[key] = operations.valueOf({ type, ...args });
}
return Keymaps.fromJSON(keymaps);
}
toJSON(): { [op: string]: string } {
return this.data;
}
buildWithOverride(op: string, keys: string): FormKeymaps {
const newData = {
...this.data,
[op]: keys,
};
return new FormKeymaps(newData);
}
static fromJSON(o: ReturnType<FormKeymaps["toJSON"]>): FormKeymaps {
const data: { [op: string]: string } = {};
for (const op of Object.keys(o)) {
data[op] = o[op] as string;
}
return new FormKeymaps(data);
}
static fromKeymaps(keymaps: Keymaps): FormKeymaps {
const json = keymaps.toJSON();
const data: { [op: string]: string } = {};
for (const key of Object.keys(json)) {
const op = json[key];
const args = { ...op };
delete args.type;
let name = op.type;
if (Object.keys(args).length > 0) {
name += "?" + JSON.stringify(args);
}
data[name] = key;
}
return new FormKeymaps(data);
}
}
export class FormSearch {
private readonly default: string;
private readonly engines: string[][];
constructor(defaultEngine: string, engines: string[][]) {
this.default = defaultEngine;
this.engines = engines;
}
toSearchSettings(): Search {
const engines: { [name: string]: string } = {};
for (const entry of this.engines) {
engines[entry[0]] = entry[1];
}
return new Search(this.default, engines);
}
toJSON(): {
default: string;
engines: string[][];
} {
return {
default: this.default,
engines: this.engines,
};
}
static fromJSON(o: ReturnType<FormSearch["toJSON"]>): FormSearch {
if (!Object.prototype.hasOwnProperty.call(o, "default")) {
throw new TypeError(`"default" field not set`);
}
if (!Object.prototype.hasOwnProperty.call(o, "engines")) {
throw new TypeError(`"engines" field not set`);
}
return new FormSearch(o.default, o.engines);
}
static fromSearch(search: Search): FormSearch {
const engines = Object.entries(search.engines).reduce(
(o: string[][], [name, url]) => {
return o.concat([[name, url]]);
},
[]
);
return new FormSearch(search.defaultEngine, engines);
}
}
export class JSONTextSettings {
constructor(private json: string) {}
toSettings(): Settings {
return Settings.fromJSON(JSON.parse(this.json));
}
toJSONText(): string {
return this.json;
}
static fromText(o: string): JSONTextSettings {
return new JSONTextSettings(o);
}
static fromSettings(data: Settings): JSONTextSettings {
const json = {
keymaps: data.keymaps.toJSON(),
search: data.search,
properties: data.properties,
blacklist: data.blacklist,
};
return new JSONTextSettings(JSON.stringify(json, undefined, 2));
}
}
export class FormSettings {
public readonly keymaps: FormKeymaps;
public readonly search: FormSearch;
public readonly properties: Properties;
public readonly blacklist: Blacklist;
constructor(
keymaps: FormKeymaps,
search: FormSearch,
properties: Properties,
blacklist: Blacklist
) {
this.keymaps = keymaps;
this.search = search;
this.properties = properties;
this.blacklist = blacklist;
}
buildWithKeymaps(keymaps: FormKeymaps): FormSettings {
return new FormSettings(
keymaps,
this.search,
this.properties,
this.blacklist
);
}
buildWithSearch(search: FormSearch): FormSettings {
return new FormSettings(
this.keymaps,
search,
this.properties,
this.blacklist
);
}
buildWithProperties(props: Properties): FormSettings {
return new FormSettings(this.keymaps, this.search, props, this.blacklist);
}
buildWithBlacklist(blacklist: Blacklist): FormSettings {
return new FormSettings(
this.keymaps,
this.search,
this.properties,
blacklist
);
}
toSettings(): Settings {
return Settings.fromJSON({
keymaps: this.keymaps.toKeymaps().toJSON(),
search: this.search.toSearchSettings().toJSON(),
properties: this.properties.toJSON(),
blacklist: this.blacklist.toJSON(),
});
}
toJSON(): {
keymaps: ReturnType<FormKeymaps["toJSON"]>;
search: ReturnType<FormSearch["toJSON"]>;
properties: ReturnType<Properties["toJSON"]>;
blacklist: ReturnType<Blacklist["toJSON"]>;
} {
return {
keymaps: this.keymaps.toJSON(),
search: this.search.toJSON(),
properties: this.properties.toJSON(),
blacklist: this.blacklist.toJSON(),
};
}
static fromJSON(o: ReturnType<FormSettings["toJSON"]>): FormSettings {
for (const name of ["keymaps", "search", "properties", "blacklist"]) {
if (!Object.prototype.hasOwnProperty.call(o, name)) {
throw new Error(`"${name}" field not set`);
}
}
return new FormSettings(
FormKeymaps.fromJSON(o.keymaps),
FormSearch.fromJSON(o.search),
Properties.fromJSON(o.properties),
Blacklist.fromJSON(o.blacklist)
);
}
static fromSettings(data: Settings): FormSettings {
return new FormSettings(
FormKeymaps.fromKeymaps(data.keymaps),
FormSearch.fromSearch(data.search),
data.properties,
data.blacklist
);
}
}
export enum SettingSource {
JSON = "json",
Form = "form",
}
export default class SettingData {
private source: SettingSource;
private json?: JSONTextSettings;
private form?: FormSettings;
constructor({
source,
json,
form,
}: {
source: SettingSource;
json?: JSONTextSettings;
form?: FormSettings;
}) {
this.source = source;
this.json = json;
this.form = form;
}
getSource(): SettingSource {
return this.source;
}
getJSON(): JSONTextSettings {
if (!this.json) {
throw new TypeError("json settings not set");
}
return this.json;
}
getForm(): FormSettings {
if (!this.form) {
throw new TypeError("form settings not set");
}
return this.form;
}
toJSON(): any {
switch (this.source) {
case SettingSource.JSON:
return {
source: this.source,
json: (this.json as JSONTextSettings).toJSONText(),
};
case SettingSource.Form:
return {
source: this.source,
form: (this.form as FormSettings).toJSON(),
};
}
throw new Error(`unknown settings source: ${this.source}`);
}
toSettings(): Settings {
switch (this.source) {
case SettingSource.JSON:
return this.getJSON().toSettings();
case SettingSource.Form:
return this.getForm().toSettings();
}
throw new Error(`unknown settings source: ${this.source}`);
}
static fromJSON(o: {
source: string;
json?: string;
form?: ReturnType<FormSettings["toJSON"]>;
}): SettingData {
switch (o.source) {
case SettingSource.JSON:
return new SettingData({
source: o.source,
json: JSONTextSettings.fromText(
o.json as ReturnType<JSONTextSettings["toJSONText"]>
),
});
case SettingSource.Form:
return new SettingData({
source: o.source,
form: FormSettings.fromJSON(
o.form as ReturnType<FormSettings["toJSON"]>
),
});
}
throw new Error(`unknown settings source: ${o.source}`);
}
}
export const DefaultSettingData: SettingData = SettingData.fromJSON({
source: "json",
json: DefaultSettingJSONText,
});
|