2021-03-25 21:11:40 +01:00
|
|
|
import type { SvelteComponent } from "svelte";
|
2021-04-08 22:11:50 +02:00
|
|
|
import type { DynamicSvelteComponent } from "sveltelib/dynamicComponent";
|
|
|
|
|
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 { checkNightMode } from "anki/nightmode";
|
|
|
|
import { setupI18n, ModuleName } from "anki/i18n";
|
2021-03-30 06:14:00 +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-03-30 01:23:39 +02:00
|
|
|
|
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-08 23:17:11 +02:00
|
|
|
function showComponent(component: Hideable) {
|
2021-04-08 22:11:50 +02:00
|
|
|
component.hidden = false;
|
|
|
|
}
|
|
|
|
|
2021-04-08 23:17:11 +02:00
|
|
|
function hideComponent(component: Hideable) {
|
2021-04-08 22:11:50 +02:00
|
|
|
component.hidden = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleComponent(component: Hideable) {
|
|
|
|
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 {
|
|
|
|
component?: SvelteComponent;
|
|
|
|
|
2021-04-09 18:42:41 +02:00
|
|
|
buttons?: Writable<
|
|
|
|
(DynamicSvelteComponent<typeof ButtonGroup> & ButtonGroupProps)[]
|
|
|
|
>;
|
|
|
|
menus?: Writable<DynamicSvelteComponent[]>;
|
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,
|
|
|
|
nightMode: checkNightMode(),
|
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: (
|
|
|
|
component: DynamicSvelteComponent<typeof ButtonGroup> & ButtonGroupProps & T
|
|
|
|
) => 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(
|
|
|
|
foundGroup as DynamicSvelteComponent<typeof ButtonGroup> &
|
|
|
|
ButtonGroupProps &
|
|
|
|
T
|
|
|
|
);
|
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-09 00:51:20 +02:00
|
|
|
insertButtonGroup(newGroup: ButtonGroupProps, group: string | number = 0) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
addButtonGroup(newGroup: ButtonGroupProps, group: string | number = -1) {
|
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-08 22:11:50 +02:00
|
|
|
updateButton<T>(
|
|
|
|
update: (component: DynamicSvelteComponent & T) => void,
|
|
|
|
group: string | number,
|
|
|
|
button: string | number
|
|
|
|
): void {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.updateButtonGroup((foundGroup) => {
|
|
|
|
const foundButton = search(
|
|
|
|
foundGroup.buttons as (DynamicSvelteComponent & Identifiable)[],
|
|
|
|
button
|
|
|
|
);
|
|
|
|
|
|
|
|
if (foundButton) {
|
|
|
|
update(foundButton as DynamicSvelteComponent & T);
|
|
|
|
}
|
|
|
|
}, group);
|
2021-04-08 22:11:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
showButton(group: string | number, button: string | number): void {
|
|
|
|
this.updateButton<Hideable>(showComponent, group, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
hideButton(group: string | number, button: string | number): void {
|
|
|
|
this.updateButton<Hideable>(hideComponent, group, button);
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleButton(group: string | number, button: string | number): void {
|
|
|
|
this.updateButton<Hideable>(toggleComponent, group, button);
|
|
|
|
}
|
2021-04-09 00:51:20 +02:00
|
|
|
|
|
|
|
insertButton(
|
|
|
|
newButton: DynamicSvelteComponent & Identifiable,
|
|
|
|
group: string | number,
|
|
|
|
button: string | number = 0
|
|
|
|
) {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.updateButtonGroup((component) => {
|
|
|
|
component.buttons = insert(
|
|
|
|
component.buttons as (DynamicSvelteComponent & Identifiable)[],
|
|
|
|
newButton,
|
|
|
|
button
|
|
|
|
);
|
|
|
|
}, group);
|
2021-04-09 00:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
addButton(
|
|
|
|
newButton: DynamicSvelteComponent & Identifiable,
|
|
|
|
group: string | number,
|
|
|
|
button: string | number = -1
|
|
|
|
) {
|
2021-04-09 18:42:41 +02:00
|
|
|
this.updateButtonGroup((component) => {
|
|
|
|
component.buttons = add(
|
|
|
|
component.buttons as (DynamicSvelteComponent & Identifiable)[],
|
|
|
|
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 */
|
|
|
|
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";
|