Move up MathjaxOverlay to be initialized only once

This commit is contained in:
Henrik Giesel 2022-09-13 22:26:48 +02:00
parent 9d9d4a97c7
commit 36eea447d1
3 changed files with 40 additions and 23 deletions

View file

@ -58,7 +58,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import FrameElement from "./FrameElement.svelte"; import FrameElement from "./FrameElement.svelte";
import { alertIcon } from "./icons"; import { alertIcon } from "./icons";
import ImageHandle from "./image-overlay"; import ImageHandle from "./image-overlay";
import MathjaxHandle from "./mathjax-overlay"; import MathjaxOverlay from "./mathjax-overlay";
import MathjaxElement from "./MathjaxElement.svelte"; import MathjaxElement from "./MathjaxElement.svelte";
import Notification from "./Notification.svelte"; import Notification from "./Notification.svelte";
import PlainTextInput from "./plain-text-input"; import PlainTextInput from "./plain-text-input";
@ -448,7 +448,6 @@ the AddCards dialog) should be implemented in the user of this component.
bind:this={richTextInputs[index]} bind:this={richTextInputs[index]}
> >
<ImageHandle maxWidth={250} maxHeight={125} /> <ImageHandle maxWidth={250} maxHeight={125} />
<MathjaxHandle />
{#if insertSymbols} {#if insertSymbols}
<SymbolsOverlay /> <SymbolsOverlay />
{/if} {/if}
@ -483,6 +482,8 @@ the AddCards dialog) should be implemented in the user of this component.
</DecoratedElements> </DecoratedElements>
</Fields> </Fields>
<MathjaxOverlay />
<div class="note-editor-tag-editor"> <div class="note-editor-tag-editor">
<TagEditor {tags} on:tagsupdate={saveTags} /> <TagEditor {tags} on:tagsupdate={saveTags} />
</div> </div>

View file

@ -4,7 +4,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
--> -->
<script lang="ts"> <script lang="ts">
import type CodeMirrorLib from "codemirror"; import type CodeMirrorLib from "codemirror";
import { onMount, tick } from "svelte"; import { tick } from "svelte";
import { writable } from "svelte/store"; import { writable } from "svelte/store";
import Popover from "../../components/Popover.svelte"; import Popover from "../../components/Popover.svelte";
@ -19,12 +19,38 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { noop } from "../../lib/functional"; import { noop } from "../../lib/functional";
import type { Callback } from "../../lib/typing"; import type { Callback } from "../../lib/typing";
import { singleCallback } from "../../lib/typing"; import { singleCallback } from "../../lib/typing";
import type { EditingInputAPI } from "../EditingArea.svelte";
import HandleBackground from "../HandleBackground.svelte"; import HandleBackground from "../HandleBackground.svelte";
import { context } from "../rich-text-input"; import { context } from "../NoteEditor.svelte";
import type { RichTextInputAPI } from "../rich-text-input";
import { editingInputIsRichText } from "../rich-text-input";
import MathjaxButtons from "./MathjaxButtons.svelte"; import MathjaxButtons from "./MathjaxButtons.svelte";
import MathjaxEditor from "./MathjaxEditor.svelte"; import MathjaxEditor from "./MathjaxEditor.svelte";
const { editable, element, preventResubscription } = context.get(); const { focusedInput } = context.get();
let cleanup: Callback;
let richTextInput: RichTextInputAPI | null = null;
async function initialize(input: EditingInputAPI | null): Promise<void> {
cleanup?.();
if (!input || !editingInputIsRichText(input)) {
richTextInput = null;
return;
}
const container = await input.element;
cleanup = singleCallback(
on(container, "click", showOverlayIfMathjaxClicked),
on(container, "movecaretafter" as any, showOnAutofocus),
on(container, "selectall" as any, showSelectAll),
);
richTextInput = input;
}
$: initialize($focusedInput);
let activeImage: HTMLImageElement | null = null; let activeImage: HTMLImageElement | null = null;
let mathjaxElement: HTMLElement | null = null; let mathjaxElement: HTMLElement | null = null;
@ -41,7 +67,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const code = writable(""); const code = writable("");
function showOverlay(image: HTMLImageElement, pos?: CodeMirrorLib.Position): void { function showOverlay(image: HTMLImageElement, pos?: CodeMirrorLib.Position): void {
allow = preventResubscription(); allow = richTextInput!.preventResubscription();
position = pos; position = pos;
/* Setting the activeImage and mathjaxElement to a non-nullish value is /* Setting the activeImage and mathjaxElement to a non-nullish value is
@ -56,7 +82,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
function placeHandle(after: boolean): void { function placeHandle(after: boolean): void {
editable.focusHandler.flushCaret(); richTextInput!.editable.focusHandler.flushCaret();
if (after) { if (after) {
(mathjaxElement as any).placeCaretAfter(); (mathjaxElement as any).placeCaretAfter();
@ -83,21 +109,21 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
let errorMessage: string; let errorMessage: string;
let cleanup: Callback | null = null; let cleanupImageError: Callback | null = null;
async function updateErrorMessage(): Promise<void> { async function updateErrorMessage(): Promise<void> {
errorMessage = activeImage!.title; errorMessage = activeImage!.title;
} }
async function updateImageErrorCallback(image: HTMLImageElement | null) { async function updateImageErrorCallback(image: HTMLImageElement | null) {
cleanup?.(); cleanupImageError?.();
cleanup = null; cleanupImageError = null;
if (!image) { if (!image) {
return; return;
} }
cleanup = on(image, "resize", updateErrorMessage); cleanupImageError = on(image, "resize", updateErrorMessage);
} }
$: updateImageErrorCallback(activeImage); $: updateImageErrorCallback(activeImage);
@ -132,16 +158,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
showOverlay(detail); showOverlay(detail);
} }
onMount(async () => {
const container = await element;
return singleCallback(
on(container, "click", showOverlayIfMathjaxClicked),
on(container, "movecaretafter" as any, showOnAutofocus),
on(container, "selectall" as any, showSelectAll),
);
});
let isBlock: boolean; let isBlock: boolean;
$: isBlock = mathjaxElement ? hasBlockAttribute(mathjaxElement) : false; $: isBlock = mathjaxElement ? hasBlockAttribute(mathjaxElement) : false;

View file

@ -25,9 +25,9 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
} }
function editingInputIsRichText( function editingInputIsRichText(
editingInput: EditingInputAPI | null, editingInput: EditingInputAPI,
): editingInput is RichTextInputAPI { ): editingInput is RichTextInputAPI {
return editingInput?.name === "rich-text"; return editingInput.name === "rich-text";
} }
import { registerPackage } from "../../lib/runtime-require"; import { registerPackage } from "../../lib/runtime-require";