11<script setup lang="ts">
22import { computed , reactive , type PropType } from ' vue'
33import * as z from ' zod'
4- import type { FormSubmitEvent } from ' @nuxt/ui'
54import { type CreateFileParams , type CreateFolderParams , type RenameFileParams , type StudioAction , type TreeItem , ContentFileExtension } from ' ../../../types'
65import { joinURL , withLeadingSlash , withoutLeadingSlash } from ' ufo'
76import { contentFileExtensions } from ' ../../../utils/content'
@@ -35,14 +34,21 @@ const props = defineProps({
3534})
3635
3736const originalName = computed (() => props .renamedItem ?.name || ' ' )
38- const originalExtension = computed (() => props .renamedItem ?.id .split (' .' ).pop () as ContentFileExtension || ContentFileExtension .Markdown )
37+ const originalExtension = computed (() => {
38+ const ext = props .renamedItem ?.id .split (' .' ).pop ()
39+ if (ext && contentFileExtensions .includes (ext as ContentFileExtension )) {
40+ return ext as ContentFileExtension
41+ }
42+
43+ return ContentFileExtension .Markdown
44+ })
3945
4046const schema = z .object ({
4147 name: z .string ()
4248 .min (1 , ' Name cannot be empty' )
4349 .refine ((name : string ) => ! name .endsWith (' .' ), ' Name cannot end with "."' )
4450 .refine ((name : string ) => ! name .startsWith (' /' ), ' Name cannot start with "/"' ),
45- extension: z .enum (ContentFileExtension ),
51+ extension: z .optional ( z . enum (ContentFileExtension ) ),
4652})
4753
4854type Schema = z .output <typeof schema >
@@ -88,23 +94,27 @@ const tooltipText = computed(() => {
8894 }
8995})
9096
91- function onSubmit(_event : FormSubmitEvent < Schema > ) {
97+ function onSubmit() {
9298 let params: CreateFileParams | CreateFolderParams | RenameFileParams
99+ const newFsPath = isFolderAction .value
100+ ? joinURL (props .parentItem .fsPath , state .name )
101+ : joinURL (props .parentItem .fsPath , ` ${state .name }.${state .extension } ` )
102+
93103 switch (props .actionId ) {
94104 case StudioItemActionId .CreateDocument :
95105 params = {
96- fsPath: withoutLeadingSlash (joinURL ( props . parentItem . fsPath , ` ${ state . name }.${ state . extension } ` ) ),
106+ fsPath: withoutLeadingSlash (newFsPath ),
97107 content: ` # ${upperFirst (state .name )} file ` ,
98108 }
99109 break
100110 case StudioItemActionId .CreateFolder :
101111 params = {
102- fsPath: withoutLeadingSlash (joinURL ( props . parentItem . fsPath , state . name ) ),
112+ fsPath: withoutLeadingSlash (newFsPath ),
103113 }
104114 break
105115 case StudioItemActionId .RenameItem :
106116 params = {
107- newFsPath: withoutLeadingSlash (joinURL ( props . parentItem . fsPath , ` ${ state . name }.${ state . extension } ` ) ),
117+ newFsPath: withoutLeadingSlash (newFsPath ),
108118 id: props .renamedItem .id ,
109119 }
110120 break
@@ -126,7 +136,7 @@ function onSubmit(_event: FormSubmitEvent<Schema>) {
126136 class =" hover:bg-white relative w-full min-w-0"
127137 >
128138 <div
129- v-if =" !isFolderAction"
139+ v-show =" !isFolderAction"
130140 class =" relative"
131141 >
132142 <div class =" z-[-1] aspect-video rounded-lg bg-elevated" />
0 commit comments