Enable edit mode for mathjax blocks

This commit is contained in:
Henrik Giesel 2021-08-04 05:38:45 +02:00
parent cd36fe2518
commit b0378690c0

View file

@ -8,16 +8,39 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export let mathjax: string; export let mathjax: string;
export let type: "inline" | "block" | "chemistry"; export let type: "inline" | "block" | "chemistry";
$: delimiters = type === "inline" ? ["\\[", "\\]"] : ["\\(", "\\)"]; let edit = false;
$: delimiters = type === "inline" ? ["\\(", "\\)"] : ["\\[", "\\]"];
$: converted = convertMathjax(`${delimiters[0]}${mathjax}${delimiters[1]}`); $: converted = convertMathjax(`${delimiters[0]}${mathjax}${delimiters[1]}`);
function autofocus(element: HTMLElement): void {
element.focus();
}
</script> </script>
<div on:click={console.log}> {#if edit}
{#if type === "block"}
<div
contenteditable="true"
bind:innerHTML={mathjax}
on:blur={() => (edit = false)}
use:autofocus
/>
{:else}
<span
contenteditable="true"
bind:innerHTML={mathjax}
on:blur={() => (edit = false)}
use:autofocus
/>
{/if}
{:else}
<div class="d-contents" on:click={() => (edit = true)}>
{@html converted} {@html converted}
</div> </div>
{/if}
<style lang="scss"> <style lang="scss">
div { .d-contents {
display: contents; display: contents;
} }
</style> </style>