From ba80640c399f3ac66ede08d5da8ae9d01924d0de Mon Sep 17 00:00:00 2001 From: Adam Krebs Date: Mon, 9 Oct 2023 21:26:41 -0400 Subject: [PATCH] Allow programmatic asset creation --- theatre/core/src/projects/TheatreProject.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/theatre/core/src/projects/TheatreProject.ts b/theatre/core/src/projects/TheatreProject.ts index bf86d60b04..8174a40016 100644 --- a/theatre/core/src/projects/TheatreProject.ts +++ b/theatre/core/src/projects/TheatreProject.ts @@ -3,7 +3,7 @@ import Project from '@theatre/core/projects/Project' import type {ISheet} from '@theatre/core/sheets/TheatreSheet' import type {ProjectAddress} from '@theatre/shared/utils/addresses' -import type {Asset, File} from '@theatre/shared/utils/assets' +import type {Asset, File as FileAsset} from '@theatre/shared/utils/assets' import type { ProjectId, SheetId, @@ -80,7 +80,7 @@ export interface IProject { * @param asset - The asset to get the URL for * @returns The URL for the asset, or `undefined` if the asset is not found */ - getAssetUrl(asset: Asset | File): string | undefined + getAssetUrl(asset: Asset | FileAsset): string | undefined } export default class TheatreProject implements IProject { @@ -121,6 +121,19 @@ export default class TheatreProject implements IProject { : undefined } + async createAsset(file: File) { + // probably should put this in project.createAsset but this will do for now + if (!this.isReady) { + console.error( + 'Calling `project.createAsset()` before `project.ready` is resolved, will always return `undefined`. ' + + 'Either use `project.ready.then(() => project.createAsset())` or `await project.ready` before calling `project.createAsset()`.', + ) + return undefined + } + + return privateAPI(this).assetStorage.createAsset(file) + } + sheet(sheetId: string, instanceId: string = 'default'): ISheet { const sanitizedPath = validateAndSanitiseSlashedPathOrThrow( sheetId,