anki/ts/editor/editor-toolbar/BlockButtons.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

185 lines
6.5 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 { onMount } from "svelte";
import ButtonGroup from "../../components/ButtonGroup.svelte";
import ButtonGroupItem, {
createProps,
setSlotHostContext,
updatePropsList,
} from "../../components/ButtonGroupItem.svelte";
import ButtonToolbar from "../../components/ButtonToolbar.svelte";
import DynamicallySlottable from "../../components/DynamicallySlottable.svelte";
import IconButton from "../../components/IconButton.svelte";
import Popover from "../../components/Popover.svelte";
import WithFloating from "../../components/WithFloating.svelte";
import { execCommand } from "../../domlib";
import { getListItem } from "../../lib/dom";
import { preventDefault } from "../../lib/events";
import * as tr from "../../lib/ftl";
import { getPlatformString, registerShortcut } from "../../lib/shortcuts";
import { context } from "../NoteEditor.svelte";
import { editingInputIsRichText } from "../rich-text-input";
import CommandIconButton from "./CommandIconButton.svelte";
import {
indentIcon,
justifyCenterIcon,
justifyFullIcon,
justifyLeftIcon,
justifyRightIcon,
listOptionsIcon,
olIcon,
outdentIcon,
ulIcon,
} from "./icons";
export let api = {};
const outdentKeyCombination = "Control+Shift+,";
function outdentListItem() {
if (getListItem(document.activeElement!.shadowRoot!)) {
execCommand("outdent");
} else {
alert("Indent/unindent currently only works with lists.");
}
}
const indentKeyCombination = "Control+Shift+.";
function indentListItem() {
if (getListItem(document.activeElement!.shadowRoot!)) {
execCommand("indent");
} else {
alert("Indent/unindent currently only works with lists.");
}
}
onMount(() => {
registerShortcut((event: KeyboardEvent) => {
preventDefault(event);
indentListItem();
}, indentKeyCombination);
registerShortcut((event: KeyboardEvent) => {
preventDefault(event);
outdentListItem();
}, outdentKeyCombination);
});
const { focusedInput } = context.get();
$: disabled = !$focusedInput || !editingInputIsRichText($focusedInput);
let showFloating = false;
</script>
<ButtonGroup>
<DynamicallySlottable
slotHost={ButtonGroupItem}
{createProps}
{updatePropsList}
{setSlotHostContext}
{api}
>
<ButtonGroupItem>
<CommandIconButton
key="insertUnorderedList"
tooltip={tr.editingUnorderedList()}
shortcut="Control+,">{@html ulIcon}</CommandIconButton
>
</ButtonGroupItem>
<ButtonGroupItem>
<CommandIconButton
key="insertOrderedList"
tooltip={tr.editingOrderedList()}
shortcut="Control+.">{@html olIcon}</CommandIconButton
>
</ButtonGroupItem>
<ButtonGroupItem>
<WithFloating
show={showFloating && !disabled}
placement="bottom"
inline
on:close={() => (showFloating = false)}
let:asReference
>
<span class="block-buttons" use:asReference>
<IconButton
{disabled}
on:click={() => (showFloating = !showFloating)}
>
{@html listOptionsIcon}
</IconButton>
</span>
<Popover slot="floating" --popover-padding-inline="0">
<ButtonToolbar wrap={false}>
<ButtonGroup>
<CommandIconButton
key="justifyLeft"
tooltip={tr.editingAlignLeft()}
--border-left-radius="5px"
--border-right-radius="0px"
>{@html justifyLeftIcon}</CommandIconButton
>
<CommandIconButton
key="justifyCenter"
tooltip={tr.editingCenter()}
>{@html justifyCenterIcon}</CommandIconButton
>
<CommandIconButton
key="justifyRight"
tooltip={tr.editingAlignRight()}
>{@html justifyRightIcon}</CommandIconButton
>
<CommandIconButton
key="justifyFull"
tooltip={tr.editingJustify()}
--border-right-radius="5px"
>{@html justifyFullIcon}</CommandIconButton
>
</ButtonGroup>
<ButtonGroup>
<IconButton
tooltip="{tr.editingOutdent()} ({getPlatformString(
outdentKeyCombination,
)})"
{disabled}
on:click={outdentListItem}
--border-left-radius="5px"
--border-right-radius="0px"
>
{@html outdentIcon}
</IconButton>
<IconButton
tooltip="{tr.editingIndent()} ({getPlatformString(
indentKeyCombination,
)})"
{disabled}
on:click={indentListItem}
--border-right-radius="5px"
>
{@html indentIcon}
</IconButton>
</ButtonGroup>
</ButtonToolbar>
</Popover>
</WithFloating>
</ButtonGroupItem>
</DynamicallySlottable>
</ButtonGroup>
<style lang="scss">
.block-buttons {
line-height: 1;
}
</style>