-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginbuilder.ts
181 lines (152 loc) · 4.08 KB
/
pluginbuilder.ts
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
import * as key from 'keymaster';
const DOMAIN_REGEX = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/;
class PluginState {
state: any;
constructor(state: any) {
for (let key in state) {
this[key] = state[key]
}
}
getFullState() : any {
return this;
}
set(obj: any) {
for (let key in obj) {
this[key] = obj[key];
}
}
}
// Plugin manages a set of keyboard shortcuts for a set of domain
// Note: stores shortcuts in DS
export class Plugin {
pluginName: string; // Should be the same as the filename
domains: string[];
shortcuts: any[]; // DS
pluginState: any;
constructor(pluginName: string, domains: string[], shortcuts: any[], state: any) {
this.pluginName = pluginName;
this.domains = domains;
this.shortcuts = shortcuts; // DS
this.pluginState = new PluginState(state);
this.init();
}
init() {
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.loadShortcuts) {
key.filter = (event) => true;
for (let s of this.shortcuts) {
key(s.keys.join(', '), (event, handler) => {
s.action(event, this.pluginState);
chrome.runtime.sendMessage({
logEvent: true,
eventCategory: 'shortcut-triggered',
eventAction: this.pluginName,
eventLabel: s.keys.join(', ')
});
});
}
}
});
}
// MDS getter for frontend
// returns a list of shortcut objects in MDS
getShortcutsMDS() : object[] {
return this.shortcuts.map(s => {
let MDS = s.keys.map(key => { return {default: [key]}; });
return {
name: s.name,
keys: MDS
};
});
}
listShortcuts() : any[] {
// Only include name and keys
return this.shortcuts.map((s) => {
return {
name: s.name,
keys: s.keys
};
});
}
// getShortcut(name: string) : any {
// return this.shortcuts[name];
// }
}
// Provides an API for third party developers to create customs plugins for a set of domains
// Note: PluginBuilder only handles shortcuts defined in developer schema (DS)
export class PluginBuilder {
pluginName: string;
domains: string[];
// PluginBuilder requires shortcuts to be in DS
shortcuts: any;
state: any;
constructor() {
this.shortcuts = {};
this.state = {};
this.domains = [];
}
// TODO: Handle scopes from keymaster
registerShortcut(name: string,
keys: string | string[],
action: Function) : void {
if (!name) {
throw 'Must include a name.';
}
if (typeof keys === 'string') {
keys = [keys];
}
let config = {
keys: keys,
action: action
}
this.validateConfig(config);
this.shortcuts[name] = config;
}
setPluginName(pluginName: string) : void {
this.pluginName = pluginName;
}
addDomainName(domainName: string) : void {
if (!DOMAIN_REGEX.test(domainName)) {
throw 'Not a valid domain';
}
this.domains.push(domainName);
}
validateConfig(config: any) : boolean {
// Structure: {
// 'keys': string[],
// 'action': <function Function>
// }
// Validate keys
if (config.keys.constructor !== Array) {
throw 'Invalid or missing keys';
}
// Validate action
if (typeof config['action'] !== 'function') {
throw 'Invalid or missing action';
}
return true;
}
setInitialState(state: any) {
this.state = state;
}
build() : Plugin {
if (!this.pluginName) {
throw 'Plugin name is missing'
}
if (this.domains.length === 0) {
throw 'Domain name is missing for plugin: ' + this.pluginName;
}
if (Object.keys(this.shortcuts).length === 0) {
throw 'You need at least one shortcut for a plugin.';
}
let shortcuts = [];
for (let name in this.shortcuts) {
shortcuts.push({
name: name,
keys: this.shortcuts[name].keys,
action: this.shortcuts[name].action
});
}
return new Plugin(this.pluginName, this.domains, shortcuts, this.state);
}
}