@@ -88,56 +88,62 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
8888 return existingItem
8989 }
9090
91- async function remove ( id : string ) {
92- const item = await storage . getItem ( id ) as DraftItem < DatabaseItem >
93- const fsPath = host . document . getFileSystemPath ( id )
94-
95- if ( item ) {
96- if ( item . status === DraftStatus . Deleted ) return
91+ async function remove ( ids : string [ ] ) {
92+ for ( const id of ids ) {
93+ const existingDraftItem = list . value . find ( item => item . id === id )
94+ const fsPath = host . document . getFileSystemPath ( id )
95+ const originalDbItem = await host . document . get ( id )
9796
9897 await storage . removeItem ( id )
9998 await host . document . delete ( id )
10099
101- if ( item . original ) {
102- const deleteDraft : DraftItem < DatabaseItem > = {
100+ let deleteDraftItem : DraftItem < DatabaseItem > | null = null
101+ if ( existingDraftItem ) {
102+ if ( existingDraftItem . status === DraftStatus . Deleted ) return
103+
104+ if ( existingDraftItem . status === DraftStatus . Created ) {
105+ list . value = list . value . filter ( item => item . id !== id )
106+ }
107+ else {
108+ deleteDraftItem = {
109+ id,
110+ fsPath : existingDraftItem . fsPath ,
111+ status : DraftStatus . Deleted ,
112+ original : existingDraftItem . original ,
113+ githubFile : existingDraftItem . githubFile ,
114+ }
115+
116+ list . value = list . value . map ( item => item . id === id ? deleteDraftItem ! : item )
117+ }
118+ }
119+ else {
120+ // TODO: check if gh file has been updated
121+ const githubFile = await git . fetchFile ( fsPath , { cached : true } ) as GithubFile
122+
123+ deleteDraftItem = {
103124 id,
104- fsPath : item . fsPath ,
125+ fsPath,
105126 status : DraftStatus . Deleted ,
106- original : item . original ,
107- githubFile : item . githubFile ,
127+ original : originalDbItem ,
128+ githubFile,
108129 }
109130
110- await storage . setItem ( id , deleteDraft )
111- await host . document . upsert ( id , item . original ! )
131+ list . value . push ( deleteDraftItem )
112132 }
113- }
114- else {
115- // Fetch github file before creating draft to detect non deployed changes
116- const githubFile = await git . fetchFile ( fsPath , { cached : true } ) as GithubFile
117- const original = await host . document . get ( id )
118-
119- const deleteItem : DraftItem = {
120- id,
121- fsPath,
122- status : DraftStatus . Deleted ,
123- original,
124- githubFile,
133+
134+ if ( deleteDraftItem ) {
135+ await storage . setItem ( id , deleteDraftItem )
125136 }
126137
127- await storage . setItem ( id , deleteItem )
138+ host . app . requestRerender ( )
128139
129- await host . document . delete ( id )
140+ await hooks . callHook ( 'studio:draft:document:updated' )
130141 }
131-
132- list . value = list . value . filter ( item => item . id !== id )
133- host . app . requestRerender ( )
134142 }
135143
136144 async function revert ( id : string ) {
137145 const draftItems = findDescendantsFromId ( list . value , id )
138146
139- console . log ( 'draftItems' , draftItems )
140-
141147 for ( const draftItem of draftItems ) {
142148 const existingItem = list . value . find ( item => item . id === draftItem . id )
143149 if ( ! existingItem ) {
@@ -206,6 +212,11 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
206212 }
207213
208214 function select ( draftItem : DraftItem < DatabaseItem > | null ) {
215+ // TODO: Handle editor with deleted file
216+ if ( draftItem ?. status === DraftStatus . Deleted ) {
217+ return
218+ }
219+
209220 current . value = draftItem
210221 }
211222
0 commit comments