diff --git a/ts/editor-toolbar/index.ts b/ts/editor-toolbar/index.ts index fc3e34b49..0ff494251 100644 --- a/ts/editor-toolbar/index.ts +++ b/ts/editor-toolbar/index.ts @@ -1,6 +1,5 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -import type { SvelteComponentDev } from "svelte/internal"; import type { ToolbarItem, IterableToolbarItem } from "./types"; import type { Identifier } from "./identifiable"; @@ -17,16 +16,16 @@ let buttonsResolve: (value: Writable) => void; let menusResolve: (value: Writable) => void; export class EditorToolbar extends HTMLElement { - component?: SvelteComponentDev; - - buttonsPromise: Promise> = new Promise( + private buttonsPromise: Promise> = new Promise( (resolve) => { buttonsResolve = resolve; } ); - menusPromise: Promise> = new Promise((resolve): void => { - menusResolve = resolve; - }); + private menusPromise: Promise> = new Promise( + (resolve): void => { + menusResolve = resolve; + } + ); connectedCallback(): void { globalThis.$editorToolbar = this; @@ -34,7 +33,7 @@ export class EditorToolbar extends HTMLElement { const buttons = writable([]); const menus = writable([]); - this.component = new EditorToolbarSvelte({ + new EditorToolbarSvelte({ target: this, props: { buttons, @@ -59,7 +58,7 @@ export class EditorToolbar extends HTMLElement { (items: IterableToolbarItem[]): IterableToolbarItem[] => updateRecursive( update, - { component: EditorToolbarSvelte, items }, + ({ items } as unknown) as ToolbarItem, ...identifiers ).items as IterableToolbarItem[] ); @@ -101,6 +100,36 @@ export class EditorToolbar extends HTMLElement { ...initIdentifiers ); } + + updateMenu( + update: (component: ToolbarItem) => ToolbarItem, + ...identifiers: Identifier[] + ): void { + this.menusPromise.then( + (menus: Writable): Writable => { + menus.update( + (items: ToolbarItem[]): ToolbarItem[] => + updateRecursive( + update, + ({ items } as unknown) as ToolbarItem, + ...identifiers + ).items as ToolbarItem[] + ); + + return menus; + } + ); + } + + addMenu(newMenu: ToolbarItem, ...identifiers: Identifier[]): void { + const initIdentifiers = identifiers.slice(0, -1); + const lastIdentifier = identifiers[identifiers.length - 1]; + this.updateMenu( + (component: ToolbarItem) => + add(component as IterableToolbarItem, newMenu, lastIdentifier), + ...initIdentifiers + ); + } } customElements.define("anki-editor-toolbar", EditorToolbar); diff --git a/ts/editor/addons.ts b/ts/editor/addons.ts index 06ba37f85..6fef47af0 100644 --- a/ts/editor/addons.ts +++ b/ts/editor/addons.ts @@ -9,7 +9,6 @@ import { iconButton, commandIconButton, selectButton, - dropdownMenu, dropdownItem, buttonDropdown, diff --git a/ts/editor/toolbar.ts b/ts/editor/toolbar.ts index 244b9dbe3..f0af19df7 100644 --- a/ts/editor/toolbar.ts +++ b/ts/editor/toolbar.ts @@ -1,8 +1,5 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -import type { ToolbarItem, IterableToolbarItem } from "editor-toolbar/types"; -import type { Writable } from "svelte/store"; - import { getNotetypeGroup } from "./notetype"; import { getFormatInlineGroup } from "./formatInline"; import { getFormatBlockGroup, getFormatBlockMenus } from "./formatBlock"; @@ -12,30 +9,18 @@ import { getTemplateGroup, getTemplateMenus } from "./template"; export function initToolbar(i18n: Promise): void { document.addEventListener("DOMContentLoaded", () => { i18n.then(() => { - globalThis.$editorToolbar.buttonsPromise.then( - ( - buttons: Writable - ): Writable => { - buttons.update(() => [ - getNotetypeGroup(), - getFormatInlineGroup(), - getFormatBlockGroup(), - getColorGroup(), - getTemplateGroup(), - ]); - return buttons; - } - ); + const buttons = [ + getNotetypeGroup(), + getFormatInlineGroup(), + getFormatBlockGroup(), + getColorGroup(), + getTemplateGroup(), + ]; - globalThis.$editorToolbar.menusPromise.then( - (menus: Writable): Writable => { - menus.update(() => [ - ...getFormatBlockMenus(), - ...getTemplateMenus(), - ]); - return menus; - } - ); + const menus = [...getFormatBlockMenus(), ...getTemplateMenus()]; + + globalThis.$editorToolbar.updateButton(() => ({ items: buttons })); + globalThis.$editorToolbar.updateMenu(() => ({ items: menus })); }); }); }