Skip to content

Commit 78cef06

Browse files
committed
fix(app): handle item prefix as string
1 parent 18c698d commit 78cef06

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

src/app/src/components/shared/item/ItemCardForm.vue

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,21 @@ const schema = computed(() => z.object({
6565
extension: z.enum([...CONTENT_EXTENSIONS, ...MEDIA_EXTENSIONS] as [string, ...string[]]).nullish(),
6666
prefix: z.preprocess(
6767
val => val === '' ? null : val,
68-
z.number().int().positive().nullish(),
68+
z.string()
69+
.regex(/^\d+$/, 'Prefix be a string containing only digits')
70+
.refine(
71+
(prefix: string | null | undefined) => {
72+
if (prefix === null || prefix === undefined) {
73+
return true
74+
}
75+
76+
const num = Number(prefix)
77+
78+
return Number.isInteger(num) && num >= 0
79+
},
80+
'Prefix must be a positive integer',
81+
)
82+
.nullish(),
6983
),
7084
}).refine(() => {
7185
const siblings = props.parentItem.children?.filter(child => !child.hide) || []
@@ -87,7 +101,7 @@ const schema = computed(() => z.object({
87101
type Schema = {
88102
name: string
89103
extension: string | null | undefined
90-
prefix: number | null | undefined
104+
prefix: string | null | undefined
91105
}
92106
const state = reactive<Schema>({
93107
name: originalName.value,
@@ -267,11 +281,11 @@ async function onSubmit() {
267281
<span />
268282
</template>
269283
<UInput
270-
v-model.number="state.prefix"
271-
type="number"
284+
v-model="state.prefix"
285+
type="text"
286+
pattern="[0-9]*"
272287
variant="soft"
273288
placeholder=""
274-
min="1"
275289
class="h-5"
276290
size="xs"
277291
:disabled="isLoading"

src/app/src/types/tree.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface TreeItem {
1515
name: string
1616
fsPath: string // can be used as id
1717
type: 'file' | 'directory' | 'root'
18-
prefix: number | null
18+
prefix: string | null
1919
collections: string[]
2020
status?: TreeStatus
2121
routePath?: string

src/app/src/utils/file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ export const FILE_ICONS = {
5050
...AUDIO_EXTENSIONS.reduce((acc, ext) => ({ ...acc, [ext]: 'i-lucide-file-audio' }), {}),
5151
}
5252

53-
export function parseName(name: string): { name: string, prefix: number | null, extension: string | null } {
53+
export function parseName(name: string): { name: string, prefix: string | null, extension: string | null } {
5454
const prefixMatch = name.match(/^(\d+)\./)
5555
const extensionMatch = name.match(/\.(\w+)$/)
5656
return {
57-
prefix: prefixMatch ? Number.parseInt(prefixMatch[1], 10) : null,
57+
prefix: prefixMatch ? prefixMatch[1] : null,
5858
extension: extensionMatch ? extensionMatch[1] : null,
5959
name: name.replace(/^\d+\./, ''),
6060
}

src/app/test/mocks/tree.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,38 @@ export const tree: TreeItem[] = [
1414
fsPath: '1.getting-started',
1515
type: 'directory',
1616
collections: ['docs'],
17-
prefix: 1,
17+
prefix: '1',
1818
children: [
1919
{
2020
name: 'introduction',
2121
fsPath: '1.getting-started/2.introduction.md',
2222
type: 'file',
2323
routePath: '/getting-started/introduction',
2424
collections: ['docs'],
25-
prefix: 2,
25+
prefix: '2',
2626
},
2727
{
2828
name: 'installation',
2929
fsPath: '1.getting-started/3.installation.md',
3030
type: 'file',
3131
routePath: '/getting-started/installation',
3232
collections: ['docs'],
33-
prefix: 3,
33+
prefix: '3',
3434
},
3535
{
3636
name: 'advanced',
3737
fsPath: '1.getting-started/1.advanced',
3838
type: 'directory',
3939
collections: ['docs'],
40-
prefix: 1,
40+
prefix: '1',
4141
children: [
4242
{
4343
name: 'studio',
4444
fsPath: '1.getting-started/1.advanced/1.studio.md',
4545
type: 'file',
4646
routePath: '/getting-started/installation/advanced/studio',
4747
collections: ['docs'],
48-
prefix: 1,
48+
prefix: '1',
4949
},
5050
],
5151
},

src/app/test/unit/utils/tree.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ describe('buildTree of documents with one level of depth', () => {
2525
name: 'getting-started',
2626
fsPath: '1.getting-started',
2727
type: 'directory',
28-
prefix: 1,
28+
prefix: '1',
2929
collections: ['docs'],
3030
children: [
3131
{
3232
name: 'introduction',
3333
fsPath: '1.getting-started/2.introduction.md',
3434
type: 'file',
3535
routePath: '/getting-started/introduction',
36-
prefix: 2,
36+
prefix: '2',
3737
collections: ['docs'],
3838
},
3939
{
4040
name: 'installation',
4141
fsPath: '1.getting-started/3.installation.md',
4242
type: 'file',
4343
routePath: '/getting-started/installation',
44-
prefix: 3,
44+
prefix: '3',
4545
collections: ['docs'],
4646
},
4747
],
@@ -102,7 +102,7 @@ describe('buildTree of documents with one level of depth', () => {
102102
type: 'file',
103103
routePath: deletedDbItem.path,
104104
status: TreeStatus.Deleted,
105-
prefix: 2,
105+
prefix: '2',
106106
collections: ['docs'],
107107
},
108108
],
@@ -138,7 +138,7 @@ describe('buildTree of documents with one level of depth', () => {
138138
type: 'file',
139139
routePath: deletedDbItem.path,
140140
status: TreeStatus.Deleted,
141-
prefix: 3,
141+
prefix: '3',
142142
collections: ['docs'],
143143
},
144144
],
@@ -297,7 +297,7 @@ describe('buildTree of documents with one level of depth', () => {
297297
name: createdDbItem.path!.split('/').pop()!,
298298
type: 'file',
299299
status: TreeStatus.Renamed,
300-
prefix: 2,
300+
prefix: '2',
301301
collections: ['docs'],
302302
},
303303
],
@@ -312,30 +312,30 @@ describe('buildTree of documents with two levels of depth', () => {
312312
name: 'essentials',
313313
fsPath: '1.essentials',
314314
type: 'directory',
315-
prefix: 1,
315+
prefix: '1',
316316
collections: ['docs'],
317317
children: [
318318
{
319319
name: 'configuration',
320320
fsPath: '1.essentials/2.configuration.md',
321321
type: 'file',
322322
routePath: '/essentials/configuration',
323-
prefix: 2,
323+
prefix: '2',
324324
collections: ['docs'],
325325
},
326326
{
327327
name: 'nested',
328328
fsPath: '1.essentials/1.nested',
329329
type: 'directory',
330-
prefix: 1,
330+
prefix: '1',
331331
collections: ['docs'],
332332
children: [
333333
{
334334
name: 'advanced',
335335
fsPath: '1.essentials/1.nested/2.advanced.md',
336336
type: 'file',
337337
routePath: '/essentials/nested/advanced',
338-
prefix: 2,
338+
prefix: '2',
339339
collections: ['docs'],
340340
},
341341
],
@@ -447,7 +447,7 @@ describe('buildTree of documents with two levels of depth', () => {
447447
routePath: deletedDbItem.path,
448448
type: 'file',
449449
status: TreeStatus.Deleted,
450-
prefix: 2,
450+
prefix: '2',
451451
collections: ['docs'],
452452
},
453453
],
@@ -478,23 +478,23 @@ describe('buildTree of documents with language prefixed', () => {
478478
name: 'getting-started',
479479
fsPath: 'en/1.getting-started',
480480
type: 'directory',
481-
prefix: 1,
481+
prefix: '1',
482482
collections: ['docs_en'],
483483
children: [
484484
{
485485
name: 'introduction',
486486
fsPath: 'en/1.getting-started/2.introduction.md',
487487
type: 'file',
488488
routePath: '/en/getting-started/introduction',
489-
prefix: 2,
489+
prefix: '2',
490490
collections: ['docs_en'],
491491
},
492492
{
493493
name: 'installation',
494494
fsPath: 'en/1.getting-started/3.installation.md',
495495
type: 'file',
496496
routePath: '/en/getting-started/installation',
497-
prefix: 3,
497+
prefix: '3',
498498
collections: ['docs_en'],
499499
},
500500
],

0 commit comments

Comments
 (0)