refactor: move snippet

This commit is contained in:
EnixCoda 2019-02-20 14:19:05 +08:00
parent deb16704f2
commit 413ee74264
No known key found for this signature in database
GPG key ID: 6825847C88AA329A
2 changed files with 33 additions and 32 deletions

View file

@ -2,6 +2,7 @@ import * as React from 'react'
import Icon from 'components/Icon'
import configHelper, { config } from 'utils/configHelper'
import keyHelper from 'utils/keyHelper'
import { friendlyFormatShortcut } from 'utils/general.js'
import { version } from '../../package.json'
const wikiLinks = {
@ -13,38 +14,6 @@ const wikiLinks = {
const ACCESS_TOKEN_REGEXP = /^[0-9a-f]{40}$/
const OperatingSystems = {
Windows: 'Windows',
macOS: 'Macintosh',
others: 'unknown',
}
function detectOS() {
const {
navigator: { userAgent },
} = window
if (userAgent.indexOf(OperatingSystems.Windows) !== -1) return OperatingSystems.Windows
else if (userAgent.indexOf(OperatingSystems.macOS) !== -1) return OperatingSystems.macOS
return OperatingSystems.others
}
function friendlyFormatShortcut(shortcut?: string) {
if (!shortcut) return ''
const OS = detectOS()
if (OS === OperatingSystems.Windows) {
return shortcut.replace(/meta/, 'win')
} else if (OS === OperatingSystems.macOS) {
return shortcut
.replace(/meta/, '⌘')
.replace(/ctrl/, '⌃')
.replace(/shift/, '⇧')
.replace(/alt/, '⌥')
.toUpperCase()
} else {
return shortcut
}
}
type Props = {
accessToken?: string
activated: boolean

View file

@ -12,3 +12,35 @@ export function pick<T>(source: T, keys: string[]): Partial<T> {
}
return {} as Partial<T>
}
const OperatingSystems = {
Windows: 'Windows',
macOS: 'Macintosh',
others: 'unknown',
}
function detectOS() {
const {
navigator: { userAgent },
} = window
if (userAgent.indexOf(OperatingSystems.Windows) !== -1) return OperatingSystems.Windows
else if (userAgent.indexOf(OperatingSystems.macOS) !== -1) return OperatingSystems.macOS
return OperatingSystems.others
}
export function friendlyFormatShortcut(shortcut?: string) {
if (!shortcut) return ''
const OS = detectOS()
if (OS === OperatingSystems.Windows) {
return shortcut.replace(/meta/, 'win')
} else if (OS === OperatingSystems.macOS) {
return shortcut
.replace(/meta/, '⌘')
.replace(/ctrl/, '⌃')
.replace(/shift/, '⇧')
.replace(/alt/, '⌥')
.toUpperCase()
} else {
return shortcut
}
}