refactor: only run detectOS once

This commit is contained in:
EnixCoda 2019-02-22 13:35:52 +08:00
parent b2daa4c3b5
commit 3d51bd6b84
No known key found for this signature in database
GPG key ID: 6825847C88AA329A
2 changed files with 8 additions and 7 deletions

View file

@ -3,7 +3,7 @@ import Icon from 'components/Icon'
import cx from 'utils/cx'
import LoadingIndicator from 'components/LoadingIndicator'
import { TreeNode } from 'utils/VisibleNodesGenerator'
import { detectOS, OperatingSystems } from 'utils/general'
import { os, OperatingSystems } from 'utils/general'
function getIconType(node: TreeNode) {
switch (node.type) {
@ -27,8 +27,8 @@ type Props = {
export default class Node extends React.PureComponent<Props> {
onClick: React.MouseEventHandler = event => {
if (
(detectOS() === OperatingSystems.macOS && event.metaKey) ||
(detectOS() === OperatingSystems.Windows && event.ctrlKey)
(os === OperatingSystems.macOS && event.metaKey) ||
(os === OperatingSystems.Windows && event.ctrlKey)
) {
// Open in new tab
return

View file

@ -21,7 +21,7 @@ export enum OperatingSystems {
others = 'unknown',
}
export function detectOS(): OperatingSystems {
function detectOS(): OperatingSystems {
const {
navigator: { userAgent },
} = window
@ -30,12 +30,13 @@ export function detectOS(): OperatingSystems {
return OperatingSystems.others
}
export const os = detectOS()
export function friendlyFormatShortcut(shortcut?: string) {
if (!shortcut) return ''
const OS = detectOS()
if (OS === OperatingSystems.Windows) {
if (os === OperatingSystems.Windows) {
return shortcut.replace(/meta/, 'win')
} else if (OS === OperatingSystems.macOS) {
} else if (os === OperatingSystems.macOS) {
return shortcut
.replace(/meta/, '⌘')
.replace(/ctrl/, '⌃')