Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feat(hotkeys): display platform specific key #421

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/components/HotKeys/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use client';

import { createStyles } from 'antd-style';
import { Fragment, memo } from 'react';
import { Fragment, memo, useEffect, useState } from 'react';
import { Flexbox } from 'react-layout-kit';

import { CLEAN_MESSAGE_KEY, PREFIX_KEY } from '@/const/hotkeys';
import { isApplePlatform } from '@/utils/platform';

const useStyles = createStyles(
({ css, token }) => css`
font-size: 12px;
Expand Down Expand Up @@ -33,12 +38,28 @@ export interface HotKeysProps {

const HotKeys = memo<HotKeysProps>(({ keys, desc }) => {
const { styles } = useStyles();
const keysGroup = keys.split('+').filter(Boolean);
const [keysGroup, setKeysGroup] = useState(keys.split('+'));
const visibility = typeof window === 'undefined' ? 'hidden' : 'visible';

useEffect(() => {
const mapping: Record<string, string> = {
[CLEAN_MESSAGE_KEY]: isApplePlatform() ? '⌫' : 'backspace',
[PREFIX_KEY]: isApplePlatform() ? 'βŒ₯' : 'alt',
};
const newValue = keys
.split('+')
.filter(Boolean)
.map((k) => mapping[k] ?? k);
setKeysGroup(newValue);
}, [keys]);

const content = (
<Flexbox align={'center'} className={styles} gap={2} horizontal>
{keysGroup.map((key, index) => (
<Fragment key={index}>
<kbd>{key.toUpperCase()}</kbd>
<kbd>
<span style={{ visibility }}>{key.toUpperCase()}</span>
</kbd>
{index + 1 < keysGroup.length && <span>+</span>}
</Fragment>
))}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/platform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import UAParser from 'ua-parser-js';

export const getPlatform = () => {
let ua = navigator.userAgent;
return new UAParser(ua).getOS();
};

export const isApplePlatform = () => {
const platform = getPlatform().name;
return platform && ['Mac OS', 'iOS'].includes(platform);
};