mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
fix: get current path more precisely
This commit is contained in:
parent
1b734a48f0
commit
1b35109bf5
2 changed files with 21 additions and 16 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue