diff --git a/src/components/MetaBar.tsx b/src/components/MetaBar.tsx index 7696872..f652bfe 100644 --- a/src/components/MetaBar.tsx +++ b/src/components/MetaBar.tsx @@ -2,6 +2,8 @@ import { BranchName, Breadcrumb, Flex, Text } from '@primer/components' import { GitBranchIcon } from '@primer/octicons-react' import { platform } from 'platforms' import * as React from 'react' +import { isOpenInNewWindowClick } from 'utils/general' +import { loadWithPJAX } from 'utils/hooks/usePJAX' type Props = { metaData: MetaData @@ -9,7 +11,7 @@ type Props = { export function MetaBar({ metaData }: Props) { const { userName, repoName, branchName } = metaData - const { repoUrl, userUrl } = platform.resolveUrlFromMetaData(metaData) + const { repoUrl, userUrl, branchUrl } = platform.resolveUrlFromMetaData(metaData) return ( <> @@ -22,7 +24,17 @@ export function MetaBar({ metaData }: Props) {
- + { + if (isOpenInNewWindowClick(e)) return + + e.preventDefault() + loadWithPJAX(branchUrl) + }} + > {branchName || '...'} diff --git a/src/components/Node.tsx b/src/components/Node.tsx index 739abad..194fbec 100644 --- a/src/components/Node.tsx +++ b/src/components/Node.tsx @@ -1,7 +1,7 @@ import { useConfigs } from 'containers/ConfigsContext' import * as React from 'react' import { cx } from 'utils/cx' -import { OperatingSystems, os } from 'utils/general' +import { isOpenInNewWindowClick } from 'utils/general' import { getFileIconURL, getFolderIconURL } from 'utils/parseIconMapCSV' import { Icon } from './Icon' @@ -43,14 +43,7 @@ export function Node({ { - if ( - (os === OperatingSystems.macOS && event.metaKey) || - (os === OperatingSystems.Linux && event.ctrlKey) || - (os === OperatingSystems.Windows && event.ctrlKey) - ) { - // The default behavior, open in new tab - return - } + if (isOpenInNewWindowClick(event)) return onClick(event, node) }} diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index 9f0e78f..e30acd4 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -142,10 +142,15 @@ export const GitHub: Platform = { async getDefaultBranchName({ userName, repoName }, accessToken) { return (await API.getRepoMeta(userName, repoName, accessToken)).default_branch }, - resolveUrlFromMetaData({ userName, repoName }) { + resolveUrlFromMetaData({ userName, repoName, branchName }) { + const repoUrl = `https://${window.location.host}/${userName}/${repoName}` + const userUrl = `https://${window.location.host}/${userName}` + const pullId = URLHelper.isInPullPage() + const branchUrl = pullId ? `${repoUrl}/pull/${pullId}` : `${repoUrl}/tree/${branchName}` return { - repoUrl: `https://${window.location.host}/${userName}/${repoName}`, - userUrl: `https://${window.location.host}/${userName}`, + repoUrl, + userUrl, + branchUrl, } }, async getTreeData(metaData, path = '/', recursive, accessToken) { diff --git a/src/platforms/Gitea/index.ts b/src/platforms/Gitea/index.ts index 24cbbdb..1fb0371 100644 --- a/src/platforms/Gitea/index.ts +++ b/src/platforms/Gitea/index.ts @@ -105,10 +105,14 @@ export const Gitea: Platform = { const data = await API.getRepoMeta(userName, repoName, accessToken) return data.default_branch }, - resolveUrlFromMetaData({ userName, repoName }) { + resolveUrlFromMetaData({ userName, repoName, branchName }) { + const repoUrl = `${window.location.protocol}//${window.location.host}/${userName}/${repoName}` + const userUrl = `${window.location.protocol}//${window.location.host}/${userName}` + const branchUrl = `${repoUrl}/src/branch/${branchName}` return { - repoUrl: `${window.location.protocol}//${window.location.host}/${userName}/${repoName}`, - userUrl: `${window.location.protocol}//${window.location.host}/${userName}`, + repoUrl, + userUrl, + branchUrl, } }, async getTreeData(metaData, path, recursive, accessToken) { diff --git a/src/platforms/Gitee/index.ts b/src/platforms/Gitee/index.ts index 8addea1..45d5177 100644 --- a/src/platforms/Gitee/index.ts +++ b/src/platforms/Gitee/index.ts @@ -102,10 +102,14 @@ export const Gitee: Platform = { const data = await API.getRepoMeta(userName, repoName, accessToken) return data.default_branch }, - resolveUrlFromMetaData({ userName, repoName }) { + resolveUrlFromMetaData({ userName, repoName, branchName }) { + const repoUrl = `https://${window.location.host}/${userName}/${repoName}` + const userUrl = `https://${window.location.host}/${userName}` + const branchUrl = `${repoUrl}/tree/${branchName}` return { - repoUrl: `https://${window.location.host}/${userName}/${repoName}`, - userUrl: `https://${window.location.host}/${userName}`, + repoUrl, + userUrl, + branchUrl, } }, async getTreeData(metaData, path, recursive, accessToken) { diff --git a/src/platforms/platform.d.ts b/src/platforms/platform.d.ts index 9fe0ec9..4f91d95 100644 --- a/src/platforms/platform.d.ts +++ b/src/platforms/platform.d.ts @@ -7,9 +7,10 @@ type Platform = { metaData: Pick, accessToken?: string, ): Promise - resolveUrlFromMetaData(metaData: Pick): { + resolveUrlFromMetaData(metaData: MetaData): { userUrl: string repoUrl: string + branchUrl: string } getTreeData( metaData: Pick, diff --git a/src/utils/general.ts b/src/utils/general.ts index f424635..3c48402 100644 --- a/src/utils/general.ts +++ b/src/utils/general.ts @@ -192,3 +192,11 @@ export function createPromiseQueue() { }, } } + +export function isOpenInNewWindowClick(event: React.MouseEvent) { + return ( + (os === OperatingSystems.macOS && event.metaKey) || + (os === OperatingSystems.Linux && event.ctrlKey) || + (os === OperatingSystems.Windows && event.ctrlKey) + ) +}