Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow programmatic asset creation #461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 15 additions & 2 deletions theatre/core/src/projects/TheatreProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down