anki/ts/editor/MathjaxHandle.svelte

106 lines
3.1 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;
let dropdownApi: any;
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) {
getComponent(activeImage!).dataset.mathjax = event.detail.mathjax;
let selectionResolve: (value: void) => void;
const afterSelectionUpdate = new Promise((resolve) => {
selectionResolve = resolve;
});
setTimeout(async () => {
errorMessage = activeImage!.title;
await updateSelection();
selectionResolve();
});
return afterSelectionUpdate;
}
2021-08-04 22:01:03 +02:00
</script>
<WithDropdown
drop="down"
autoOpen={true}
autoClose={false}
let:createDropdown
let:dropdownObject
>
{#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={async (event) => {
await onEditorUpdate(event);
dropdownObject.update();
}}
/>
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>