diff --git a/src/driver/core/FileExplorer.ts b/src/driver/core/FileExplorer.ts index 91206eb..53b6091 100644 --- a/src/driver/core/FileExplorer.ts +++ b/src/driver/core/FileExplorer.ts @@ -242,7 +242,7 @@ export const onNodeClick: BoundMethodCreator<[ const [, { loadWithPJAX }] = dispatch.get() dispatch.call(focusNode, node, true) if (node.url) { - if (node.url.startsWith('#')) { + if (node.url.includes('#')) { preventDefault = false } else { loadWithPJAX(node.url) diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index 1185994..e411651 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -90,6 +90,15 @@ export async function getPullTreeData( return await request(url, { accessToken }) } +export async function getPullPageDocument( + userName: string, + repoName: string, + pullId: string, +): Promise { + const url = `https://${window.location.host}/${userName}/${repoName}/pull/${pullId}/files?_pjax=%23js-repo-pjax-container` + return new DOMParser().parseFromString(await (await fetch(url)).text(), 'text/html') +} + export async function getBlobData( userName: string, repoName: string, diff --git a/src/platforms/GitHub/URLHelper.ts b/src/platforms/GitHub/URLHelper.ts index 2091666..7cc1062 100644 --- a/src/platforms/GitHub/URLHelper.ts +++ b/src/platforms/GitHub/URLHelper.ts @@ -30,7 +30,8 @@ export function isInRepoPage() { } export function isInPullPage() { - return parse().type === 'pull' + const { type, path } = parse() + return type === 'pull' ? path[0] : false } function isCommitPath(path: string[]) { diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index bc6767b..b497a90 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -97,21 +97,21 @@ export const GitHub: Platform = { async getTreeData(metaData, accessToken) { const { userName, repoName, branchName } = metaData - if (URLHelper.isInPullPage()) { - const treeData = await API.getPullTreeData( - userName, - repoName, - URLHelper.parse().path[0], - accessToken, - ) + const pullId = URLHelper.isInPullPage() + if (pullId) { + const treeData = await API.getPullTreeData(userName, repoName, pullId, accessToken) + + const creator = await createPullFileResolver(userName, repoName, pullId) const nodes: TreeNode[] = treeData.map(item => { - const id = document.querySelector(`*[data-path^="${item.filename}"]`)?.parentElement?.id + const id = creator(item.filename) return { path: item.filename || '', type: 'blob', name: item.filename?.replace(/^.*\//, '') || '', - url: `#${id}`, + url: id + ? `https://${window.location.host}/${metaData.userName}/${metaData.repoName}/pull/${pullId}/files#${id}` + : `#`, contents: undefined, sha: item.sha, } @@ -173,7 +173,7 @@ export const GitHub: Platform = { return root }, shouldShow() { - return DOMHelper.isInCodePage() || URLHelper.isInPullPage() + return Boolean(DOMHelper.isInCodePage() || URLHelper.isInPullPage()) }, getCurrentPath(branchName) { return URLHelper.getCurrentPath(branchName) @@ -191,6 +191,18 @@ export const GitHub: Platform = { }, } +async function createPullFileResolver(userName: string, repoName: string, pullId: string) { + const doc = + URLHelper.parse().path[1] === 'files' + ? document + : await API.getPullPageDocument(userName, repoName, pullId) + + return (path: string) => { + const id = doc.querySelector(`*[data-path^="${path}"]`)?.parentElement?.id + return id + } +} + function findMissingFolders(nodes: TreeNode[]) { const folders = new Set() const foundFolders = new Set()