From 1b35109bf50e5b11cb304ec2390493ddcb4cf663 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Tue, 6 Nov 2018 11:38:22 +0800 Subject: [PATCH] fix: get current path more precisely --- src/driver/core/FileExplorer.js | 2 +- src/utils/URLHelper.js | 35 +++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/driver/core/FileExplorer.js b/src/driver/core/FileExplorer.js index 1d75be7..e1cd0f0 100644 --- a/src/driver/core/FileExplorer.js +++ b/src/driver/core/FileExplorer.js @@ -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) { diff --git a/src/utils/URLHelper.js b/src/utils/URLHelper.js index d7d1e82..e29988b 100644 --- a/src/utils/URLHelper.js +++ b/src/utils/URLHelper.js @@ -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 {