anki/ts/editor-toolbar/formatBlock.ts

138 lines
4.1 KiB
TypeScript
Raw Normal View History

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
2021-04-16 15:26:49 +02:00
import ButtonGroup from "./ButtonGroup.svelte";
import type { ButtonGroupProps } from "./ButtonGroup";
import ButtonDropdown from "./ButtonDropdown.svelte";
import type { ButtonDropdownProps } from "./ButtonDropdown";
import WithDropdownMenu from "./WithDropdownMenu.svelte";
import type { WithDropdownMenuProps } from "./WithDropdownMenu";
import CommandIconButton from "./CommandIconButton.svelte";
import type { CommandIconButtonProps } from "./CommandIconButton";
import IconButton from "./IconButton.svelte";
import type { IconButtonProps } from "./IconButton";
import { DynamicSvelteComponent, dynamicComponent } from "sveltelib/dynamicComponent";
import * as tr from "anki/i18n";
import ulIcon from "./list-ul.svg";
import olIcon from "./list-ol.svg";
import listOptionsIcon from "./text-paragraph.svg";
2021-04-16 15:26:49 +02:00
import justifyFullIcon from "./justify.svg";
import justifyLeftIcon from "./text-left.svg";
import justifyRightIcon from "./text-right.svg";
import justifyCenterIcon from "./text-center.svg";
import indentIcon from "./text-indent-left.svg";
import outdentIcon from "./text-indent-right.svg";
const commandIconButton = dynamicComponent<
typeof CommandIconButton,
CommandIconButtonProps
>(CommandIconButton);
2021-04-16 15:26:49 +02:00
const buttonGroup = dynamicComponent<typeof ButtonGroup, ButtonGroupProps>(ButtonGroup);
2021-04-16 15:26:49 +02:00
const buttonDropdown = dynamicComponent<typeof ButtonDropdown, ButtonDropdownProps>(
ButtonDropdown
);
const withDropdownMenu = dynamicComponent<
typeof WithDropdownMenu,
WithDropdownMenuProps
>(WithDropdownMenu);
export function getFormatBlockMenus(): (DynamicSvelteComponent<typeof ButtonDropdown> &
ButtonDropdownProps)[] {
const justifyLeftButton = commandIconButton({
icon: justifyLeftIcon,
command: "justifyLeft",
2021-04-16 16:08:00 +02:00
tooltip: tr.editingAlignLeft(),
2021-04-16 15:26:49 +02:00
});
const justifyCenterButton = commandIconButton({
icon: justifyCenterIcon,
command: "justifyCenter",
2021-04-16 16:08:00 +02:00
tooltip: tr.editingCenter(),
2021-04-16 15:26:49 +02:00
});
const justifyRightButton = commandIconButton({
icon: justifyRightIcon,
command: "justifyRight",
2021-04-16 16:08:00 +02:00
tooltip: tr.editingAlignRight(),
});
const justifyFullButton = commandIconButton({
icon: justifyFullIcon,
command: "justifyFull",
tooltip: tr.editingJustify(),
2021-04-16 15:26:49 +02:00
});
const justifyGroup = buttonGroup({
id: "justify",
buttons: [
justifyLeftButton,
justifyCenterButton,
justifyRightButton,
2021-04-16 16:08:00 +02:00
justifyFullButton,
2021-04-16 15:26:49 +02:00
],
});
const outdentButton = commandIconButton({
icon: outdentIcon,
command: "outdent",
2021-04-16 16:08:00 +02:00
tooltip: tr.editingOutdent(),
activatable: false,
2021-04-16 16:08:00 +02:00
});
const indentButton = commandIconButton({
icon: indentIcon,
command: "indent",
tooltip: tr.editingIndent(),
activatable: false,
2021-04-16 15:26:49 +02:00
});
const indentationGroup = buttonGroup({
id: "indentation",
2021-04-16 16:08:00 +02:00
buttons: [outdentButton, indentButton],
2021-04-16 15:26:49 +02:00
});
const formattingOptions = buttonDropdown({
id: "listFormatting",
buttons: [justifyGroup, indentationGroup],
});
return [formattingOptions];
}
const iconButton = dynamicComponent<typeof IconButton, IconButtonProps>(IconButton);
export function getFormatBlockGroup(): DynamicSvelteComponent<typeof ButtonGroup> &
ButtonGroupProps {
const ulButton = commandIconButton({
icon: ulIcon,
command: "insertUnorderedList",
2021-04-16 16:08:00 +02:00
tooltip: tr.editingUnorderedList(),
});
const olButton = commandIconButton({
icon: olIcon,
command: "insertOrderedList",
2021-04-16 16:08:00 +02:00
tooltip: tr.editingOrderedList(),
});
const listFormattingButton = iconButton({
icon: listOptionsIcon,
});
2021-04-16 15:26:49 +02:00
const listFormatting = withDropdownMenu({
button: listFormattingButton,
menuId: "listFormatting",
});
return buttonGroup({
2021-04-16 16:08:00 +02:00
id: "formatBlock",
2021-04-16 15:26:49 +02:00
buttons: [ulButton, olButton, listFormatting],
});
}