Skip to content

Commit 97a5cfb

Browse files
ice201508Jiuling.Lei
andauthored
fix-wrong-path-create-file-in-sub-file (#1456)
Co-authored-by: Jiuling.Lei <[email protected]>
1 parent 23d491c commit 97a5cfb

File tree

6 files changed

+141
-19
lines changed

6 files changed

+141
-19
lines changed

frontend/src/components/shared/EditFile.vue

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,14 @@
6363
</template>
6464
<script setup>
6565
import { ref, onMounted } from 'vue'
66+
import { useRouter } from 'vue-router'
6667
import CodeEditor from '../shared/CodeEditor.vue'
6768
import CommunityMDTextarea from '../community/CommunityMDTextarea.vue'
6869
import useFetchApi from '../../packs/useFetchApi'
6970
import { ElMessage } from 'element-plus'
7071
import { atob_utf8 } from '../../packs/utils'
7172
import { useRepoTabStore } from '../../stores/RepoTabStore'
73+
7274
const props = defineProps({
7375
repoName: String,
7476
namespacePath: String,
@@ -78,6 +80,7 @@
7880
7981
const originalCodeContent = ref('')
8082
const { repoTab, setRepoTab } = useRepoTabStore()
83+
const router = useRouter()
8184
8285
const codeContent = ref('')
8386
const sha = ref('')
@@ -155,17 +158,45 @@
155158
156159
const redirectToFilePreview = () => {
157160
// window.location.href = `/${prefixPath}/${props.namespacePath}/blob/${props.currentBranch}/${fileName.value}`
161+
// 确保路径不带开头的 /
162+
const normalizedPath = fileName.value.startsWith('/') ? fileName.value.slice(1) : fileName.value
163+
164+
const query = {
165+
tab: 'files',
166+
actionName: 'blob',
167+
path: normalizedPath,
168+
branch: props.currentBranch
169+
}
170+
158171
setRepoTab({
159172
actionName: 'blob',
160-
lastPath: fileName.value
173+
lastPath: normalizedPath
174+
})
175+
176+
router.push({
177+
query
161178
})
162179
}
163180
164181
const cancel = () => {
165182
// redirectToFilePreview()
183+
// 确保路径不带开头的 /
184+
const normalizedPath = fileName.value.startsWith('/') ? fileName.value.slice(1) : fileName.value
185+
186+
const query = {
187+
tab: 'files',
188+
actionName: 'blob',
189+
path: normalizedPath,
190+
branch: props.currentBranch
191+
}
192+
166193
setRepoTab({
167194
actionName: 'blob',
168-
lastPath: fileName.value
195+
lastPath: normalizedPath
196+
})
197+
198+
router.push({
199+
query
169200
})
170201
}
171202

frontend/src/components/shared/FileList.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,18 @@
236236
shouldReinit = true
237237
}
238238
239+
// 辅助函数:规范化路径,确保不带开头的 /
240+
const normalizePath = (path) => {
241+
if (!path) return ''
242+
return path.startsWith('/') ? path.slice(1) : path
243+
}
244+
239245
// 同步 repoTab 状态
240-
if (newQuery.actionName !== repoTab.actionName || newQuery.path !== repoTab.lastPath) {
246+
const normalizedPath = normalizePath(newQuery.path || '')
247+
if (newQuery.actionName !== repoTab.actionName || normalizedPath !== repoTab.lastPath) {
241248
setRepoTab({
242249
actionName: newQuery.actionName || 'files',
243-
lastPath: newQuery.path || '',
250+
lastPath: normalizedPath,
244251
currentBranch: newQuery.branch || repoTab.currentBranch
245252
})
246253
}
@@ -359,7 +366,7 @@
359366
360367
setRepoTab({
361368
actionName: 'new_file',
362-
lastPath: currentPath.value && currentPath.value.length > 0 ? '/' + currentPath.value : ''
369+
lastPath: currentPath.value || ''
363370
})
364371
365372
router.push({
@@ -382,7 +389,7 @@
382389
383390
setRepoTab({
384391
actionName: 'upload_file',
385-
lastPath: currentPath.value && currentPath.value.length > 0 ? '/' + currentPath.value : ''
392+
lastPath: currentPath.value || ''
386393
})
387394
388395
router.push({

frontend/src/components/shared/NewFile.vue

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="flex flex-col gap-4 my-[30px] md:px-5">
33
<div class="flex items-center gap-[10px]">
4-
<div class="whitespace-nowrap">{{ repoName }}</div>
4+
<div class="whitespace-nowrap">{{ repoName + (repoTab.lastPath ? '/' + repoTab.lastPath : '') }}</div>
55
<div class="text-gray-500">/</div>
66
<el-input
77
v-model="fileName"
@@ -57,6 +57,7 @@
5757
</template>
5858
<script setup>
5959
import { ref } from 'vue'
60+
import { useRouter } from 'vue-router'
6061
import CodeEditor from '../shared/CodeEditor.vue'
6162
import CommunityMDTextarea from '../community/CommunityMDTextarea.vue'
6263
import useFetchApi from '../../packs/useFetchApi'
@@ -71,6 +72,7 @@
7172
})
7273
7374
const { repoTab, setRepoTab } = useRepoTabStore()
75+
const router = useRouter()
7476
7577
const codeContent = ref(props.originalCodeContent)
7678
const commitTitle = ref('')
@@ -117,7 +119,8 @@
117119
const createFile = async () => {
118120
submiting.value = true
119121
// TODO: main branch for now; should support different branches
120-
const createFileEndpoint = `/${apiPrefixPath}/${props.namespacePath}/raw/${fileName.value}`
122+
const pathPrefix = repoTab.lastPath ? `${repoTab.lastPath}/` : ''
123+
const createFileEndpoint = `/${apiPrefixPath}/${props.namespacePath}/raw/${pathPrefix}${fileName.value}`
121124
const bodyData = {
122125
content: btoa_utf8(codeContent.value),
123126
message: buildCommitMessage(),
@@ -142,17 +145,44 @@
142145
143146
const redirectToFilePreview = () => {
144147
// window.location.href = `/${prefixPath}/${props.namespacePath}/blob/${props.currentBranch}/${fileName.value}`
148+
const newPath = repoTab.lastPath ? `${repoTab.lastPath}/${fileName.value}` : fileName.value
149+
150+
const query = {
151+
tab: 'files',
152+
actionName: 'blob',
153+
path: newPath,
154+
branch: router.currentRoute.value.query.branch || props.currentBranch
155+
}
156+
145157
setRepoTab({
146158
actionName: 'blob',
147-
lastPath: fileName.value
159+
lastPath: newPath
160+
})
161+
162+
router.push({
163+
query
148164
})
149165
}
150166
151167
const cancel = () => {
152168
// window.location.href = `/${prefixPath}/${props.namespacePath}/files/${props.currentBranch}`
169+
// 确保路径不带开头的 /
170+
const normalizedPath = repoTab.lastPath.startsWith('/') ? repoTab.lastPath.slice(1) : repoTab.lastPath
171+
172+
const query = {
173+
tab: 'files',
174+
actionName: 'files',
175+
branch: router.currentRoute.value.query.branch || props.currentBranch
176+
}
177+
178+
// 只有当路径不为空时才添加到 query
179+
if (normalizedPath) {
180+
query.path = normalizedPath
181+
}
182+
153183
setRepoTab({
154184
actionName: 'files',
155-
lastPath: ''
185+
lastPath: normalizedPath
156186
})
157187
}
158188
</script>

frontend/src/components/shared/RepoDetail.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
const urlParams = getUrlParams();
136136
setRepoTab({
137137
currentBranch: urlParams.branch || props.currentBranch || repoDetailStore.defaultBranch,
138+
lastPath: normalizePath(urlParams.path || '')
138139
});
139140
return true
140141
} catch (error) {
@@ -195,6 +196,12 @@
195196
fetchLastCommit()
196197
}
197198
199+
// 辅助函数:规范化路径,确保不带开头的 /
200+
const normalizePath = (path) => {
201+
if (!path) return ''
202+
return path.startsWith('/') ? path.slice(1) : path
203+
}
204+
198205
onMounted(async () => {
199206
resetFileNotFound()
200207
const urlParams = getUrlParams()
@@ -205,7 +212,7 @@
205212
repoName: props.repoName,
206213
tab: urlParams.tab || props.defaultTab || 'summary',
207214
actionName: urlParams.actionName || props.actionName || 'files',
208-
lastPath: urlParams.path || props.currentPath || '',
215+
lastPath: normalizePath(urlParams.path || props.currentPath || ''),
209216
currentBranch: urlParams.branch || props.currentBranch || ''
210217
}
211218

frontend/src/components/shared/TabContainer.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,10 +339,16 @@
339339
activeName.value = newTab
340340
341341
if (newTab === 'files') {
342+
// 辅助函数:规范化路径,确保不带开头的 /
343+
const normalizePath = (path) => {
344+
if (!path) return ''
345+
return path.startsWith('/') ? path.slice(1) : path
346+
}
347+
342348
setRepoTab({
343349
tab: newTab,
344350
actionName: validateActionName(newQuery.actionName),
345-
lastPath: newQuery.path || '',
351+
lastPath: normalizePath(newQuery.path || ''),
346352
currentBranch: newQuery.branch || repoTab.currentBranch
347353
})
348354
} else if (newTab === 'community') {
@@ -458,10 +464,16 @@
458464
if (urlDiscussionId) query.discussionId = urlDiscussionId
459465
}
460466
467+
// 辅助函数:规范化路径,确保不带开头的 /
468+
const normalizePath = (path) => {
469+
if (!path) return ''
470+
return path.startsWith('/') ? path.slice(1) : path
471+
}
472+
461473
setRepoTab({
462474
tab,
463475
actionName: tab === 'files' ? (query.actionName || 'files') : (tab === 'community' ? (query.actionName || 'list') : 'files'),
464-
lastPath: tab === 'files' ? (query.path || '') : '',
476+
lastPath: tab === 'files' ? normalizePath(query.path || '') : '',
465477
communityActionName: tab === 'community' ? (query.actionName || 'list') : 'list',
466478
discussionId: tab === 'community' ? (query.discussionId || '') : ''
467479
})

frontend/src/components/shared/UploadFile.vue

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="flex flex-col gap-[16px] min-h-[300px] py-[32px] md:px-5">
3-
<div class="test-[14px]"><p>{{ repoName + repoTab.lastPath }}/</p></div>
3+
<div class="text-[14px]"><p>{{ repoName + (repoTab.lastPath ? '/' + repoTab.lastPath : '') }}/</p></div>
44
<div class="border border-gray-200 rounded-xs bg-gray-100">
55
<div class="flex text-sm text-brand-500 leading-[22px]">
66
<div class="px-[20px] py-[9px] border-r bg-white w-[140px]">
@@ -59,6 +59,7 @@
5959
<script setup>
6060
import CommunityMDTextarea from '../community/CommunityMDTextarea.vue'
6161
import { ref } from 'vue'
62+
import { useRouter } from 'vue-router'
6263
import {ElMessage} from "element-plus"
6364
import { useI18n } from 'vue-i18n'
6465
import useFetchApi from '../../packs/useFetchApi'
@@ -71,6 +72,7 @@ const props = defineProps({
7172
})
7273
7374
const { repoTab, setRepoTab } = useRepoTabStore()
75+
const router = useRouter()
7476
7577
const { t } = useI18n();
7678
const uploadRef = ref();
@@ -117,10 +119,26 @@ const handleRemove = (file, fileList) => {
117119
118120
const cancel = () => {
119121
// window.location.href = `/${prefixPath}/${props.namespacePath}/files/${props.currentBranch}`
120-
const toPath = repoTab.lastPath && repoTab.lastPath.startsWith('/') ? repoTab.lastPath.slice(1) : repoTab.lastPath || ''
122+
const normalizedPath = repoTab.lastPath || ''
123+
124+
const query = {
125+
tab: 'files',
126+
actionName: 'files',
127+
branch: router.currentRoute.value.query.branch || props.currentBranch
128+
}
129+
130+
// 只有当路径不为空时才添加到 query
131+
if (normalizedPath) {
132+
query.path = normalizedPath
133+
}
134+
121135
setRepoTab({
122136
actionName: 'files',
123-
lastPath: toPath
137+
lastPath: normalizedPath
138+
})
139+
140+
router.push({
141+
query
124142
})
125143
}
126144
@@ -134,7 +152,8 @@ const buildCommitMessage = () => {
134152
const appendFilesToFormData = (formData, files) => {
135153
files.forEach((file) => {
136154
formData.append('file', file.raw)
137-
formData.append('file_path', (repoTab.lastPath || '') + '/' + file.name)
155+
const filePath = repoTab.lastPath ? `${repoTab.lastPath}/${file.name}` : file.name
156+
formData.append('file_path', filePath)
138157
})
139158
}
140159
@@ -159,10 +178,26 @@ const syncUploadFile = async () => {
159178
} else {
160179
filesList.value = []
161180
// window.location.href = `/${prefixPath}/${props.namespacePath}/files/${props.currentBranch}`
162-
const toPath = repoTab.lastPath && repoTab.lastPath.startsWith('/') ? repoTab.lastPath.slice(1) : repoTab.lastPath || ''
181+
const normalizedPath = repoTab.lastPath || ''
182+
183+
const query = {
184+
tab: 'files',
185+
actionName: 'files',
186+
branch: router.currentRoute.value.query.branch || props.currentBranch
187+
}
188+
189+
// 只有当路径不为空时才添加到 query
190+
if (normalizedPath) {
191+
query.path = normalizedPath
192+
}
193+
163194
setRepoTab({
164195
actionName: 'files',
165-
lastPath: toPath
196+
lastPath: normalizedPath
197+
})
198+
199+
router.push({
200+
query
166201
})
167202
}
168203
} catch (error) {

0 commit comments

Comments
 (0)