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";
|
|
|
|
|
2021-08-06 04:59:33 +02:00
|
|
|
import WithDropdown from "components/WithDropdown.svelte";
|
2021-08-07 20:48:02 +02:00
|
|
|
import ButtonToolbar from "components/ButtonToolbar.svelte";
|
2021-08-06 16:52:34 +02:00
|
|
|
import DropdownMenu from "components/DropdownMenu.svelte";
|
2021-08-06 04:59:33 +02:00
|
|
|
import Item from "components/Item.svelte";
|
|
|
|
|
2021-08-05 23:34:26 +02:00
|
|
|
import HandleSelection from "./HandleSelection.svelte";
|
2021-08-05 23:45:45 +02:00
|
|
|
import HandleBackground from "./HandleBackground.svelte";
|
|
|
|
import HandleControl from "./HandleControl.svelte";
|
2021-08-06 00:59:52 +02:00
|
|
|
import MathjaxHandleInlineBlock from "./MathjaxHandleInlineBlock.svelte";
|
2021-08-06 16:52:34 +02:00
|
|
|
import MathjaxHandleEditor from "./MathjaxHandleEditor.svelte";
|
2021-08-05 23:34:26 +02:00
|
|
|
|
|
|
|
export let activeImage: HTMLImageElement | null = null;
|
|
|
|
export let container: HTMLElement;
|
2021-08-06 00:59:52 +02:00
|
|
|
export let isRtl: boolean;
|
|
|
|
|
2021-09-15 17:05:43 +02:00
|
|
|
let dropdownApi: any;
|
|
|
|
|
|
|
|
const resizeObserver = new ResizeObserver(async () => {
|
2021-08-07 01:30:26 +02:00
|
|
|
if (activeImage) {
|
2021-09-15 17:05:43 +02:00
|
|
|
await updateSelection();
|
|
|
|
dropdownApi.update();
|
2021-08-07 01:30:26 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
resizeObserver.observe(container);
|
|
|
|
|
2021-09-15 17:05:43 +02:00
|
|
|
let updateSelection: () => Promise<void>;
|
|
|
|
let errorMessage: string;
|
2021-08-06 17:40:27 +02:00
|
|
|
|
2021-08-08 02:32:59 +02:00
|
|
|
function getComponent(image: HTMLImageElement): HTMLElement {
|
2021-08-08 04:42:24 +02:00
|
|
|
return image.closest("anki-mathjax")! as HTMLElement;
|
2021-08-08 02:32:59 +02:00
|
|
|
}
|
|
|
|
|
2021-09-15 17:05:43 +02:00
|
|
|
function onEditorUpdate(event: CustomEvent) {
|
2021-08-08 02:32:59 +02:00
|
|
|
getComponent(activeImage!).dataset.mathjax = event.detail.mathjax;
|
2021-09-15 15:52:56 +02:00
|
|
|
|
2021-09-15 17:05:43 +02:00
|
|
|
let selectionResolve: (value: void) => void;
|
|
|
|
const afterSelectionUpdate = new Promise((resolve) => {
|
|
|
|
selectionResolve = resolve;
|
|
|
|
});
|
|
|
|
|
|
|
|
setTimeout(async () => {
|
|
|
|
errorMessage = activeImage!.title;
|
|
|
|
await updateSelection();
|
|
|
|
selectionResolve();
|
2021-08-07 19:44:06 +02:00
|
|
|
});
|
2021-09-15 17:05:43 +02:00
|
|
|
|
|
|
|
return afterSelectionUpdate;
|
2021-08-06 17:40:27 +02:00
|
|
|
}
|
2021-08-04 22:01:03 +02:00
|
|
|
</script>
|
2021-08-05 23:34:26 +02:00
|
|
|
|
2021-09-15 17:05:43 +02:00
|
|
|
<WithDropdown
|
|
|
|
drop="down"
|
|
|
|
autoOpen={true}
|
|
|
|
autoClose={false}
|
|
|
|
let:createDropdown
|
|
|
|
let:dropdownObject
|
|
|
|
>
|
2021-08-06 04:59:33 +02:00
|
|
|
{#if activeImage}
|
|
|
|
<HandleSelection
|
|
|
|
image={activeImage}
|
|
|
|
{container}
|
|
|
|
bind:updateSelection
|
2021-09-15 15:52:56 +02:00
|
|
|
on:mount={(event) => (dropdownApi = createDropdown(event.detail.selection))}
|
2021-08-06 00:59:52 +02:00
|
|
|
>
|
2021-09-15 17:05:43 +02:00
|
|
|
<HandleBackground tooltip={errorMessage} />
|
2021-08-06 04:59:33 +02:00
|
|
|
|
|
|
|
<HandleControl offsetX={1} offsetY={1} />
|
|
|
|
</HandleSelection>
|
|
|
|
|
2021-08-07 02:25:16 +02:00
|
|
|
<DropdownMenu>
|
|
|
|
<MathjaxHandleEditor
|
2021-08-08 02:32:59 +02:00
|
|
|
initialValue={getComponent(activeImage).dataset.mathjax ?? ""}
|
2021-09-15 17:05:43 +02:00
|
|
|
on:update={async (event) => {
|
|
|
|
await onEditorUpdate(event);
|
|
|
|
dropdownObject.update();
|
|
|
|
}}
|
2021-08-07 02:25:16 +02:00
|
|
|
/>
|
2021-08-07 20:48:02 +02:00
|
|
|
<div class="margin-x">
|
|
|
|
<ButtonToolbar>
|
|
|
|
<Item>
|
|
|
|
<MathjaxHandleInlineBlock
|
|
|
|
{activeImage}
|
|
|
|
{isRtl}
|
2021-09-15 17:05:43 +02:00
|
|
|
on:click={updateSelection}
|
2021-08-07 20:48:02 +02:00
|
|
|
/>
|
|
|
|
</Item>
|
|
|
|
</ButtonToolbar>
|
2021-09-15 15:52:56 +02:00
|
|
|
</div>
|
|
|
|
</DropdownMenu>
|
2021-08-06 04:59:33 +02:00
|
|
|
{/if}
|
|
|
|
</WithDropdown>
|
2021-08-07 20:48:02 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.margin-x {
|
|
|
|
margin: 0 0.125rem;
|
|
|
|
}
|
|
|
|
</style>
|