mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -04:00
Add addMenu and updateMenu, avoid using {buttons,menus}Promise outside of class
This commit is contained in:
parent
1f05ebbcf7
commit
53bb8c7b28
3 changed files with 49 additions and 36 deletions
|
@ -1,6 +1,5 @@
|
||||||
// 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 { ToolbarItem, IterableToolbarItem } from "./types";
|
import type { ToolbarItem, IterableToolbarItem } from "./types";
|
||||||
import type { Identifier } from "./identifiable";
|
import type { Identifier } from "./identifiable";
|
||||||
|
|
||||||
|
@ -17,16 +16,16 @@ let buttonsResolve: (value: Writable<IterableToolbarItem[]>) => 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;
|
private buttonsPromise: Promise<Writable<IterableToolbarItem[]>> = new Promise(
|
||||||
|
|
||||||
buttonsPromise: Promise<Writable<IterableToolbarItem[]>> = new Promise(
|
|
||||||
(resolve) => {
|
(resolve) => {
|
||||||
buttonsResolve = resolve;
|
buttonsResolve = resolve;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
menusPromise: Promise<Writable<ToolbarItem[]>> = new Promise((resolve): void => {
|
private menusPromise: Promise<Writable<ToolbarItem[]>> = new Promise(
|
||||||
|
(resolve): void => {
|
||||||
menusResolve = resolve;
|
menusResolve = resolve;
|
||||||
});
|
}
|
||||||
|
);
|
||||||
|
|
||||||
connectedCallback(): void {
|
connectedCallback(): void {
|
||||||
globalThis.$editorToolbar = this;
|
globalThis.$editorToolbar = this;
|
||||||
|
@ -34,7 +33,7 @@ export class EditorToolbar extends HTMLElement {
|
||||||
const buttons = writable([]);
|
const buttons = writable([]);
|
||||||
const menus = writable([]);
|
const menus = writable([]);
|
||||||
|
|
||||||
this.component = new EditorToolbarSvelte({
|
new EditorToolbarSvelte({
|
||||||
target: this,
|
target: this,
|
||||||
props: {
|
props: {
|
||||||
buttons,
|
buttons,
|
||||||
|
@ -59,7 +58,7 @@ export class EditorToolbar extends HTMLElement {
|
||||||
(items: IterableToolbarItem[]): IterableToolbarItem[] =>
|
(items: IterableToolbarItem[]): IterableToolbarItem[] =>
|
||||||
updateRecursive(
|
updateRecursive(
|
||||||
update,
|
update,
|
||||||
{ component: EditorToolbarSvelte, items },
|
({ items } as unknown) as ToolbarItem,
|
||||||
...identifiers
|
...identifiers
|
||||||
).items as IterableToolbarItem[]
|
).items as IterableToolbarItem[]
|
||||||
);
|
);
|
||||||
|
@ -101,6 +100,36 @@ export class EditorToolbar extends HTMLElement {
|
||||||
...initIdentifiers
|
...initIdentifiers
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateMenu(
|
||||||
|
update: (component: ToolbarItem) => ToolbarItem,
|
||||||
|
...identifiers: Identifier[]
|
||||||
|
): void {
|
||||||
|
this.menusPromise.then(
|
||||||
|
(menus: Writable<ToolbarItem[]>): Writable<ToolbarItem[]> => {
|
||||||
|
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);
|
customElements.define("anki-editor-toolbar", EditorToolbar);
|
||||||
|
|
|
@ -9,7 +9,6 @@ import {
|
||||||
iconButton,
|
iconButton,
|
||||||
commandIconButton,
|
commandIconButton,
|
||||||
selectButton,
|
selectButton,
|
||||||
|
|
||||||
dropdownMenu,
|
dropdownMenu,
|
||||||
dropdownItem,
|
dropdownItem,
|
||||||
buttonDropdown,
|
buttonDropdown,
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
// 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 { ToolbarItem, IterableToolbarItem } from "editor-toolbar/types";
|
|
||||||
import type { Writable } from "svelte/store";
|
|
||||||
|
|
||||||
import { getNotetypeGroup } from "./notetype";
|
import { getNotetypeGroup } from "./notetype";
|
||||||
import { getFormatInlineGroup } from "./formatInline";
|
import { getFormatInlineGroup } from "./formatInline";
|
||||||
import { getFormatBlockGroup, getFormatBlockMenus } from "./formatBlock";
|
import { getFormatBlockGroup, getFormatBlockMenus } from "./formatBlock";
|
||||||
|
@ -12,30 +9,18 @@ import { getTemplateGroup, getTemplateMenus } from "./template";
|
||||||
export function initToolbar(i18n: Promise<void>): void {
|
export function initToolbar(i18n: Promise<void>): void {
|
||||||
document.addEventListener("DOMContentLoaded", () => {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
i18n.then(() => {
|
i18n.then(() => {
|
||||||
globalThis.$editorToolbar.buttonsPromise.then(
|
const buttons = [
|
||||||
(
|
|
||||||
buttons: Writable<IterableToolbarItem[]>
|
|
||||||
): Writable<IterableToolbarItem[]> => {
|
|
||||||
buttons.update(() => [
|
|
||||||
getNotetypeGroup(),
|
getNotetypeGroup(),
|
||||||
getFormatInlineGroup(),
|
getFormatInlineGroup(),
|
||||||
getFormatBlockGroup(),
|
getFormatBlockGroup(),
|
||||||
getColorGroup(),
|
getColorGroup(),
|
||||||
getTemplateGroup(),
|
getTemplateGroup(),
|
||||||
]);
|
];
|
||||||
return buttons;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
globalThis.$editorToolbar.menusPromise.then(
|
const menus = [...getFormatBlockMenus(), ...getTemplateMenus()];
|
||||||
(menus: Writable<ToolbarItem[]>): Writable<ToolbarItem[]> => {
|
|
||||||
menus.update(() => [
|
globalThis.$editorToolbar.updateButton(() => ({ items: buttons }));
|
||||||
...getFormatBlockMenus(),
|
globalThis.$editorToolbar.updateMenu(() => ({ items: menus }));
|
||||||
...getTemplateMenus(),
|
|
||||||
]);
|
|
||||||
return menus;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue