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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
import * as operations from './operations';
import Settings, * as settings from './Settings';
export class FormKeymaps {
private data: {[op: string]: string};
constructor(data: {[op: string]: string}) {
this.data = data;
}
toKeymaps(): settings.Keymaps {
let keymaps: settings.Keymaps = {};
for (let name of Object.keys(this.data)) {
let [type, argStr] = name.split('?');
let args = {};
if (argStr) {
args = JSON.parse(argStr);
}
let key = this.data[name];
keymaps[key] = operations.valueOf({ type, ...args });
}
return keymaps;
}
toJSON(): {[op: string]: string} {
return this.data;
}
buildWithOverride(op: string, keys: string): FormKeymaps {
let newData = {
...this.data,
[op]: keys,
};
return new FormKeymaps(newData);
}
static valueOf(o: ReturnType<FormKeymaps['toJSON']>): FormKeymaps {
let data: {[op: string]: string} = {};
for (let op of Object.keys(o)) {
data[op] = o[op] as string;
}
return new FormKeymaps(data);
}
static fromKeymaps(keymaps: settings.Keymaps): FormKeymaps {
let data: {[op: string]: string} = {};
for (let key of Object.keys(keymaps)) {
let op = keymaps[key];
let 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 default: string;
private engines: string[][];
constructor(defaultEngine: string, engines: string[][]) {
this.default = defaultEngine;
this.engines = engines;
}
toSearchSettings(): settings.Search {
return {
default: this.default,
engines: this.engines.reduce(
(o: {[key: string]: string}, [name, url]) => {
o[name] = url;
return o;
}, {}),
};
}
toJSON(): {
default: string;
engines: string[][];
} {
return {
default: this.default,
engines: this.engines,
};
}
static valueOf(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: settings.Search): FormSearch {
let engines = Object.entries(search.engines).reduce(
(o: string[][], [name, url]) => {
return o.concat([[name, url]]);
}, []);
return new FormSearch(search.default, engines);
}
}
export class JSONSettings {
private json: string;
constructor(json: any) {
this.json = json;
}
toSettings(): Settings {
return settings.valueOf(JSON.parse(this.json));
}
toJSON(): string {
return this.json;
}
static valueOf(o: ReturnType<JSONSettings['toJSON']>): JSONSettings {
return new JSONSettings(o);
}
static fromSettings(data: Settings): JSONSettings {
return new JSONSettings(JSON.stringify(data, undefined, 2));
}
}
export class FormSettings {
private keymaps: FormKeymaps;
private search: FormSearch;
private properties: settings.Properties;
private blacklist: string[];
constructor(
keymaps: FormKeymaps,
search: FormSearch,
properties: settings.Properties,
blacklist: string[],
) {
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: settings.Properties): FormSettings {
return new FormSettings(
this.keymaps,
this.search,
props,
this.blacklist,
);
}
buildWithBlacklist(blacklist: string[]): FormSettings {
return new FormSettings(
this.keymaps,
this.search,
this.properties,
blacklist,
);
}
toSettings(): Settings {
return settings.valueOf({
keymaps: this.keymaps.toKeymaps(),
search: this.search.toSearchSettings(),
properties: this.properties,
blacklist: this.blacklist,
});
}
toJSON(): {
keymaps: ReturnType<FormKeymaps['toJSON']>;
search: ReturnType<FormSearch['toJSON']>;
properties: settings.Properties;
blacklist: string[];
} {
return {
keymaps: this.keymaps.toJSON(),
search: this.search.toJSON(),
properties: this.properties,
blacklist: this.blacklist,
};
}
static valueOf(o: ReturnType<FormSettings['toJSON']>): 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),
settings.propertiesValueOf(o.properties),
settings.blacklistValueOf(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?: JSONSettings;
private form?: FormSettings;
constructor({
source, json, form
}: {
source: SettingSource,
json?: JSONSettings,
form?: FormSettings,
}) {
this.source = source;
this.json = json;
this.form = form;
}
getSource(): SettingSource {
return this.source;
}
getJSON(): JSONSettings {
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 JSONSettings).toJSON(),
};
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 valueOf(o: {
source: string;
json?: string;
form?: ReturnType<FormSettings['toJSON']>;
}): SettingData {
switch (o.source) {
case SettingSource.JSON:
return new SettingData({
source: o.source,
json: JSONSettings.valueOf(
o.json as ReturnType<JSONSettings['toJSON']>),
});
case SettingSource.Form:
return new SettingData({
source: o.source,
form: FormSettings.valueOf(
o.form as ReturnType<FormSettings['toJSON']>),
});
}
throw new Error(`unknown settings source: ${o.source}`);
}
}
export const DefaultSettingData: SettingData = SettingData.valueOf({
source: 'json',
json: `{
"keymaps": {
"0": { "type": "scroll.home" },
":": { "type": "command.show" },
"o": { "type": "command.show.open", "alter": false },
"O": { "type": "command.show.open", "alter": true },
"t": { "type": "command.show.tabopen", "alter": false },
"T": { "type": "command.show.tabopen", "alter": true },
"w": { "type": "command.show.winopen", "alter": false },
"W": { "type": "command.show.winopen", "alter": true },
"b": { "type": "command.show.buffer" },
"a": { "type": "command.show.addbookmark", "alter": true },
"k": { "type": "scroll.vertically", "count": -1 },
"j": { "type": "scroll.vertically", "count": 1 },
"h": { "type": "scroll.horizonally", "count": -1 },
"l": { "type": "scroll.horizonally", "count": 1 },
"<C-U>": { "type": "scroll.pages", "count": -0.5 },
"<C-D>": { "type": "scroll.pages", "count": 0.5 },
"<C-B>": { "type": "scroll.pages", "count": -1 },
"<C-F>": { "type": "scroll.pages", "count": 1 },
"gg": { "type": "scroll.top" },
"G": { "type": "scroll.bottom" },
"$": { "type": "scroll.end" },
"d": { "type": "tabs.close" },
"D": { "type": "tabs.close", "select": "left" },
"x$": { "type": "tabs.close.right" },
"!d": { "type": "tabs.close.force" },
"u": { "type": "tabs.reopen" },
"K": { "type": "tabs.prev" },
"J": { "type": "tabs.next" },
"gT": { "type": "tabs.prev" },
"gt": { "type": "tabs.next" },
"g0": { "type": "tabs.first" },
"g$": { "type": "tabs.last" },
"<C-6>": { "type": "tabs.prevsel" },
"r": { "type": "tabs.reload", "cache": false },
"R": { "type": "tabs.reload", "cache": true },
"zp": { "type": "tabs.pin.toggle" },
"zd": { "type": "tabs.duplicate" },
"zi": { "type": "zoom.in" },
"zo": { "type": "zoom.out" },
"zz": { "type": "zoom.neutral" },
"f": { "type": "follow.start", "newTab": false },
"F": { "type": "follow.start", "newTab": true, "background": false },
"m": { "type": "mark.set.prefix" },
"'": { "type": "mark.jump.prefix" },
"H": { "type": "navigate.history.prev" },
"L": { "type": "navigate.history.next" },
"[[": { "type": "navigate.link.prev" },
"]]": { "type": "navigate.link.next" },
"gu": { "type": "navigate.parent" },
"gU": { "type": "navigate.root" },
"gi": { "type": "focus.input" },
"gf": { "type": "page.source" },
"gh": { "type": "page.home" },
"gH": { "type": "page.home", "newTab": true },
"y": { "type": "urls.yank" },
"p": { "type": "urls.paste", "newTab": false },
"P": { "type": "urls.paste", "newTab": true },
"/": { "type": "find.start" },
"n": { "type": "find.next" },
"N": { "type": "find.prev" },
".": { "type": "repeat.last" },
"<S-Esc>": { "type": "addon.toggle.enabled" }
},
"search": {
"default": "google",
"engines": {
"google": "https://google.com/search?q={}",
"yahoo": "https://search.yahoo.com/search?p={}",
"bing": "https://www.bing.com/search?q={}",
"duckduckgo": "https://duckduckgo.com/?q={}",
"twitter": "https://twitter.com/search?q={}",
"wikipedia": "https://en.wikipedia.org/w/index.php?search={}"
}
},
"properties": {
"hintchars": "abcdefghijklmnopqrstuvwxyz",
"smoothscroll": false,
"complete": "sbh"
},
"blacklist": [
]
}`,
});
|