Focus on mount when using Mathjax shortcuts

This commit is contained in:
Henrik Giesel 2021-08-07 22:52:48 +02:00
parent af556c391d
commit e06ff2ae3c
5 changed files with 33 additions and 10 deletions

View file

@ -3,12 +3,14 @@ Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
--> -->
<script lang="ts"> <script lang="ts">
import { getContext } from "svelte"; import { onMount, getContext } from "svelte";
import { nightModeKey } from "components/context-keys"; import { nightModeKey } from "components/context-keys";
import { convertMathjax } from "./mathjax"; import { convertMathjax } from "./mathjax";
export let mathjax: string; export let mathjax: string;
export let block: boolean; export let block: boolean;
export let autofocus = false;
/* have fixed fontSize for normal */ /* have fixed fontSize for normal */
export const fontSize: number = 20; export const fontSize: number = 20;
@ -17,9 +19,18 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
$: [converted, title] = convertMathjax(mathjax, nightMode, fontSize); $: [converted, title] = convertMathjax(mathjax, nightMode, fontSize);
$: empty = title === "MathJax"; $: empty = title === "MathJax";
$: encoded = encodeURIComponent(converted); $: encoded = encodeURIComponent(converted);
let image: HTMLImageElement;
onMount(() => {
if (autofocus) {
image.click();
}
});
</script> </script>
<img <img
bind:this={image}
src="data:image/svg+xml,{encoded}" src="data:image/svg+xml,{encoded}"
class:block class:block
class:empty class:empty

View file

@ -163,7 +163,11 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax
this.component = new Mathjax_svelte({ this.component = new Mathjax_svelte({
target: this, target: this,
props: { mathjax, block: this.block }, props: {
mathjax,
block: this.block,
autofocus: this.hasAttribute("focusonmount"),
},
context, context,
} as any); } as any);
} }
@ -172,6 +176,7 @@ export const Mathjax: DecoratedElementConstructor = class Mathjax
this.innerHTML = this.dataset.mathjax ?? ""; this.innerHTML = this.dataset.mathjax ?? "";
delete this.dataset.mathjax; delete this.dataset.mathjax;
this.removeAttribute("style"); this.removeAttribute("style");
this.removeAttribute("focusonmount");
this.component?.$destroy(); this.component?.$destroy();
this.component = undefined; this.component = undefined;

View file

@ -8,7 +8,7 @@ const parser = new DOMParser();
function getCSS(nightMode: boolean, fontSize: number): string { function getCSS(nightMode: boolean, fontSize: number): string {
const color = nightMode ? "white" : "black"; const color = nightMode ? "white" : "black";
/* color is set for Maths, fill for the empty icon */ /* color is set for Maths, fill for the empty icon */
return `svg { color: ${color}; fill: ${color}; font-size: ${fontSize}px; }`; return `svg { color: ${color}; fill: ${color}; font-size: ${fontSize}px; };`;
} }
function getStyle(css: string): HTMLStyleElement { function getStyle(css: string): HTMLStyleElement {

View file

@ -102,7 +102,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
> >
<DropdownItem <DropdownItem
on:click={() => on:click={() =>
wrapCurrent("<anki-mathjax>", "</anki-mathjax>")} wrapCurrent(
"<anki-mathjax focusonmount>",
"</anki-mathjax>"
)}
on:mount={withButton(createShortcut)} on:mount={withButton(createShortcut)}
> >
{tr.editingMathjaxInline()} {tr.editingMathjaxInline()}
@ -118,7 +121,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
<DropdownItem <DropdownItem
on:click={() => on:click={() =>
wrapCurrent( wrapCurrent(
'<anki-mathjax block="true">', '<anki-mathjax block="true" focusonmount>',
"</anki-matjax>" "</anki-matjax>"
)} )}
on:mount={withButton(createShortcut)} on:mount={withButton(createShortcut)}
@ -135,7 +138,10 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
> >
<DropdownItem <DropdownItem
on:click={() => on:click={() =>
wrapCurrent("<anki-mathjax>\\ce{", "}</anki-mathjax>")} wrapCurrent(
"<anki-mathjax focusonmount>\\ce{",
"}</anki-mathjax>"
)}
on:mount={withButton(createShortcut)} on:mount={withButton(createShortcut)}
> >
{tr.editingMathjaxChemistry()} {tr.editingMathjaxChemistry()}

View file

@ -238,18 +238,19 @@ export class EditingArea extends HTMLDivElement {
async showHandles(event: MouseEvent): Promise<void> { async showHandles(event: MouseEvent): Promise<void> {
if (event.target instanceof HTMLImageElement) { if (event.target instanceof HTMLImageElement) {
const image = event.target as HTMLImageElement;
await this.resetHandles(); await this.resetHandles();
if (!event.target.dataset.anki) { if (!image.dataset.anki) {
await this.imageHandle.then((imageHandle) => await this.imageHandle.then((imageHandle) =>
(imageHandle as any).$set({ (imageHandle as any).$set({
activeImage: event.target, activeImage: image,
isRtl: this.isRightToLeft(), isRtl: this.isRightToLeft(),
}) })
); );
} else if (event.target.dataset.anki === "mathjax") { } else if (image.dataset.anki === "mathjax") {
(this.mathjaxHandle as any).$set({ (this.mathjaxHandle as any).$set({
activeImage: event.target, activeImage: image,
isRtl: this.isRightToLeft(), isRtl: this.isRightToLeft(),
}); });
} }