Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playground/docus/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineNuxtConfig({
owner: 'nuxt-content',
repo: 'studio',
branch: 'main',
rootDir: 'playground/docus',
rootDir: 'playground/docus/content',
private: false,
},
},
Expand Down
2 changes: 1 addition & 1 deletion playground/minimal/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default defineNuxtConfig({
owner: 'nuxt-content',
repo: 'studio',
branch: 'main',
rootDir: 'playground/minimal',
rootDir: 'playground/minimal/content',
private: false,
},
},
Expand Down
23 changes: 17 additions & 6 deletions src/app/src/composables/useDraftBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function useDraftBase<T extends DatabaseItem | MediaItem>(
const list = ref<DraftItem<DatabaseItem | MediaItem>[]>([])
const current = ref<DraftItem<DatabaseItem | MediaItem> | null>(null)

const remotePathPrefix = type === 'media' ? 'public' : 'content'
const remotePathPrefix = type === 'media' ? 'public' : ''
const hostDb = type === 'media' ? host.media : host.document.db
const hookName = `studio:draft:${type}:updated` as const
const areDocumentsEqual = host.document.utils.areEqual
Expand All @@ -31,13 +31,26 @@ export function useDraftBase<T extends DatabaseItem | MediaItem>(
return list.value.find(item => item.fsPath === fsPath) as DraftItem<T>
}

// Helper to fetch file with fallback strategy
async function fetchRemoteFile(fsPath: string): Promise<GitFile | null> {
const path = joinURL(remotePathPrefix, fsPath)
let remoteFile = await gitProvider.api.fetchFile(path, { cached: true })

if (!remoteFile && type === 'document' && !path.startsWith('content/')) {
const standardPath = joinURL('content', fsPath)
remoteFile = await gitProvider.api.fetchFile(standardPath, { cached: true })
}

return remoteFile
}

async function create(fsPath: string, item: T, original?: T, { rerender = true }: { rerender?: boolean } = {}): Promise<DraftItem<T>> {
const existingItem = list.value?.find(draft => draft.fsPath === fsPath)
if (existingItem) {
throw new Error(`Draft file already exists for document at ${fsPath}`)
}

const remoteFile = await gitProvider.api.fetchFile(joinURL(remotePathPrefix, fsPath), { cached: true }) as GitFile
const remoteFile = await fetchRemoteFile(fsPath) as GitFile

const draftItem: DraftItem<T> = {
fsPath,
Expand Down Expand Up @@ -83,8 +96,7 @@ export function useDraftBase<T extends DatabaseItem | MediaItem>(
list.value = list.value.filter(item => item.fsPath !== fsPath)
}
else {
// TODO: check if remote file has been updated
const remoteFile = await gitProvider.api.fetchFile(joinURL('content', fsPath), { cached: true }) as GitFile
const remoteFile = await fetchRemoteFile(fsPath) as GitFile

deleteDraftItem = {
fsPath: existingDraftItem.fsPath,
Expand All @@ -97,8 +109,7 @@ export function useDraftBase<T extends DatabaseItem | MediaItem>(
}
}
else {
// TODO: check if gh file has been updated
const remoteFile = await gitProvider.api.fetchFile(joinURL('content', fsPath), { cached: true }) as GitFile
const remoteFile = await fetchRemoteFile(fsPath) as GitFile

deleteDraftItem = {
fsPath,
Expand Down
9 changes: 6 additions & 3 deletions src/app/src/composables/useDraftDocuments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { DraftStatus } from '../types/draft'
import type { useGitProvider } from './useGitProvider'
import { createSharedComposable } from '@vueuse/core'
import { useHooks } from './useHooks'
import { joinURL } from 'ufo'
import { documentStorage as storage } from '../utils/storage'
import { getFileExtension } from '../utils/file'
import { useDraftBase } from './useDraftBase'
Expand Down Expand Up @@ -112,14 +111,18 @@ export const useDraftDocuments = createSharedComposable((host: StudioHost, gitPr
async function listAsRawFiles(): Promise<RawFile[]> {
const files = [] as RawFile[]
for (const draftItem of list.value) {
// FIX: Do NOT force 'content/' prefix. Use fsPath directly.
// This allows custom paths from remote repositories (e.g. '1.docs/...')
const filePath = draftItem.fsPath

if (draftItem.status === DraftStatus.Deleted) {
files.push({ path: joinURL('content', draftItem.fsPath), content: null, status: draftItem.status, encoding: 'utf-8' })
files.push({ path: filePath, content: null, status: draftItem.status, encoding: 'utf-8' })
continue
}

const content = await generateContentFromDocument(draftItem.modified as DatabaseItem)
files.push({
path: joinURL('content', draftItem.fsPath),
path: filePath,
content: content!,
status: draftItem.status,
encoding: 'utf-8',
Expand Down
5 changes: 3 additions & 2 deletions src/app/src/composables/useDraftMedias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,15 @@ export const useDraftMedias = createSharedComposable((host: StudioHost, gitProvi
if (draftItem.status === DraftStatus.Pristine) {
continue
}
const filePath = draftItem.fsPath

if (draftItem.status === DraftStatus.Deleted) {
files.push({ path: joinURL('public', draftItem.fsPath), content: null, status: draftItem.status, encoding: 'base64' })
files.push({ path: filePath, content: null, status: draftItem.status, encoding: 'base64' })
continue
}

const content = (await draftItem.modified?.raw as string).replace(/^data:\w+\/\w+;base64,/, '')
files.push({ path: joinURL('public', draftItem.fsPath), content, status: draftItem.status, encoding: 'base64' })
files.push({ path: filePath, content, status: draftItem.status, encoding: 'base64' })
}

return files
Expand Down
Loading
Loading