2021-05-06 18:51:44 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="typescript">
|
2021-07-09 01:33:06 +02:00
|
|
|
import { getContext, setContext } from "svelte";
|
2021-05-06 19:26:50 +02:00
|
|
|
import { writable } from "svelte/store";
|
2021-05-29 17:44:08 +02:00
|
|
|
import Item from "./Item.svelte";
|
2021-07-09 01:33:06 +02:00
|
|
|
import { sectionKey, nightModeKey } from "./context-keys";
|
2021-05-06 19:26:50 +02:00
|
|
|
import type { Identifier } from "./identifier";
|
2021-05-27 15:50:49 +02:00
|
|
|
import { insertElement, appendElement } from "./identifier";
|
2021-05-27 13:55:13 +02:00
|
|
|
import type { SvelteComponent, Registration } from "./registration";
|
2021-05-06 19:26:50 +02:00
|
|
|
import { makeInterface } from "./registration";
|
|
|
|
|
2021-05-06 18:51:44 +02:00
|
|
|
export let id: string | undefined = undefined;
|
2021-05-06 19:26:50 +02:00
|
|
|
let className: string = "";
|
2021-05-06 18:51:44 +02:00
|
|
|
export { className as class };
|
|
|
|
|
2021-05-27 17:13:36 +02:00
|
|
|
export let size: number | undefined = undefined;
|
|
|
|
export let wrap: boolean | undefined = undefined;
|
|
|
|
|
|
|
|
$: buttonSize = size ? `--buttons-size: ${size}rem; ` : "";
|
|
|
|
let buttonWrap: string;
|
|
|
|
$: if (wrap === undefined) {
|
|
|
|
buttonWrap = "";
|
|
|
|
} else {
|
|
|
|
buttonWrap = wrap ? `--buttons-wrap: wrap; ` : `--buttons-wrap: nowrap; `;
|
|
|
|
}
|
|
|
|
|
|
|
|
$: style = buttonSize + buttonWrap;
|
2021-05-06 19:26:50 +02:00
|
|
|
|
2021-05-27 13:55:13 +02:00
|
|
|
function makeRegistration(): Registration {
|
2021-05-06 19:26:50 +02:00
|
|
|
const detach = writable(false);
|
|
|
|
return { detach };
|
|
|
|
}
|
|
|
|
|
2021-05-26 01:21:33 +02:00
|
|
|
const { registerComponent, dynamicItems, getDynamicInterface } =
|
|
|
|
makeInterface(makeRegistration);
|
2021-05-06 19:26:50 +02:00
|
|
|
|
2021-05-27 13:55:13 +02:00
|
|
|
setContext(sectionKey, registerComponent);
|
2021-05-06 19:26:50 +02:00
|
|
|
|
2021-05-06 20:39:52 +02:00
|
|
|
export let api: Record<string, unknown> | undefined = undefined;
|
2021-05-06 19:26:50 +02:00
|
|
|
let buttonToolbarRef: HTMLDivElement;
|
|
|
|
|
2021-05-06 20:39:52 +02:00
|
|
|
$: if (buttonToolbarRef && api) {
|
2021-05-26 01:21:33 +02:00
|
|
|
const { addComponent, updateRegistration } =
|
|
|
|
getDynamicInterface(buttonToolbarRef);
|
2021-05-06 19:26:50 +02:00
|
|
|
|
2021-05-06 20:29:55 +02:00
|
|
|
const insertGroup = (group: SvelteComponent, position: Identifier = 0) =>
|
2021-05-27 15:50:49 +02:00
|
|
|
addComponent(group, (added, parent) =>
|
2021-10-19 01:06:00 +02:00
|
|
|
insertElement(added, parent, position),
|
2021-05-27 15:50:49 +02:00
|
|
|
);
|
2021-05-06 20:29:55 +02:00
|
|
|
const appendGroup = (group: SvelteComponent, position: Identifier = -1) =>
|
2021-05-27 15:50:49 +02:00
|
|
|
addComponent(group, (added, parent) =>
|
2021-10-19 01:06:00 +02:00
|
|
|
appendElement(added, parent, position),
|
2021-05-27 15:50:49 +02:00
|
|
|
);
|
2021-05-06 19:26:50 +02:00
|
|
|
|
|
|
|
const showGroup = (id: Identifier) =>
|
|
|
|
updateRegistration(({ detach }) => detach.set(false), id);
|
|
|
|
const hideGroup = (id: Identifier) =>
|
|
|
|
updateRegistration(({ detach }) => detach.set(true), id);
|
|
|
|
const toggleGroup = (id: Identifier) =>
|
|
|
|
updateRegistration(
|
|
|
|
({ detach }) => detach.update((old: boolean): boolean => !old),
|
2021-10-19 01:06:00 +02:00
|
|
|
id,
|
2021-05-06 19:26:50 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
Object.assign(api, {
|
|
|
|
insertGroup,
|
|
|
|
appendGroup,
|
|
|
|
showGroup,
|
|
|
|
hideGroup,
|
|
|
|
toggleGroup,
|
|
|
|
});
|
|
|
|
}
|
2021-07-09 01:33:06 +02:00
|
|
|
|
|
|
|
const nightMode = getContext<boolean>(nightModeKey);
|
2021-05-06 18:51:44 +02:00
|
|
|
</script>
|
|
|
|
|
2021-05-06 19:26:50 +02:00
|
|
|
<div
|
|
|
|
bind:this={buttonToolbarRef}
|
|
|
|
{id}
|
2021-10-18 14:01:15 +02:00
|
|
|
class="button-toolbar btn-toolbar {className}"
|
2021-07-09 01:33:06 +02:00
|
|
|
class:nightMode
|
2021-05-27 17:13:36 +02:00
|
|
|
{style}
|
2021-05-26 01:21:33 +02:00
|
|
|
role="toolbar"
|
2021-06-29 20:13:28 +02:00
|
|
|
on:focusout
|
2021-05-26 01:21:33 +02:00
|
|
|
>
|
2021-05-06 18:51:44 +02:00
|
|
|
<slot />
|
2021-05-06 19:26:50 +02:00
|
|
|
{#each $dynamicItems as item}
|
2021-05-29 17:44:08 +02:00
|
|
|
<Item id={item[0].id} registration={item[1]}>
|
2021-05-06 19:26:50 +02:00
|
|
|
<svelte:component this={item[0].component} {...item[0].props} />
|
2021-05-29 17:44:08 +02:00
|
|
|
</Item>
|
2021-05-06 19:26:50 +02:00
|
|
|
{/each}
|
2021-05-06 18:51:44 +02:00
|
|
|
</div>
|
2021-05-27 17:13:36 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
2021-10-09 02:25:03 +02:00
|
|
|
@use "sass/scrollbar";
|
2021-07-09 01:33:06 +02:00
|
|
|
|
|
|
|
.nightMode {
|
|
|
|
@include scrollbar.night-mode;
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
.button-toolbar {
|
2021-05-27 17:13:36 +02:00
|
|
|
flex-wrap: var(--buttons-wrap);
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
> :global(*) {
|
|
|
|
/* TODO replace with gap once available */
|
|
|
|
margin-right: 0.15rem;
|
|
|
|
margin-bottom: 0.15rem;
|
|
|
|
}
|
2021-05-27 17:13:36 +02:00
|
|
|
}
|
|
|
|
</style>
|