fix: get current path more precisely

This commit is contained in:
EnixCoda 2018-11-06 11:38:22 +08:00
parent 1b734a48f0
commit 1b35109bf5
No known key found for this signature in database
GPG key ID: 6825847C88AA329A
2 changed files with 21 additions and 16 deletions

View file

@ -64,7 +64,7 @@ const setUpTree = dispatch => () => dispatch(async (state, { treeData, metaData,
tasksAfterRender.push(DOMHelper.focusSearchInput)
dispatch(setStateText, null)
const currentPath = URLHelper.getCurrentPath(true)
const currentPath = URLHelper.getCurrentPath(metaData.branchName)
if (currentPath.length) {
const nodeExpandedTo = visibleNodesGenerator.expandTo(currentPath.join('/'))
if (nodeExpandedTo) {

View file

@ -1,28 +1,21 @@
function parseRaw() {
import { raiseError } from "analytics";
function parse() {
const { pathname } = window.location
let [
, // ignore content before the first '/'
userName,
repoName,
type,
branchName,
...path
...path // should be [...branchName.split('/'), ...filePath.split('/')]
] = pathname.split('/')
return {
userName,
repoName,
type,
branchName,
path,
}
}
function parse() {
const parsedData = parseRaw()
if (!isInCodePage()) {
delete parsedData.branchName
}
return parsedData
}
function isInRepoPage() {
const repoHeaderSelector = '.repohead'
@ -39,7 +32,7 @@ const TYPES = {
}
function isInCodePage(metaData = {}) {
const mergedRepo = { ...parseRaw(), ...metaData }
const mergedRepo = { ...parse(), ...metaData }
const { type, branchName } = mergedRepo
return Boolean(
isInRepoPage(mergedRepo) &&
@ -49,9 +42,21 @@ function isInCodePage(metaData = {}) {
)
}
function getCurrentPath(decode = false) {
const { path } = parseRaw()
return decode ? path.map(decodeURIComponent) : path
function getCurrentPath(branchName = '') {
const { path, type } = parse()
const slicedBranchName = branchName.split('/')
if (type === 'blob' || type === 'tree') {
while (slicedBranchName.length) {
if (slicedBranchName[0] === path[0]) {
slicedBranchName.shift()
path.shift()
} else {
raiseError(new Error(`branch name and path prefix not match`))
return []
}
}
}
return path.map(decodeURIComponent)
}
export default {