|
17 | 17 | } |
18 | 18 | const os = guessOS(); |
19 | 19 |
|
| 20 | + const macosModifiers = { |
| 21 | + "command": "⌘", |
| 22 | + "cmd": "⌘", |
| 23 | + "shift": "⇧", |
| 24 | + "ctrl": "⌃", |
| 25 | + "control": "⌃", |
| 26 | + "option": "⌥", |
| 27 | + }; |
| 28 | + const keyRegex = /^[a-z0-9_]+$/; |
| 29 | + // use a non-capturing group to avoid capturing the key names |
| 30 | + |
| 31 | + const macosModifiersRegex = new RegExp( |
| 32 | + // use a non-capturing group to avoid capturing the key names |
| 33 | + `^(?:${Object.keys(macosModifiers).join("|")})-`, |
| 34 | + "gi" |
| 35 | + ); |
| 36 | + const shortcutParses = (text) => { |
| 37 | + text = text.toLocaleLowerCase(); |
| 38 | + while (text.match(macosModifiersRegex)) { |
| 39 | + text = text.replace(macosModifiersRegex, ""); |
| 40 | + } |
| 41 | + return text.match(keyRegex) !== null; |
| 42 | + } |
| 43 | + |
20 | 44 | // deno-lint-ignore no-window-prefix |
21 | 45 | window.addEventListener("DOMContentLoaded", (_) => { |
22 | 46 | for (const el of Array.from(document.querySelectorAll("kbd"))) { |
23 | 47 | el.classList.add("kbd"); |
24 | 48 | if (el.dataset[os.name] !== undefined) { |
25 | 49 | el.innerText = el.dataset[os.name]; |
26 | 50 | } |
27 | | - if (os.name === "mac") { |
28 | | - el.innerText = el.innerText |
29 | | - .replaceAll(/command-?/gi, "⌘") |
30 | | - .replaceAll(/cmd-?/gi, "⌘") |
31 | | - .replaceAll(/shift-?/gi, "⇧") |
32 | | - .replaceAll(/ctrl-?/gi, "⌃") |
33 | | - .replaceAll(/control-?/gi, "⌃") |
34 | | - .replaceAll(/option-?/gi, "⌥"); |
| 51 | + if (os.name === "mac" && shortcutParses(el.innerText)) { |
| 52 | + el.classList.add("mac"); |
| 53 | + for (const [key, value] of Object.entries(macosModifiers)) { |
| 54 | + el.innerText = el.innerText.replaceAll(new RegExp(`${key}-`, "gi"), value); |
| 55 | + } |
| 56 | + el.innerText = el.innerText.toLocaleUpperCase(); |
35 | 57 | } |
36 | 58 | } |
37 | 59 | }); |
|
0 commit comments