2021-10-18 14:01:15 +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">
|
2022-03-31 15:39:49 +02:00
|
|
|
import type CodeMirrorLib from "codemirror";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { onDestroy, onMount, tick } from "svelte";
|
2021-11-23 01:27:32 +01:00
|
|
|
import { writable } from "svelte/store";
|
2022-02-04 09:36:34 +01:00
|
|
|
|
2022-01-24 02:43:09 +01:00
|
|
|
import WithDropdown from "../../components/WithDropdown.svelte";
|
2022-04-25 05:42:54 +02:00
|
|
|
import { escapeSomeEntities, unescapeSomeEntities } from "../../editable/mathjax";
|
2022-01-08 02:46:01 +01:00
|
|
|
import { Mathjax } from "../../editable/mathjax-element";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { on } from "../../lib/events";
|
|
|
|
import { noop } from "../../lib/functional";
|
2022-01-24 02:43:09 +01:00
|
|
|
import HandleBackground from "../HandleBackground.svelte";
|
|
|
|
import HandleControl from "../HandleControl.svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import HandleSelection from "../HandleSelection.svelte";
|
2022-02-03 05:52:11 +01:00
|
|
|
import { context } from "../rich-text-input";
|
2022-01-24 02:43:09 +01:00
|
|
|
import MathjaxMenu from "./MathjaxMenu.svelte";
|
2021-10-18 14:01:15 +02:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
const { container, api } = context.get();
|
2022-03-31 03:17:13 +02:00
|
|
|
const { editable, preventResubscription } = api;
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
let activeImage: HTMLImageElement | null = null;
|
2021-11-23 01:27:32 +01:00
|
|
|
let mathjaxElement: HTMLElement | null = null;
|
|
|
|
let allow = noop;
|
|
|
|
let unsubscribe = noop;
|
|
|
|
|
2022-03-31 15:39:49 +02:00
|
|
|
let selectAll = false;
|
|
|
|
let position: CodeMirrorLib.Position | undefined = undefined;
|
|
|
|
|
2022-04-25 05:42:54 +02:00
|
|
|
/**
|
|
|
|
* Will contain the Mathjax text with unescaped entities.
|
|
|
|
* This is the text displayed in the actual editor window.
|
|
|
|
*/
|
|
|
|
const code = writable("");
|
|
|
|
|
2022-03-31 15:39:49 +02:00
|
|
|
function showHandle(image: HTMLImageElement, pos?: CodeMirrorLib.Position): void {
|
2022-01-08 02:46:01 +01:00
|
|
|
allow = preventResubscription();
|
2022-03-31 15:39:49 +02:00
|
|
|
position = pos;
|
2021-11-23 01:27:32 +01:00
|
|
|
|
2022-03-31 15:39:49 +02:00
|
|
|
/* Setting the activeImage and mathjaxElement to a non-nullish value is
|
|
|
|
* what triggers the Mathjax editor to show */
|
2021-10-18 14:01:15 +02:00
|
|
|
activeImage = image;
|
2022-01-08 02:46:01 +01:00
|
|
|
mathjaxElement = activeImage.closest(Mathjax.tagName)!;
|
2021-11-23 01:27:32 +01:00
|
|
|
|
2022-04-25 05:42:54 +02:00
|
|
|
code.set(unescapeSomeEntities(mathjaxElement.dataset.mathjax ?? ""));
|
2021-11-23 01:27:32 +01:00
|
|
|
unsubscribe = code.subscribe((value: string) => {
|
2022-04-25 05:42:54 +02:00
|
|
|
mathjaxElement!.dataset.mathjax = escapeSomeEntities(value);
|
2021-11-23 01:27:32 +01:00
|
|
|
});
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
function placeHandle(after: boolean): void {
|
2022-03-31 03:17:13 +02:00
|
|
|
editable.focusHandler.flushCaret();
|
2022-01-11 23:39:41 +01:00
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
if (after) {
|
|
|
|
(mathjaxElement as any).placeCaretAfter();
|
|
|
|
} else {
|
|
|
|
(mathjaxElement as any).placeCaretBefore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resetHandle(): Promise<void> {
|
|
|
|
selectAll = false;
|
2022-03-31 15:39:49 +02:00
|
|
|
position = undefined;
|
2022-01-08 02:46:01 +01:00
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
if (activeImage && mathjaxElement) {
|
|
|
|
unsubscribe();
|
|
|
|
activeImage = null;
|
|
|
|
mathjaxElement = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
await tick();
|
|
|
|
allow();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function maybeShowHandle({ target }: Event): Promise<void> {
|
|
|
|
await resetHandle();
|
2021-10-18 14:01:15 +02:00
|
|
|
if (target instanceof HTMLImageElement && target.dataset.anki === "mathjax") {
|
|
|
|
showHandle(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function showAutofocusHandle({
|
|
|
|
detail,
|
2022-03-31 15:39:49 +02:00
|
|
|
}: CustomEvent<{
|
|
|
|
image: HTMLImageElement;
|
|
|
|
position?: [number, number];
|
|
|
|
}>): Promise<void> {
|
|
|
|
let position: CodeMirrorLib.Position | undefined = undefined;
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
await resetHandle();
|
2022-03-31 15:39:49 +02:00
|
|
|
|
|
|
|
if (detail.position) {
|
|
|
|
const [line, ch] = detail.position;
|
|
|
|
position = { line, ch };
|
|
|
|
}
|
|
|
|
|
|
|
|
showHandle(detail.image, position);
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2022-01-08 02:46:01 +01:00
|
|
|
async function showSelectAll({
|
|
|
|
detail,
|
|
|
|
}: CustomEvent<HTMLImageElement>): Promise<void> {
|
|
|
|
await resetHandle();
|
|
|
|
selectAll = true;
|
|
|
|
showHandle(detail);
|
|
|
|
}
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
onMount(() => {
|
2021-11-23 01:27:32 +01:00
|
|
|
const removeClick = on(container, "click", maybeShowHandle);
|
2022-01-08 02:46:01 +01:00
|
|
|
const removeCaretAfter = on(
|
|
|
|
container,
|
|
|
|
"movecaretafter" as any,
|
|
|
|
showAutofocusHandle,
|
|
|
|
);
|
|
|
|
const removeSelectAll = on(container, "selectall" as any, showSelectAll);
|
2021-10-18 14:01:15 +02:00
|
|
|
|
|
|
|
return () => {
|
2021-11-23 01:27:32 +01:00
|
|
|
removeClick();
|
2022-01-08 02:46:01 +01:00
|
|
|
removeCaretAfter();
|
|
|
|
removeSelectAll();
|
2021-10-18 14:01:15 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
let updateSelection: () => Promise<void>;
|
|
|
|
let errorMessage: string;
|
2021-10-18 14:01:15 +02:00
|
|
|
let dropdownApi: any;
|
2021-11-23 01:27:32 +01:00
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
async function onImageResize(): Promise<void> {
|
2021-11-23 01:27:32 +01:00
|
|
|
errorMessage = activeImage!.title;
|
|
|
|
await updateSelection();
|
|
|
|
dropdownApi.update();
|
2021-10-18 14:01:15 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
const resizeObserver = new ResizeObserver(onImageResize);
|
|
|
|
|
2021-10-18 14:01:15 +02:00
|
|
|
let clearResize = noop;
|
|
|
|
function handleImageResizing(activeImage: HTMLImageElement | null) {
|
|
|
|
if (activeImage) {
|
2021-11-23 01:27:32 +01:00
|
|
|
resizeObserver.observe(container);
|
|
|
|
clearResize = on(activeImage, "resize", onImageResize);
|
2021-10-18 14:01:15 +02:00
|
|
|
} else {
|
2021-11-23 01:27:32 +01:00
|
|
|
resizeObserver.unobserve(container);
|
2021-10-18 14:01:15 +02:00
|
|
|
clearResize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$: handleImageResizing(activeImage);
|
|
|
|
|
2021-11-23 01:27:32 +01:00
|
|
|
onDestroy(() => {
|
|
|
|
resizeObserver.disconnect();
|
|
|
|
clearResize();
|
2021-10-18 14:01:15 +02:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2022-02-25 02:14:26 +01:00
|
|
|
<WithDropdown drop="down" autoOpen autoClose={false} distance={4} let:createDropdown>
|
2021-11-23 01:27:32 +01:00
|
|
|
{#if activeImage && mathjaxElement}
|
|
|
|
<MathjaxMenu
|
2022-01-08 02:46:01 +01:00
|
|
|
element={mathjaxElement}
|
2021-11-23 01:27:32 +01:00
|
|
|
{code}
|
2022-01-08 02:46:01 +01:00
|
|
|
{selectAll}
|
2022-03-31 15:39:49 +02:00
|
|
|
{position}
|
2021-10-18 14:01:15 +02:00
|
|
|
bind:updateSelection
|
2022-01-08 02:46:01 +01:00
|
|
|
on:reset={resetHandle}
|
|
|
|
on:moveoutstart={() => {
|
|
|
|
placeHandle(false);
|
|
|
|
resetHandle();
|
|
|
|
}}
|
|
|
|
on:moveoutend={() => {
|
|
|
|
placeHandle(true);
|
|
|
|
resetHandle();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<HandleSelection
|
|
|
|
image={activeImage}
|
|
|
|
{container}
|
|
|
|
bind:updateSelection
|
|
|
|
on:mount={(event) =>
|
|
|
|
(dropdownApi = createDropdown(event.detail.selection))}
|
|
|
|
>
|
|
|
|
<HandleBackground tooltip={errorMessage} />
|
|
|
|
<HandleControl offsetX={1} offsetY={1} />
|
|
|
|
</HandleSelection>
|
|
|
|
</MathjaxMenu>
|
2021-10-18 14:01:15 +02:00
|
|
|
{/if}
|
|
|
|
</WithDropdown>
|