Skip to content

Commit

Permalink
Chore: Use key decoder from box react ui to prevent duplication (#749)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyajeet authored and tonyjin committed Mar 28, 2018
1 parent 85b8ee4 commit 575821f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 60 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"babel-preset-react": "^6.23.0",
"box-annotations": "^1.1.0",
"box-locales": "^0.0.1",
"box-react-ui": "^22.7.0",
"chai": "^4.1.2",
"chai-dom": "^1.5.0",
"codeceptjs-webdriverio": "^1.1.0",
Expand Down
62 changes: 2 additions & 60 deletions src/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Uri from 'jsuri';
import 'whatwg-fetch';
import { decode } from 'box-react-ui/lib/utils/keys';
import DownloadReachability from './DownloadReachability';
import Location from './Location';

Expand Down Expand Up @@ -540,66 +541,7 @@ export function loadScripts(urls, disableAMD = false) {
* @return {string} Decoded keydown key
*/
export function decodeKeydown(event) {
let modifier = '';

// KeyboardEvent.key is the new spec supported in Chrome, Firefox and IE.
// KeyboardEvent.keyIdentifier is the old spec supported in Safari.
// Priority is given to the new spec.
let key = event.key || event.keyIdentifier || '';

// Get the modifiers on their own
if (event.ctrlKey) {
modifier = 'Control';
} else if (event.shiftKey) {
modifier = 'Shift';
} else if (event.metaKey) {
modifier = 'Meta';
}

// The key and keyIdentifier specs also include modifiers.
// Since we are manually getting the modifiers above we do
// not want to trap them again here.
if (key === modifier) {
key = '';
}

// keyIdentifier spec returns UTF8 char codes
// Need to convert them back to ascii.
if (key.indexOf('U+') === 0) {
if (key === 'U+001B') {
key = 'Escape';
} else {
key = String.fromCharCode(key.replace('U+', '0x'));
}
}

// If nothing was pressed just return
if (!key) {
return '';
}

// Special casing for space bar
if (key === ' ') {
key = 'Space';
}

// Edge bug which outputs "Esc" instead of "Escape"
// https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/5290772/
if (key === 'Esc') {
key = 'Escape';
}

// keyIdentifier spec does not prefix the word Arrow.
// Newer key spec does it automatically.
if (key === 'Right' || key === 'Left' || key === 'Down' || key === 'Up') {
key = `Arrow${key}`;
}

if (modifier) {
modifier += '+';
}

return modifier + key;
return decode(event);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,10 @@ box-locales@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/box-locales/-/box-locales-0.0.1.tgz#1724959f293ae85f235b681b775d0f1a52a76b54"

box-react-ui@^22.7.0:
version "22.7.0"
resolved "https://registry.yarnpkg.com/box-react-ui/-/box-react-ui-22.7.0.tgz#14ff057cc7b76fcedda35765817ce83306d7058e"

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down

0 comments on commit 575821f

Please sign in to comment.