Skip to content

Commit

Permalink
Change how the Mac modifiers are stored
Browse files Browse the repository at this point in the history
Since babel + almond.js seems to mangle \u unicode strings for some reason, store the mappings from characters like ^ to "Ctrl" as an array of code points and generate the strings at runtime.
Use Object.assign instead of spread operator, to avoid complaints from r.js.
Add /dist to .gitignore.
  • Loading branch information
fwextensions committed Jun 6, 2023
1 parent 0d7bd1c commit 8cf35e3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/build/build.txt
/build/rjs
/build/out
/dist
/node_modules
/release
84 changes: 36 additions & 48 deletions src/js/background/get-chrome-shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,46 @@ define([
], function(
cp
) {
const KeyAliases = {
"Left Arrow": "ArrowLeft",
"Right Arrow": "ArrowRight",
"Up Arrow": "ArrowUp",
"Down Arrow": "ArrowDown",
"Page Up": "PageUp",
"Page Down": "PageDown",
"Ins": "Insert",
"Del": "Delete",
"Comma": ",",
"Period": ".",
"Media Previous Track": "MediaTrackPrevious",
"Media Next Track": "MediaTrackNext",
"Media Play/Pause": "MediaPlayPause",
"Media Stop": "MediaStop",
// Unicode chars used as keys show up as broken chars in Chrome after
// r.js combines the files, possibly only after adding bluebird.min.js
// "←": "ArrowLeft",
// "→": "ArrowRight",
// "↑": "ArrowUp",
// "↓": "ArrowDown",
// "⌃": "Ctrl",
// "⇧": "Shift",
// "⌥": "Opt",
// "⌘": "Cmd",
// babel converts these back to single chars during the build
// ["\u2303"]: "Ctrl",
// ["\u21E7"]: "Shift",
// ["\u2325"]: "Opt",
// ["\u2318"]: "Cmd",
// "\u2303": "Ctrl",
// "\u21E7": "Shift",
// "\u2325": "Opt",
// "\u2318": "Cmd",
};
const fromCodePoints = (entries) => entries
.map(([codePoint, alias]) => [String.fromCodePoint(codePoint), alias]);

const KeyAliases = Object.assign(
{
"Left Arrow": "ArrowLeft",
"Right Arrow": "ArrowRight",
"Up Arrow": "ArrowUp",
"Down Arrow": "ArrowDown",
"Page Up": "PageUp",
"Page Down": "PageDown",
"Ins": "Insert",
"Del": "Delete",
"Comma": ",",
"Period": ".",
"Media Previous Track": "MediaTrackPrevious",
"Media Next Track": "MediaTrackNext",
"Media Play/Pause": "MediaPlayPause",
"Media Stop": "MediaStop",
},
// Unicode chars used as keys show up as broken chars in Chrome
// after r.js and babel combine the files. so build the lookup table
// at runtime from the code point values, to avoid depending on a
// string like "\u2190", which also gets mangled.
Object.fromEntries(fromCodePoints([
[0x2190, "ArrowLeft"], // ←
[0x2192, "ArrowRight"], // →
[0x2191, "ArrowUp"], // ↑
[0x2193, "ArrowDown"], // ↓
[0x2303, "Ctrl"], // ⌃
[0x21E7, "Shift"], // ⇧
[0x2325, "Opt"], // ⌥
[0x2318, "Cmd"] // ⌘
]))
);
const ShortcutSeparator = "+";
const ShortcutSeparatorPattern = /\s*\+\s*/;
const MacShortcutPattern = /([\u2303\u21E7\u2325\u2318]+)(.+)/;
// Unicode chars in a regex also show up broken
// const MacShortcutPattern = /([⌃⇧⌥⌘]+)(.+)/;

// the only way to prevent babel from converting the \u strings to
// literal chars seems to be to set the keys this way after the object's
// been created
KeyAliases["\u2190"] = "ArrowLeft";
KeyAliases["\u2192"] = "ArrowRight";
KeyAliases["\u2191"] = "ArrowUp";
KeyAliases["\u2193"] = "ArrowDown";
KeyAliases["\u2303"] = "Ctrl";
KeyAliases["\u21E7"] = "Shift";
KeyAliases["\u2325"] = "Opt";
KeyAliases["\u2318"] = "Cmd";
const MacShortcutPattern = /([\u2303\u21E7\u2325\u2318]+)(.+)/;

return function getShortcuts()
{
Expand Down

0 comments on commit 8cf35e3

Please sign in to comment.