anki/ts/editor/editor-toolbar/BlockButtons.svelte
Henrik Giesel 8f8f3bd465
Insert symbols overlay (#2051)
* Add flag for enabling insert symbols feature

* Add symbols overlay directory

* Detect if :xy is inserted into editable

* Allow naive updating of overlay, and special handling of ':'

* First step towards better Virtual Element support

* Update floating to reference range on insert text

* Position SymbolsOverlay always on top or bottom

* Add a data-provider to emulate API

* Show correct suggestions in symbols overlay

* Rename to replacementLength

* Allow replacing via clicking in menu

* Optionally remove inline padding of Popover

* Hide Symbols overlay on blur of content editable

* Add specialKey to inputHandler and generalize how arrow movement is detected

- This way macOS users can use Ctrl-N to mean down, etc.

* Detect special key from within SymbolsOverlay

* Implement full backwards search while typing

* Allow navigating symbol menu and accepting with enter

* Add some entries to data-provider

* Satisfy eslint

* Generate symbolsTable from sources

* Use other github source, allow multiple names

In return, symbol must be unique

* Automatically scroll in symbols dropdown

* Use from npm packages rather than downloading from URL

* Remove console.log

* Remove print

* Add pointerDown event to input-handler

- so that SymbolsOverlay can reset on field click

* Make tab do the same as enter

* Make font a bit smaller but increase relative icon size

* Satisfy type requirement of handlerlist

* Revert changing default size of DropdownItems

* Remove some now unused code for bootstrap dropdowns
2022-09-10 18:46:59 +10:00

182 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 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 Shortcut from "../../components/Shortcut.svelte";
import WithFloating from "../../components/WithFloating.svelte";
import { execCommand } from "../../domlib";
import { getListItem } from "../../lib/dom";
import * as tr from "../../lib/ftl";
import { getPlatformString } 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.");
}
}
const { focusedInput } = context.get();
$: disabled = !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>
<Shortcut
keyCombination={outdentKeyCombination}
on:action={outdentListItem}
/>
<IconButton
tooltip="{tr.editingIndent()} ({getPlatformString(
indentKeyCombination,
)})"
{disabled}
on:click={indentListItem}
--border-right-radius="5px"
>
{@html indentIcon}
</IconButton>
<Shortcut
keyCombination={indentKeyCombination}
on:action={indentListItem}
/>
</ButtonGroup>
</ButtonToolbar>
</Popover>
</WithFloating>
</ButtonGroupItem>
</DynamicallySlottable>
</ButtonGroup>
<style lang="scss">
.block-buttons {
line-height: 1;
}
</style>