2022-01-24 02:43:09 +01: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-02 05:21:19 +01:00
|
|
|
import { writable } from "svelte/store";
|
|
|
|
|
2022-01-24 02:43:09 +01:00
|
|
|
import DropdownItem from "../../components/DropdownItem.svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import IconButton from "../../components/IconButton.svelte";
|
2022-03-02 05:21:19 +01:00
|
|
|
import Popover from "../../components/Popover.svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import Shortcut from "../../components/Shortcut.svelte";
|
2022-03-02 05:21:19 +01:00
|
|
|
import WithFloating from "../../components/WithFloating.svelte";
|
2022-02-04 09:36:34 +01:00
|
|
|
import * as tr from "../../lib/ftl";
|
2022-01-24 02:43:09 +01:00
|
|
|
import { getPlatformString } from "../../lib/shortcuts";
|
|
|
|
import { wrapInternal } from "../../lib/wrap";
|
2022-02-03 05:52:11 +01:00
|
|
|
import { context as noteEditorContext } from "../NoteEditor.svelte";
|
2022-01-24 02:43:09 +01:00
|
|
|
import type { RichTextInputAPI } from "../rich-text-input";
|
2022-02-03 05:52:11 +01:00
|
|
|
import { editingInputIsRichText } from "../rich-text-input";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { functionIcon } from "./icons";
|
2022-01-24 02:43:09 +01:00
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
const { focusedInput } = noteEditorContext.get();
|
|
|
|
$: richTextAPI = $focusedInput as RichTextInputAPI;
|
2022-01-24 02:43:09 +01:00
|
|
|
|
|
|
|
async function surround(front: string, back: string): Promise<void> {
|
|
|
|
const element = await richTextAPI.element;
|
|
|
|
wrapInternal(element, front, back, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMathjaxInline(): void {
|
|
|
|
surround("<anki-mathjax focusonmount>", "</anki-mathjax>");
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMathjaxBlock(): void {
|
|
|
|
surround('<anki-mathjax block="true" focusonmount>', "</anki-matjax>");
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMathjaxChemistry(): void {
|
|
|
|
surround("<anki-mathjax focusonmount>\\ce{", "}</anki-mathjax>");
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLatex(): void {
|
|
|
|
surround("[latex]", "[/latex]");
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLatexEquation(): void {
|
|
|
|
surround("[$]", "[/$]");
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLatexMathEnv(): void {
|
|
|
|
surround("[$$]", "[/$$]");
|
|
|
|
}
|
|
|
|
|
|
|
|
type LatexItem = [() => void, string, string];
|
|
|
|
|
|
|
|
const dropdownItems: LatexItem[] = [
|
|
|
|
[onMathjaxInline, "Control+M, M", tr.editingMathjaxInline()],
|
|
|
|
[onMathjaxBlock, "Control+M, E", tr.editingMathjaxBlock()],
|
|
|
|
[onMathjaxChemistry, "Control+M, C", tr.editingMathjaxChemistry()],
|
|
|
|
[onLatex, "Control+T, T", tr.editingLatex()],
|
|
|
|
[onLatexEquation, "Control+T, E", tr.editingLatexEquation()],
|
|
|
|
[onLatexMathEnv, "Control+T, M", tr.editingLatexMathEnv()],
|
|
|
|
];
|
|
|
|
|
2022-02-03 05:52:11 +01:00
|
|
|
$: disabled = !editingInputIsRichText($focusedInput);
|
2022-03-02 05:21:19 +01:00
|
|
|
|
|
|
|
const showDropdown = writable(false);
|
|
|
|
|
|
|
|
$: if (disabled) {
|
|
|
|
$showDropdown = false;
|
|
|
|
}
|
2022-01-24 02:43:09 +01:00
|
|
|
</script>
|
|
|
|
|
2022-03-11 06:48:49 +01:00
|
|
|
<WithFloating show={showDropdown} closeOnInsideClick>
|
|
|
|
<span
|
|
|
|
class="latex-button"
|
|
|
|
slot="reference"
|
|
|
|
let:asReference
|
|
|
|
use:asReference
|
|
|
|
let:toggle
|
|
|
|
>
|
|
|
|
<IconButton slot="reference" {disabled} on:click={toggle}>
|
|
|
|
{@html functionIcon}
|
|
|
|
</IconButton>
|
|
|
|
</span>
|
2022-01-24 02:43:09 +01:00
|
|
|
|
2022-03-02 05:21:19 +01:00
|
|
|
<Popover slot="floating">
|
2022-01-24 02:43:09 +01:00
|
|
|
{#each dropdownItems as [callback, keyCombination, label]}
|
|
|
|
<DropdownItem on:click={callback}>
|
|
|
|
{label}
|
2022-02-22 13:17:22 +01:00
|
|
|
<span class="ms-auto ps-2 shortcut"
|
|
|
|
>{getPlatformString(keyCombination)}</span
|
|
|
|
>
|
2022-01-24 02:43:09 +01:00
|
|
|
</DropdownItem>
|
|
|
|
<Shortcut {keyCombination} on:action={callback} />
|
|
|
|
{/each}
|
2022-03-02 05:21:19 +01:00
|
|
|
</Popover>
|
|
|
|
</WithFloating>
|
2022-02-22 13:17:22 +01:00
|
|
|
|
|
|
|
<style lang="scss">
|
2022-03-11 06:48:49 +01:00
|
|
|
.latex-button {
|
|
|
|
line-height: 1;
|
|
|
|
}
|
|
|
|
|
2022-02-22 13:17:22 +01:00
|
|
|
.shortcut {
|
|
|
|
font: Verdana;
|
|
|
|
}
|
|
|
|
</style>
|