Anki/ts/editor/editor-toolbar/CommandIconButton.svelte
Henrik Giesel f534dbb8e5
Separate input components into their own directories / Remove WithShortcut (#1613)
* Put PlainTextInput into its own directory

* Create a directory for RichTextInput

* Create editor-toolbar directory

* Move PreviewButton into editor-toolbar

* The time to refactor this is not quite yet here

* Create tag-editor directory

* Remove some of the uses of WithShortcut

* Remove all uses of WithShortcut from editor package

* Remove last uses of WithShortcut

* Fix typo
2022-01-24 11:43:09 +10:00

66 lines
1.7 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 IconButton from "../../components/IconButton.svelte";
import Shortcut from "../../components/Shortcut.svelte";
import WithState from "../../components/WithState.svelte";
import { execCommand, queryCommandState } from "../helpers";
import { getNoteEditor } from "../OldEditorAdapter.svelte";
export let key: string;
export let tooltip: string;
export let shortcut: string = "";
export let withoutShortcut = false;
export let withoutState = false;
const { focusInRichText } = getNoteEditor();
function action() {
execCommand(key);
}
$: disabled = !$focusInRichText;
</script>
{#if withoutState}
<IconButton {tooltip} {disabled} on:click={action}>
<slot />
</IconButton>
{#if !withoutShortcut}
<Shortcut keyCombination={shortcut} on:click={action} />
{/if}
{:else}
<WithState
{key}
update={async () => queryCommandState(key)}
let:state={active}
let:updateState
>
<IconButton
{tooltip}
{active}
{disabled}
on:click={(event) => {
action();
updateState(event);
}}
>
<slot />
</IconButton>
{#if !withoutShortcut}
<Shortcut
keyCombination={shortcut}
on:action={(event) => {
action();
updateState(event);
}}
/>
{/if}
</WithState>
{/if}