11import { createSharedComposable } from '@vueuse/core'
22import { computed , ref } from 'vue'
33import {
4+ type RenameFileParams ,
5+ type TreeItem ,
46 type UploadMediaParams ,
57 type CreateFileParams ,
68 type StudioHost ,
79 type StudioAction ,
810 type ActionHandlerParams ,
911 type StudioActionInProgress ,
12+ type CreateFolderParams ,
1013 StudioItemActionId ,
1114} from '../types'
1215import { oneStepActions , STUDIO_ITEM_ACTION_DEFINITIONS , twoStepActions } from '../utils/context'
@@ -33,13 +36,13 @@ export const useContext = createSharedComposable((
3336 } )
3437
3538 const itemActions = computed < StudioAction [ ] > ( ( ) => {
36- return STUDIO_ITEM_ACTION_DEFINITIONS . map ( action => ( {
39+ return STUDIO_ITEM_ACTION_DEFINITIONS . map ( < K extends StudioItemActionId > ( action : StudioAction < K > ) => ( {
3740 ...action ,
38- handler : async ( args ) => {
41+ handler : async ( args : ActionHandlerParams [ K ] ) => {
42+ // Two steps actions need to be already in progress to be executed
3943 if ( actionInProgress . value ?. id === action . id ) {
40- // Two steps actions need to be already in progress to be executed
4144 if ( twoStepActions . includes ( action . id ) ) {
42- await itemActionHandler [ action . id ] ( args as never )
45+ await itemActionHandler [ action . id ] ( args )
4346 unsetActionInProgress ( )
4447 return
4548 }
@@ -49,22 +52,31 @@ export const useContext = createSharedComposable((
4952 }
5053 }
5154
52- actionInProgress . value = { id : action . id , item : args . item }
55+ actionInProgress . value = { id : action . id }
56+
57+ if ( action . id === StudioItemActionId . RenameItem ) {
58+ if ( activeTree . value . currentItem . value ) {
59+ const itemToRename = args as TreeItem
60+ await activeTree . value . selectParentById ( itemToRename . id )
61+ actionInProgress . value . item = itemToRename
62+ }
63+ }
5364
5465 // One step actions can be executed immediately
5566 if ( oneStepActions . includes ( action . id ) ) {
56- await itemActionHandler [ action . id ] ( args as never )
67+ await itemActionHandler [ action . id ] ( args )
5768 unsetActionInProgress ( )
5869 }
5970 } ,
6071 } ) )
6172 } )
6273
6374 const itemActionHandler : { [ K in StudioItemActionId ] : ( args : ActionHandlerParams [ K ] ) => Promise < void > } = {
64- [ StudioItemActionId . CreateFolder ] : async ( args : string ) => {
65- alert ( `create folder ${ args } ` )
75+ [ StudioItemActionId . CreateFolder ] : async ( params : CreateFolderParams ) => {
76+ alert ( `create folder in ${ params . fsPath } ` )
6677 } ,
67- [ StudioItemActionId . CreateDocument ] : async ( { fsPath, routePath, content } : CreateFileParams ) => {
78+ [ StudioItemActionId . CreateDocument ] : async ( params : CreateFileParams ) => {
79+ const { fsPath, routePath, content } = params
6880 const document = await host . document . create ( fsPath , routePath , content )
6981 const draftItem = await activeTree . value . draft . create ( document )
7082 await activeTree . value . selectItemById ( draftItem . id )
@@ -74,23 +86,23 @@ export const useContext = createSharedComposable((
7486 await ( activeTree . value . draft as ReturnType < typeof useDraftMedias > ) . upload ( directory , file )
7587 }
7688 } ,
77- [ StudioItemActionId . RevertItem ] : async ( id : string ) => {
78- modal . openConfirmActionModal ( id , StudioItemActionId . RevertItem , async ( ) => {
79- await activeTree . value . draft . revert ( id )
89+ [ StudioItemActionId . RevertItem ] : async ( item : TreeItem ) => {
90+ modal . openConfirmActionModal ( item . id , StudioItemActionId . RevertItem , async ( ) => {
91+ await activeTree . value . draft . revert ( item . id )
8092 } )
8193 } ,
82- [ StudioItemActionId . RenameItem ] : async ( { id, newNameWithExtension } : { id : string , newNameWithExtension : string } ) => {
83- alert ( `rename file ${ id } ${ newNameWithExtension } ` )
94+ [ StudioItemActionId . RenameItem ] : async ( params : TreeItem | RenameFileParams ) => {
95+ const { id, newFsPath } = params as RenameFileParams
96+ await activeTree . value . draft . rename ( id , newFsPath )
8497 } ,
85- [ StudioItemActionId . DeleteItem ] : async ( id : string ) => {
86- modal . openConfirmActionModal ( id , StudioItemActionId . DeleteItem , async ( ) => {
87- const ids : string [ ] = findDescendantsFileItemsFromId ( activeTree . value . root . value , id ) . map ( item => item . id )
98+ [ StudioItemActionId . DeleteItem ] : async ( item : TreeItem ) => {
99+ modal . openConfirmActionModal ( item . id , StudioItemActionId . DeleteItem , async ( ) => {
100+ const ids : string [ ] = findDescendantsFileItemsFromId ( activeTree . value . root . value , item . id ) . map ( item => item . id )
88101 await activeTree . value . draft . remove ( ids )
89- await activeTree . value . selectParentById ( id )
90102 } )
91103 } ,
92- [ StudioItemActionId . DuplicateItem ] : async ( id : string ) => {
93- const draftItem = await activeTree . value . draft . duplicate ( id )
104+ [ StudioItemActionId . DuplicateItem ] : async ( item : TreeItem ) => {
105+ const draftItem = await activeTree . value . draft . duplicate ( item . id )
94106 await activeTree . value . selectItemById ( draftItem . id )
95107 } ,
96108 }
@@ -103,7 +115,6 @@ export const useContext = createSharedComposable((
103115 activeTree,
104116 itemActions,
105117 actionInProgress,
106-
107118 unsetActionInProgress,
108119 itemActionHandler,
109120 }
0 commit comments