11import type { DatabaseItem , DraftItem , StudioHost , RawFile } from '../types'
22import { DraftStatus } from '../types/draft'
33import type { useGit } from './useGit'
4- import { generateContentFromDocument } from '../utils/content'
54import { getDraftStatus } from '../utils/draft'
65import { createSharedComposable } from '@vueuse/core'
76import { useHooks } from './useHooks'
@@ -26,6 +25,8 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
2625 } = useDraftBase < DatabaseItem > ( 'document' , host , git , storage )
2726
2827 const hooks = useHooks ( )
28+ const hostDb = host . document . db
29+ const generateContentFromDocument = host . document . generate . contentFromDocument
2930
3031 async function update ( fsPath : string , document : DatabaseItem ) : Promise < DraftItem < DatabaseItem > > {
3132 const existingItem = list . value . find ( item => item . fsPath === fsPath ) as DraftItem < DatabaseItem >
@@ -34,15 +35,15 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
3435 }
3536
3637 const oldStatus = existingItem . status
37- existingItem . status = getDraftStatus ( document , existingItem . original )
38+ existingItem . status = getDraftStatus ( document , existingItem . original as DatabaseItem , host . document . utils . areEqual )
3839 existingItem . modified = document
3940
4041 await storage . setItem ( fsPath , existingItem )
4142
4243 list . value = list . value . map ( item => item . fsPath === fsPath ? existingItem : item )
4344
4445 // Upsert document in database
45- await host . document . upsert ( fsPath , existingItem . modified )
46+ await hostDb . upsert ( fsPath , existingItem . modified )
4647
4748 // Trigger hook to warn that draft list has changed
4849 if ( existingItem . status !== oldStatus ) {
@@ -61,7 +62,7 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
6162 const { fsPath, newFsPath } = item
6263
6364 const existingDraftToRename = list . value . find ( draftItem => draftItem . fsPath === fsPath ) as DraftItem < DatabaseItem >
64- const dbItemToRename = await host . document . get ( fsPath )
65+ const dbItemToRename = await hostDb . get ( fsPath )
6566 if ( ! dbItemToRename ) {
6667 throw new Error ( `Database item not found for document fsPath: ${ fsPath } ` )
6768 }
@@ -76,7 +77,7 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
7677
7778 await remove ( [ fsPath ] , { rerender : false } )
7879
79- const newDbItem = await host . document . create ( newFsPath , content ! )
80+ const newDbItem = await hostDb . create ( newFsPath , content ! )
8081
8182 await create ( newFsPath , newDbItem , originalDbItem , { rerender : false } )
8283 }
@@ -85,7 +86,7 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
8586 }
8687
8788 async function duplicate ( fsPath : string ) : Promise < DraftItem < DatabaseItem > > {
88- let currentDbItem = await host . document . get ( fsPath )
89+ let currentDbItem = await hostDb . get ( fsPath )
8990 if ( ! currentDbItem ) {
9091 throw new Error ( `Database item not found for document fsPath: ${ fsPath } ` )
9192 }
@@ -103,7 +104,7 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, git:
103104
104105 const newFsPath = `${ currentFsPath . split ( '/' ) . slice ( 0 , - 1 ) . join ( '/' ) } /${ currentNameWithoutExtension } -copy.${ currentExtension } `
105106
106- const newDbItem = await host . document . create ( newFsPath , currentContent )
107+ const newDbItem = await hostDb . create ( newFsPath , currentContent )
107108
108109 return await create ( newFsPath , newDbItem )
109110 }
0 commit comments