mirror of
https://github.com/codex-team/editor.js
synced 2026-03-17 08:05:47 +01:00
* Toolbox making * Add Toolbox buttons click handler * Toolbar, Toolbox, UI * Updates * update css prefix
101 lines
No EOL
2.1 KiB
JavaScript
101 lines
No EOL
2.1 KiB
JavaScript
/**
|
|
* DOM manupulations helper
|
|
*/
|
|
export default class Dom {
|
|
|
|
/**
|
|
* Helper for making Elements with classname and attributes
|
|
*
|
|
* @param {string} tagName - new Element tag name
|
|
* @param {array|string} classNames - list or name of CSS classname(s)
|
|
* @param {Object} attributes - any attributes
|
|
* @return {Element}
|
|
*/
|
|
static make(tagName, classNames = null, attributes = {}) {
|
|
|
|
var el = document.createElement(tagName);
|
|
|
|
if ( Array.isArray(classNames) ) {
|
|
|
|
el.classList.add(...classNames);
|
|
|
|
} else if( classNames ) {
|
|
|
|
el.classList.add(classNames);
|
|
|
|
}
|
|
|
|
for (let attrName in attributes) {
|
|
|
|
el[attrName] = attributes[attrName];
|
|
|
|
}
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
/**
|
|
* Append one or several elements to the parent
|
|
*
|
|
* @param {Element} parent - where to append
|
|
* @param {Element|Element[]} - element ore elements list
|
|
*/
|
|
static append(parent, elements) {
|
|
|
|
if ( Array.isArray(elements) ) {
|
|
|
|
elements.forEach( el => parent.appendChild(el) );
|
|
|
|
} else {
|
|
|
|
parent.appendChild(elements);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Selector Decorator
|
|
*
|
|
* Returns first match
|
|
*
|
|
* @param {Element} el - element we searching inside. Default - DOM Document
|
|
* @param {String} selector - searching string
|
|
*
|
|
* @returns {Element}
|
|
*/
|
|
static find(el = document, selector) {
|
|
|
|
return el.querySelector(selector);
|
|
|
|
}
|
|
|
|
/**
|
|
* Selector Decorator.
|
|
*
|
|
* Returns all matches
|
|
*
|
|
* @param {Element} el - element we searching inside. Default - DOM Document
|
|
* @param {String} selector - searching string
|
|
* @returns {NodeList}
|
|
*/
|
|
static findAll(el = document, selector) {
|
|
|
|
return el.querySelectorAll(selector);
|
|
|
|
}
|
|
|
|
/**
|
|
* Check if object is DOM node
|
|
*
|
|
* @param {Object} node
|
|
* @returns {boolean}
|
|
*/
|
|
static isElement(node) {
|
|
|
|
return node && typeof node === 'object' && node.nodeType && node.nodeType === Node.ELEMENT_NODE;
|
|
|
|
}
|
|
|
|
}; |