UNPKG

narrative-studio-sdk

Version:

Narrative SDK for building apps on the Narrative Studio

122 lines (121 loc) 3.9 kB
export class SchemeBuilder { constructor(scheme) { this.scheme = { ...scheme, categories: scheme.categories ?? [], }; } static create(scheme) { return new SchemeBuilder(scheme); } withSerializationRules(rules) { this.scheme.serializationRules = rules; return this; } withViewModes(modes) { this.scheme.viewModes = modes; return this; } addCategory(name) { this.finalizePendingItems(); this.currentCategory = { name, assets: [], constructs: [], containers: [], }; this.scheme.categories.push(this.currentCategory); return this; } addConstruct(construct) { this.ensureCategoryExists(); this.currentConstruct = { ...construct }; this.currentCategory.constructs.push(this.currentConstruct); return this; } addAsset(asset) { this.ensureCategoryExists(); this.currentCategory.assets.push(asset); return this; } addContainer(container) { this.ensureCategoryExists(); if (!this.currentCategory.containers) this.currentCategory.containers = []; this.currentCategory.containers.push(container); return this; } withZones(zones) { this.ensureConstructExists(); if (!this.currentConstruct.zones) this.currentConstruct.zones = []; this.currentConstruct.zones = zones; return this; } addScript(script) { this.ensureConstructExists(); this.currentScript = { ...script, frameGroups: [], laneGroups: [] }; this.currentConstruct.script = this.currentScript; return this; } addFrameGroup(frameGroup) { this.ensureScriptExists(); this.currentFrameGroup = { ...frameGroup, frames: [] }; this.currentScript.frameGroups.push(this.currentFrameGroup); this.currentLaneGroup = undefined; // prevent lanes in frameGroup context return this; } addFrame(frame) { this.ensureFrameGroupExists(); this.currentFrameGroup.frames.push(frame); return this; } addLaneGroup(laneGroup) { this.ensureScriptExists(); this.currentLaneGroup = { ...laneGroup, lanes: [] }; this.currentScript.laneGroups.push(this.currentLaneGroup); this.currentFrameGroup = undefined; // prevent frames in laneGroup context return this; } addLane(lane) { this.ensureLaneGroupExists(); this.currentLaneGroup.lanes.push(lane); return this; } build() { this.finalizePendingItems(); return this.scheme; } finalizePendingItems() { this.currentCategory = undefined; this.currentConstruct = undefined; this.currentScript = undefined; this.currentFrameGroup = undefined; this.currentLaneGroup = undefined; } ensureCategoryExists() { if (!this.currentCategory) { throw new Error('Cannot add elements without a category. Call addCategory() first.'); } } ensureConstructExists() { if (!this.currentConstruct) { throw new Error('Cannot add a script without a construct. Call addConstruct() first.'); } } ensureScriptExists() { if (!this.currentScript) { throw new Error('Cannot add elements without a script. Call addScript() first.'); } } ensureFrameGroupExists() { if (!this.currentFrameGroup) { throw new Error('Cannot add frames without a frame group. Call addFrameGroup() first.'); } } ensureLaneGroupExists() { if (!this.currentLaneGroup) { throw new Error('Cannot add lanes without a lane group. Call addLaneGroup() first.'); } } }