Skip to content

Commit 9647666

Browse files
committed
fix: improve new filename logic
1 parent c6a23f0 commit 9647666

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/editor/FileSelector.vue

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,7 @@ import { computed, inject, ref, VNode, Ref } from 'vue'
55
const store = inject('store') as Store
66
77
const pending = ref(false)
8-
const newFileCount = ref(0)
9-
const customizedFilename = ref('')
10-
const pendingFilename = computed({
11-
get(): string {
12-
if (customizedFilename.value) {
13-
return customizedFilename.value
14-
}
15-
return newFileCount.value > 0 ? `Comp${newFileCount.value}.vue` : 'Comp.vue'
16-
},
17-
set(name: string) {
18-
customizedFilename.value = name
19-
}
20-
})
8+
const pendingFilename = ref('Comp.vue')
219
const importMapFile = 'import-map.json'
2210
const showImportMap = inject('import-map') as Ref<boolean>
2311
const files = computed(() =>
@@ -27,6 +15,24 @@ const files = computed(() =>
2715
)
2816
2917
function startAddFile() {
18+
let i = 0
19+
let name = `Comp.vue`
20+
21+
while (true) {
22+
let hasConflict = false
23+
for (const file in store.state.files) {
24+
if (file === name) {
25+
hasConflict = true
26+
name = `Comp${++i}.vue`
27+
break
28+
}
29+
}
30+
if (!hasConflict) {
31+
break
32+
}
33+
}
34+
35+
pendingFilename.value = name
3036
pending.value = true
3137
}
3238
@@ -57,17 +63,14 @@ function doneAddFile() {
5763
store.state.errors = []
5864
cancelAddFile()
5965
store.addFile(filename)
60-
customizedFilename.value = ''
61-
if (filename === pendingFilename.value) {
62-
newFileCount.value++
63-
}
6466
}
6567
6668
const fileSel = ref(null)
6769
function horizontalScroll(e: WheelEvent) {
6870
e.preventDefault()
6971
const el = fileSel.value! as HTMLElement
70-
const direction = Math.abs(e.deltaX) >= Math.abs(e.deltaY) ? e.deltaX : e.deltaY
72+
const direction =
73+
Math.abs(e.deltaX) >= Math.abs(e.deltaY) ? e.deltaX : e.deltaY
7174
const distance = 30 * (direction > 0 ? 1 : -1)
7275
el.scrollTo({
7376
left: el.scrollLeft + distance
@@ -76,7 +79,12 @@ function horizontalScroll(e: WheelEvent) {
7679
</script>
7780

7881
<template>
79-
<div class="file-selector" :class="{ 'has-import-map': showImportMap }" @wheel="horizontalScroll" ref="fileSel">
82+
<div
83+
class="file-selector"
84+
:class="{ 'has-import-map': showImportMap }"
85+
@wheel="horizontalScroll"
86+
ref="fileSel"
87+
>
8088
<div
8189
v-for="(file, i) in files"
8290
class="file"

0 commit comments

Comments
 (0)