anki/ts/editor/MathjaxHandle.svelte

110 lines
3.2 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">
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;
let dropdownApi: any;
let removeEventListener: () => void = () => {
/* noop */
};
function onImageResize(): void {
if (activeImage) {
errorMessage = activeImage.title;
updateSelection().then(() => dropdownApi.update());
}
}
$: if (activeImage) {
activeImage.addEventListener("resize", onImageResize);
const lastImage = activeImage;
removeEventListener = () =>
lastImage.removeEventListener("resize", onImageResize);
} else {
removeEventListener();
}
const resizeObserver = new ResizeObserver(async () => {
2021-08-07 01:30:26 +02:00
if (activeImage) {
await updateSelection();
dropdownApi.update();
2021-08-07 01:30:26 +02:00
}
});
resizeObserver.observe(container);
let updateSelection: () => Promise<void>;
let errorMessage: string;
function getComponent(image: HTMLImageElement): HTMLElement {
2021-08-08 04:42:24 +02:00
return image.closest("anki-mathjax")! as HTMLElement;
}
function onEditorUpdate(event: CustomEvent): void {
/* this updates the image in Mathjax.svelte */
getComponent(activeImage!).dataset.mathjax = event.detail.mathjax;
}
2021-08-04 22:01:03 +02:00
</script>
<WithDropdown
drop="down"
autoOpen={true}
autoClose={false}
distance={4}
let:createDropdown
>
{#if activeImage}
<HandleSelection
image={activeImage}
{container}
bind:updateSelection
on:mount={(event) => (dropdownApi = createDropdown(event.detail.selection))}
>
<HandleBackground tooltip={errorMessage} />
<HandleControl offsetX={1} offsetY={1} />
</HandleSelection>
<DropdownMenu>
<MathjaxHandleEditor
initialValue={getComponent(activeImage).dataset.mathjax ?? ""}
on:update={onEditorUpdate}
/>
2021-08-07 20:48:02 +02:00
<div class="margin-x">
<ButtonToolbar>
<Item>
<MathjaxHandleInlineBlock
{activeImage}
{isRtl}
on:click={updateSelection}
2021-08-07 20:48:02 +02:00
/>
</Item>
</ButtonToolbar>
</div>
</DropdownMenu>
{/if}
</WithDropdown>
2021-08-07 20:48:02 +02:00
<style lang="scss">
.margin-x {
margin: 0 0.125rem;
}
</style>