@dxos/keyboard 0.8.4-main.ead640a → 0.8.4-main.f466a3d56e
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/LICENSE +102 -5
- package/README.md +1 -1
- package/dist/lib/browser/index.mjs +20 -45
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +20 -45
- package/dist/lib/node-esm/index.mjs.map +3 -3
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/keyboard.d.ts +3 -2
- package/dist/types/src/keyboard.d.ts.map +1 -1
- package/dist/types/src/keyboard.stories.d.ts.map +1 -1
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +14 -13
- package/src/keyboard.stories.tsx +1 -1
- package/src/keyboard.ts +4 -3
package/LICENSE
CHANGED
|
@@ -1,8 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
Copyright (c) 2022 DXOS
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
## Abbreviation
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 DXOS
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@ Utility for managing contextual keyboard bindings.
|
|
|
9
9
|
- Tip: Use https://tableconvert.com/markdown-to-markdown to edit the table.
|
|
10
10
|
|
|
11
11
|
| | macos | windows | chrome (macos) | chrome (windows) |
|
|
12
|
-
|
|
12
|
+
| ----------------- | ---------------------------------------- | -------------------------------------------------------------------------------------------------------------- | -------------- | --------------------------------------------------------------------------------- |
|
|
13
13
|
| | https://support.apple.com/en-us/HT201236 | https://support.microsoft.com/en-us/windows/keyboard-shortcuts-in-windows-dcc61a57-8ff0-cffe-9796-cb9706c75eec | | https://support.google.com/chrome/answer/157179?hl=en&co=GENIE.Platform%3DDesktop |
|
|
14
14
|
| Alt-Tab | | x | | |
|
|
15
15
|
| Alt-F4 | | x | | |
|
|
@@ -2,19 +2,6 @@ import "@dxos/node-std/globals";
|
|
|
2
2
|
|
|
3
3
|
// src/keyboard.ts
|
|
4
4
|
import { invariant } from "@dxos/invariant";
|
|
5
|
-
function _define_property(obj, key, value) {
|
|
6
|
-
if (key in obj) {
|
|
7
|
-
Object.defineProperty(obj, key, {
|
|
8
|
-
value,
|
|
9
|
-
enumerable: true,
|
|
10
|
-
configurable: true,
|
|
11
|
-
writable: true
|
|
12
|
-
});
|
|
13
|
-
} else {
|
|
14
|
-
obj[key] = value;
|
|
15
|
-
}
|
|
16
|
-
return obj;
|
|
17
|
-
}
|
|
18
5
|
var __dxlog_file = "/__w/dxos/dxos/packages/common/keyboard/src/keyboard.ts";
|
|
19
6
|
var modifiers = [
|
|
20
7
|
"ctrl",
|
|
@@ -25,21 +12,14 @@ var modifiers = [
|
|
|
25
12
|
var parseShortcut = (shortcut, delimiter = /[+-]/) => {
|
|
26
13
|
const parts = shortcut.toLowerCase().split(delimiter);
|
|
27
14
|
const mods = modifiers.filter((key) => parts.includes(key));
|
|
28
|
-
invariant(mods.length === 0 || mods.length === parts.length - 1, void 0, {
|
|
29
|
-
F: __dxlog_file,
|
|
30
|
-
L: 31,
|
|
31
|
-
S: void 0,
|
|
32
|
-
A: [
|
|
33
|
-
"mods.length === 0 || mods.length === parts.length - 1",
|
|
34
|
-
""
|
|
35
|
-
]
|
|
36
|
-
});
|
|
15
|
+
invariant(mods.length === 0 || mods.length === parts.length - 1, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 17, S: void 0, A: ["mods.length === 0 || mods.length === parts.length - 1", ""] });
|
|
37
16
|
return mods.length ? [
|
|
38
17
|
...mods,
|
|
39
18
|
parts[parts.length - 1]
|
|
40
19
|
].join("+") : shortcut;
|
|
41
20
|
};
|
|
42
21
|
var KeyboardContext = class {
|
|
22
|
+
_keyMap = /* @__PURE__ */ new Map();
|
|
43
23
|
get bindings() {
|
|
44
24
|
return Array.from(this._keyMap.values());
|
|
45
25
|
}
|
|
@@ -53,18 +33,30 @@ var KeyboardContext = class {
|
|
|
53
33
|
unbind(binding) {
|
|
54
34
|
this._keyMap.delete(binding);
|
|
55
35
|
}
|
|
56
|
-
constructor() {
|
|
57
|
-
_define_property(this, "_keyMap", /* @__PURE__ */ new Map());
|
|
58
|
-
}
|
|
59
36
|
};
|
|
60
37
|
var ROOT = "";
|
|
61
|
-
var Keyboard = class {
|
|
38
|
+
var Keyboard = class _Keyboard {
|
|
39
|
+
static singleton = new _Keyboard();
|
|
40
|
+
_root = new KeyboardContext();
|
|
41
|
+
_keyMap = /* @__PURE__ */ new Map([
|
|
42
|
+
[
|
|
43
|
+
ROOT,
|
|
44
|
+
this._root
|
|
45
|
+
]
|
|
46
|
+
]);
|
|
47
|
+
_contexts = [
|
|
48
|
+
ROOT
|
|
49
|
+
];
|
|
50
|
+
_keyHandler = this.handleKeyDown.bind(this);
|
|
51
|
+
_path = ROOT;
|
|
62
52
|
initialize() {
|
|
63
53
|
document.addEventListener("keydown", this._keyHandler);
|
|
64
54
|
}
|
|
65
55
|
destroy() {
|
|
66
56
|
document.removeEventListener("keydown", this._keyHandler);
|
|
67
57
|
}
|
|
58
|
+
bind = this._root.bind.bind(this._root);
|
|
59
|
+
unbind = this._root.unbind.bind(this._root);
|
|
68
60
|
setCurrentContext(path = ROOT) {
|
|
69
61
|
this._path = path;
|
|
70
62
|
}
|
|
@@ -93,7 +85,7 @@ var Keyboard = class {
|
|
|
93
85
|
}
|
|
94
86
|
return Array.from(bindings.values());
|
|
95
87
|
}
|
|
96
|
-
handleKeyDown(event) {
|
|
88
|
+
async handleKeyDown(event) {
|
|
97
89
|
const { altKey, ctrlKey, metaKey, shiftKey, key } = event;
|
|
98
90
|
if (key !== "Alt" && key !== "Control" && key !== "Meta" && key !== "Shift") {
|
|
99
91
|
const tagName = event.target?.tagName;
|
|
@@ -110,7 +102,7 @@ var Keyboard = class {
|
|
|
110
102
|
if (this._path.startsWith(path)) {
|
|
111
103
|
const { data, handler, disableInput } = this.getContext(path).get(str) ?? {};
|
|
112
104
|
if (handler && (!isInput || !disableInput)) {
|
|
113
|
-
const result = handler({
|
|
105
|
+
const result = await handler({
|
|
114
106
|
context: path,
|
|
115
107
|
shortcut: str,
|
|
116
108
|
data,
|
|
@@ -126,24 +118,7 @@ var Keyboard = class {
|
|
|
126
118
|
}
|
|
127
119
|
}
|
|
128
120
|
}
|
|
129
|
-
constructor() {
|
|
130
|
-
_define_property(this, "_root", new KeyboardContext());
|
|
131
|
-
_define_property(this, "_keyMap", /* @__PURE__ */ new Map([
|
|
132
|
-
[
|
|
133
|
-
ROOT,
|
|
134
|
-
this._root
|
|
135
|
-
]
|
|
136
|
-
]));
|
|
137
|
-
_define_property(this, "_contexts", [
|
|
138
|
-
ROOT
|
|
139
|
-
]);
|
|
140
|
-
_define_property(this, "_keyHandler", this.handleKeyDown.bind(this));
|
|
141
|
-
_define_property(this, "_path", ROOT);
|
|
142
|
-
_define_property(this, "bind", this._root.bind.bind(this._root));
|
|
143
|
-
_define_property(this, "unbind", this._root.unbind.bind(this._root));
|
|
144
|
-
}
|
|
145
121
|
};
|
|
146
|
-
_define_property(Keyboard, "singleton", new Keyboard());
|
|
147
122
|
|
|
148
123
|
// src/util.ts
|
|
149
124
|
import { getHostPlatform } from "@dxos/util";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/keyboard.ts", "../../../src/util.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\n\n// TODO(burdon): Replace with hotkeys-js, react-hotkeys, and react-hotkeys-hook.\n\nexport type KeyHandler = (props: {\n context: string;\n shortcut: string;\n data?: any;\n event: KeyboardEvent;\n}) => boolean | void
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,iBAAiB
|
|
6
|
-
"names": ["invariant", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\n// TODO(burdon): Replace with hotkeys-js, react-hotkeys, and react-hotkeys-hook.\n\nexport type KeyHandler = (props: {\n context: string;\n shortcut: string;\n data?: any;\n event: KeyboardEvent;\n}) => MaybePromise<boolean | void>;\n\nexport type KeyBinding = {\n shortcut: string;\n handler: KeyHandler;\n disableInput?: boolean;\n data?: any;\n};\n\n// Keybinding is normalized to this order.\n// https://support.apple.com/en-us/HT201236\nconst modifiers = ['ctrl', 'shift', 'alt', 'meta'];\n\n// Normalize order and case of modifiers.\nexport const parseShortcut = (shortcut: string, delimiter = /[+-]/): string => {\n const parts = shortcut.toLowerCase().split(delimiter);\n const mods = modifiers.filter((key) => parts.includes(key));\n invariant(mods.length === 0 || mods.length === parts.length - 1);\n // Assume single natural key.\n return mods.length ? [...mods, parts[parts.length - 1]].join('+') : shortcut;\n};\n\nclass KeyboardContext {\n readonly _keyMap = new Map<string, KeyBinding>();\n\n get bindings() {\n return Array.from(this._keyMap.values());\n }\n\n get(binding: string): KeyBinding | undefined {\n return this._keyMap.get(binding);\n }\n\n bind(config: KeyBinding): void {\n config.shortcut = parseShortcut(config.shortcut);\n this._keyMap.set(config.shortcut, config);\n }\n\n unbind(binding: string): void {\n this._keyMap.delete(binding);\n }\n}\n\nconst ROOT = '';\n\n/**\n * Manages context-aware key bindings.\n */\nexport class Keyboard {\n static singleton = new Keyboard();\n\n private readonly _root = new KeyboardContext();\n private readonly _keyMap = new Map<string, KeyboardContext>([[ROOT, this._root]]);\n private readonly _contexts: string[] = [ROOT];\n private readonly _keyHandler = this.handleKeyDown.bind(this);\n private _path = ROOT;\n\n initialize(): void {\n document.addEventListener('keydown', this._keyHandler);\n }\n\n destroy(): void {\n document.removeEventListener('keydown', this._keyHandler);\n }\n\n bind = this._root.bind.bind(this._root);\n unbind = this._root.unbind.bind(this._root);\n\n setCurrentContext(path = ROOT): void {\n this._path = path;\n }\n\n getCurrentContext(): string {\n return this._path;\n }\n\n getContext(path = ROOT): KeyboardContext {\n let context = this._keyMap.get(path);\n if (!context) {\n context = new KeyboardContext();\n this._keyMap.set(path, context);\n this._contexts.push(path);\n this._contexts.sort();\n }\n\n return context;\n }\n\n getBindings(): KeyBinding[] {\n const bindings = new Map<string, KeyBinding>();\n for (let i = 0; i < this._contexts.length; ++i) {\n const path = this._contexts[i];\n if (this._path.startsWith(path)) {\n this.getContext(path).bindings.forEach((binding) => {\n bindings.set(binding.shortcut, binding);\n });\n }\n }\n\n return Array.from(bindings.values());\n }\n\n async handleKeyDown(event: KeyboardEvent): Promise<void> {\n const { altKey, ctrlKey, metaKey, shiftKey, key } = event;\n\n if (key !== 'Alt' && key !== 'Control' && key !== 'Meta' && key !== 'Shift') {\n // Binding option to check for input or contenteditable.\n const tagName = (event.target as any)?.tagName;\n const isInput =\n tagName === 'INPUT' || tagName === 'TEXTAREA' || (event.target as any)?.getAttribute('contenteditable');\n\n // Normalized key binding (order matters, see note above).\n const str = [ctrlKey && 'ctrl', shiftKey && 'shift', altKey && 'alt', metaKey && 'meta', key]\n .filter(Boolean)\n .join('+');\n\n // Scan matching contexts.\n for (let i = this._contexts.length - 1; i >= 0; --i) {\n const path = this._contexts[i];\n if (this._path.startsWith(path)) {\n const { data, handler, disableInput } = this.getContext(path).get(str) ?? {};\n if (handler && (!isInput || !disableInput)) {\n const result = await handler({ context: path, shortcut: str, data, event });\n if (result !== false) {\n // TODO(burdon): This doesn't prevent actions in markdown editor.\n // Reference: https://codemirror.net/docs/ref/#view.KeyBinding\n event.preventDefault();\n event.stopPropagation();\n }\n\n return;\n }\n }\n }\n }\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { getHostPlatform } from '@dxos/util';\n\n// Resources.\n// https://www.w3.org/TR/DOM-Level-3-Events/#events-keyboardevents\n// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key\n// https://developer.apple.com/design/human-interface-guidelines/designing-for-macos\n// https://support.apple.com/en-us/HT201236\n// https://support.apple.com/guide/mac-help/what-are-those-symbols-shown-in-menus-cpmh0011/mac\n\nconst ctrl: Record<string, string> = {\n macos: '⌃',\n ios: '⌃',\n windows: 'Ctrl',\n linux: 'Ctrl',\n unknown: 'Ctrl',\n};\n\nconst alt: Record<string, string> = {\n macos: '⌥',\n ios: '⌥',\n windows: 'Alt',\n linux: 'Alt',\n unknown: 'Alt',\n};\n\nconst meta: Record<string, string> = {\n macos: '⌘',\n ios: '⌘',\n windows: '⊞',\n // TODO(wittjosiah): Use ⌘ or ⊞ instead? Wait for user feedback.\n // From https://en.wikipedia.org/wiki/Super_key_(keyboard_button).\n linux: '❖',\n unknown: '❖',\n};\n\nconst getSymbol = (part: string) => {\n const platform = getHostPlatform();\n switch (part.toLowerCase()) {\n // Mods.\n case 'alt':\n return alt[platform];\n case 'ctrl':\n return ctrl[platform];\n case 'meta':\n return meta[platform];\n case 'shift':\n return '⇧';\n // Special keys.\n case 'backspace':\n return '⌫';\n case 'enter':\n return '⏎';\n case 'escape':\n return '⎋';\n case 'space':\n return '␣';\n case 'tab':\n return '⇥';\n default:\n return part.toUpperCase();\n }\n};\n\nexport const keySymbols = (keyBinding: string): string[] => {\n const parts = keyBinding.split('+');\n return parts.map(getSymbol);\n};\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,iBAAiB;AAmB1B,IAAA,eAAA;IAE2B,YAAA;EAAS;EAA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AAUK,MAAAA,QAAAA;cAAKK,SAAAA;cAAMR,UAAAA;cAAM;cACrEY;;gBAEF,WAAA,OAAA;AAGF,oBAAA,eAAA;AAEA,oBAAA,gBAAA;YACF;AACF;UACF;QACF;MACF;IACF;;;;;AClJA,SAASC,uBAAuB;AAShC,IAAMC,OAA+B;EACnCC,OAAO;EACPC,KAAK;EACLC,SAAS;EACTC,OAAO;EACPC,SAAS;AACX;AAEA,IAAMC,MAA8B;EAClCL,OAAO;EACPC,KAAK;EACLC,SAAS;EACTC,OAAO;EACPC,SAAS;AACX;AAEA,IAAME,OAA+B;EACnCN,OAAO;EACPC,KAAK;EACLC,SAAS;;;EAGTC,OAAO;EACPC,SAAS;AACX;AAEA,IAAMG,YAAY,CAACC,SAAAA;AACjB,QAAMC,WAAWX,gBAAAA;AACjB,UAAQU,KAAKE,YAAW,GAAA;;IAEtB,KAAK;AACH,aAAOL,IAAII,QAAAA;IACb,KAAK;AACH,aAAOV,KAAKU,QAAAA;IACd,KAAK;AACH,aAAOH,KAAKG,QAAAA;IACd,KAAK;AACH,aAAO;;IAET,KAAK;AACH,aAAO;IACT,KAAK;AACH,aAAO;IACT,KAAK;AACH,aAAO;IACT,KAAK;AACH,aAAO;IACT,KAAK;AACH,aAAO;IACT;AACE,aAAOD,KAAKG,YAAW;EAC3B;AACF;AAEO,IAAMC,aAAa,CAACC,eAAAA;AACzB,QAAMC,QAAQD,WAAWE,MAAM,GAAA;AAC/B,SAAOD,MAAME,IAAIT,SAAAA;AACnB;",
|
|
6
|
+
"names": ["invariant", "mods", "filter", "key", "includes", "length", "parts", "join", "KeyboardContext", "Array", "from", "binding", "bind", "_keyMap", "unbind", "_root", "ROOT", "_contexts", "_keyHandler", "_path", "initialize", "document", "destroy", "setCurrentContext", "path", "getCurrentContext", "getContext", "context", "sort", "getBindings", "i", "startsWith", "bindings", "shortcut", "handleKeyDown", "event", "tagName", "target", "str", "shiftKey", "altKey", "metaKey", "Boolean", "data", "handler", "disableInput", "isInput", "result", "getHostPlatform", "ctrl", "macos", "ios", "windows", "linux", "unknown", "alt", "meta", "getSymbol", "part", "platform", "toLowerCase", "toUpperCase", "keySymbols", "keyBinding", "parts", "split", "map"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/keyboard.ts":{"bytes":
|
|
1
|
+
{"inputs":{"src/keyboard.ts":{"bytes":15976,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/util.ts":{"bytes":5581,"imports":[{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":456,"imports":[{"path":"src/keyboard.ts","kind":"import-statement","original":"./keyboard"},{"path":"src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"}},"outputs":{"dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":10173},"dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["Keyboard","keySymbols","parseShortcut"],"entryPoint":"src/index.ts","inputs":{"src/keyboard.ts":{"bytesInOutput":3495},"src/index.ts":{"bytesInOutput":0},"src/util.ts":{"bytesInOutput":1184}},"bytes":4837}}}
|
|
@@ -2,19 +2,6 @@ import { createRequire } from 'node:module';const require = createRequire(import
|
|
|
2
2
|
|
|
3
3
|
// src/keyboard.ts
|
|
4
4
|
import { invariant } from "@dxos/invariant";
|
|
5
|
-
function _define_property(obj, key, value) {
|
|
6
|
-
if (key in obj) {
|
|
7
|
-
Object.defineProperty(obj, key, {
|
|
8
|
-
value,
|
|
9
|
-
enumerable: true,
|
|
10
|
-
configurable: true,
|
|
11
|
-
writable: true
|
|
12
|
-
});
|
|
13
|
-
} else {
|
|
14
|
-
obj[key] = value;
|
|
15
|
-
}
|
|
16
|
-
return obj;
|
|
17
|
-
}
|
|
18
5
|
var __dxlog_file = "/__w/dxos/dxos/packages/common/keyboard/src/keyboard.ts";
|
|
19
6
|
var modifiers = [
|
|
20
7
|
"ctrl",
|
|
@@ -25,21 +12,14 @@ var modifiers = [
|
|
|
25
12
|
var parseShortcut = (shortcut, delimiter = /[+-]/) => {
|
|
26
13
|
const parts = shortcut.toLowerCase().split(delimiter);
|
|
27
14
|
const mods = modifiers.filter((key) => parts.includes(key));
|
|
28
|
-
invariant(mods.length === 0 || mods.length === parts.length - 1, void 0, {
|
|
29
|
-
F: __dxlog_file,
|
|
30
|
-
L: 31,
|
|
31
|
-
S: void 0,
|
|
32
|
-
A: [
|
|
33
|
-
"mods.length === 0 || mods.length === parts.length - 1",
|
|
34
|
-
""
|
|
35
|
-
]
|
|
36
|
-
});
|
|
15
|
+
invariant(mods.length === 0 || mods.length === parts.length - 1, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 17, S: void 0, A: ["mods.length === 0 || mods.length === parts.length - 1", ""] });
|
|
37
16
|
return mods.length ? [
|
|
38
17
|
...mods,
|
|
39
18
|
parts[parts.length - 1]
|
|
40
19
|
].join("+") : shortcut;
|
|
41
20
|
};
|
|
42
21
|
var KeyboardContext = class {
|
|
22
|
+
_keyMap = /* @__PURE__ */ new Map();
|
|
43
23
|
get bindings() {
|
|
44
24
|
return Array.from(this._keyMap.values());
|
|
45
25
|
}
|
|
@@ -53,18 +33,30 @@ var KeyboardContext = class {
|
|
|
53
33
|
unbind(binding) {
|
|
54
34
|
this._keyMap.delete(binding);
|
|
55
35
|
}
|
|
56
|
-
constructor() {
|
|
57
|
-
_define_property(this, "_keyMap", /* @__PURE__ */ new Map());
|
|
58
|
-
}
|
|
59
36
|
};
|
|
60
37
|
var ROOT = "";
|
|
61
|
-
var Keyboard = class {
|
|
38
|
+
var Keyboard = class _Keyboard {
|
|
39
|
+
static singleton = new _Keyboard();
|
|
40
|
+
_root = new KeyboardContext();
|
|
41
|
+
_keyMap = /* @__PURE__ */ new Map([
|
|
42
|
+
[
|
|
43
|
+
ROOT,
|
|
44
|
+
this._root
|
|
45
|
+
]
|
|
46
|
+
]);
|
|
47
|
+
_contexts = [
|
|
48
|
+
ROOT
|
|
49
|
+
];
|
|
50
|
+
_keyHandler = this.handleKeyDown.bind(this);
|
|
51
|
+
_path = ROOT;
|
|
62
52
|
initialize() {
|
|
63
53
|
document.addEventListener("keydown", this._keyHandler);
|
|
64
54
|
}
|
|
65
55
|
destroy() {
|
|
66
56
|
document.removeEventListener("keydown", this._keyHandler);
|
|
67
57
|
}
|
|
58
|
+
bind = this._root.bind.bind(this._root);
|
|
59
|
+
unbind = this._root.unbind.bind(this._root);
|
|
68
60
|
setCurrentContext(path = ROOT) {
|
|
69
61
|
this._path = path;
|
|
70
62
|
}
|
|
@@ -93,7 +85,7 @@ var Keyboard = class {
|
|
|
93
85
|
}
|
|
94
86
|
return Array.from(bindings.values());
|
|
95
87
|
}
|
|
96
|
-
handleKeyDown(event) {
|
|
88
|
+
async handleKeyDown(event) {
|
|
97
89
|
const { altKey, ctrlKey, metaKey, shiftKey, key } = event;
|
|
98
90
|
if (key !== "Alt" && key !== "Control" && key !== "Meta" && key !== "Shift") {
|
|
99
91
|
const tagName = event.target?.tagName;
|
|
@@ -110,7 +102,7 @@ var Keyboard = class {
|
|
|
110
102
|
if (this._path.startsWith(path)) {
|
|
111
103
|
const { data, handler, disableInput } = this.getContext(path).get(str) ?? {};
|
|
112
104
|
if (handler && (!isInput || !disableInput)) {
|
|
113
|
-
const result = handler({
|
|
105
|
+
const result = await handler({
|
|
114
106
|
context: path,
|
|
115
107
|
shortcut: str,
|
|
116
108
|
data,
|
|
@@ -126,24 +118,7 @@ var Keyboard = class {
|
|
|
126
118
|
}
|
|
127
119
|
}
|
|
128
120
|
}
|
|
129
|
-
constructor() {
|
|
130
|
-
_define_property(this, "_root", new KeyboardContext());
|
|
131
|
-
_define_property(this, "_keyMap", /* @__PURE__ */ new Map([
|
|
132
|
-
[
|
|
133
|
-
ROOT,
|
|
134
|
-
this._root
|
|
135
|
-
]
|
|
136
|
-
]));
|
|
137
|
-
_define_property(this, "_contexts", [
|
|
138
|
-
ROOT
|
|
139
|
-
]);
|
|
140
|
-
_define_property(this, "_keyHandler", this.handleKeyDown.bind(this));
|
|
141
|
-
_define_property(this, "_path", ROOT);
|
|
142
|
-
_define_property(this, "bind", this._root.bind.bind(this._root));
|
|
143
|
-
_define_property(this, "unbind", this._root.unbind.bind(this._root));
|
|
144
|
-
}
|
|
145
121
|
};
|
|
146
|
-
_define_property(Keyboard, "singleton", new Keyboard());
|
|
147
122
|
|
|
148
123
|
// src/util.ts
|
|
149
124
|
import { getHostPlatform } from "@dxos/util";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/keyboard.ts", "../../../src/util.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\n\n// TODO(burdon): Replace with hotkeys-js, react-hotkeys, and react-hotkeys-hook.\n\nexport type KeyHandler = (props: {\n context: string;\n shortcut: string;\n data?: any;\n event: KeyboardEvent;\n}) => boolean | void
|
|
5
|
-
"mappings": ";;;AAIA,SAASA,iBAAiB
|
|
6
|
-
"names": ["invariant", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\n// TODO(burdon): Replace with hotkeys-js, react-hotkeys, and react-hotkeys-hook.\n\nexport type KeyHandler = (props: {\n context: string;\n shortcut: string;\n data?: any;\n event: KeyboardEvent;\n}) => MaybePromise<boolean | void>;\n\nexport type KeyBinding = {\n shortcut: string;\n handler: KeyHandler;\n disableInput?: boolean;\n data?: any;\n};\n\n// Keybinding is normalized to this order.\n// https://support.apple.com/en-us/HT201236\nconst modifiers = ['ctrl', 'shift', 'alt', 'meta'];\n\n// Normalize order and case of modifiers.\nexport const parseShortcut = (shortcut: string, delimiter = /[+-]/): string => {\n const parts = shortcut.toLowerCase().split(delimiter);\n const mods = modifiers.filter((key) => parts.includes(key));\n invariant(mods.length === 0 || mods.length === parts.length - 1);\n // Assume single natural key.\n return mods.length ? [...mods, parts[parts.length - 1]].join('+') : shortcut;\n};\n\nclass KeyboardContext {\n readonly _keyMap = new Map<string, KeyBinding>();\n\n get bindings() {\n return Array.from(this._keyMap.values());\n }\n\n get(binding: string): KeyBinding | undefined {\n return this._keyMap.get(binding);\n }\n\n bind(config: KeyBinding): void {\n config.shortcut = parseShortcut(config.shortcut);\n this._keyMap.set(config.shortcut, config);\n }\n\n unbind(binding: string): void {\n this._keyMap.delete(binding);\n }\n}\n\nconst ROOT = '';\n\n/**\n * Manages context-aware key bindings.\n */\nexport class Keyboard {\n static singleton = new Keyboard();\n\n private readonly _root = new KeyboardContext();\n private readonly _keyMap = new Map<string, KeyboardContext>([[ROOT, this._root]]);\n private readonly _contexts: string[] = [ROOT];\n private readonly _keyHandler = this.handleKeyDown.bind(this);\n private _path = ROOT;\n\n initialize(): void {\n document.addEventListener('keydown', this._keyHandler);\n }\n\n destroy(): void {\n document.removeEventListener('keydown', this._keyHandler);\n }\n\n bind = this._root.bind.bind(this._root);\n unbind = this._root.unbind.bind(this._root);\n\n setCurrentContext(path = ROOT): void {\n this._path = path;\n }\n\n getCurrentContext(): string {\n return this._path;\n }\n\n getContext(path = ROOT): KeyboardContext {\n let context = this._keyMap.get(path);\n if (!context) {\n context = new KeyboardContext();\n this._keyMap.set(path, context);\n this._contexts.push(path);\n this._contexts.sort();\n }\n\n return context;\n }\n\n getBindings(): KeyBinding[] {\n const bindings = new Map<string, KeyBinding>();\n for (let i = 0; i < this._contexts.length; ++i) {\n const path = this._contexts[i];\n if (this._path.startsWith(path)) {\n this.getContext(path).bindings.forEach((binding) => {\n bindings.set(binding.shortcut, binding);\n });\n }\n }\n\n return Array.from(bindings.values());\n }\n\n async handleKeyDown(event: KeyboardEvent): Promise<void> {\n const { altKey, ctrlKey, metaKey, shiftKey, key } = event;\n\n if (key !== 'Alt' && key !== 'Control' && key !== 'Meta' && key !== 'Shift') {\n // Binding option to check for input or contenteditable.\n const tagName = (event.target as any)?.tagName;\n const isInput =\n tagName === 'INPUT' || tagName === 'TEXTAREA' || (event.target as any)?.getAttribute('contenteditable');\n\n // Normalized key binding (order matters, see note above).\n const str = [ctrlKey && 'ctrl', shiftKey && 'shift', altKey && 'alt', metaKey && 'meta', key]\n .filter(Boolean)\n .join('+');\n\n // Scan matching contexts.\n for (let i = this._contexts.length - 1; i >= 0; --i) {\n const path = this._contexts[i];\n if (this._path.startsWith(path)) {\n const { data, handler, disableInput } = this.getContext(path).get(str) ?? {};\n if (handler && (!isInput || !disableInput)) {\n const result = await handler({ context: path, shortcut: str, data, event });\n if (result !== false) {\n // TODO(burdon): This doesn't prevent actions in markdown editor.\n // Reference: https://codemirror.net/docs/ref/#view.KeyBinding\n event.preventDefault();\n event.stopPropagation();\n }\n\n return;\n }\n }\n }\n }\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { getHostPlatform } from '@dxos/util';\n\n// Resources.\n// https://www.w3.org/TR/DOM-Level-3-Events/#events-keyboardevents\n// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key\n// https://developer.apple.com/design/human-interface-guidelines/designing-for-macos\n// https://support.apple.com/en-us/HT201236\n// https://support.apple.com/guide/mac-help/what-are-those-symbols-shown-in-menus-cpmh0011/mac\n\nconst ctrl: Record<string, string> = {\n macos: '⌃',\n ios: '⌃',\n windows: 'Ctrl',\n linux: 'Ctrl',\n unknown: 'Ctrl',\n};\n\nconst alt: Record<string, string> = {\n macos: '⌥',\n ios: '⌥',\n windows: 'Alt',\n linux: 'Alt',\n unknown: 'Alt',\n};\n\nconst meta: Record<string, string> = {\n macos: '⌘',\n ios: '⌘',\n windows: '⊞',\n // TODO(wittjosiah): Use ⌘ or ⊞ instead? Wait for user feedback.\n // From https://en.wikipedia.org/wiki/Super_key_(keyboard_button).\n linux: '❖',\n unknown: '❖',\n};\n\nconst getSymbol = (part: string) => {\n const platform = getHostPlatform();\n switch (part.toLowerCase()) {\n // Mods.\n case 'alt':\n return alt[platform];\n case 'ctrl':\n return ctrl[platform];\n case 'meta':\n return meta[platform];\n case 'shift':\n return '⇧';\n // Special keys.\n case 'backspace':\n return '⌫';\n case 'enter':\n return '⏎';\n case 'escape':\n return '⎋';\n case 'space':\n return '␣';\n case 'tab':\n return '⇥';\n default:\n return part.toUpperCase();\n }\n};\n\nexport const keySymbols = (keyBinding: string): string[] => {\n const parts = keyBinding.split('+');\n return parts.map(getSymbol);\n};\n"],
|
|
5
|
+
"mappings": ";;;AAIA,SAASA,iBAAiB;AAmB1B,IAAA,eAAA;IAE2B,YAAA;EAAS;EAAO;EAAO;EAElD;AACA;AAEE,IAAMC,gBAAiBC,CAAAA,UAAQC,YAAcC,WAASD;AACtDH,QAAAA,QAAUC,SAAW,YAAUA,EAAKI,MAAM,SAAKC;AAC/C,QAAA,OAAA,UAAA,OAA6B,CAAA,QAAA,MAAA,SAAA,GAAA,CAAA;AAC7B,YAAOL,KAAKI,WAAS,KAAA,KAAA,WAAA,MAAA,SAAA,GAAA,QAAA,EAAA,YAAA,YAAA,GAAA,cAAA,GAAA,IAAA,GAAA,QAAA,GAAA,CAAA,yDAAA,EAAA,EAAA,CAAA;SAAUC,KAAMA,SAAMD;IAAaE,GAAK;IAC7D,MAAA,MAAA,SAAA,CAAA;EAEF,EAAMC,KAAAA,GAAAA,IAAAA;;IAGJ,wBAAe;YACb,oBAAOC,IAAMC;EACf,IAAA,WAAA;AAEIC,WAAe,MAA0B,KAAA,KAAA,QAAA,OAAA,CAAA;;EAE7C,IAAA,SAAA;AAEAC,WAAuB,KAAQ,QAAA,IAAA,OAAA;;OAE7B,QAAKC;AACP,WAAA,WAAA,cAAA,OAAA,QAAA;AAEAC,SAAOH,QAAuB,IAAA,OAAA,UAAA,MAAA;;EAE9B,OAAA,SAAA;AACF,SAAA,QAAA,OAAA,OAAA;EAEA;AAEA;;AAMmBI,IAAYP,WAAZO,M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YAAW;EAC3B;AACF;AAEO,IAAMC,aAAa,CAACC,eAAAA;AACzB,QAAMC,QAAQD,WAAWE,MAAM,GAAA;AAC/B,SAAOD,MAAME,IAAIT,SAAAA;AACnB;",
|
|
6
|
+
"names": ["invariant", "mods", "filter", "key", "includes", "length", "parts", "join", "KeyboardContext", "Array", "from", "binding", "bind", "_keyMap", "unbind", "_root", "ROOT", "_contexts", "_keyHandler", "_path", "initialize", "document", "destroy", "setCurrentContext", "path", "getCurrentContext", "getContext", "context", "sort", "getBindings", "i", "startsWith", "bindings", "shortcut", "handleKeyDown", "event", "tagName", "target", "str", "shiftKey", "altKey", "metaKey", "Boolean", "data", "handler", "disableInput", "isInput", "result", "getHostPlatform", "ctrl", "macos", "ios", "windows", "linux", "unknown", "alt", "meta", "getSymbol", "part", "platform", "toLowerCase", "toUpperCase", "keySymbols", "keyBinding", "parts", "split", "map"]
|
|
7
7
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"src/keyboard.ts":{"bytes":
|
|
1
|
+
{"inputs":{"src/keyboard.ts":{"bytes":15976,"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"src/util.ts":{"bytes":5581,"imports":[{"path":"@dxos/util","kind":"import-statement","external":true}],"format":"esm"},"src/index.ts":{"bytes":456,"imports":[{"path":"src/keyboard.ts","kind":"import-statement","original":"./keyboard"},{"path":"src/util.ts","kind":"import-statement","original":"./util"}],"format":"esm"}},"outputs":{"dist/lib/node-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":10173},"dist/lib/node-esm/index.mjs":{"imports":[{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/util","kind":"import-statement","external":true}],"exports":["Keyboard","keySymbols","parseShortcut"],"entryPoint":"src/index.ts","inputs":{"src/keyboard.ts":{"bytesInOutput":3495},"src/index.ts":{"bytesInOutput":0},"src/util.ts":{"bytesInOutput":1184}},"bytes":4896}}}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
import { type MaybePromise } from '@dxos/util';
|
|
1
2
|
export type KeyHandler = (props: {
|
|
2
3
|
context: string;
|
|
3
4
|
shortcut: string;
|
|
4
5
|
data?: any;
|
|
5
6
|
event: KeyboardEvent;
|
|
6
|
-
}) => boolean | void
|
|
7
|
+
}) => MaybePromise<boolean | void>;
|
|
7
8
|
export type KeyBinding = {
|
|
8
9
|
shortcut: string;
|
|
9
10
|
handler: KeyHandler;
|
|
@@ -36,7 +37,7 @@ export declare class Keyboard {
|
|
|
36
37
|
getCurrentContext(): string;
|
|
37
38
|
getContext(path?: string): KeyboardContext;
|
|
38
39
|
getBindings(): KeyBinding[];
|
|
39
|
-
handleKeyDown(event: KeyboardEvent): void
|
|
40
|
+
handleKeyDown(event: KeyboardEvent): Promise<void>;
|
|
40
41
|
}
|
|
41
42
|
export {};
|
|
42
43
|
//# sourceMappingURL=keyboard.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"keyboard.d.ts","sourceRoot":"","sources":["../../../src/keyboard.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"keyboard.d.ts","sourceRoot":"","sources":["../../../src/keyboard.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAI/C,MAAM,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,KAAK,EAAE,aAAa,CAAC;CACtB,KAAK,YAAY,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;AAEnC,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,UAAU,CAAC;IACpB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ,CAAC;AAOF,eAAO,MAAM,a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
+
{"version":3,"file":"keyboard.stories.d.ts","sourceRoot":"","sources":["../../../src/keyboard.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAa,KAAK,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,KAA8B,MAAM,OAAO,CAAC;AA+DnD,QAAA,MAAM,IAAI;;;;CAIoB,CAAC;eAEhB,IAAI;AAEnB,KAAK,KAAK,GAAG,QAAQ,CAAC,OAAO,IAAI,CAAC,CAAC;AAEnC,eAAO,MAAM,OAAO,EAAE,KAAU,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"AAmEA,eAAO,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/util.ts"],"names":[],"mappings":"AAmEA,eAAO,MAAM,UAAU,eAAgB,MAAM,KAAG,MAAM,EAGrD,CAAC"}
|