Initial project baseline
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import { BackendAsset, CanvasDocument, CanvasTemplate } from '../types';
|
||||
import { cloneDocument, normalizeDocument } from './canvasDocument';
|
||||
|
||||
const API_BASE = '';
|
||||
|
||||
export function templateId(template: CanvasTemplate) {
|
||||
return template.template_id || template.id || '';
|
||||
}
|
||||
|
||||
export function templateCreatedAt(template: CanvasTemplate) {
|
||||
return template.created_at || template.createdAt || '';
|
||||
}
|
||||
|
||||
export function templateUpdatedAt(template: CanvasTemplate) {
|
||||
return template.updated_at || template.updatedAt || '';
|
||||
}
|
||||
|
||||
export function templateReferenceIds(template: CanvasTemplate) {
|
||||
return template.reference_asset_ids || template.referenceAssetIds || [];
|
||||
}
|
||||
|
||||
export function templateCoverId(template: CanvasTemplate) {
|
||||
return template.cover_asset_id || template.coverAssetId || templateReferenceIds(template)[0] || '';
|
||||
}
|
||||
|
||||
export async function listDesignTemplates(): Promise<CanvasTemplate[]> {
|
||||
const res = await fetch(`${API_BASE}/api/design-templates`);
|
||||
if (!res.ok) throw new Error(`读取模板库失败 (${res.status})`);
|
||||
const items = (await res.json()) as CanvasTemplate[];
|
||||
return items.map(item => ({ ...item, document: normalizeDocument(item.document) }));
|
||||
}
|
||||
|
||||
export async function createCanvasTemplate(input: {
|
||||
name: string;
|
||||
description: string;
|
||||
document: CanvasDocument;
|
||||
referenceAssetIds?: string[];
|
||||
coverAssetId?: string;
|
||||
}): Promise<CanvasTemplate> {
|
||||
const fd = new FormData();
|
||||
fd.append('name', input.name.trim() || '未命名模板');
|
||||
fd.append('description', input.description.trim());
|
||||
fd.append('document', JSON.stringify(normalizeDocument(input.document)));
|
||||
fd.append('reference_asset_ids', JSON.stringify(input.referenceAssetIds || []));
|
||||
fd.append('cover_asset_id', input.coverAssetId || input.referenceAssetIds?.[0] || '');
|
||||
const res = await fetch(`${API_BASE}/api/design-templates`, { method: 'POST', body: fd });
|
||||
if (!res.ok) throw new Error(`保存模板失败 (${res.status})`);
|
||||
const template = (await res.json()) as CanvasTemplate;
|
||||
return { ...template, document: normalizeDocument(template.document) };
|
||||
}
|
||||
|
||||
export async function updateCanvasTemplate(
|
||||
id: string,
|
||||
partial: {
|
||||
name?: string;
|
||||
description?: string;
|
||||
document?: CanvasDocument;
|
||||
referenceAssetIds?: string[];
|
||||
coverAssetId?: string;
|
||||
},
|
||||
): Promise<CanvasTemplate> {
|
||||
const fd = new FormData();
|
||||
if (partial.name !== undefined) fd.append('name', partial.name.trim() || '未命名模板');
|
||||
if (partial.description !== undefined) fd.append('description', partial.description.trim());
|
||||
if (partial.document) fd.append('document', JSON.stringify(normalizeDocument(partial.document)));
|
||||
if (partial.referenceAssetIds) fd.append('reference_asset_ids', JSON.stringify(partial.referenceAssetIds));
|
||||
if (partial.coverAssetId !== undefined) fd.append('cover_asset_id', partial.coverAssetId);
|
||||
const res = await fetch(`${API_BASE}/api/design-templates/${id}`, { method: 'PATCH', body: fd });
|
||||
if (!res.ok) throw new Error(`更新模板失败 (${res.status})`);
|
||||
const template = (await res.json()) as CanvasTemplate;
|
||||
return { ...template, document: normalizeDocument(template.document) };
|
||||
}
|
||||
|
||||
export async function deleteCanvasTemplate(id: string) {
|
||||
const res = await fetch(`${API_BASE}/api/design-templates/${id}`, { method: 'DELETE' });
|
||||
if (!res.ok) throw new Error(`删除模板失败 (${res.status})`);
|
||||
}
|
||||
|
||||
export async function listAssets(type = ''): Promise<BackendAsset[]> {
|
||||
const query = type ? `?type=${encodeURIComponent(type)}` : '';
|
||||
const res = await fetch(`${API_BASE}/api/assets${query}`);
|
||||
if (!res.ok) throw new Error(`读取素材失败 (${res.status})`);
|
||||
return (await res.json()) as BackendAsset[];
|
||||
}
|
||||
|
||||
export async function uploadAsset(file: File, type = 'reference'): Promise<BackendAsset> {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file);
|
||||
fd.append('name', file.name.replace(/\.(svg|png|jpe?g)$/i, ''));
|
||||
fd.append('type', type);
|
||||
const res = await fetch(`${API_BASE}/api/assets`, { method: 'POST', body: fd });
|
||||
if (!res.ok) throw new Error(`上传参考图失败 (${res.status})`);
|
||||
return (await res.json()) as BackendAsset;
|
||||
}
|
||||
|
||||
export function assetUrl(assetOrPath?: BackendAsset | string) {
|
||||
if (!assetOrPath) return '';
|
||||
const raw = typeof assetOrPath === 'string' ? assetOrPath : assetOrPath.file_url;
|
||||
if (!raw) return '';
|
||||
return raw.startsWith('http') ? raw : `${API_BASE}${raw}`;
|
||||
}
|
||||
|
||||
export function duplicateDocument(document: CanvasDocument): CanvasDocument {
|
||||
return cloneDocument(document);
|
||||
}
|
||||
Reference in New Issue
Block a user