anki/ts/editor-toolbar/color.ts

54 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-04-15 15:59:52 +02:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import IconButton from "./IconButton.svelte";
import type { IconButtonProps } from "./IconButton";
import ColorPicker from "./ColorPicker.svelte";
import type { ColorPickerProps } from "./ColorPicker";
import ButtonGroup from "./ButtonGroup.svelte";
import type { ButtonGroupProps } from "./ButtonGroup";
2021-04-15 02:49:46 +02:00
import { DynamicSvelteComponent, dynamicComponent } from "sveltelib/dynamicComponent";
import * as tr from "anki/i18n";
import squareFillIcon from "./square-fill.svg";
import "./color.css";
const foregroundColorKeyword = "--foreground-color";
function setForegroundColor(color: string): void {
document.documentElement.style.setProperty(foregroundColorKeyword, color);
}
function getForecolor(): string {
return document.documentElement.style.getPropertyValue(foregroundColorKeyword);
}
function wrapWithForecolor(color: string): void {
document.execCommand("forecolor", false, color);
}
const iconButton = dynamicComponent<typeof IconButton, IconButtonProps>(IconButton);
const colorPicker = dynamicComponent<typeof ColorPicker, ColorPickerProps>(ColorPicker);
const buttonGroup = dynamicComponent<typeof ButtonGroup, ButtonGroupProps>(ButtonGroup);
2021-04-15 14:58:13 +02:00
export function getColorGroup(): DynamicSvelteComponent<typeof ButtonGroup> &
ButtonGroupProps {
const forecolorButton = iconButton({
2021-04-01 01:38:50 +02:00
icon: squareFillIcon,
className: "forecolor",
onClick: () => wrapWithForecolor(getForecolor()),
tooltip: tr.editingSetForegroundColourF7(),
});
const colorpickerButton = colorPicker({
onChange: ({ currentTarget }) =>
setForegroundColor((currentTarget as HTMLInputElement).value),
tooltip: tr.editingChangeColourF8(),
});
return buttonGroup({
id: "color",
buttons: [forecolorButton, colorpickerButton],
});
}