fix: resolve file links even if not available in dom

This commit is contained in:
EnixCoda 2020-06-21 13:02:42 +08:00
parent a6a4acb792
commit 42b659ce09
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
4 changed files with 34 additions and 12 deletions

View file

@ -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)

View file

@ -90,6 +90,15 @@ export async function getPullTreeData(
return await request(url, { accessToken })
}
export async function getPullPageDocument(
userName: string,
repoName: string,
pullId: string,
): Promise<Document> {
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,

View file

@ -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[]) {

View file

@ -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<string>()
const foundFolders = new Set<string>()