import React from "react" import { useStyletron } from "baseui" import Add from "~/components/Icons/Add" import useDesignEditorPages from "~/hooks/useDesignEditorScenes" import useAppContext from "~/hooks/useAppContext" import { DesignEditorContext } from "~/contexts/DesignEditor" import { nanoid } from "nanoid" import { getDefaultTemplate } from "~/constants/design-editor" import { useEditor } from "@layerhub-io/react" import { IScene } from "@layerhub-io/types" import { Block } from "baseui/block" export default function () { const scenes = useDesignEditorPages() const { setScenes, setCurrentScene, currentScene, setCurrentDesign, currentDesign } = React.useContext(DesignEditorContext) const editor = useEditor() const { params } = useAppContext() const { hasMultiScenes = false } = params const [css] = useStyletron() const [currentPreview, setCurrentPreview] = React.useState("") React.useEffect(() => { if (editor && scenes && currentScene) { const isCurrentSceneLoaded = scenes.find((s) => s.id === currentScene?.id) if (!isCurrentSceneLoaded) { setCurrentScene(scenes[0]) } } }, [editor, scenes, currentScene]) React.useEffect(() => { let watcher = async () => { const updatedTemplate = editor.scene.exportToJSON() const updatedPreview = (await editor.renderer.render(updatedTemplate)) as string setCurrentPreview(updatedPreview) } if (editor) { editor.on("history:changed", watcher) } return () => { if (editor) { editor.off("history:changed", watcher) } } }, [editor]) React.useEffect(() => { if (editor) { if (currentScene) { updateCurrentScene(currentScene) } else { const defaultTemplate = getDefaultTemplate({ width: 1920, height: 1080, }) setCurrentDesign({ id: nanoid(), frame: defaultTemplate.frame, metadata: {}, name: "Untitled Design", preview: "", scenes: [], type: "GRAPHIC", }) editor.scene .importFromJSON(defaultTemplate) .then(() => { const initialDesign = editor.scene.exportToJSON() as any editor.renderer.render(initialDesign).then((data) => { setCurrentScene({ ...initialDesign, preview: data }) setScenes([{ ...initialDesign, preview: data }]) }) }) .catch(console.log) } } }, [editor, currentScene]) const updateCurrentScene = React.useCallback( async (design: IScene) => { await editor.scene.importFromJSON(design) const updatedPreview = (await editor.renderer.render(design)) as string setCurrentPreview(updatedPreview) }, [editor, currentScene] ) const addScene = React.useCallback(async () => { setCurrentPreview("") const updatedTemplate = editor.scene.exportToJSON() const updatedPreview = await editor.renderer.render(updatedTemplate) const updatedPages = scenes.map((p) => { if (p.id === updatedTemplate.id) { return { ...updatedTemplate, preview: updatedPreview } } return p }) const defaultTemplate = getDefaultTemplate(currentDesign.frame) const newPreview = await editor.renderer.render(defaultTemplate) const newPage = { ...defaultTemplate, id: nanoid(), preview: newPreview } as any const newPages = [...updatedPages, newPage] as any[] setScenes(newPages) setCurrentScene(newPage) }, [scenes, currentDesign]) const changePage = React.useCallback( async (page: any) => { setCurrentPreview("") if (editor) { const updatedTemplate = editor.scene.exportToJSON() const updatedPreview = await editor.renderer.render(updatedTemplate) const updatedPages = scenes.map((p) => { if (p.id === updatedTemplate.id) { return { ...updatedTemplate, preview: updatedPreview } } return p }) as any[] setScenes(updatedPages) setCurrentScene(page) } }, [editor, scenes, currentScene] ) if(!hasMultiScenes) return null return ( {scenes.map((page, index) => (
changePage(page)} className={css({ cursor: "pointer", position: "relative", border: page.id === currentScene?.id ? "2px solid #7158e2" : "2px solid rgba(0,0,0,.15)", })} >
{index + 1}
))}
) }