generated from mengxinssfd/lib-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeymap.ts
330 lines (313 loc) · 8.22 KB
/
Keymap.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
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
import type { HandledKeyOptions, KeyOptions, StrategyType } from './types';
import { keymapStrategy } from './strategy';
import { handleKeys, castArray, defaultKeyAliasMap } from './utils';
/**
* Keymap配置项
*/
export interface KeymapConfig {
/**
* 按键监听事件绑定对象
*/
el?: HTMLElement | Window;
/**
* 快捷键策略
*/
strategy?: StrategyType;
/**
* 配置别名
*
* @see defaultKeyAliasMap
*/
keyAliasMap?: Record<string, string>;
}
/**
* 主类
*/
export class Keymap {
/**
* 按键别名映射map
*
* @see defaultKeyAliasMap
*/
private _keyAliasMap: Record<string, string>;
/**
* 已注册的所有组合键
*/
private readonly registeredMaps!: HandledKeyOptions[];
/**
* 取消所有注册,并移除事件监听
*/
private readonly canceler: Function;
/**
* 使用默认配置
*
* @example
* ```ts
* new Keymap([{ desc:'select all', keys: 'Control+a', handler() {\/* do something *\/} }]);
* new Keymap([{ desc:'select all', keys: ['Control+a','Control+b'], handler() {\/* do something *\/} }]);
* ```
* @param maps 事件绑定选项数组
*/
constructor(maps: KeyOptions[]);
/**
* 使用自定义配置
*
* @example
* ```ts
* new Keymap({el:window, strategy:'recordCompose'},[{ desc:'select all', keys: 'Control+a', handler() {\/* do something *\/} }]);
* ```
* @param config
* @param [config.el=window] 绑定的dom对象,默认为window
* @param [config.strategy='recordCompose'] 绑定策略,分为记录全部(recordAll)和只记录组合键(recordCompose),默认为只记录组合键
* @param maps 事件绑定选项数组
*/
constructor(config: KeymapConfig, maps?: KeyOptions[]);
constructor(...args: any[]) {
const config: Required<KeymapConfig> = {
el: window,
strategy: 'recordCompose',
keyAliasMap: defaultKeyAliasMap,
};
let maps: KeyOptions[];
if (args.length === 2) {
Object.assign(config, args[0]);
maps = args[1];
} else maps = args[0];
this._keyAliasMap = config.keyAliasMap;
// 处理keys
this.registeredMaps = this.handleMaps(maps);
this.canceler = keymapStrategy[config.strategy](config.el, this.registeredMaps);
}
/**
* 获取按键别名配置
*
* 注意⚠️:由于复制了一份obj,所以使用Object(keyAliasMap,{})不会起效
*/
get keyAliasMap(): Record<string, string> {
return { ...this._keyAliasMap };
}
/**
* 设置按键别名
*
* @example
* ```ts
*
* const km = new Keymap([{ desc: 'test', keys: ['Control + a', 'Meta + a'], handler: () => {} }]);
*
* // [
* // {
* // desc: 'test',
* // keyList: ['control', 'a'],
* // keys: 'control + a',
* // rawKeys: ['Control + a', 'Meta + a'],
* // },
* // {
* // desc: 'test',
* // keyList: ['meta', 'a'],
* // keys: 'meta + a',
* // rawKeys: ['Control + a', 'Meta + a'],
* // },
* // ]
* console.log(km.maps);
*
* console.log(km.has('ctrl+a')); // true
*
* // 使用 Object.assign(km.keyAliasMap, { ctrl: '' }) 不起作用
* Object.assign(km.keyAliasMap, { ctrl: '' });
* console.log(km.has('ctrl+a')); // true
*
* // 清理所有别名
* km.keyAliasMap = {};
* // ctrl不再是Control的别名,所以 ctrl+a 不存在
* console.log(km.has('ctrl+a')); // false
*
* // 让control成为alt的别名
* console.log(km.has('alt+a')); // false
* km.keyAliasMap = { control: 'alt' };
* console.log(km.has('alt+a')); // true
*
* // [
* // {
* // desc: 'test',
* // keyList: ['alt', 'a'],
* // keys: 'control + a',
* // rawKeys: ['Control + a', 'Meta + a'],
* // },
* // {
* // desc: 'test',
* // keyList: ['meta', 'a'],
* // keys: 'meta + a',
* // rawKeys: ['Control + a', 'Meta + a'],
* // },
* // ]
* console.log(km.maps);
* ```
*/
set keyAliasMap(alias: Record<string, string>) {
this._keyAliasMap = { ...alias };
const originMaps: KeyOptions[] = [];
this.registeredMaps.forEach((m) => {
if (originMaps.find((o) => o.keys === m.rawKeys)) return;
const keyOptions: KeyOptions = { keys: m.rawKeys, handler: m.handler };
if (m.desc) keyOptions.desc = m.desc;
originMaps.push(keyOptions);
});
const registeredMaps = this.handleMaps(originMaps);
this.registeredMaps.length = 0;
this.registeredMaps.push(...registeredMaps);
}
/**
* 处理组合键数组
*/
private handleMaps(maps: KeyOptions[]) {
return maps.reduce((res, item) => {
const keysList = castArray(item.keys);
keysList.forEach((k) => {
const [keys, keyList] = handleKeys(k, this._keyAliasMap);
const index = this.findIndex(k, [keys, keyList], res);
// 后面不能覆盖前面的
if (index !== -1) {
console.warn('Keymap: `' + res[index]?.keys + '` duplicate');
return;
}
res.push({ ...item, rawKeys: item.keys, keys, keyList });
});
return res;
}, [] as HandledKeyOptions[]);
}
/**
* 根据keys查找所在index
*/
private findIndex(
keys: string,
handledKeys = handleKeys(keys, this._keyAliasMap),
registeredMaps = this.registeredMaps,
): number {
return registeredMaps.findIndex((map) => {
if (map.rawKeys === keys) return true;
if (map.keys === handledKeys[0]) return true;
return (
handledKeys[1].length === map.keyList.length &&
handledKeys[1].every((key) => map.keyList.includes(key))
);
});
}
/**
* 手动触发快捷键
*
* @example
*
* ```ts
* let count = 0;
* const km = new Keymap([{ keys: 'Control+a', handler: ()=> count++ }]);
*
* km.trigger('Control+a');
* console.log(count); // 1
*
* km.trigger('Ctrl+a');
* console.log(count); // 2
* ```
*/
trigger(keys: string) {
const index = this.findIndex(keys);
this.registeredMaps[index]?.handler();
}
/**
* 可用于显示所有的快捷键
* @returns 所有已注册快捷键
*/
get maps(): Omit<HandledKeyOptions, 'handler'>[] {
return JSON.parse(JSON.stringify(this.registeredMaps));
}
/**
* 判断快捷键是否已经被绑定
*
* @example
* ```ts
* const km = new Keymap([{ keys: 'Control+a', handler: () => {} }]);
* console.log(km.has('Control+a')); // true
* console.log(km.has('control+a')); // true
* console.log(km.has('ctrl+a')); // true
* console.log(km.has('ctrl+b')); // false
* ```
*/
has(keys: string): boolean {
return this.findIndex(keys) > -1;
}
/**
* 添加快捷键
*
* @example
* ```ts
* let count = 0;
* const km = new Keymap([{ keys: 'Control+a', handler: () => {} }]);
*
* km.add({ keys: 'ctrl+b', handler: () => count++ });
* console.log(km.has('ctrl+b')); // true
*
* km.trigger('Control+b');
* console.log(count) // 1;
* ```
*
* @returns 是否添加成功数量
*/
add(map: KeyOptions): number {
const keys = castArray(map.keys).filter((keys) => !this.has(keys));
if (keys.length > 0)
this.registeredMaps.push(
...this.handleMaps([
{
...map,
keys: Array.isArray(map.keys)
? keys.length === 1
? (keys[0] as string)
: keys
: map.keys,
},
]),
);
return keys.length;
}
/**
* 移除快捷键键
*
* @example
*```ts
* let count = 0;
* const km = new Keymap([
* { keys: 'Control+a', handler: () => {} },
* { keys: 'Control+b', handler: () => count++ },
* ]);
*
* km.trigger('ctrl+a');
* km.trigger('ctrl+b');
*
* console.log(count); // 1
*
* km.remove('ctrl+b');
*
* km.trigger('ctrl+a');
* km.trigger('ctrl+b');
*
* console.log(count); // 1
*
* ```
*/
remove(keys: string): void {
const index = this.findIndex(keys);
if (index === -1) return;
this.registeredMaps.splice(index, 1);
}
/**
* 销毁
*
* 之后再调用add会报错
*/
destroy() {
this.canceler();
this.registeredMaps.length = 0;
(this.add as any) = () => {
throw new Error('destroyed');
};
}
}