2021-05-27 15:50:49 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
2021-10-26 00:43:02 +02:00
|
|
|
<script lang="ts">
|
2021-05-27 15:50:49 +02:00
|
|
|
import { setContext } from "svelte";
|
|
|
|
import { writable } from "svelte/store";
|
2021-05-29 17:44:08 +02:00
|
|
|
import Item from "./Item.svelte";
|
2021-06-30 19:55:56 +02:00
|
|
|
import { sectionKey } from "./context-keys";
|
2021-05-27 15:50:49 +02:00
|
|
|
import type { Identifier } from "./identifier";
|
|
|
|
import { insertElement, appendElement } from "./identifier";
|
|
|
|
import type { SvelteComponent, Registration } from "./registration";
|
|
|
|
import { makeInterface } from "./registration";
|
|
|
|
|
|
|
|
export let id: string | undefined = undefined;
|
|
|
|
|
|
|
|
function makeRegistration(): Registration {
|
|
|
|
const detach = writable(false);
|
|
|
|
return { detach };
|
|
|
|
}
|
|
|
|
|
|
|
|
const { registerComponent, dynamicItems, getDynamicInterface } =
|
|
|
|
makeInterface(makeRegistration);
|
|
|
|
|
|
|
|
setContext(sectionKey, registerComponent);
|
|
|
|
|
|
|
|
export let api: Record<string, never> | undefined = undefined;
|
|
|
|
let sectionRef: HTMLDivElement;
|
|
|
|
|
|
|
|
$: if (sectionRef && api) {
|
|
|
|
const { addComponent, updateRegistration } = getDynamicInterface(sectionRef);
|
|
|
|
|
|
|
|
const insert = (group: SvelteComponent, position: Identifier = 0) =>
|
|
|
|
addComponent(group, (added, parent) =>
|
2021-10-19 01:06:00 +02:00
|
|
|
insertElement(added, parent, position),
|
2021-05-27 15:50:49 +02:00
|
|
|
);
|
|
|
|
const append = (group: SvelteComponent, position: Identifier = -1) =>
|
|
|
|
addComponent(group, (added, parent) =>
|
2021-10-19 01:06:00 +02:00
|
|
|
appendElement(added, parent, position),
|
2021-05-27 15:50:49 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const show = (id: Identifier) =>
|
|
|
|
updateRegistration(({ detach }) => detach.set(false), id);
|
|
|
|
const hide = (id: Identifier) =>
|
|
|
|
updateRegistration(({ detach }) => detach.set(true), id);
|
|
|
|
const toggle = (id: Identifier) =>
|
|
|
|
updateRegistration(
|
|
|
|
({ detach }) => detach.update((old: boolean): boolean => !old),
|
2021-10-19 01:06:00 +02:00
|
|
|
id,
|
2021-05-27 15:50:49 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Object.assign(api, { insert, append, show, hide, toggle });
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-05-29 17:01:56 +02:00
|
|
|
<div bind:this={sectionRef} {id}>
|
2021-05-27 15:50:49 +02:00
|
|
|
<slot />
|
|
|
|
{#each $dynamicItems as item}
|
2021-05-29 17:44:08 +02:00
|
|
|
<Item id={item[0].id} registration={item[1]}>
|
2021-05-27 15:50:49 +02:00
|
|
|
<svelte:component this={item[0].component} {...item[0].props} />
|
2021-05-29 17:44:08 +02:00
|
|
|
</Item>
|
2021-05-27 15:50:49 +02:00
|
|
|
{/each}
|
|
|
|
</div>
|
2021-05-29 17:01:56 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
div {
|
|
|
|
display: contents;
|
|
|
|
}
|
|
|
|
</style>
|