2021-08-20 20:27:38 +12:00
|
|
|
import { derived, get } from "svelte/store"
|
2020-11-18 01:08:24 +13:00
|
|
|
import { routeStore } from "./routes"
|
2020-11-24 22:31:54 +13:00
|
|
|
import { builderStore } from "./builder"
|
2021-07-01 07:35:02 +12:00
|
|
|
import { appStore } from "./app"
|
2021-08-20 20:27:38 +12:00
|
|
|
import {
|
|
|
|
findComponentPathById,
|
|
|
|
findChildrenByType,
|
|
|
|
findComponentById,
|
|
|
|
} from "../utils/components"
|
2020-11-14 04:42:32 +13:00
|
|
|
|
2020-11-19 08:18:18 +13:00
|
|
|
const createScreenStore = () => {
|
2020-11-24 22:31:54 +13:00
|
|
|
const store = derived(
|
2021-07-01 07:35:02 +12:00
|
|
|
[appStore, routeStore, builderStore],
|
|
|
|
([$appStore, $routeStore, $builderStore]) => {
|
2021-07-07 22:28:53 +12:00
|
|
|
let activeLayout, activeScreen
|
|
|
|
let layouts, screens
|
2020-11-24 22:31:54 +13:00
|
|
|
if ($builderStore.inBuilder) {
|
|
|
|
// Use builder defined definitions if inside the builder preview
|
2020-11-28 05:36:31 +13:00
|
|
|
activeLayout = $builderStore.layout
|
2020-11-24 22:31:54 +13:00
|
|
|
activeScreen = $builderStore.screen
|
2021-07-07 22:28:53 +12:00
|
|
|
layouts = [activeLayout]
|
|
|
|
screens = [activeScreen]
|
2020-11-24 22:31:54 +13:00
|
|
|
} else {
|
2021-05-21 01:47:17 +12:00
|
|
|
activeLayout = { props: { _component: "screenslot" } }
|
|
|
|
|
|
|
|
// Find the correct screen by matching the current route
|
2021-07-07 22:48:38 +12:00
|
|
|
screens = $appStore.screens
|
|
|
|
layouts = $appStore.layouts
|
2021-05-21 01:47:17 +12:00
|
|
|
if ($routeStore.activeRoute) {
|
2020-12-05 03:04:07 +13:00
|
|
|
activeScreen = screens.find(
|
2021-05-04 22:32:22 +12:00
|
|
|
screen => screen._id === $routeStore.activeRoute.screenId
|
2020-12-05 03:04:07 +13:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if (activeScreen) {
|
|
|
|
activeLayout = layouts.find(
|
2021-05-04 22:32:22 +12:00
|
|
|
layout => layout._id === activeScreen.layoutId
|
2020-12-05 03:04:07 +13:00
|
|
|
)
|
|
|
|
}
|
2020-11-24 22:31:54 +13:00
|
|
|
}
|
2021-07-07 22:28:53 +12:00
|
|
|
return { layouts, screens, activeLayout, activeScreen }
|
2020-11-18 01:08:24 +13:00
|
|
|
}
|
2020-11-24 22:31:54 +13:00
|
|
|
)
|
2020-11-14 04:42:32 +13:00
|
|
|
|
2021-08-20 20:27:38 +12:00
|
|
|
// Utils to parse component definitions
|
|
|
|
const actions = {
|
|
|
|
findComponentById: componentId => {
|
|
|
|
const { activeScreen, activeLayout } = get(store)
|
|
|
|
let result = findComponentById(activeScreen?.props, componentId)
|
|
|
|
if (result) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return findComponentById(activeLayout?.props)
|
|
|
|
},
|
|
|
|
findComponentPathById: componentId => {
|
|
|
|
const { activeScreen, activeLayout } = get(store)
|
|
|
|
let result = findComponentPathById(activeScreen?.props, componentId)
|
|
|
|
if (result) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
return findComponentPathById(activeLayout?.props)
|
|
|
|
},
|
|
|
|
findChildrenByType: (componentId, type) => {
|
|
|
|
const component = actions.findComponentById(componentId)
|
|
|
|
if (!component || !component._children) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
let children = []
|
|
|
|
findChildrenByType(component, type, children)
|
|
|
|
console.log(children)
|
|
|
|
return children
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-18 01:08:24 +13:00
|
|
|
return {
|
|
|
|
subscribe: store.subscribe,
|
2021-08-20 20:27:38 +12:00
|
|
|
actions,
|
2020-11-14 04:42:32 +13:00
|
|
|
}
|
2020-11-18 01:08:24 +13:00
|
|
|
}
|
2020-11-14 04:42:32 +13:00
|
|
|
|
2020-11-19 08:18:18 +13:00
|
|
|
export const screenStore = createScreenStore()
|