Move derived component stores out of main component store

This commit is contained in:
Andrew Kingston 2024-02-05 11:26:47 +00:00
parent b04d92f8b2
commit 48677aeaab
1 changed files with 32 additions and 36 deletions

View File

@ -72,39 +72,6 @@ export class ComponentStore extends BudiStore {
this.isCached = this.isCached.bind(this) this.isCached = this.isCached.bind(this)
this.cacheSettings = this.cacheSettings.bind(this) this.cacheSettings = this.cacheSettings.bind(this)
this.getComponentSettings = this.getComponentSettings.bind(this) this.getComponentSettings = this.getComponentSettings.bind(this)
this.selected = derived(
[this.store, selectedScreen],
([$store, $selectedScreen]) => {
if (
$selectedScreen &&
$store.selectedComponentId?.startsWith(`${$selectedScreen._id}-`)
) {
return $selectedScreen?.props
}
if (!$selectedScreen || !$store.selectedComponentId) {
return null
}
const selected = findComponent(
$selectedScreen?.props,
$store.selectedComponentId
)
const clone = selected ? cloneDeep(selected) : selected
this.migrateSettings(clone)
return clone
}
)
this.selectedComponentPath = derived(
[this.store, selectedScreen],
([$store, $selectedScreen]) => {
return findComponentPath(
$selectedScreen?.props,
$store.selectedComponentId
).map(component => component._id)
}
)
} }
/** /**
@ -202,7 +169,7 @@ export class ComponentStore extends BudiStore {
const componentPrefix = "@budibase/standard-components" const componentPrefix = "@budibase/standard-components"
let migrated = false let migrated = false
if (enrichedComponent?._component == `${componentPrefix}/formblock`) { if (enrichedComponent?._component === `${componentPrefix}/formblock`) {
// Use default config if the 'buttons' prop has never been initialised // Use default config if the 'buttons' prop has never been initialised
if (!("buttons" in enrichedComponent)) { if (!("buttons" in enrichedComponent)) {
enrichedComponent["buttons"] = enrichedComponent["buttons"] =
@ -1149,6 +1116,35 @@ export class ComponentStore extends BudiStore {
export const componentStore = new ComponentStore() export const componentStore = new ComponentStore()
export const selectedComponent = componentStore.selected export const selectedComponent = derived(
[componentStore, selectedScreen],
([$store, $selectedScreen]) => {
if (
$selectedScreen &&
$store.selectedComponentId?.startsWith(`${$selectedScreen._id}-`)
) {
return $selectedScreen?.props
}
if (!$selectedScreen || !$store.selectedComponentId) {
return null
}
const selected = findComponent(
$selectedScreen?.props,
$store.selectedComponentId
)
export const selectedComponentPath = componentStore.selectedComponentPath const clone = selected ? cloneDeep(selected) : selected
componentStore.migrateSettings(clone)
return clone
}
)
export const selectedComponentPath = derived(
[componentStore, selectedScreen],
([$store, $selectedScreen]) => {
return findComponentPath(
$selectedScreen?.props,
$store.selectedComponentId
).map(component => component._id)
}
)