From 220e6a23f68c847be8beb3c2a2fa8a4f0896dd01 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Fri, 22 Feb 2019 10:52:32 +0800 Subject: [PATCH] feat: support clicking file tree item with ctrl key pressed to open in new tab --- src/components/Node.tsx | 9 ++++++++- src/utils/general.ts | 10 +++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/components/Node.tsx b/src/components/Node.tsx index c40b26b..f6c8522 100644 --- a/src/components/Node.tsx +++ b/src/components/Node.tsx @@ -3,6 +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' function getIconType(node: TreeNode) { switch (node.type) { @@ -25,7 +26,13 @@ type Props = { } export default class Node extends React.PureComponent { onClick: React.MouseEventHandler = event => { - if (event.metaKey) return + if ( + (detectOS() === OperatingSystems.macOS && event.metaKey) || + (detectOS() === OperatingSystems.Windows && event.ctrlKey) + ) { + // Open in new tab + return + } event.preventDefault() const { node, onClick } = this.props onClick(node) diff --git a/src/utils/general.ts b/src/utils/general.ts index de9ebee..718f7e4 100644 --- a/src/utils/general.ts +++ b/src/utils/general.ts @@ -15,13 +15,13 @@ export function pick(source: T, keys: string[]): Partial { return {} as Partial } -const OperatingSystems = { - Windows: 'Windows', - macOS: 'Macintosh', - others: 'unknown', +export enum OperatingSystems { + Windows = 'Windows', + macOS = 'Macintosh', + others = 'unknown', } -function detectOS() { +export function detectOS(): OperatingSystems { const { navigator: { userAgent }, } = window