@@ -3,7 +3,7 @@ import { computed, reactive, type PropType } from 'vue'
33import { Image } from ' @unpic/vue'
44import * as z from ' zod'
55import type { FormSubmitEvent } from ' @nuxt/ui'
6- import { type StudioAction , type TreeItem , ContentFileExtension } from ' ../../../types'
6+ import { type CreateFileParams , type CreateFolderParams , type RenameFileParams , type StudioAction , type TreeItem , ContentFileExtension } from ' ../../../types'
77import { joinURL , withLeadingSlash } from ' ufo'
88import { contentFileExtensions } from ' ../../../utils/content'
99import { useStudio } from ' ../../../composables/useStudio'
@@ -14,15 +14,22 @@ const { context } = useStudio()
1414
1515const props = defineProps ({
1616 actionId: {
17- type: String as PropType <StudioItemActionId >,
17+ type: String as PropType <StudioItemActionId . CreateDocument | StudioItemActionId . CreateFolder | StudioItemActionId . RenameItem >,
1818 required: true ,
1919 },
2020 parentItem: {
2121 type: Object as PropType <TreeItem >,
2222 required: true ,
2323 },
24+ renamedItem: {
25+ type: Object as PropType <TreeItem >,
26+ default: null ,
27+ },
2428})
2529
30+ const originalName = computed (() => props .renamedItem ?.name || ' ' )
31+ const originalExtension = computed (() => props .renamedItem ?.id .split (' .' ).pop () as ContentFileExtension || ContentFileExtension .Markdown )
32+
2633const schema = z .object ({
2734 name: z .string ()
2835 .min (1 , ' Name cannot be empty' )
@@ -33,8 +40,8 @@ const schema = z.object({
3340
3441type Schema = z .output <typeof schema >
3542const state = reactive <Schema >({
36- name: ' ' ,
37- extension: ContentFileExtension . Markdown ,
43+ name: originalName . value ,
44+ extension: originalExtension . value ,
3845})
3946
4047const action = computed <StudioAction >(() => {
@@ -69,11 +76,29 @@ const tooltipText = computed(() => {
6976function onSubmit(_event : FormSubmitEvent <Schema >) {
7077 const fsPath = joinURL (props .parentItem .fsPath , ` ${state .name }.${state .extension } ` )
7178
72- action .value .handler !({
73- routePath: routePath .value ,
74- fsPath ,
75- content: ` New ${state .name } file ` ,
76- })
79+ let params: CreateFileParams | CreateFolderParams | RenameFileParams
80+ switch (props .actionId ) {
81+ case StudioItemActionId .CreateDocument :
82+ params = {
83+ routePath: routePath .value ,
84+ fsPath ,
85+ content: ` New ${state .name } file ` ,
86+ }
87+ break
88+ case StudioItemActionId .CreateFolder :
89+ params = {
90+ fsPath ,
91+ }
92+ break
93+ case StudioItemActionId .RenameItem :
94+ params = {
95+ id: props .renamedItem .id ,
96+ newFsPath: joinURL (props .parentItem .fsPath , ` ${state .name }.${state .extension } ` ),
97+ }
98+ break
99+ }
100+
101+ action .value .handler !(params )
77102}
78103 </script >
79104
0 commit comments