Reduce typing overhead in ts/editor-toolbar/index.ts and export buttonGroup

This commit is contained in:
Henrik Giesel 2021-04-23 19:02:28 +02:00
parent bda99ee0f1
commit 6ae368ebac
5 changed files with 47 additions and 67 deletions

View File

@ -173,10 +173,10 @@ class Editor:
) )
righttopbtns_js = ( righttopbtns_js = (
f""" f"""
$editorToolbar.addButtonGroup({{ $editorToolbar.addButtonGroup(editorToolbar.buttonGroup({{
id: "addons", id: "addons",
items: [ {righttopbtns_defs} ] items: [ {righttopbtns_defs} ]
}}); }}));
""" """
if righttopbtns_defs if righttopbtns_defs
else "" else ""

View File

@ -0,0 +1,17 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
interface Hideable {
hidden?: boolean;
}
export function showComponent(component: Hideable): void {
component.hidden = false;
}
export function hideComponent(component: Hideable): void {
component.hidden = true;
}
export function toggleComponent(component: Hideable): void {
component.hidden = !component.hidden;
}

View File

@ -1,6 +1,6 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
export interface Identifiable { interface Identifiable {
id?: string; id?: string;
} }

View File

@ -1,51 +1,28 @@
// Copyright: Ankitects Pty Ltd and contributors // Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { SvelteComponentDev } from "svelte/internal"; import type { SvelteComponentDev } from "svelte/internal";
import type { ToolbarItem } from "./types"; import type { ToolbarItem, IterableToolbarItem } from "./types";
import ButtonGroup from "./ButtonGroup.svelte";
import type { ButtonGroupProps } from "./ButtonGroup";
import { dynamicComponent } from "sveltelib/dynamicComponent";
import { Writable, writable } from "svelte/store"; import { Writable, writable } from "svelte/store";
import EditorToolbarSvelte from "./EditorToolbar.svelte"; import EditorToolbarSvelte from "./EditorToolbar.svelte";
import "./bootstrap.css"; import "./bootstrap.css";
import { Identifiable, search, add, insert } from "./identifiable"; import { search, add, insert } from "./identifiable";
import { showComponent, hideComponent, toggleComponent } from "./hideable";
interface Hideable { let buttonsResolve: (value: Writable<IterableToolbarItem[]>) => void;
hidden?: boolean;
}
function showComponent(component: Hideable): void {
component.hidden = false;
}
function hideComponent(component: Hideable): void {
component.hidden = true;
}
function toggleComponent(component: Hideable): void {
component.hidden = !component.hidden;
}
const buttonGroup = dynamicComponent<typeof ButtonGroup, ButtonGroupProps>(ButtonGroup);
let buttonsResolve: (
value: Writable<(ToolbarItem<typeof ButtonGroup> & ButtonGroupProps)[]>
) => void;
let menusResolve: (value: Writable<ToolbarItem[]>) => void; let menusResolve: (value: Writable<ToolbarItem[]>) => void;
export class EditorToolbar extends HTMLElement { export class EditorToolbar extends HTMLElement {
component?: SvelteComponentDev; component?: SvelteComponentDev;
buttonsPromise: Promise< buttonsPromise: Promise<Writable<IterableToolbarItem[]>> = new Promise(
Writable<(ToolbarItem<typeof ButtonGroup> & ButtonGroupProps)[]> (resolve) => {
> = new Promise((resolve) => {
buttonsResolve = resolve; buttonsResolve = resolve;
}); }
);
menusPromise: Promise<Writable<ToolbarItem[]>> = new Promise((resolve): void => { menusPromise: Promise<Writable<ToolbarItem[]>> = new Promise((resolve): void => {
menusResolve = resolve; menusResolve = resolve;
}); });
@ -69,10 +46,8 @@ export class EditorToolbar extends HTMLElement {
menusResolve(menus); menusResolve(menus);
} }
updateButtonGroup<T>( updateButtonGroup(
update: ( update: (component: IterableToolbarItem) => void,
component: ToolbarItem<typeof ButtonGroup> & ButtonGroupProps & T
) => void,
group: string | number group: string | number
): void { ): void {
this.buttonsPromise.then((buttons) => { this.buttonsPromise.then((buttons) => {
@ -80,11 +55,7 @@ export class EditorToolbar extends HTMLElement {
const foundGroup = search(buttonGroups, group); const foundGroup = search(buttonGroups, group);
if (foundGroup) { if (foundGroup) {
update( update(foundGroup as IterableToolbarItem);
foundGroup as ToolbarItem<typeof ButtonGroup> &
ButtonGroupProps &
T
);
} }
return buttonGroups; return buttonGroups;
@ -95,33 +66,31 @@ export class EditorToolbar extends HTMLElement {
} }
showButtonGroup(group: string | number): void { showButtonGroup(group: string | number): void {
this.updateButtonGroup<Hideable>(showComponent, group); this.updateButtonGroup(showComponent, group);
} }
hideButtonGroup(group: string | number): void { hideButtonGroup(group: string | number): void {
this.updateButtonGroup<Hideable>(hideComponent, group); this.updateButtonGroup(hideComponent, group);
} }
toggleButtonGroup(group: string | number): void { toggleButtonGroup(group: string | number): void {
this.updateButtonGroup<Hideable>(toggleComponent, group); this.updateButtonGroup(toggleComponent, group);
} }
insertButtonGroup(newGroup: ButtonGroupProps, group: string | number = 0): void { insertButtonGroup(newGroup: IterableToolbarItem, group: string | number = 0): void {
this.buttonsPromise.then((buttons) => { this.buttonsPromise.then((buttons) => {
buttons.update((buttonGroups) => { buttons.update((buttonGroups) => {
const newButtonGroup = buttonGroup(newGroup); return insert(buttonGroups, newGroup, group);
return insert(buttonGroups, newButtonGroup, group);
}); });
return buttons; return buttons;
}); });
} }
addButtonGroup(newGroup: ButtonGroupProps, group: string | number = -1): void { addButtonGroup(newGroup: IterableToolbarItem, group: string | number = -1): void {
this.buttonsPromise.then((buttons) => { this.buttonsPromise.then((buttons) => {
buttons.update((buttonGroups) => { buttons.update((buttonGroups) => {
const newButtonGroup = buttonGroup(newGroup); return add(buttonGroups, newGroup, group);
return add(buttonGroups, newButtonGroup, group);
}); });
return buttons; return buttons;
@ -155,30 +124,22 @@ export class EditorToolbar extends HTMLElement {
} }
insertButton( insertButton(
newButton: ToolbarItem & Identifiable, newButton: ToolbarItem,
group: string | number, group: string | number,
button: string | number = 0 button: string | number = 0
): void { ): void {
this.updateButtonGroup((component) => { this.updateButtonGroup((component: IterableToolbarItem) => {
component.items = insert( component.items = insert(component.items, newButton, button);
component.items as (ToolbarItem & Identifiable)[],
newButton,
button
);
}, group); }, group);
} }
addButton( addButton(
newButton: ToolbarItem & Identifiable, newButton: ToolbarItem,
group: string | number, group: string | number,
button: string | number = -1 button: string | number = -1
): void { ): void {
this.updateButtonGroup((component) => { this.updateButtonGroup((component: IterableToolbarItem) => {
component.items = add( component.items = add(component.items, newButton, button);
component.items as (ToolbarItem & Identifiable)[],
newButton,
button
);
}, group); }, group);
} }
} }

View File

@ -3,6 +3,7 @@
import type { DynamicSvelteComponent } from "sveltelib/dynamicComponent"; import type { DynamicSvelteComponent } from "sveltelib/dynamicComponent";
import { import {
buttonGroup,
rawButton, rawButton,
labelButton, labelButton,
iconButton, iconButton,
@ -18,6 +19,7 @@ export const editorToolbar: Record<
string, string,
(props: Record<string, unknown>) => DynamicSvelteComponent (props: Record<string, unknown>) => DynamicSvelteComponent
> = { > = {
buttonGroup,
rawButton, rawButton,
labelButton, labelButton,
iconButton, iconButton,