Disregard block components from ejection which are unmounted during parent blocks lifecycles

This commit is contained in:
Andrew Kingston 2022-11-15 12:58:52 +00:00
parent e9c6aa2593
commit 7b80dabfe2
2 changed files with 17 additions and 2 deletions

View File

@ -9,7 +9,7 @@
let structureLookupMap = {} let structureLookupMap = {}
const registerBlockComponent = (id, order, parentId, instance) => { const registerBlockComponent = (id, order, parentId, instance) => {
// Ensure child array exists // Ensure child map exists
if (!structureLookupMap[parentId]) { if (!structureLookupMap[parentId]) {
structureLookupMap[parentId] = {} structureLookupMap[parentId] = {}
} }
@ -18,6 +18,14 @@
structureLookupMap[parentId][order] = instance structureLookupMap[parentId][order] = instance
} }
const unregisterBlockComponent = (order, parentId) => {
// Ensure child map exists
if (!structureLookupMap[parentId]) {
return
}
delete structureLookupMap[parentId][order]
}
const eject = () => { const eject = () => {
// Start the new structure with the root component // Start the new structure with the root component
let definition = structureLookupMap[$component.id][0] let definition = structureLookupMap[$component.id][0]
@ -73,6 +81,7 @@
// We register block components with their raw props so that we can eject // We register block components with their raw props so that we can eject
// blocks later on // blocks later on
registerComponent: registerBlockComponent, registerComponent: registerBlockComponent,
unregisterComponent: unregisterBlockComponent,
}) })
onMount(() => { onMount(() => {

View File

@ -1,5 +1,5 @@
<script> <script>
import { getContext } from "svelte" import { getContext, onDestroy } from "svelte"
import { generate } from "shortid" import { generate } from "shortid"
import { builderStore } from "../stores/builder.js" import { builderStore } from "../stores/builder.js"
import Component from "components/Component.svelte" import Component from "components/Component.svelte"
@ -41,6 +41,12 @@
block.registerComponent(id, order ?? 0, $component?.id, instance) block.registerComponent(id, order ?? 0, $component?.id, instance)
} }
} }
onDestroy(() => {
if ($builderStore.inBuilder) {
block.unregisterComponent(order ?? 0, $component?.id)
}
})
</script> </script>
<Component {instance} isBlock> <Component {instance} isBlock>