2021-04-09 18:59:57 +02:00
|
|
|
import type { SvelteComponentDev } from "svelte/internal";
|
|
|
|
import type { ToolbarItem } from "./types";
|
2021-04-08 22:11:50 +02:00
|
|
|
|
2021-04-09 00:51:20 +02:00
|
|
|
import ButtonGroup from "./ButtonGroup.svelte";
|
2021-04-08 22:11:50 +02:00
|
|
|
import type { ButtonGroupProps } from "./ButtonGroup";
|
|
|
|
|
2021-04-09 00:51:20 +02:00
|
|
|
import { dynamicComponent } from "sveltelib/dynamicComponent";
|
2021-04-09 18:42:41 +02:00
|
|
|
import { Writable, writable } from "svelte/store";
|
2021-03-30 06:14:00 +02:00
|
|
|
|
2021-03-25 21:11:40 +01:00
|
|
|
import EditorToolbarSvelte from "./EditorToolbar.svelte";
|
|
|
|
|
2021-04-01 02:26:56 +02:00
|
|
|
import { setupI18n, ModuleName } from "anki/i18n";
|
2021-03-30 06:14:00 +02:00
|
|
|
|
2021-04-14 14:37:14 +02:00
|
|
|
import "./bootstrap.css";
|
2021-04-14 14:29:06 +02:00
|
|
|
|
2021-04-09 18:42:41 +02:00
|
|
|
import { getNotetypeGroup } from "./notetype";
|
|
|
|
import { getFormatGroup } from "./format";
|
|
|
|
import { getColorGroup } from "./color";
|
|
|
|
import { getTemplateGroup, getTemplateMenus } from "./template";
|
2021-04-09 00:51:20 +02:00
|
|
|
import { Identifiable, search, add, insert } from "./identifiable";
|
2021-03-30 01:23:39 +02:00
|
|
|
|
2021-04-08 22:11:50 +02:00
|
|
|
interface Hideable {
|
|
|
|
hidden?: boolean;
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:49:46 +02:00
|
|
|
function showComponent(component: Hideable): void {
|
2021-04-08 22:11:50 +02:00
|
|
|
component.hidden = false;
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:49:46 +02:00
|
|
|
function hideComponent(component: Hideable): void {
|
2021-04-08 22:11:50 +02:00
|
|
|
component.hidden = true;
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:49:46 +02:00
|
|
|
function toggleComponent(component: Hideable): void {
|
2021-04-08 22:11:50 +02:00
|
|
|
component.hidden = !component.hidden;
|
|
|
|
}
|
|
|
|
|
2021-04-09 18:42:41 +02:00
|
|
|
const buttonGroup = dynamicComponent<typeof ButtonGroup, ButtonGroupProps>(ButtonGroup);
|
2021-04-09 00:51:20 +02:00
|
|
|
|
2021-03-25 21:11:40 +01:00
|
|
|
class EditorToolbar extends HTMLElement {
|
2021-04-09 18:59:57 +02:00
|
|
|
component?: SvelteComponentDev;
|
2021-03-25 21:11:40 +01:00
|
|
|
|
2021-04-09 18:59:57 +02:00
|
|
|
buttons?: Writable<(ToolbarItem<typeof ButtonGroup> & ButtonGroupProps)[]>;
|
|
|
|
menus?: Writable<ToolbarItem[]>;
|
2021-03-30 06:14:00 +02:00
|
|
|
|
2021-04-01 02:18:04 +02:00
|
|
|
connectedCallback(): void {
|
2021-03-31 22:05:24 +02:00
|
|
|
setupI18n({ modules: [ModuleName.EDITING] }).then(() => {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.buttons = writable([
|
|
|
|
getNotetypeGroup(),
|
|
|
|
getFormatGroup(),
|
|
|
|
getColorGroup(),
|
|
|
|
getTemplateGroup(),
|
|
|
|
]);
|
|
|
|
this.menus = writable([...getTemplateMenus()]);
|
|
|
|
|
2021-03-31 22:05:24 +02:00
|
|
|
this.component = new EditorToolbarSvelte({
|
|
|
|
target: this,
|
|
|
|
props: {
|
2021-04-01 02:18:04 +02:00
|
|
|
buttons: this.buttons,
|
|
|
|
menus: this.menus,
|
2021-04-14 21:58:58 +02:00
|
|
|
nightMode: document.documentElement.classList.contains(
|
|
|
|
"night-mode"
|
|
|
|
),
|
2021-03-31 22:05:24 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2021-03-30 06:14:00 +02:00
|
|
|
}
|
2021-04-08 22:11:50 +02:00
|
|
|
|
|
|
|
updateButtonGroup<T>(
|
2021-04-09 18:42:41 +02:00
|
|
|
update: (
|
2021-04-09 18:59:57 +02:00
|
|
|
component: ToolbarItem<typeof ButtonGroup> & ButtonGroupProps & T
|
2021-04-09 18:42:41 +02:00
|
|
|
) => void,
|
2021-04-08 22:11:50 +02:00
|
|
|
group: string | number
|
|
|
|
): void {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.buttons?.update((buttonGroups) => {
|
|
|
|
const foundGroup = search(buttonGroups, group);
|
|
|
|
|
|
|
|
if (foundGroup) {
|
|
|
|
update(
|
2021-04-09 18:59:57 +02:00
|
|
|
foundGroup as ToolbarItem<typeof ButtonGroup> & ButtonGroupProps & T
|
2021-04-09 18:42:41 +02:00
|
|
|
);
|
2021-04-08 22:11:50 +02:00
|
|
|
}
|
2021-04-09 18:42:41 +02:00
|
|
|
|
|
|
|
return buttonGroups;
|
|
|
|
});
|
2021-04-08 22:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
showButtonGroup(group: string | number): void {
|
|
|
|
this.updateButtonGroup<Hideable>(showComponent, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
hideButtonGroup(group: string | number): void {
|
|
|
|
this.updateButtonGroup<Hideable>(hideComponent, group);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleButtonGroup(group: string | number): void {
|
|
|
|
this.updateButtonGroup<Hideable>(toggleComponent, group);
|
|
|
|
}
|
|
|
|
|
2021-04-15 02:49:46 +02:00
|
|
|
insertButtonGroup(newGroup: ButtonGroupProps, group: string | number = 0): void {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.buttons?.update((buttonGroups) => {
|
|
|
|
const newButtonGroup = buttonGroup(newGroup);
|
|
|
|
return insert(buttonGroups, newButtonGroup, group);
|
|
|
|
});
|
2021-04-09 00:51:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-15 02:49:46 +02:00
|
|
|
addButtonGroup(newGroup: ButtonGroupProps, group: string | number = -1): void {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.buttons?.update((buttonGroups) => {
|
|
|
|
const newButtonGroup = buttonGroup(newGroup);
|
|
|
|
return add(buttonGroups, newButtonGroup, group);
|
|
|
|
});
|
2021-04-09 00:51:20 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 18:59:57 +02:00
|
|
|
updateButton(
|
|
|
|
update: (component: ToolbarItem) => void,
|
2021-04-08 22:11:50 +02:00
|
|
|
group: string | number,
|
|
|
|
button: string | number
|
|
|
|
): void {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.updateButtonGroup((foundGroup) => {
|
2021-04-09 18:59:57 +02:00
|
|
|
const foundButton = search(foundGroup.buttons, button);
|
2021-04-09 18:42:41 +02:00
|
|
|
|
|
|
|
if (foundButton) {
|
2021-04-09 18:59:57 +02:00
|
|
|
update(foundButton);
|
2021-04-09 18:42:41 +02:00
|
|
|
}
|
|
|
|
}, group);
|
2021-04-08 22:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
showButton(group: string | number, button: string | number): void {
|
2021-04-09 18:59:57 +02:00
|
|
|
this.updateButton(showComponent, group, button);
|
2021-04-08 22:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hideButton(group: string | number, button: string | number): void {
|
2021-04-09 18:59:57 +02:00
|
|
|
this.updateButton(hideComponent, group, button);
|
2021-04-08 22:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleButton(group: string | number, button: string | number): void {
|
2021-04-09 18:59:57 +02:00
|
|
|
this.updateButton(toggleComponent, group, button);
|
2021-04-08 22:11:50 +02:00
|
|
|
}
|
2021-04-09 00:51:20 +02:00
|
|
|
|
|
|
|
insertButton(
|
2021-04-09 18:59:57 +02:00
|
|
|
newButton: ToolbarItem & Identifiable,
|
2021-04-09 00:51:20 +02:00
|
|
|
group: string | number,
|
|
|
|
button: string | number = 0
|
2021-04-15 02:49:46 +02:00
|
|
|
): void {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.updateButtonGroup((component) => {
|
|
|
|
component.buttons = insert(
|
2021-04-09 18:59:57 +02:00
|
|
|
component.buttons as (ToolbarItem & Identifiable)[],
|
2021-04-09 18:42:41 +02:00
|
|
|
newButton,
|
|
|
|
button
|
|
|
|
);
|
|
|
|
}, group);
|
2021-04-09 00:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addButton(
|
2021-04-09 18:59:57 +02:00
|
|
|
newButton: ToolbarItem & Identifiable,
|
2021-04-09 00:51:20 +02:00
|
|
|
group: string | number,
|
|
|
|
button: string | number = -1
|
2021-04-15 02:49:46 +02:00
|
|
|
): void {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.updateButtonGroup((component) => {
|
|
|
|
component.buttons = add(
|
2021-04-09 18:59:57 +02:00
|
|
|
component.buttons as (ToolbarItem & Identifiable)[],
|
2021-04-09 18:42:41 +02:00
|
|
|
newButton,
|
|
|
|
button
|
|
|
|
);
|
|
|
|
}, group);
|
2021-04-09 00:51:20 +02:00
|
|
|
}
|
2021-03-25 21:11:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("anki-editor-toolbar", EditorToolbar);
|
2021-04-01 02:26:56 +02:00
|
|
|
|
2021-04-09 00:51:20 +02:00
|
|
|
/* Exports for editor
|
|
|
|
* @ts-expect-error */
|
|
|
|
export { updateActiveButtons, clearActiveButtons } from "./CommandIconButton.svelte";
|
|
|
|
export { enableButtons, disableButtons } from "./EditorToolbar.svelte";
|
|
|
|
|
2021-04-01 02:26:56 +02:00
|
|
|
/* Exports for add-ons */
|
2021-04-09 20:55:49 +02:00
|
|
|
export { default as RawButton } from "./RawButton.svelte";
|
2021-04-01 02:26:56 +02:00
|
|
|
export { default as LabelButton } from "./LabelButton.svelte";
|
|
|
|
export { default as IconButton } from "./IconButton.svelte";
|
|
|
|
export { default as CommandIconButton } from "./CommandIconButton.svelte";
|
|
|
|
export { default as SelectButton } from "./SelectButton.svelte";
|
|
|
|
|
|
|
|
export { default as DropdownMenu } from "./DropdownMenu.svelte";
|
|
|
|
export { default as DropdownItem } from "./DropdownItem.svelte";
|
2021-04-01 16:29:24 +02:00
|
|
|
export { default as ButtonDropdown } from "./DropdownMenu.svelte";
|
2021-04-01 02:26:56 +02:00
|
|
|
export { default as WithDropdownMenu } from "./WithDropdownMenu.svelte";
|