anki/ts/editor/MathjaxHandle.svelte

101 lines
3.0 KiB
Svelte
Raw Normal View History

2021-08-04 22:01:03 +02:00
<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
2021-08-07 20:48:02 +02:00
import { tick } from "svelte";
import WithDropdown from "components/WithDropdown.svelte";
2021-08-07 20:48:02 +02:00
import ButtonToolbar from "components/ButtonToolbar.svelte";
import DropdownMenu from "components/DropdownMenu.svelte";
import Item from "components/Item.svelte";
import HandleSelection from "./HandleSelection.svelte";
2021-08-05 23:45:45 +02:00
import HandleBackground from "./HandleBackground.svelte";
import HandleControl from "./HandleControl.svelte";
import MathjaxHandleInlineBlock from "./MathjaxHandleInlineBlock.svelte";
import MathjaxHandleEditor from "./MathjaxHandleEditor.svelte";
export let activeImage: HTMLImageElement | null = null;
export let container: HTMLElement;
export let isRtl: boolean;
2021-08-07 01:30:26 +02:00
const resizeObserver = new ResizeObserver(() => {
if (activeImage) {
updateSelection();
}
});
resizeObserver.observe(container);
let updateSelection: () => void;
let title: string;
function getComponent(image: HTMLImageElement): HTMLElement {
return (image.closest("anki-mathjax")! as HTMLElement);
}
function onUpdate(event: CustomEvent) {
getComponent(activeImage!).dataset.mathjax = event.detail.mathjax;
setTimeout(() => {
updateSelection();
title = activeImage!.title;
});
}
2021-08-04 22:01:03 +02:00
</script>
<WithDropdown
placement="bottom"
autoOpen={true}
autoClose={false}
let:createDropdown
let:dropdownObject
>
{#if activeImage}
<HandleSelection
image={activeImage}
{container}
offsetX={2}
offsetY={2}
bind:updateSelection
on:mount={(event) => createDropdown(event.detail.selection)}
>
<HandleBackground {title} />
<HandleControl offsetX={1} offsetY={1} />
</HandleSelection>
<DropdownMenu>
<MathjaxHandleEditor
initialValue={getComponent(activeImage).dataset.mathjax ?? ""}
on:update={(event) => {
onUpdate(event);
setTimeout(dropdownObject.update);
}}
/>
2021-08-07 20:48:02 +02:00
<div class="margin-x">
<ButtonToolbar>
<Item>
<MathjaxHandleInlineBlock
{activeImage}
{isRtl}
on:click={async () => {
await tick();
updateSelection();
dropdownObject.update();
}}
/>
</Item>
</ButtonToolbar>
<div />
</div></DropdownMenu
>
{/if}
</WithDropdown>
2021-08-07 20:48:02 +02:00
<style lang="scss">
.margin-x {
margin: 0 0.125rem;
}
</style>