2021-08-04 01:55:39 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
2021-11-23 01:27:32 +01:00
|
|
|
<script context="module" lang="ts">
|
|
|
|
import type { Writable } from "svelte/store";
|
|
|
|
|
|
|
|
const imageToHeightMap = new Map<string, Writable<number>>();
|
|
|
|
const observer = new ResizeObserver((entries: ResizeObserverEntry[]) => {
|
|
|
|
for (const entry of entries) {
|
|
|
|
const image = entry.target as HTMLImageElement;
|
|
|
|
const store = imageToHeightMap.get(image.dataset.uuid!)!;
|
|
|
|
store.set(entry.contentRect.height);
|
|
|
|
|
|
|
|
setTimeout(() => entry.target.dispatchEvent(new Event("resize")));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2021-08-04 01:55:39 +02:00
|
|
|
<script lang="ts">
|
2021-11-24 22:17:41 +01:00
|
|
|
import { onDestroy } from "svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
|
|
|
import { randomUUID } from "../lib/uuid";
|
2021-11-24 22:17:41 +01:00
|
|
|
import { pageTheme } from "../sveltelib/theme";
|
2021-08-04 01:55:39 +02:00
|
|
|
import { convertMathjax } from "./mathjax";
|
|
|
|
|
|
|
|
export let mathjax: string;
|
2021-08-05 06:01:43 +02:00
|
|
|
export let block: boolean;
|
2022-01-08 02:46:01 +01:00
|
|
|
export let fontSize: number;
|
2021-08-04 04:21:35 +02:00
|
|
|
|
2021-11-24 22:17:41 +01:00
|
|
|
$: [converted, title] = convertMathjax(mathjax, $pageTheme.isDark, fontSize);
|
2021-08-07 22:23:18 +02:00
|
|
|
$: empty = title === "MathJax";
|
2021-11-23 01:27:32 +01:00
|
|
|
$: encoded = encodeURIComponent(converted);
|
2021-08-08 04:42:24 +02:00
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
const uuid = randomUUID();
|
|
|
|
const imageHeight = writable(0);
|
|
|
|
imageToHeightMap.set(uuid, imageHeight);
|
2021-08-08 04:42:24 +02:00
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
$: verticalCenter = -$imageHeight / 2 + fontSize / 4;
|
2021-08-07 22:52:48 +02:00
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
let image: HTMLImageElement;
|
2021-08-07 22:52:48 +02:00
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
export function moveCaretAfter(): void {
|
2021-11-23 01:27:32 +01:00
|
|
|
// This should trigger a focusing of the Mathjax Handle
|
2022-01-08 02:46:01 +01:00
|
|
|
image.dispatchEvent(
|
|
|
|
new CustomEvent("movecaretafter", {
|
|
|
|
detail: image,
|
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2021-09-17 20:54:37 +02:00
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
export function selectAll(): void {
|
|
|
|
image.dispatchEvent(
|
|
|
|
new CustomEvent("selectall", {
|
|
|
|
detail: image,
|
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
}),
|
|
|
|
);
|
2021-11-23 01:27:32 +01:00
|
|
|
}
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
function observe(image: Element) {
|
|
|
|
observer.observe(image);
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
return {
|
|
|
|
destroy() {
|
|
|
|
observer.unobserve(image);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-09-17 20:54:37 +02:00
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
onDestroy(() => imageToHeightMap.delete(uuid));
|
2021-08-04 01:55:39 +02:00
|
|
|
</script>
|
|
|
|
|
2021-08-07 04:04:42 +02:00
|
|
|
<img
|
2022-01-08 02:46:01 +01:00
|
|
|
bind:this={image}
|
2021-08-07 04:04:42 +02:00
|
|
|
src="data:image/svg+xml,{encoded}"
|
|
|
|
class:block
|
2021-08-07 22:23:18 +02:00
|
|
|
class:empty
|
2021-11-23 01:27:32 +01:00
|
|
|
style="--vertical-center: {verticalCenter}px;"
|
2021-08-07 04:04:42 +02:00
|
|
|
alt="Mathjax"
|
2021-08-07 19:33:01 +02:00
|
|
|
{title}
|
2021-08-07 04:04:42 +02:00
|
|
|
data-anki="mathjax"
|
2021-11-23 01:27:32 +01:00
|
|
|
data-uuid={uuid}
|
2021-08-07 04:04:42 +02:00
|
|
|
on:dragstart|preventDefault
|
2021-11-23 01:27:32 +01:00
|
|
|
use:observe
|
2021-08-07 04:04:42 +02:00
|
|
|
/>
|
2021-08-04 05:13:42 +02:00
|
|
|
|
|
|
|
<style lang="scss">
|
2021-10-18 14:01:15 +02:00
|
|
|
:global(anki-mathjax) {
|
|
|
|
white-space: pre;
|
|
|
|
}
|
|
|
|
|
2021-08-08 04:42:24 +02:00
|
|
|
img {
|
|
|
|
vertical-align: var(--vertical-center);
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:18:31 +02:00
|
|
|
.block {
|
|
|
|
display: block;
|
2021-11-23 01:27:32 +01:00
|
|
|
margin: 1rem auto;
|
2021-08-04 05:13:42 +02:00
|
|
|
}
|
2021-08-07 22:23:18 +02:00
|
|
|
|
|
|
|
.empty {
|
|
|
|
vertical-align: sub;
|
|
|
|
}
|
2021-08-04 05:13:42 +02:00
|
|
|
</style>
|