2021-11-26 14:25:02 +01:00
|
|
|
import { get, writable, derived } from "svelte/store"
|
|
|
|
import Manifest from "manifest.json"
|
|
|
|
import { findComponentById, findComponentPathById } from "../utils/components"
|
|
|
|
import { devToolsStore } from "./devTools"
|
|
|
|
import { screenStore } from "./screens"
|
|
|
|
import { builderStore } from "./builder"
|
|
|
|
|
|
|
|
const createComponentStore = () => {
|
|
|
|
const store = writable({})
|
|
|
|
|
|
|
|
const derivedStore = derived(
|
2022-02-24 22:48:54 +01:00
|
|
|
[store, builderStore, devToolsStore, screenStore],
|
|
|
|
([$store, $builderState, $devToolsState, $screenState]) => {
|
2021-11-26 14:25:02 +01:00
|
|
|
// Avoid any of this logic if we aren't in the builder preview
|
|
|
|
if (!$builderState.inBuilder && !$devToolsState.visible) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive the selected component instance and definition
|
|
|
|
let asset
|
2022-05-12 10:42:25 +02:00
|
|
|
const { screen, selectedComponentId } = $builderState
|
2021-11-26 14:25:02 +01:00
|
|
|
if ($builderState.inBuilder) {
|
2022-05-12 10:42:25 +02:00
|
|
|
asset = screen
|
2021-11-26 14:25:02 +01:00
|
|
|
} else {
|
|
|
|
asset = $screenState.activeScreen
|
|
|
|
}
|
|
|
|
const component = findComponentById(asset?.props, selectedComponentId)
|
|
|
|
const prefix = "@budibase/standard-components/"
|
|
|
|
const type = component?._component?.replace(prefix, "")
|
|
|
|
const definition = type ? Manifest[type] : null
|
|
|
|
|
|
|
|
// Derive the selected component path
|
|
|
|
const path =
|
|
|
|
findComponentPathById(asset?.props, selectedComponentId) || []
|
|
|
|
|
|
|
|
return {
|
2022-02-24 22:48:54 +01:00
|
|
|
selectedComponentInstance: $store[selectedComponentId],
|
2021-11-26 14:25:02 +01:00
|
|
|
selectedComponent: component,
|
|
|
|
selectedComponentDefinition: definition,
|
|
|
|
selectedComponentPath: path?.map(component => component._id),
|
2022-02-24 22:48:54 +01:00
|
|
|
mountedComponents: Object.keys($store).length,
|
|
|
|
currentAsset: asset,
|
2021-11-26 14:25:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const registerInstance = (id, instance) => {
|
|
|
|
store.update(state => ({
|
|
|
|
...state,
|
|
|
|
[id]: instance,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
const unregisterInstance = id => {
|
|
|
|
store.update(state => {
|
|
|
|
delete state[id]
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-24 22:48:54 +01:00
|
|
|
const isComponentRegistered = id => {
|
|
|
|
return get(store)[id] != null
|
|
|
|
}
|
|
|
|
|
|
|
|
const getComponentById = id => {
|
|
|
|
const asset = get(derivedStore).currentAsset
|
|
|
|
return findComponentById(asset?.props, id)
|
|
|
|
}
|
|
|
|
|
2022-05-17 15:33:12 +02:00
|
|
|
const getComponentDefinition = type => {
|
|
|
|
const prefix = "@budibase/standard-components/"
|
|
|
|
type = type?.replace(prefix, "")
|
|
|
|
return type ? Manifest[type] : null
|
|
|
|
}
|
|
|
|
|
2021-11-26 14:25:02 +01:00
|
|
|
return {
|
|
|
|
...derivedStore,
|
2022-02-24 22:48:54 +01:00
|
|
|
actions: {
|
|
|
|
registerInstance,
|
|
|
|
unregisterInstance,
|
|
|
|
isComponentRegistered,
|
|
|
|
getComponentById,
|
2022-05-17 15:33:12 +02:00
|
|
|
getComponentDefinition,
|
2022-02-24 22:48:54 +01:00
|
|
|
},
|
2021-11-26 14:25:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const componentStore = createComponentStore()
|