import { normalizeLegacyHtml, normalizeLegacyMessage } from "./Ckeditor/Normalizer";
import { element as scrollToElement } from "../Ui/Scroll";
import Devtools from "../Devtools";
-import { ClassicEditor, CodeBlockConfig, EditorConfig, Element as CkeElement } from "./Ckeditor/Types";
+import type {
+ ClassicEditor,
+ CodeBlockConfig,
+ EditorConfig,
+ Element as CkeElement,
+ ToolbarConfigItem,
+} from "./Ckeditor/Types";
import { setupSubmitShortcut } from "./Ckeditor/Keyboard";
import { setup as setupLayer } from "./Ckeditor/Layer";
import { browser, touch } from "../Environment";
features,
});
- for (const { name } of bbcodes) {
- (configuration.toolbar as any).push(`woltlabBbcode_${name}`);
+ const toolbar = configuration.toolbar as ToolbarConfigItem[];
+ for (let { name } of bbcodes) {
+ name = `woltlabBbcode_${name}`;
+
+ if (hasToolbarButton(toolbar, name)) {
+ continue;
+ }
+
+ toolbar.push(name);
}
return configuration;
}
+function hasToolbarButton(items: ToolbarConfigItem[], name: string): boolean {
+ for (const item of items) {
+ if (typeof item === "string") {
+ if (item === name) {
+ return true;
+ }
+ } else if (hasToolbarButton(item.items, name)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
export async function setupCkeditor(
element: HTMLElement,
features: Features,
import type {} from "@ckeditor/ckeditor5-autosave";
import type {} from "@ckeditor/ckeditor5-mention";
-import type { EditorConfig as UpstreamEditorConfig } from "@ckeditor/ckeditor5-core";
+import type { EditorConfig as UpstreamEditorConfig, ToolbarConfigItem } from "@ckeditor/ckeditor5-core";
type WoltlabBbcodeButton = {
icon?: string;
woltlabBbcode?: WoltlabBbcodeConfig;
}
-export type { EditorConfig };
+export type { EditorConfig, ToolbarConfigItem };
configuration,
features,
});
- for (const { name } of bbcodes) {
- configuration.toolbar.push(`woltlabBbcode_${name}`);
+ const toolbar = configuration.toolbar;
+ for (let { name } of bbcodes) {
+ name = `woltlabBbcode_${name}`;
+ if (hasToolbarButton(toolbar, name)) {
+ continue;
+ }
+ toolbar.push(name);
}
return configuration;
}
+ function hasToolbarButton(items, name) {
+ for (const item of items) {
+ if (typeof item === "string") {
+ if (item === name) {
+ return true;
+ }
+ }
+ else if (hasToolbarButton(item.items, name)) {
+ return true;
+ }
+ }
+ return false;
+ }
async function setupCkeditor(element, features, bbcodes, codeBlockLanguages) {
if (instances.has(element)) {
throw new TypeError(`Cannot initialize the editor for '${element.id}' twice.`);