timvir 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/index.js +19 -623
- package/package.json +3 -2
package/core/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { pipe, filter
|
|
1
|
+
import { pipe, filter, subscribe } from 'wonka';
|
|
2
2
|
import { useContext, Provider } from 'timvir/context';
|
|
3
3
|
export { useContext } from 'timvir/context';
|
|
4
4
|
import * as React from 'react';
|
|
5
|
-
import React__default
|
|
5
|
+
import React__default from 'react';
|
|
6
6
|
import { useImmer } from 'use-immer';
|
|
7
7
|
import { MDXProvider } from '@mdx-js/react';
|
|
8
|
+
import { useHotkeys } from 'react-hotkeys-hook';
|
|
8
9
|
import { makeBus } from 'timvir/bus';
|
|
9
10
|
import { castDraft } from 'immer';
|
|
10
11
|
import * as ReactDOM from 'react-dom';
|
|
@@ -105,605 +106,6 @@ const classes$5 = {
|
|
|
105
106
|
meta: "m1ojti9g"
|
|
106
107
|
};
|
|
107
108
|
|
|
108
|
-
/**!
|
|
109
|
-
* hotkeys-js v3.9.4
|
|
110
|
-
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
|
|
111
|
-
*
|
|
112
|
-
* Copyright (c) 2022 kenny wong <wowohoo@qq.com>
|
|
113
|
-
* http://jaywcjlove.github.io/hotkeys
|
|
114
|
-
* Licensed under the MIT license
|
|
115
|
-
*/
|
|
116
|
-
|
|
117
|
-
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件
|
|
118
|
-
|
|
119
|
-
function addEvent(object, event, method, useCapture) {
|
|
120
|
-
if (object.addEventListener) {
|
|
121
|
-
object.addEventListener(event, method, useCapture);
|
|
122
|
-
} else if (object.attachEvent) {
|
|
123
|
-
object.attachEvent("on".concat(event), function () {
|
|
124
|
-
method(window.event);
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
} // 修饰键转换成对应的键码
|
|
128
|
-
|
|
129
|
-
function getMods(modifier, key) {
|
|
130
|
-
var mods = key.slice(0, key.length - 1);
|
|
131
|
-
for (var i = 0; i < mods.length; i++) {
|
|
132
|
-
mods[i] = modifier[mods[i].toLowerCase()];
|
|
133
|
-
}
|
|
134
|
-
return mods;
|
|
135
|
-
} // 处理传的key字符串转换成数组
|
|
136
|
-
|
|
137
|
-
function getKeys(key) {
|
|
138
|
-
if (typeof key !== 'string') key = '';
|
|
139
|
-
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等
|
|
140
|
-
|
|
141
|
-
var keys = key.split(','); // 同时设置多个快捷键,以','分割
|
|
142
|
-
|
|
143
|
-
var index = keys.lastIndexOf(''); // 快捷键可能包含',',需特殊处理
|
|
144
|
-
|
|
145
|
-
for (; index >= 0;) {
|
|
146
|
-
keys[index - 1] += ',';
|
|
147
|
-
keys.splice(index, 1);
|
|
148
|
-
index = keys.lastIndexOf('');
|
|
149
|
-
}
|
|
150
|
-
return keys;
|
|
151
|
-
} // 比较修饰键的数组
|
|
152
|
-
|
|
153
|
-
function compareArray(a1, a2) {
|
|
154
|
-
var arr1 = a1.length >= a2.length ? a1 : a2;
|
|
155
|
-
var arr2 = a1.length >= a2.length ? a2 : a1;
|
|
156
|
-
var isIndex = true;
|
|
157
|
-
for (var i = 0; i < arr1.length; i++) {
|
|
158
|
-
if (arr2.indexOf(arr1[i]) === -1) isIndex = false;
|
|
159
|
-
}
|
|
160
|
-
return isIndex;
|
|
161
|
-
}
|
|
162
|
-
var _keyMap = {
|
|
163
|
-
backspace: 8,
|
|
164
|
-
tab: 9,
|
|
165
|
-
clear: 12,
|
|
166
|
-
enter: 13,
|
|
167
|
-
return: 13,
|
|
168
|
-
esc: 27,
|
|
169
|
-
escape: 27,
|
|
170
|
-
space: 32,
|
|
171
|
-
left: 37,
|
|
172
|
-
up: 38,
|
|
173
|
-
right: 39,
|
|
174
|
-
down: 40,
|
|
175
|
-
del: 46,
|
|
176
|
-
delete: 46,
|
|
177
|
-
ins: 45,
|
|
178
|
-
insert: 45,
|
|
179
|
-
home: 36,
|
|
180
|
-
end: 35,
|
|
181
|
-
pageup: 33,
|
|
182
|
-
pagedown: 34,
|
|
183
|
-
capslock: 20,
|
|
184
|
-
num_0: 96,
|
|
185
|
-
num_1: 97,
|
|
186
|
-
num_2: 98,
|
|
187
|
-
num_3: 99,
|
|
188
|
-
num_4: 100,
|
|
189
|
-
num_5: 101,
|
|
190
|
-
num_6: 102,
|
|
191
|
-
num_7: 103,
|
|
192
|
-
num_8: 104,
|
|
193
|
-
num_9: 105,
|
|
194
|
-
num_multiply: 106,
|
|
195
|
-
num_add: 107,
|
|
196
|
-
num_enter: 108,
|
|
197
|
-
num_subtract: 109,
|
|
198
|
-
num_decimal: 110,
|
|
199
|
-
num_divide: 111,
|
|
200
|
-
'⇪': 20,
|
|
201
|
-
',': 188,
|
|
202
|
-
'.': 190,
|
|
203
|
-
'/': 191,
|
|
204
|
-
'`': 192,
|
|
205
|
-
'-': isff ? 173 : 189,
|
|
206
|
-
'=': isff ? 61 : 187,
|
|
207
|
-
';': isff ? 59 : 186,
|
|
208
|
-
'\'': 222,
|
|
209
|
-
'[': 219,
|
|
210
|
-
']': 221,
|
|
211
|
-
'\\': 220
|
|
212
|
-
}; // Modifier Keys
|
|
213
|
-
|
|
214
|
-
var _modifier = {
|
|
215
|
-
// shiftKey
|
|
216
|
-
'⇧': 16,
|
|
217
|
-
shift: 16,
|
|
218
|
-
// altKey
|
|
219
|
-
'⌥': 18,
|
|
220
|
-
alt: 18,
|
|
221
|
-
option: 18,
|
|
222
|
-
// ctrlKey
|
|
223
|
-
'⌃': 17,
|
|
224
|
-
ctrl: 17,
|
|
225
|
-
control: 17,
|
|
226
|
-
// metaKey
|
|
227
|
-
'⌘': 91,
|
|
228
|
-
cmd: 91,
|
|
229
|
-
command: 91
|
|
230
|
-
};
|
|
231
|
-
var modifierMap = {
|
|
232
|
-
16: 'shiftKey',
|
|
233
|
-
18: 'altKey',
|
|
234
|
-
17: 'ctrlKey',
|
|
235
|
-
91: 'metaKey',
|
|
236
|
-
shiftKey: 16,
|
|
237
|
-
ctrlKey: 17,
|
|
238
|
-
altKey: 18,
|
|
239
|
-
metaKey: 91
|
|
240
|
-
};
|
|
241
|
-
var _mods = {
|
|
242
|
-
16: false,
|
|
243
|
-
18: false,
|
|
244
|
-
17: false,
|
|
245
|
-
91: false
|
|
246
|
-
};
|
|
247
|
-
var _handlers = {}; // F1~F12 special key
|
|
248
|
-
|
|
249
|
-
for (var k = 1; k < 20; k++) {
|
|
250
|
-
_keyMap["f".concat(k)] = 111 + k;
|
|
251
|
-
}
|
|
252
|
-
var _downKeys = []; // 记录摁下的绑定键
|
|
253
|
-
|
|
254
|
-
var winListendFocus = false; // window是否已经监听了focus事件
|
|
255
|
-
|
|
256
|
-
var _scope = 'all'; // 默认热键范围
|
|
257
|
-
|
|
258
|
-
var elementHasBindEvent = []; // 已绑定事件的节点记录
|
|
259
|
-
// 返回键码
|
|
260
|
-
|
|
261
|
-
var code$1 = function code(x) {
|
|
262
|
-
return _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
|
|
263
|
-
}; // 设置获取当前范围(默认为'所有')
|
|
264
|
-
|
|
265
|
-
function setScope(scope) {
|
|
266
|
-
_scope = scope || 'all';
|
|
267
|
-
} // 获取当前范围
|
|
268
|
-
|
|
269
|
-
function getScope() {
|
|
270
|
-
return _scope || 'all';
|
|
271
|
-
} // 获取摁下绑定键的键值
|
|
272
|
-
|
|
273
|
-
function getPressedKeyCodes() {
|
|
274
|
-
return _downKeys.slice(0);
|
|
275
|
-
} // 表单控件控件判断 返回 Boolean
|
|
276
|
-
// hotkey is effective only when filter return true
|
|
277
|
-
|
|
278
|
-
function filter(event) {
|
|
279
|
-
var target = event.target || event.srcElement;
|
|
280
|
-
var tagName = target.tagName;
|
|
281
|
-
var flag = true; // ignore: isContentEditable === 'true', <input> and <textarea> when readOnly state is false, <select>
|
|
282
|
-
|
|
283
|
-
if (target.isContentEditable || (tagName === 'INPUT' || tagName === 'TEXTAREA' || tagName === 'SELECT') && !target.readOnly) {
|
|
284
|
-
flag = false;
|
|
285
|
-
}
|
|
286
|
-
return flag;
|
|
287
|
-
} // 判断摁下的键是否为某个键,返回true或者false
|
|
288
|
-
|
|
289
|
-
function isPressed(keyCode) {
|
|
290
|
-
if (typeof keyCode === 'string') {
|
|
291
|
-
keyCode = code$1(keyCode); // 转换成键码
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
return _downKeys.indexOf(keyCode) !== -1;
|
|
295
|
-
} // 循环删除handlers中的所有 scope(范围)
|
|
296
|
-
|
|
297
|
-
function deleteScope(scope, newScope) {
|
|
298
|
-
var handlers;
|
|
299
|
-
var i; // 没有指定scope,获取scope
|
|
300
|
-
|
|
301
|
-
if (!scope) scope = getScope();
|
|
302
|
-
for (var key in _handlers) {
|
|
303
|
-
if (Object.prototype.hasOwnProperty.call(_handlers, key)) {
|
|
304
|
-
handlers = _handlers[key];
|
|
305
|
-
for (i = 0; i < handlers.length;) {
|
|
306
|
-
if (handlers[i].scope === scope) handlers.splice(i, 1);else i++;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
} // 如果scope被删除,将scope重置为all
|
|
310
|
-
|
|
311
|
-
if (getScope() === scope) setScope(newScope || 'all');
|
|
312
|
-
} // 清除修饰键
|
|
313
|
-
|
|
314
|
-
function clearModifier(event) {
|
|
315
|
-
var key = event.keyCode || event.which || event.charCode;
|
|
316
|
-
var i = _downKeys.indexOf(key); // 从列表中清除按压过的键
|
|
317
|
-
|
|
318
|
-
if (i >= 0) {
|
|
319
|
-
_downKeys.splice(i, 1);
|
|
320
|
-
} // 特殊处理 cmmand 键,在 cmmand 组合快捷键 keyup 只执行一次的问题
|
|
321
|
-
|
|
322
|
-
if (event.key && event.key.toLowerCase() === 'meta') {
|
|
323
|
-
_downKeys.splice(0, _downKeys.length);
|
|
324
|
-
} // 修饰键 shiftKey altKey ctrlKey (command||metaKey) 清除
|
|
325
|
-
|
|
326
|
-
if (key === 93 || key === 224) key = 91;
|
|
327
|
-
if (key in _mods) {
|
|
328
|
-
_mods[key] = false; // 将修饰键重置为false
|
|
329
|
-
|
|
330
|
-
for (var k in _modifier) {
|
|
331
|
-
if (_modifier[k] === key) hotkeys[k] = false;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
function unbind(keysInfo) {
|
|
336
|
-
// unbind(), unbind all keys
|
|
337
|
-
if (typeof keysInfo === 'undefined') {
|
|
338
|
-
Object.keys(_handlers).forEach(function (key) {
|
|
339
|
-
return delete _handlers[key];
|
|
340
|
-
});
|
|
341
|
-
} else if (Array.isArray(keysInfo)) {
|
|
342
|
-
// support like : unbind([{key: 'ctrl+a', scope: 's1'}, {key: 'ctrl-a', scope: 's2', splitKey: '-'}])
|
|
343
|
-
keysInfo.forEach(function (info) {
|
|
344
|
-
if (info.key) eachUnbind(info);
|
|
345
|
-
});
|
|
346
|
-
} else if (typeof keysInfo === 'object') {
|
|
347
|
-
// support like unbind({key: 'ctrl+a, ctrl+b', scope:'abc'})
|
|
348
|
-
if (keysInfo.key) eachUnbind(keysInfo);
|
|
349
|
-
} else if (typeof keysInfo === 'string') {
|
|
350
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
351
|
-
args[_key - 1] = arguments[_key];
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
// support old method
|
|
355
|
-
// eslint-disable-line
|
|
356
|
-
var scope = args[0],
|
|
357
|
-
method = args[1];
|
|
358
|
-
if (typeof scope === 'function') {
|
|
359
|
-
method = scope;
|
|
360
|
-
scope = '';
|
|
361
|
-
}
|
|
362
|
-
eachUnbind({
|
|
363
|
-
key: keysInfo,
|
|
364
|
-
scope: scope,
|
|
365
|
-
method: method,
|
|
366
|
-
splitKey: '+'
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
|
-
} // 解除绑定某个范围的快捷键
|
|
370
|
-
|
|
371
|
-
var eachUnbind = function eachUnbind(_ref) {
|
|
372
|
-
var key = _ref.key,
|
|
373
|
-
scope = _ref.scope,
|
|
374
|
-
method = _ref.method,
|
|
375
|
-
_ref$splitKey = _ref.splitKey,
|
|
376
|
-
splitKey = _ref$splitKey === void 0 ? '+' : _ref$splitKey;
|
|
377
|
-
var multipleKeys = getKeys(key);
|
|
378
|
-
multipleKeys.forEach(function (originKey) {
|
|
379
|
-
var unbindKeys = originKey.split(splitKey);
|
|
380
|
-
var len = unbindKeys.length;
|
|
381
|
-
var lastKey = unbindKeys[len - 1];
|
|
382
|
-
var keyCode = lastKey === '*' ? '*' : code$1(lastKey);
|
|
383
|
-
if (!_handlers[keyCode]) return; // 判断是否传入范围,没有就获取范围
|
|
384
|
-
|
|
385
|
-
if (!scope) scope = getScope();
|
|
386
|
-
var mods = len > 1 ? getMods(_modifier, unbindKeys) : [];
|
|
387
|
-
_handlers[keyCode] = _handlers[keyCode].filter(function (record) {
|
|
388
|
-
// 通过函数判断,是否解除绑定,函数相等直接返回
|
|
389
|
-
var isMatchingMethod = method ? record.method === method : true;
|
|
390
|
-
return !(isMatchingMethod && record.scope === scope && compareArray(record.mods, mods));
|
|
391
|
-
});
|
|
392
|
-
});
|
|
393
|
-
}; // 对监听对应快捷键的回调函数进行处理
|
|
394
|
-
|
|
395
|
-
function eventHandler(event, handler, scope, element) {
|
|
396
|
-
if (handler.element !== element) {
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
var modifiersMatch; // 看它是否在当前范围
|
|
400
|
-
|
|
401
|
-
if (handler.scope === scope || handler.scope === 'all') {
|
|
402
|
-
// 检查是否匹配修饰符(如果有返回true)
|
|
403
|
-
modifiersMatch = handler.mods.length > 0;
|
|
404
|
-
for (var y in _mods) {
|
|
405
|
-
if (Object.prototype.hasOwnProperty.call(_mods, y)) {
|
|
406
|
-
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) {
|
|
407
|
-
modifiersMatch = false;
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
} // 调用处理程序,如果是修饰键不做处理
|
|
411
|
-
|
|
412
|
-
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') {
|
|
413
|
-
if (handler.method(event, handler) === false) {
|
|
414
|
-
if (event.preventDefault) event.preventDefault();else event.returnValue = false;
|
|
415
|
-
if (event.stopPropagation) event.stopPropagation();
|
|
416
|
-
if (event.cancelBubble) event.cancelBubble = true;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
} // 处理keydown事件
|
|
421
|
-
|
|
422
|
-
function dispatch(event, element) {
|
|
423
|
-
var asterisk = _handlers['*'];
|
|
424
|
-
var key = event.keyCode || event.which || event.charCode; // 表单控件过滤 默认表单控件不触发快捷键
|
|
425
|
-
|
|
426
|
-
if (!hotkeys.filter.call(this, event)) return; // Gecko(Firefox)的command键值224,在Webkit(Chrome)中保持一致
|
|
427
|
-
// Webkit左右 command 键值不一样
|
|
428
|
-
|
|
429
|
-
if (key === 93 || key === 224) key = 91;
|
|
430
|
-
/**
|
|
431
|
-
* Collect bound keys
|
|
432
|
-
* If an Input Method Editor is processing key input and the event is keydown, return 229.
|
|
433
|
-
* https://stackoverflow.com/questions/25043934/is-it-ok-to-ignore-keydown-events-with-keycode-229
|
|
434
|
-
* http://lists.w3.org/Archives/Public/www-dom/2010JulSep/att-0182/keyCode-spec.html
|
|
435
|
-
*/
|
|
436
|
-
|
|
437
|
-
if (_downKeys.indexOf(key) === -1 && key !== 229) _downKeys.push(key);
|
|
438
|
-
/**
|
|
439
|
-
* Jest test cases are required.
|
|
440
|
-
* ===============================
|
|
441
|
-
*/
|
|
442
|
-
|
|
443
|
-
['ctrlKey', 'altKey', 'shiftKey', 'metaKey'].forEach(function (keyName) {
|
|
444
|
-
var keyNum = modifierMap[keyName];
|
|
445
|
-
if (event[keyName] && _downKeys.indexOf(keyNum) === -1) {
|
|
446
|
-
_downKeys.push(keyNum);
|
|
447
|
-
} else if (!event[keyName] && _downKeys.indexOf(keyNum) > -1) {
|
|
448
|
-
_downKeys.splice(_downKeys.indexOf(keyNum), 1);
|
|
449
|
-
} else if (keyName === 'metaKey' && event[keyName] && _downKeys.length === 3) {
|
|
450
|
-
/**
|
|
451
|
-
* Fix if Command is pressed:
|
|
452
|
-
* ===============================
|
|
453
|
-
*/
|
|
454
|
-
if (!(event.ctrlKey || event.shiftKey || event.altKey)) {
|
|
455
|
-
_downKeys = _downKeys.slice(_downKeys.indexOf(keyNum));
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
/**
|
|
460
|
-
* -------------------------------
|
|
461
|
-
*/
|
|
462
|
-
|
|
463
|
-
if (key in _mods) {
|
|
464
|
-
_mods[key] = true; // 将特殊字符的key注册到 hotkeys 上
|
|
465
|
-
|
|
466
|
-
for (var k in _modifier) {
|
|
467
|
-
if (_modifier[k] === key) hotkeys[k] = true;
|
|
468
|
-
}
|
|
469
|
-
if (!asterisk) return;
|
|
470
|
-
} // 将 modifierMap 里面的修饰键绑定到 event 中
|
|
471
|
-
|
|
472
|
-
for (var e in _mods) {
|
|
473
|
-
if (Object.prototype.hasOwnProperty.call(_mods, e)) {
|
|
474
|
-
_mods[e] = event[modifierMap[e]];
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
/**
|
|
478
|
-
* https://github.com/jaywcjlove/hotkeys/pull/129
|
|
479
|
-
* This solves the issue in Firefox on Windows where hotkeys corresponding to special characters would not trigger.
|
|
480
|
-
* An example of this is ctrl+alt+m on a Swedish keyboard which is used to type μ.
|
|
481
|
-
* Browser support: https://caniuse.com/#feat=keyboardevent-getmodifierstate
|
|
482
|
-
*/
|
|
483
|
-
|
|
484
|
-
if (event.getModifierState && !(event.altKey && !event.ctrlKey) && event.getModifierState('AltGraph')) {
|
|
485
|
-
if (_downKeys.indexOf(17) === -1) {
|
|
486
|
-
_downKeys.push(17);
|
|
487
|
-
}
|
|
488
|
-
if (_downKeys.indexOf(18) === -1) {
|
|
489
|
-
_downKeys.push(18);
|
|
490
|
-
}
|
|
491
|
-
_mods[17] = true;
|
|
492
|
-
_mods[18] = true;
|
|
493
|
-
} // 获取范围 默认为 `all`
|
|
494
|
-
|
|
495
|
-
var scope = getScope(); // 对任何快捷键都需要做的处理
|
|
496
|
-
|
|
497
|
-
if (asterisk) {
|
|
498
|
-
for (var i = 0; i < asterisk.length; i++) {
|
|
499
|
-
if (asterisk[i].scope === scope && (event.type === 'keydown' && asterisk[i].keydown || event.type === 'keyup' && asterisk[i].keyup)) {
|
|
500
|
-
eventHandler(event, asterisk[i], scope, element);
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
} // key 不在 _handlers 中返回
|
|
504
|
-
|
|
505
|
-
if (!(key in _handlers)) return;
|
|
506
|
-
for (var _i = 0; _i < _handlers[key].length; _i++) {
|
|
507
|
-
if (event.type === 'keydown' && _handlers[key][_i].keydown || event.type === 'keyup' && _handlers[key][_i].keyup) {
|
|
508
|
-
if (_handlers[key][_i].key) {
|
|
509
|
-
var record = _handlers[key][_i];
|
|
510
|
-
var splitKey = record.splitKey;
|
|
511
|
-
var keyShortcut = record.key.split(splitKey);
|
|
512
|
-
var _downKeysCurrent = []; // 记录当前按键键值
|
|
513
|
-
|
|
514
|
-
for (var a = 0; a < keyShortcut.length; a++) {
|
|
515
|
-
_downKeysCurrent.push(code$1(keyShortcut[a]));
|
|
516
|
-
}
|
|
517
|
-
if (_downKeysCurrent.sort().join('') === _downKeys.sort().join('')) {
|
|
518
|
-
// 找到处理内容
|
|
519
|
-
eventHandler(event, record, scope, element);
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
}
|
|
524
|
-
} // 判断 element 是否已经绑定事件
|
|
525
|
-
|
|
526
|
-
function isElementBind(element) {
|
|
527
|
-
return elementHasBindEvent.indexOf(element) > -1;
|
|
528
|
-
}
|
|
529
|
-
function hotkeys(key, option, method) {
|
|
530
|
-
_downKeys = [];
|
|
531
|
-
var keys = getKeys(key); // 需要处理的快捷键列表
|
|
532
|
-
|
|
533
|
-
var mods = [];
|
|
534
|
-
var scope = 'all'; // scope默认为all,所有范围都有效
|
|
535
|
-
|
|
536
|
-
var element = document; // 快捷键事件绑定节点
|
|
537
|
-
|
|
538
|
-
var i = 0;
|
|
539
|
-
var keyup = false;
|
|
540
|
-
var keydown = true;
|
|
541
|
-
var splitKey = '+';
|
|
542
|
-
var capture = false; // 对为设定范围的判断
|
|
543
|
-
|
|
544
|
-
if (method === undefined && typeof option === 'function') {
|
|
545
|
-
method = option;
|
|
546
|
-
}
|
|
547
|
-
if (Object.prototype.toString.call(option) === '[object Object]') {
|
|
548
|
-
if (option.scope) scope = option.scope; // eslint-disable-line
|
|
549
|
-
|
|
550
|
-
if (option.element) element = option.element; // eslint-disable-line
|
|
551
|
-
|
|
552
|
-
if (option.keyup) keyup = option.keyup; // eslint-disable-line
|
|
553
|
-
|
|
554
|
-
if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line
|
|
555
|
-
|
|
556
|
-
if (option.capture !== undefined) capture = option.capture; // eslint-disable-line
|
|
557
|
-
|
|
558
|
-
if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
if (typeof option === 'string') scope = option; // 对于每个快捷键进行处理
|
|
562
|
-
|
|
563
|
-
for (; i < keys.length; i++) {
|
|
564
|
-
key = keys[i].split(splitKey); // 按键列表
|
|
565
|
-
|
|
566
|
-
mods = []; // 如果是组合快捷键取得组合快捷键
|
|
567
|
-
|
|
568
|
-
if (key.length > 1) mods = getMods(_modifier, key); // 将非修饰键转化为键码
|
|
569
|
-
|
|
570
|
-
key = key[key.length - 1];
|
|
571
|
-
key = key === '*' ? '*' : code$1(key); // *表示匹配所有快捷键
|
|
572
|
-
// 判断key是否在_handlers中,不在就赋一个空数组
|
|
573
|
-
|
|
574
|
-
if (!(key in _handlers)) _handlers[key] = [];
|
|
575
|
-
_handlers[key].push({
|
|
576
|
-
keyup: keyup,
|
|
577
|
-
keydown: keydown,
|
|
578
|
-
scope: scope,
|
|
579
|
-
mods: mods,
|
|
580
|
-
shortcut: keys[i],
|
|
581
|
-
method: method,
|
|
582
|
-
key: keys[i],
|
|
583
|
-
splitKey: splitKey,
|
|
584
|
-
element: element
|
|
585
|
-
});
|
|
586
|
-
} // 在全局document上设置快捷键
|
|
587
|
-
|
|
588
|
-
if (typeof element !== 'undefined' && !isElementBind(element) && window) {
|
|
589
|
-
elementHasBindEvent.push(element);
|
|
590
|
-
addEvent(element, 'keydown', function (e) {
|
|
591
|
-
dispatch(e, element);
|
|
592
|
-
}, capture);
|
|
593
|
-
if (!winListendFocus) {
|
|
594
|
-
winListendFocus = true;
|
|
595
|
-
addEvent(window, 'focus', function () {
|
|
596
|
-
_downKeys = [];
|
|
597
|
-
}, capture);
|
|
598
|
-
}
|
|
599
|
-
addEvent(element, 'keyup', function (e) {
|
|
600
|
-
dispatch(e, element);
|
|
601
|
-
clearModifier(e);
|
|
602
|
-
}, capture);
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
function trigger(shortcut) {
|
|
606
|
-
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all';
|
|
607
|
-
Object.keys(_handlers).forEach(function (key) {
|
|
608
|
-
var data = _handlers[key].find(function (item) {
|
|
609
|
-
return item.scope === scope && item.shortcut === shortcut;
|
|
610
|
-
});
|
|
611
|
-
if (data && data.method) {
|
|
612
|
-
data.method();
|
|
613
|
-
}
|
|
614
|
-
});
|
|
615
|
-
}
|
|
616
|
-
var _api = {
|
|
617
|
-
setScope: setScope,
|
|
618
|
-
getScope: getScope,
|
|
619
|
-
deleteScope: deleteScope,
|
|
620
|
-
getPressedKeyCodes: getPressedKeyCodes,
|
|
621
|
-
isPressed: isPressed,
|
|
622
|
-
filter: filter,
|
|
623
|
-
trigger: trigger,
|
|
624
|
-
unbind: unbind,
|
|
625
|
-
keyMap: _keyMap,
|
|
626
|
-
modifier: _modifier,
|
|
627
|
-
modifierMap: modifierMap
|
|
628
|
-
};
|
|
629
|
-
for (var a$1 in _api) {
|
|
630
|
-
if (Object.prototype.hasOwnProperty.call(_api, a$1)) {
|
|
631
|
-
hotkeys[a$1] = _api[a$1];
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
if (typeof window !== 'undefined') {
|
|
635
|
-
var _hotkeys = window.hotkeys;
|
|
636
|
-
hotkeys.noConflict = function (deep) {
|
|
637
|
-
if (deep && window.hotkeys === hotkeys) {
|
|
638
|
-
window.hotkeys = _hotkeys;
|
|
639
|
-
}
|
|
640
|
-
return hotkeys;
|
|
641
|
-
};
|
|
642
|
-
window.hotkeys = hotkeys;
|
|
643
|
-
}
|
|
644
|
-
|
|
645
|
-
hotkeys.filter = function () {
|
|
646
|
-
return true;
|
|
647
|
-
};
|
|
648
|
-
var tagFilter = function tagFilter(_ref, enableOnTags) {
|
|
649
|
-
var target = _ref.target;
|
|
650
|
-
var targetTagName = target && target.tagName;
|
|
651
|
-
return Boolean(targetTagName && enableOnTags && enableOnTags.includes(targetTagName));
|
|
652
|
-
};
|
|
653
|
-
var isKeyboardEventTriggeredByInput = function isKeyboardEventTriggeredByInput(ev) {
|
|
654
|
-
return tagFilter(ev, ['INPUT', 'TEXTAREA', 'SELECT']);
|
|
655
|
-
};
|
|
656
|
-
function useHotkeys(keys, callback, options, deps) {
|
|
657
|
-
if (options instanceof Array) {
|
|
658
|
-
deps = options;
|
|
659
|
-
options = undefined;
|
|
660
|
-
}
|
|
661
|
-
var _ref2 = options || {},
|
|
662
|
-
enableOnTags = _ref2.enableOnTags,
|
|
663
|
-
filter = _ref2.filter,
|
|
664
|
-
keyup = _ref2.keyup,
|
|
665
|
-
keydown = _ref2.keydown,
|
|
666
|
-
_ref2$filterPreventDe = _ref2.filterPreventDefault,
|
|
667
|
-
filterPreventDefault = _ref2$filterPreventDe === void 0 ? true : _ref2$filterPreventDe,
|
|
668
|
-
_ref2$enabled = _ref2.enabled,
|
|
669
|
-
enabled = _ref2$enabled === void 0 ? true : _ref2$enabled,
|
|
670
|
-
_ref2$enableOnContent = _ref2.enableOnContentEditable,
|
|
671
|
-
enableOnContentEditable = _ref2$enableOnContent === void 0 ? false : _ref2$enableOnContent;
|
|
672
|
-
var ref = useRef(null); // The return value of this callback determines if the browsers default behavior is prevented.
|
|
673
|
-
|
|
674
|
-
var memoisedCallback = useCallback(function (keyboardEvent, hotkeysEvent) {
|
|
675
|
-
var _keyboardEvent$target, _ref$current;
|
|
676
|
-
if (filter && !filter(keyboardEvent)) {
|
|
677
|
-
return !filterPreventDefault;
|
|
678
|
-
} // Check whether the hotkeys was triggered inside an input and that input is enabled or if it was triggered by a content editable tag and it is enabled.
|
|
679
|
-
|
|
680
|
-
if (isKeyboardEventTriggeredByInput(keyboardEvent) && !tagFilter(keyboardEvent, enableOnTags) || (_keyboardEvent$target = keyboardEvent.target) != null && _keyboardEvent$target.isContentEditable && !enableOnContentEditable) {
|
|
681
|
-
return true;
|
|
682
|
-
}
|
|
683
|
-
if (ref.current === null || document.activeElement === ref.current || (_ref$current = ref.current) != null && _ref$current.contains(document.activeElement)) {
|
|
684
|
-
callback(keyboardEvent, hotkeysEvent);
|
|
685
|
-
return true;
|
|
686
|
-
}
|
|
687
|
-
return false;
|
|
688
|
-
}, deps ? [ref, enableOnTags, filter].concat(deps) : [ref, enableOnTags, filter]);
|
|
689
|
-
useEffect(function () {
|
|
690
|
-
if (!enabled) {
|
|
691
|
-
hotkeys.unbind(keys, memoisedCallback);
|
|
692
|
-
return;
|
|
693
|
-
} // In this case keydown is likely undefined, so we set it to false, since hotkeys needs the `keydown` key to have a value.
|
|
694
|
-
|
|
695
|
-
if (keyup && keydown !== true) {
|
|
696
|
-
options.keydown = false;
|
|
697
|
-
}
|
|
698
|
-
hotkeys(keys, options || {}, memoisedCallback);
|
|
699
|
-
return function () {
|
|
700
|
-
return hotkeys.unbind(keys, memoisedCallback);
|
|
701
|
-
};
|
|
702
|
-
}, [memoisedCallback, keys, enabled]);
|
|
703
|
-
return ref;
|
|
704
|
-
}
|
|
705
|
-
hotkeys.isPressed;
|
|
706
|
-
|
|
707
109
|
const theme = "t1amubg3";
|
|
708
110
|
|
|
709
111
|
function Action(props) {
|
|
@@ -907,12 +309,12 @@ function Commands(props) {
|
|
|
907
309
|
close();
|
|
908
310
|
}
|
|
909
311
|
}, {
|
|
910
|
-
|
|
312
|
+
enableOnFormTags: ["INPUT"]
|
|
911
313
|
});
|
|
912
314
|
useHotkeys("escape", () => {
|
|
913
315
|
close();
|
|
914
316
|
}, {
|
|
915
|
-
|
|
317
|
+
enableOnFormTags: ["INPUT"]
|
|
916
318
|
});
|
|
917
319
|
|
|
918
320
|
/*
|
|
@@ -1357,26 +759,20 @@ function Page(props, ref) {
|
|
|
1357
759
|
blocks,
|
|
1358
760
|
toc
|
|
1359
761
|
}), [bus, location, Link, blocks, toc]);
|
|
1360
|
-
useHotkeys("
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
mutate(draft => {
|
|
1373
|
-
draft.search.open = false;
|
|
1374
|
-
});
|
|
1375
|
-
return;
|
|
1376
|
-
}
|
|
1377
|
-
}
|
|
762
|
+
useHotkeys("meta+p", ev => {
|
|
763
|
+
ev.preventDefault();
|
|
764
|
+
mutate(draft => {
|
|
765
|
+
draft.search.open = !draft.search.open;
|
|
766
|
+
});
|
|
767
|
+
}, {
|
|
768
|
+
enableOnFormTags: true
|
|
769
|
+
});
|
|
770
|
+
useHotkeys("escape", () => {
|
|
771
|
+
mutate(draft => {
|
|
772
|
+
draft.search.open = false;
|
|
773
|
+
});
|
|
1378
774
|
}, {
|
|
1379
|
-
|
|
775
|
+
enableOnFormTags: true
|
|
1380
776
|
});
|
|
1381
777
|
return /*#__PURE__*/React.createElement(Provider, {
|
|
1382
778
|
value: context
|
|
@@ -1463,7 +859,7 @@ function useMailbox(id) {
|
|
|
1463
859
|
const {
|
|
1464
860
|
bus
|
|
1465
861
|
} = useContext();
|
|
1466
|
-
return React.useMemo(() => pipe(bus.source, filter
|
|
862
|
+
return React.useMemo(() => pipe(bus.source, filter(x => id ? x.path === `/dev/timvir/block/${id}` : false)), [bus, id]);
|
|
1467
863
|
}
|
|
1468
864
|
function useBlock(props) {
|
|
1469
865
|
const {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "timvir",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.6",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"exports": {
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"@mdx-js/react": "*",
|
|
29
29
|
"react": "*",
|
|
30
|
-
"react-dom": "*"
|
|
30
|
+
"react-dom": "*",
|
|
31
|
+
"react-hotkeys-hook": "^4"
|
|
31
32
|
}
|
|
32
33
|
}
|