Always correctly update MathjaxHandle position

This commit is contained in:
Henrik Giesel 2021-09-15 17:05:43 +02:00
parent ce9674f824
commit 9b7ea75399
5 changed files with 51 additions and 36 deletions

View File

@ -24,10 +24,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
$: {
encoded = encodeURIComponent(converted);
setTimeout(() => {
imageHeight = image.getBoundingClientRect().height;
});
setTimeout(() => (imageHeight = image.getBoundingClientRect().height));
}
let image: HTMLImageElement;

View File

@ -5,7 +5,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<script lang="ts">
import { onMount, createEventDispatcher } from "svelte";
export let title: string | undefined = undefined;
export let tooltip: string | undefined = undefined;
let background: HTMLDivElement;
const dispatch = createEventDispatcher();
@ -15,7 +15,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<div
bind:this={background}
{title}
title={tooltip}
on:mousedown|preventDefault
on:click|stopPropagation
on:dblclick

View File

@ -15,7 +15,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
let width: number;
let height: number;
export function updateSelection(_div: HTMLDivElement): void {
function setSelection(_selection?: HTMLDivElement): void {
const containerRect = container.getBoundingClientRect();
const imageRect = image!.getBoundingClientRect();
@ -28,6 +28,18 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
height = image!.clientHeight;
}
export function updateSelection(): Promise<void> {
let updateResolve: () => void;
const afterUpdate: Promise<void> = new Promise((resolve) => {
updateResolve = resolve;
});
setSelection();
setTimeout(() => updateResolve());
return afterUpdate;
}
const dispatch = createEventDispatcher();
let selection: HTMLDivElement;
@ -36,7 +48,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<div
bind:this={selection}
use:updateSelection
use:setSelection
on:click={(event) =>
/* prevent triggering Bootstrap dropdown */ event.stopImmediatePropagation()}
style="--left: {left}px; --top: {top}px; --width: {width}px; --height: {height}px; --offsetX: {offsetX}px; --offsetY: {offsetY}px;"

View File

@ -53,10 +53,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
}
}
let updateSelection: () => void;
let updateSelection: () => Promise<void>;
async function updateSizesWithDimensions() {
updateSelection();
await updateSelection();
updateDimensions();
}

View File

@ -20,51 +20,57 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export let container: HTMLElement;
export let isRtl: boolean;
const resizeObserver = new ResizeObserver(() => {
let dropdownApi: any;
const resizeObserver = new ResizeObserver(async () => {
if (activeImage) {
updateSelection();
await updateSelection();
dropdownApi.update();
}
});
resizeObserver.observe(container);
let updateSelection: () => void;
let dropdownApi: any;
let title: string;
let updateSelection: () => Promise<void>;
let errorMessage: string;
function getComponent(image: HTMLImageElement): HTMLElement {
return image.closest("anki-mathjax")! as HTMLElement;
}
function scheduleDropdownUpdate() {
setTimeout(async () => {
await tick();
dropdownApi.update();
});
}
async function onEditorUpdate(event: CustomEvent) {
function onEditorUpdate(event: CustomEvent) {
getComponent(activeImage!).dataset.mathjax = event.detail.mathjax;
setTimeout(() => {
updateSelection();
title = activeImage!.title;
scheduleDropdownUpdate();
let selectionResolve: (value: void) => void;
const afterSelectionUpdate = new Promise((resolve) => {
selectionResolve = resolve;
});
setTimeout(async () => {
errorMessage = activeImage!.title;
await updateSelection();
selectionResolve();
});
return afterSelectionUpdate;
}
</script>
<WithDropdown drop="down" autoOpen={true} autoClose={false} let:createDropdown>
<WithDropdown
drop="down"
autoOpen={true}
autoClose={false}
let:createDropdown
let:dropdownObject
>
{#if activeImage}
<HandleSelection
image={activeImage}
{container}
offsetX={2}
offsetY={2}
bind:updateSelection
on:mount={(event) => (dropdownApi = createDropdown(event.detail.selection))}
>
<HandleBackground {title} />
<HandleBackground tooltip={errorMessage} />
<HandleControl offsetX={1} offsetY={1} />
</HandleSelection>
@ -72,7 +78,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<DropdownMenu>
<MathjaxHandleEditor
initialValue={getComponent(activeImage).dataset.mathjax ?? ""}
on:update={onEditorUpdate}
on:update={async (event) => {
await onEditorUpdate(event);
dropdownObject.update();
}}
/>
<div class="margin-x">
<ButtonToolbar>
@ -80,10 +89,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<MathjaxHandleInlineBlock
{activeImage}
{isRtl}
on:click={() => {
updateSelection();
scheduleDropdownUpdate();
}}
on:click={updateSelection}
/>
</Item>
</ButtonToolbar>