devextreme
Version:
JavaScript/TypeScript Component Suite for Responsive Web Development
157 lines (153 loc) • 5.49 kB
JavaScript
/**
* DevExtreme (esm/__internal/ui/text_box/texteditor_button_collection/index.js)
* Version: 25.2.7
* Build date: Tue May 05 2026
*
* Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import $ from "../../../../core/renderer";
import errors from "../../../../ui/widget/ui.errors";
import CustomButton from "../../../ui/text_box/texteditor_button_collection/custom";
const TEXTEDITOR_BUTTONS_CONTAINER_CLASS = "dx-texteditor-buttons-container";
function checkButtonInfo(buttonInfo) {
(() => {
if (!buttonInfo || "object" !== typeof buttonInfo || Array.isArray(buttonInfo)) {
throw errors.Error("E1053")
}
})();
(() => {
if (!("name" in buttonInfo)) {
throw errors.Error("E1054")
}
})();
(() => {
const {
name: name
} = buttonInfo;
if ("string" !== typeof name) {
throw errors.Error("E1055")
}
})();
(() => {
const {
location: location
} = buttonInfo;
if ("location" in buttonInfo && "after" !== location && "before" !== location) {
buttonInfo.location = "after"
}
})()
}
function checkNamesUniqueness(existingNames, newName) {
if (existingNames.includes(newName)) {
throw errors.Error("E1055", newName)
}
existingNames.push(newName)
}
function isPredefinedButtonName(name, predefinedButtonsInfo) {
return !!predefinedButtonsInfo.find(info => info.name === name)
}
export default class TextEditorButtonCollection {
constructor(editor, defaultButtonsInfo) {
this.buttons = [];
this.defaultButtonsInfo = defaultButtonsInfo;
this.editor = editor
}
_compileButtonInfo(buttons) {
const names = [];
return buttons.map(button => {
if ("string" === typeof button) {
const defaultButtonInfo = this.defaultButtonsInfo.find(_ref => {
let {
name: name
} = _ref;
return name === button
});
if (!defaultButtonInfo) {
throw errors.Error("E1056", this.editor.NAME, button)
}
checkNamesUniqueness(names, button);
return defaultButtonInfo
}
checkButtonInfo(button);
const isDefaultButton = isPredefinedButtonName(button.name, this.defaultButtonsInfo);
if (isDefaultButton) {
const defaultButtonInfo = this.defaultButtonsInfo.find(_ref2 => {
let {
name: name
} = _ref2;
return name === button.name
});
if (!defaultButtonInfo) {
throw errors.Error("E1056", this.editor.NAME, button)
}
checkNamesUniqueness(names, button);
return defaultButtonInfo
}
const {
name: name = ""
} = button;
checkNamesUniqueness(names, name);
return Object.assign({}, button, {
Ctor: CustomButton
})
})
}
_createButton(buttonsInfo) {
const {
Ctor: Ctor,
options: options,
name: name
} = buttonsInfo;
const button = new Ctor(name, this.editor, options ?? {});
this.buttons.push(button);
return button
}
_renderButtons(buttons, $container, targetLocation) {
let $buttonsContainer = null;
const buttonsInfo = buttons ? this._compileButtonInfo(buttons) : this.defaultButtonsInfo;
buttonsInfo.forEach(buttonInfo => {
const {
location: location = "after"
} = buttonInfo;
if (location === targetLocation) {
this._createButton(buttonInfo).render((() => {
$buttonsContainer = $buttonsContainer ?? $("<div>").addClass("dx-texteditor-buttons-container");
if ("before" === targetLocation) {
$container.prepend($buttonsContainer)
} else {
$container.append($buttonsContainer)
}
return $buttonsContainer
})())
}
});
return $buttonsContainer
}
clean() {
this.buttons.forEach(button => button.dispose());
this.buttons = []
}
getButton(buttonName) {
const button = this.buttons.find(_ref3 => {
let {
name: name
} = _ref3;
return name === buttonName
});
return null === button || void 0 === button ? void 0 : button.instance
}
renderAfterButtons(buttons, $container) {
return this._renderButtons(buttons, $container, "after")
}
renderBeforeButtons(buttons, $container) {
return this._renderButtons(buttons, $container, "before")
}
updateButtons(names) {
this.buttons.forEach(button => {
if (!names || names.includes(button.name)) {
button.update()
}
})
}
}