anki/ts/editable/Mathjax.svelte
Henrik Giesel 2be1f4c56d
Instead of transforming to {\lt}, transform to < (#1818)
* Instead of transforming to {\lt}, transform to <

- In Mathjax editor
- This way you can also use Mathjax convenience shortcuts like <=> in
  chemistry mode: \ce{<=>}

* Remove unused translateEntitiesToMathjax() (dae)

https://github.com/ankitects/anki/pull/1818#discussion_r857238310
2022-04-25 13:42:54 +10:00

114 lines
2.8 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<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>
<script lang="ts">
import { onDestroy } from "svelte";
import { writable } from "svelte/store";
import { randomUUID } from "../lib/uuid";
import { pageTheme } from "../sveltelib/theme";
import { convertMathjax, unescapeSomeEntities } from "./mathjax";
export let mathjax: string;
export let block: boolean;
export let fontSize: number;
$: [converted, title] = convertMathjax(
unescapeSomeEntities(mathjax),
$pageTheme.isDark,
fontSize,
);
$: empty = title === "MathJax";
$: encoded = encodeURIComponent(converted);
const uuid = randomUUID();
const imageHeight = writable(0);
imageToHeightMap.set(uuid, imageHeight);
$: verticalCenter = -$imageHeight / 2 + fontSize / 4;
let image: HTMLImageElement;
export function moveCaretAfter(position?: [number, number]): void {
// This should trigger a focusing of the Mathjax Handle
image.dispatchEvent(
new CustomEvent("movecaretafter", {
detail: { image, position },
bubbles: true,
composed: true,
}),
);
}
export function selectAll(): void {
image.dispatchEvent(
new CustomEvent("selectall", {
detail: image,
bubbles: true,
composed: true,
}),
);
}
function observe(image: Element) {
observer.observe(image);
return {
destroy() {
observer.unobserve(image);
},
};
}
onDestroy(() => imageToHeightMap.delete(uuid));
</script>
<img
bind:this={image}
src="data:image/svg+xml,{encoded}"
class:block
class:empty
style="--vertical-center: {verticalCenter}px;"
alt="Mathjax"
{title}
data-anki="mathjax"
data-uuid={uuid}
on:dragstart|preventDefault
use:observe
/>
<style lang="scss">
:global(anki-mathjax) {
white-space: pre;
}
img {
vertical-align: var(--vertical-center);
}
.block {
display: block;
margin: 1rem auto;
}
.empty {
vertical-align: sub;
}
</style>