From 0beeed8203f221dc4dfe9d45b9fbad984a56eaf6 Mon Sep 17 00:00:00 2001 From: Stefan Wagner Date: Tue, 30 Jun 2020 09:16:32 +0200 Subject: [PATCH] Initialize camera when a tale is loaded --- js/project.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/js/project.js b/js/project.js index 5b2adce..20208bc 100644 --- a/js/project.js +++ b/js/project.js @@ -1,4 +1,11 @@ -import { db, handler, connect, connector, withInputSignals } from "flyps"; +import { + db, + handler, + connect, + connector, + trigger, + withInputSignals, +} from "flyps"; export function findTale([tales, slug]) { return tales.find(tale => tale.slug === slug); @@ -8,7 +15,7 @@ connector( "tales", withInputSignals( () => db, - db => db.tales, + db => db.tales || [], ), ); @@ -25,6 +32,9 @@ connector( withInputSignals(() => [connect("tales"), connect("tale-slug")], findTale), ); +// triggers an event whenever the tale changes +connect("tale").connect(tale => trigger("project/loaded", tale.value())); + handler("projects/get-all", () => ({ xhr: { url: "/api/tales/", @@ -95,3 +105,15 @@ handler("projects/request-error", response => { handler("projects/activate", ({ db }, eventId, slug) => ({ db: { ...db, activeTale: slug }, })); + +handler("project/loaded", ({ db }, eventId, tale) => { + let effects = { + db: { ...db, editor: { ...db.editor, activeSlide: undefined } }, + }; + if (tale.dimensions) { + let { width, height } = tale.dimensions; + let rect = { x: 0, y: 0, width: width, height: height }; + effects.trigger = ["camera/fit-rect", rect]; + } + return effects; +});