anki/ts/editor/ClozeButtons.svelte
Matthias Metelka 68fa661b53
Finish #2070: Single overlay instead of per field (#2144)
* Move up MathjaxOverlay to be initialized only once

* Move ImageOverlay to NoteEditor root

* Move Symbols Overlay to NoteEditor root

* Refactor image overlay to not require second mutation observer

* Use elevation + overflow:hidden  in Editorfield

* Make it possible to show input next to each other again

* Set handle background color to code bg

* Make Collapsible unmount the component

* Simplify how decorated elements are mounted

* Set RichTextInput background to frame-bg again

* Strip out FocusTrap code

* Revert "Make Collapsible unmount the component"

This reverts commit 52722065ea199fa57ae750fa34bf47ee1c5aab3c.

* Allow clicking on label container to unfocus field

* Fix mathjax overlay resetting too its api too soon

* Allow scrolling on overlays

* Set focus-border border-color in focused field

* Fix background color of fields

* Add back grid-gap

removed it during merge to see if margin-top would behave any differently - which is not the case.

* Fix double border issue within Collapsible.svelte

* Format

* Edit appearance of focused fields a bit

* Remove unused properties

* Include elevation in button_mixins_lib

* Give label-container a background color

Co-authored-by: Henrik Giesel <hengiesel@gmail.com>
2022-10-27 09:11:36 +10:00

107 lines
3.1 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import { createEventDispatcher } from "svelte";
import { get } from "svelte/store";
import ButtonGroup from "../components/ButtonGroup.svelte";
import IconButton from "../components/IconButton.svelte";
import Shortcut from "../components/Shortcut.svelte";
import * as tr from "../lib/ftl";
import { isApplePlatform } from "../lib/platform";
import { getPlatformString } from "../lib/shortcuts";
import { clozeIcon, incrementClozeIcon } from "./icons";
import { context as noteEditorContext } from "./NoteEditor.svelte";
import { editingInputIsRichText } from "./rich-text-input";
const { focusedInput, fields } = noteEditorContext.get();
// Workaround for Cmd+Option+Shift+C not working on macOS. The keyup approach works
// on Linux as well, but fails on Windows.
const event = isApplePlatform() ? "keyup" : "keydown";
const clozePattern = /\{\{c(\d+)::/gu;
function getCurrentHighestCloze(increment: boolean): number {
let highest = 0;
for (const field of fields) {
const content = field.editingArea?.content;
const fieldHTML = content ? get(content) : "";
const matches: number[] = [];
let match: RegExpMatchArray | null = null;
while ((match = clozePattern.exec(fieldHTML))) {
matches.push(Number(match[1]));
}
highest = Math.max(highest, ...matches);
}
if (increment) {
highest++;
}
return Math.max(1, highest);
}
const dispatch = createEventDispatcher();
async function onIncrementCloze(): Promise<void> {
const highestCloze = getCurrentHighestCloze(true);
dispatch("surround", {
prefix: `{{c${highestCloze}::`,
suffix: "}}",
});
}
async function onSameCloze(): Promise<void> {
const highestCloze = getCurrentHighestCloze(false);
dispatch("surround", {
prefix: `{{c${highestCloze}::`,
suffix: "}}",
});
}
$: disabled = !$focusedInput || !editingInputIsRichText($focusedInput);
const incrementKeyCombination = "Control+Shift+C";
const sameKeyCombination = "Control+Alt+Shift+C";
</script>
<ButtonGroup>
<IconButton
tooltip="{tr.editingClozeDeletion()} ({getPlatformString(
incrementKeyCombination,
)})"
{disabled}
on:click={onIncrementCloze}
--border-left-radius="5px"
>
{@html incrementClozeIcon}
</IconButton>
<Shortcut
keyCombination={incrementKeyCombination}
event="keydown"
on:action={onIncrementCloze}
/>
<IconButton
tooltip="{tr.editingClozeDeletionRepeat()} ({getPlatformString(
sameKeyCombination,
)})"
{disabled}
on:click={onSameCloze}
--border-right-radius="5px"
>
{@html clozeIcon}
</IconButton>
<Shortcut keyCombination={sameKeyCombination} {event} on:action={onSameCloze} />
</ButtonGroup>