2020-11-05 06:09:45 +13:00
|
|
|
import { getFrontendStore } from "./store/frontend"
|
2020-03-21 07:47:01 +13:00
|
|
|
import { getBackendUiStore } from "./store/backend"
|
2020-09-22 00:49:34 +12:00
|
|
|
import { getAutomationStore } from "./store/automation/"
|
2020-10-31 02:23:49 +13:00
|
|
|
import { getThemeStore } from "./store/theme"
|
2020-11-06 06:47:27 +13:00
|
|
|
import { derived } from "svelte/store"
|
2020-09-30 03:26:56 +13:00
|
|
|
import analytics from "analytics"
|
2020-12-04 06:31:38 +13:00
|
|
|
import { LAYOUT_NAMES } from "../constants"
|
2020-12-08 04:27:46 +13:00
|
|
|
import { makePropsSafe } from "components/userInterface/assetParsing/createProps"
|
2019-07-13 21:35:57 +12:00
|
|
|
|
2020-11-05 06:09:45 +13:00
|
|
|
export const store = getFrontendStore()
|
2020-03-21 07:47:01 +13:00
|
|
|
export const backendUiStore = getBackendUiStore()
|
2020-09-22 00:49:34 +12:00
|
|
|
export const automationStore = getAutomationStore()
|
2020-10-31 02:23:49 +13:00
|
|
|
export const themeStore = getThemeStore()
|
2019-07-13 21:35:57 +12:00
|
|
|
|
2020-11-25 07:11:34 +13:00
|
|
|
export const currentAsset = derived(store, $store => {
|
2020-12-02 05:22:06 +13:00
|
|
|
const layout = $store.layouts
|
|
|
|
? $store.layouts.find(layout => layout._id === $store.currentAssetId)
|
|
|
|
: null
|
2020-12-06 01:09:58 +13:00
|
|
|
|
|
|
|
if (layout) return layout
|
|
|
|
|
2020-12-02 05:22:06 +13:00
|
|
|
const screen = $store.screens
|
|
|
|
? $store.screens.find(screen => screen._id === $store.currentAssetId)
|
|
|
|
: null
|
2020-12-06 01:09:58 +13:00
|
|
|
|
|
|
|
if (screen) return screen
|
|
|
|
|
2020-11-25 07:11:34 +13:00
|
|
|
return null
|
|
|
|
})
|
|
|
|
|
2020-12-08 04:27:46 +13:00
|
|
|
export const selectedComponent = derived(
|
|
|
|
[store, currentAsset],
|
|
|
|
([$store, $currentAsset]) => {
|
|
|
|
if (!$currentAsset || !$store.selectedComponentId) return null
|
|
|
|
|
|
|
|
function traverse(node, callback) {
|
|
|
|
if (node._id === $store.selectedComponentId) return callback(node)
|
|
|
|
|
|
|
|
if (node._children) {
|
|
|
|
node._children.forEach(child => traverse(child, callback))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.props) {
|
|
|
|
traverse(node.props, callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let component
|
|
|
|
traverse($currentAsset, found => {
|
|
|
|
const componentIdentifier = found._component ?? found.props._component
|
|
|
|
const componentDef = componentIdentifier.startsWith("##")
|
|
|
|
? found
|
|
|
|
: $store.components[componentIdentifier]
|
|
|
|
|
|
|
|
component = makePropsSafe(componentDef, found)
|
|
|
|
})
|
|
|
|
|
|
|
|
return component
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-11-25 07:11:34 +13:00
|
|
|
export const currentAssetName = derived(store, () => {
|
|
|
|
return currentAsset.name
|
|
|
|
})
|
|
|
|
|
|
|
|
// leave this as before for consistency
|
|
|
|
export const allScreens = derived(store, $store => {
|
|
|
|
return $store.screens
|
2020-11-06 06:47:27 +13:00
|
|
|
})
|
|
|
|
|
2020-11-26 06:56:09 +13:00
|
|
|
export const mainLayout = derived(store, $store => {
|
2020-12-02 05:22:06 +13:00
|
|
|
return $store.layouts?.find(
|
2020-12-09 04:21:01 +13:00
|
|
|
layout => layout._id === LAYOUT_NAMES.MASTER.PRIVATE
|
2020-12-02 05:22:06 +13:00
|
|
|
)
|
2020-11-06 06:47:27 +13:00
|
|
|
})
|
|
|
|
|
2019-07-13 21:35:57 +12:00
|
|
|
export const initialise = async () => {
|
2020-02-03 22:24:25 +13:00
|
|
|
try {
|
2020-10-13 09:35:10 +13:00
|
|
|
await analytics.activate()
|
2020-09-30 03:26:56 +13:00
|
|
|
analytics.captureEvent("Builder Started")
|
2020-02-03 22:24:25 +13:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|