@douyinfe/semi-foundation 2.66.0-alpha.3 → 2.66.0

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.
@@ -8,62 +8,94 @@ const strings = {
8
8
  };
9
9
 
10
10
  export { cssClasses, strings };
11
+ const keyCodeMap = {
12
+ // alpha
13
+ 'a': 'KeyA', 'b': 'KeyB', 'c': 'KeyC', 'd': 'KeyD', 'e': 'KeyE',
14
+ 'f': 'KeyF', 'g': 'KeyG', 'h': 'KeyH', 'i': 'KeyI', 'j': 'KeyJ',
15
+ 'k': 'KeyK', 'l': 'KeyL', 'm': 'KeyM', 'n': 'KeyN', 'o': 'KeyO',
16
+ 'p': 'KeyP', 'q': 'KeyQ', 'r': 'KeyR', 's': 'KeyS', 't': 'KeyT',
17
+ 'u': 'KeyU', 'v': 'KeyV', 'w': 'KeyW', 'x': 'KeyX', 'y': 'KeyY',
18
+ 'z': 'KeyZ',
11
19
 
12
- export function keyToCode(key: KeyboardEvent["key"]) {
13
- const keyCodeMap = {
14
- // alpha
15
- 'a': 'KeyA', 'b': 'KeyB', 'c': 'KeyC', 'd': 'KeyD', 'e': 'KeyE',
16
- 'f': 'KeyF', 'g': 'KeyG', 'h': 'KeyH', 'i': 'KeyI', 'j': 'KeyJ',
17
- 'k': 'KeyK', 'l': 'KeyL', 'm': 'KeyM', 'n': 'KeyN', 'o': 'KeyO',
18
- 'p': 'KeyP', 'q': 'KeyQ', 'r': 'KeyR', 's': 'KeyS', 't': 'KeyT',
19
- 'u': 'KeyU', 'v': 'KeyV', 'w': 'KeyW', 'x': 'KeyX', 'y': 'KeyY',
20
- 'z': 'KeyZ',
21
-
22
- // digit
23
- '0': 'Digit0', '1': 'Digit1', '2': 'Digit2', '3': 'Digit3',
24
- '4': 'Digit4', '5': 'Digit5', '6': 'Digit6', '7': 'Digit7',
25
- '8': 'Digit8', '9': 'Digit9',
26
-
27
- // punctuation
28
- ' ': 'Space', 'enter': 'Enter', 'escape': 'Escape', 'backspace': 'Backspace',
29
- 'tab': 'Tab', '-': 'Minus', '=': 'Equal', '[': 'BracketLeft',
30
- ']': 'BracketRight', '\\': 'Backslash', ';': 'Semicolon',
31
- "'": 'Quote', '`': 'Backquote', ',': 'Comma', '.': 'Period',
32
- '/': 'Slash', '?': 'Slash', '!': 'Digit1', '@': 'Digit2',
33
- '#': 'Digit3', '$': 'Digit4', '%': 'Digit5', '^': 'Digit6',
34
- '&': 'Digit7', '*': 'Digit8', '(': 'Digit9', ')': 'Digit0',
35
-
36
- // arrow
37
- 'arrowup': 'ArrowUp', 'arrowdown': 'ArrowDown',
38
- 'arrowleft': 'ArrowLeft', 'arrowright': 'ArrowRight',
39
-
40
- // function
41
- 'shift': 'ShiftLeft', 'control': 'ControlLeft', 'alt': 'AltLeft',
42
- 'meta': 'MetaLeft', 'capslock': 'CapsLock', 'f1': 'F1',
43
- 'f2': 'F2', 'f3': 'F3', 'f4': 'F4', 'f5': 'F5', 'f6': 'F6',
44
- 'f7': 'F7', 'f8': 'F8', 'f9': 'F9', 'f10': 'F10', 'f11': 'F11',
45
- 'f12': 'F12', 'insert': 'Insert', 'delete': 'Delete', 'home': 'Home',
46
- 'end': 'End', 'pageup': 'PageUp', 'pagedown': 'PageDown',
47
- 'numlock': 'NumLock', 'scrolllock': 'ScrollLock', 'pause': 'Pause',
48
-
49
- // numpad
50
- 'numpad0': 'Numpad0', 'numpad1': 'Numpad1', 'numpad2': 'Numpad2',
51
- 'numpad3': 'Numpad3', 'numpad4': 'Numpad4', 'numpad5': 'Numpad5',
52
- 'numpad6': 'Numpad6', 'numpad7': 'Numpad7', 'numpad8': 'Numpad8',
53
- 'numpad9': 'Numpad9', 'numpaddecimal': 'NumpadDecimal',
54
- 'numpaddivide': 'NumpadDivide', 'numpadmultiply': 'NumpadMultiply',
55
- 'numpadsubtract': 'NumpadSubtract', 'numpadadd': 'NumpadAdd',
56
- 'numpadenter': 'NumpadEnter',
57
-
58
- // Others
59
- 'printscreen': 'PrintScreen', 'contextmenu': 'ContextMenu',
60
- 'help': 'Help', 'select': 'Select', 'execute': 'Execute',
61
- 'clear': 'Clear', 'kana': 'KanaMode', 'kanji': 'KanjiMode',
62
- 'nonconvert': 'NonConvert', 'convert': 'Convert',
63
- 'process': 'Process', 'accept': 'Accept', 'modechange': 'ModeChange',
64
- 'launchapp1': 'LaunchApp1', 'launchapp2': 'LaunchApp2',
65
- 'launchmail': 'LaunchMail', 'mediaselect': 'MediaSelect'
66
- };
20
+ // digit
21
+ '0': 'Digit0', '1': 'Digit1', '2': 'Digit2', '3': 'Digit3',
22
+ '4': 'Digit4', '5': 'Digit5', '6': 'Digit6', '7': 'Digit7',
23
+ '8': 'Digit8', '9': 'Digit9',
24
+
25
+ // punctuation
26
+ ' ': 'Space', 'enter': 'Enter', 'escape': 'Escape', 'backspace': 'Backspace',
27
+ 'tab': 'Tab', '-': 'Minus', '=': 'Equal', '[': 'BracketLeft',
28
+ ']': 'BracketRight', '\\': 'Backslash', ';': 'Semicolon',
29
+ "'": 'Quote', '`': 'Backquote', ',': 'Comma', '.': 'Period',
30
+ '/': 'Slash', '?': 'Slash', '!': 'Digit1', '@': 'Digit2',
31
+ '#': 'Digit3', '$': 'Digit4', '%': 'Digit5', '^': 'Digit6',
32
+ '&': 'Digit7', '*': 'Digit8', '(': 'Digit9', ')': 'Digit0',
33
+
34
+ // arrow
35
+ 'arrowup': 'ArrowUp', 'arrowdown': 'ArrowDown',
36
+ 'arrowleft': 'ArrowLeft', 'arrowright': 'ArrowRight',
37
+
38
+ // function
39
+ 'shift': 'ShiftLeft', 'control': 'ControlLeft', 'alt': 'AltLeft',
40
+ 'meta': 'MetaLeft', 'capslock': 'CapsLock', 'f1': 'F1',
41
+ 'f2': 'F2', 'f3': 'F3', 'f4': 'F4', 'f5': 'F5', 'f6': 'F6',
42
+ 'f7': 'F7', 'f8': 'F8', 'f9': 'F9', 'f10': 'F10', 'f11': 'F11',
43
+ 'f12': 'F12', 'insert': 'Insert', 'delete': 'Delete', 'home': 'Home',
44
+ 'end': 'End', 'pageup': 'PageUp', 'pagedown': 'PageDown',
45
+ 'numlock': 'NumLock', 'scrolllock': 'ScrollLock', 'pause': 'Pause',
67
46
 
47
+ // numpad
48
+ 'numpad0': 'Numpad0', 'numpad1': 'Numpad1', 'numpad2': 'Numpad2',
49
+ 'numpad3': 'Numpad3', 'numpad4': 'Numpad4', 'numpad5': 'Numpad5',
50
+ 'numpad6': 'Numpad6', 'numpad7': 'Numpad7', 'numpad8': 'Numpad8',
51
+ 'numpad9': 'Numpad9', 'numpaddecimal': 'NumpadDecimal',
52
+ 'numpaddivide': 'NumpadDivide', 'numpadmultiply': 'NumpadMultiply',
53
+ 'numpadsubtract': 'NumpadSubtract', 'numpadadd': 'NumpadAdd',
54
+ 'numpadenter': 'NumpadEnter',
55
+ };
56
+ export function keyToCode(key: KeyboardEvent["key"]) {
68
57
  return keyCodeMap[key.toLowerCase()] || undefined;
69
58
  }
59
+
60
+ enum Keys {
61
+ A = 'a', B = 'b', C = 'c', D = 'd', E = 'e',
62
+ F = 'f', G = 'g', H = 'h', I = 'i', J = 'j',
63
+ K = 'k', L = 'l', M = 'm', N = 'n', O = 'o',
64
+ P = 'p', Q = 'q', R = 'r', S = 's', T = 't',
65
+ U = 'u', V = 'v', W = 'w', X = 'x', Y = 'y',
66
+ Z = 'z',
67
+
68
+ Digit0 = '0', Digit1 = '1', Digit2 = '2', Digit3 = '3',
69
+ Digit4 = '4', Digit5 = '5', Digit6 = '6', Digit7 = '7',
70
+ Digit8 = '8', Digit9 = '9',
71
+
72
+ Space = ' ', Enter = 'enter', Escape = 'escape', Backspace = 'backspace',
73
+ Tab = 'tab', Minus = '-', Equal = '=', BracketLeft = '[',
74
+ BracketRight = ']', Backslash = '\\', Semicolon = ';',
75
+ Quote = "'", Backquote = '`', Comma = ',', Period = '.',
76
+ Slash = '/', Exclamation = '!', At = '@', Hash = '#',
77
+ Dollar = '$', Percent = '%', Caret = '^', Ampersand = '&',
78
+ Asterisk = '*', LeftParenthesis = '(', RightParenthesis = ')',
79
+
80
+ ArrowUp = 'arrowup', ArrowDown = 'arrowdown',
81
+ ArrowLeft = 'arrowleft', ArrowRight = 'arrowright',
82
+
83
+ Shift = 'shift', Control = 'control', Alt = 'alt',
84
+ Meta = 'meta', CapsLock = 'capslock', F1 = 'f1',
85
+ F2 = 'f2', F3 = 'f3', F4 = 'f4', F5 = 'f5', F6 = 'f6',
86
+ F7 = 'f7', F8 = 'f8', F9 = 'f9', F10 = 'f10', F11 = 'f11',
87
+ F12 = 'f12', Insert = 'insert', Delete = 'delete', Home = 'home',
88
+ End = 'end', PageUp = 'pageup', PageDown = 'pagedown',
89
+ NumLock = 'numlock', ScrollLock = 'scrolllock', Pause = 'pause',
90
+
91
+ Numpad0 = 'numpad0', Numpad1 = 'numpad1', Numpad2 = 'numpad2',
92
+ Numpad3 = 'numpad3', Numpad4 = 'numpad4', Numpad5 = 'numpad5',
93
+ Numpad6 = 'numpad6', Numpad7 = 'numpad7', Numpad8 = 'numpad8',
94
+ Numpad9 = 'numpad9', NumpadDecimal = 'numpaddecimal',
95
+ NumpadDivide = 'numpaddivide', NumpadMultiply = 'numpadmultiply',
96
+ NumpadSubtract = 'numpadsubtract', NumpadAdd = 'numpadadd',
97
+ NumpadEnter = 'numpadenter',
98
+ }
99
+
100
+
101
+ export { Keys };
@@ -1,8 +1,8 @@
1
1
  import BaseFoundation, { DefaultAdapter } from '../base/foundation';
2
- import { keyToCode } from './constants';
2
+ import { keyToCode, Keys } from './constants';
3
3
 
4
4
  export interface HotKeysAdapter<P = Record<string, any>, S = Record<string, any>> extends DefaultAdapter<P, S> {
5
- notifyClick: () => void;
5
+ notifyHotKey: (e: KeyboardEvent) => void;
6
6
  getListenerTarget: () => HTMLElement
7
7
  }
8
8
 
@@ -15,27 +15,45 @@ export default class HotKeysFoundation<P = Record<string, any>, S = Record<strin
15
15
  // init Listener
16
16
  const target = this._adapter.getListenerTarget();
17
17
  target?.addEventListener('keydown', this.handleKeyDown);
18
+ const hotKeys = this.getProps().hotKeys;
19
+ if (!this.isValidHotKeys(hotKeys)) {
20
+ throw new Error('HotKeys must have one common key and 0/some modifier key');
21
+ }
22
+ }
23
+
24
+ isValidHotKeys = (hotKeys: string[]): boolean => {
25
+ let commonKeyCnt = 0;
26
+ const modifierKeys: string[] = [Keys.Meta, Keys.Alt, Keys.Shift, Keys.Control];
27
+
28
+ hotKeys.forEach(key => {
29
+ key = key.toLowerCase();
30
+ if (!Object.values(Keys).some((value) => value === key)) {
31
+ throw new Error(`${key} is not a valid key`);
32
+ }
33
+ if (!modifierKeys.includes(key)) {
34
+ commonKeyCnt += 1;
35
+ }
36
+ });
37
+
38
+ return commonKeyCnt === 1;
18
39
  }
19
40
 
20
41
  handleKeyDown = (event: KeyboardEvent): void => {
21
- const disabled = this.getProps().disabled;
22
- if (disabled) {
23
- return;
24
- }
25
- const hotKeys = this.getProps().hotKeys;
42
+ const { mergeMetaCtrl: merged, hotKeys, preventDefault } = this.getProps();
26
43
  let allModifier = new Array(4).fill(false); // Meta Shift Alt Ctrl
27
44
  let clickedModifier = [event.metaKey, event.shiftKey, event.altKey, event.ctrlKey];
28
- const keysPressed = hotKeys?.map((key: KeyboardEvent["key"])=> {
29
- if (key === "Meta") {
45
+ const keysPressed = hotKeys?.map((key: KeyboardEvent["key"]) => {
46
+ key = key.toLowerCase();
47
+ if (key === Keys.Meta) {
30
48
  allModifier[0] = true;
31
49
  return event.metaKey;
32
- } else if (key === "Shift") {
50
+ } else if (key === Keys.Shift) {
33
51
  allModifier[1] = true;
34
52
  return event.shiftKey;
35
- } else if (key === "Alt") {
53
+ } else if (key === Keys.Alt) {
36
54
  allModifier[2] = true;
37
55
  return event.altKey;
38
- } else if (key === "Control") {
56
+ } else if (key === Keys.Control) {
39
57
  allModifier[3] = true;
40
58
  return event.ctrlKey;
41
59
  }
@@ -46,17 +64,15 @@ export default class HotKeysFoundation<P = Record<string, any>, S = Record<strin
46
64
  return;
47
65
  }
48
66
  if (keysPressed.every(Boolean)) {
49
- event.preventDefault();
50
- this.handleClick();
67
+ if (preventDefault) {
68
+ event.preventDefault();
69
+ }
70
+ this._adapter.notifyHotKey(event);
51
71
  return;
52
72
  }
53
73
 
54
74
  }
55
75
 
56
- handleClick(): void {
57
- this._adapter.notifyClick();
58
- }
59
-
60
76
  destroy(): void {
61
77
  // remove Listener
62
78
  const target = this._adapter.getListenerTarget();
@@ -24,6 +24,7 @@ $module: #{$prefix}-hotKeys;
24
24
  &-split {
25
25
  @include font-size-small;
26
26
  margin: 0 3px;
27
+ color: $color-hotKeys-split;
27
28
  }
28
29
 
29
30
  }
@@ -1,5 +1,6 @@
1
1
  $color-hotKeys-bg: var(--semi-color-fill-0); // 快捷键背景颜色
2
2
  $color-hotKeys-text: var(--semi-color-text-2); // 快捷键文字颜色
3
+ $color-hotKeys-split: var(--semi-color-text-0); // 快捷键文字颜色
3
4
 
4
5
  $width-hotKeys-border: 1px; // 快捷键描边宽度
5
6
  $radius-hotKeys: 2px; // 边框半径
@@ -4,3 +4,114 @@ declare const cssClasses: {
4
4
  declare const strings: {};
5
5
  export { cssClasses, strings };
6
6
  export declare function keyToCode(key: KeyboardEvent["key"]): any;
7
+ declare enum Keys {
8
+ A = "a",
9
+ B = "b",
10
+ C = "c",
11
+ D = "d",
12
+ E = "e",
13
+ F = "f",
14
+ G = "g",
15
+ H = "h",
16
+ I = "i",
17
+ J = "j",
18
+ K = "k",
19
+ L = "l",
20
+ M = "m",
21
+ N = "n",
22
+ O = "o",
23
+ P = "p",
24
+ Q = "q",
25
+ R = "r",
26
+ S = "s",
27
+ T = "t",
28
+ U = "u",
29
+ V = "v",
30
+ W = "w",
31
+ X = "x",
32
+ Y = "y",
33
+ Z = "z",
34
+ Digit0 = "0",
35
+ Digit1 = "1",
36
+ Digit2 = "2",
37
+ Digit3 = "3",
38
+ Digit4 = "4",
39
+ Digit5 = "5",
40
+ Digit6 = "6",
41
+ Digit7 = "7",
42
+ Digit8 = "8",
43
+ Digit9 = "9",
44
+ Space = " ",
45
+ Enter = "enter",
46
+ Escape = "escape",
47
+ Backspace = "backspace",
48
+ Tab = "tab",
49
+ Minus = "-",
50
+ Equal = "=",
51
+ BracketLeft = "[",
52
+ BracketRight = "]",
53
+ Backslash = "\\",
54
+ Semicolon = ";",
55
+ Quote = "'",
56
+ Backquote = "`",
57
+ Comma = ",",
58
+ Period = ".",
59
+ Slash = "/",
60
+ Exclamation = "!",
61
+ At = "@",
62
+ Hash = "#",
63
+ Dollar = "$",
64
+ Percent = "%",
65
+ Caret = "^",
66
+ Ampersand = "&",
67
+ Asterisk = "*",
68
+ LeftParenthesis = "(",
69
+ RightParenthesis = ")",
70
+ ArrowUp = "arrowup",
71
+ ArrowDown = "arrowdown",
72
+ ArrowLeft = "arrowleft",
73
+ ArrowRight = "arrowright",
74
+ Shift = "shift",
75
+ Control = "control",
76
+ Alt = "alt",
77
+ Meta = "meta",
78
+ CapsLock = "capslock",
79
+ F1 = "f1",
80
+ F2 = "f2",
81
+ F3 = "f3",
82
+ F4 = "f4",
83
+ F5 = "f5",
84
+ F6 = "f6",
85
+ F7 = "f7",
86
+ F8 = "f8",
87
+ F9 = "f9",
88
+ F10 = "f10",
89
+ F11 = "f11",
90
+ F12 = "f12",
91
+ Insert = "insert",
92
+ Delete = "delete",
93
+ Home = "home",
94
+ End = "end",
95
+ PageUp = "pageup",
96
+ PageDown = "pagedown",
97
+ NumLock = "numlock",
98
+ ScrollLock = "scrolllock",
99
+ Pause = "pause",
100
+ Numpad0 = "numpad0",
101
+ Numpad1 = "numpad1",
102
+ Numpad2 = "numpad2",
103
+ Numpad3 = "numpad3",
104
+ Numpad4 = "numpad4",
105
+ Numpad5 = "numpad5",
106
+ Numpad6 = "numpad6",
107
+ Numpad7 = "numpad7",
108
+ Numpad8 = "numpad8",
109
+ Numpad9 = "numpad9",
110
+ NumpadDecimal = "numpaddecimal",
111
+ NumpadDivide = "numpaddivide",
112
+ NumpadMultiply = "numpadmultiply",
113
+ NumpadSubtract = "numpadsubtract",
114
+ NumpadAdd = "numpadadd",
115
+ NumpadEnter = "numpadenter"
116
+ }
117
+ export { Keys };