2
0
mirror of https://github.com/Ionaru/easy-markdown-editor synced 2025-07-05 09:04:28 -06:00

Add support for custom icon markup

This commit is contained in:
kolaente 2020-03-14 22:18:23 +01:00
parent 3096bbe291
commit a12ba4790a
No known key found for this signature in database
GPG Key ID: F40E70337AB24C9B

View File

@ -167,29 +167,35 @@ function createToolbarButton(options, enableActions, enableTooltips, shortcuts,
el.classList.add('no-mobile'); el.classList.add('no-mobile');
} }
// Provide backwards compatibility with simple-markdown-editor by adding custom classes to the button. // If there is a custom icon, use that
var classNameParts = options.className.split(' '); if (typeof options.icon !== 'undefined') {
var iconClasses = []; el.innerHTML = options.icon;
for (var classNameIndex = 0; classNameIndex < classNameParts.length; classNameIndex++) { } else {
var classNamePart = classNameParts[classNameIndex]; // Provide backwards compatibility with simple-markdown-editor by adding custom classes to the button.
// Split icon classes from the button.
// Regex will detect "fa", "fas", "fa-something" and "fa-some-icon-1", but not "fanfare". var classNameParts = options.className.split(' ');
if (classNamePart.match(/^fa([srlb]|(-[\w-]*)|$)/)) { var iconClasses = [];
iconClasses.push(classNamePart); for (var classNameIndex = 0; classNameIndex < classNameParts.length; classNameIndex++) {
} else { var classNamePart = classNameParts[classNameIndex];
el.classList.add(classNamePart); // Split icon classes from the button.
// Regex will detect "fa", "fas", "fa-something" and "fa-some-icon-1", but not "fanfare".
if (classNamePart.match(/^fa([srlb]|(-[\w-]*)|$)/)) {
iconClasses.push(classNamePart);
} else {
el.classList.add(classNamePart);
}
} }
}
el.tabIndex = -1; el.tabIndex = -1;
// Create icon element and append as a child to the button // Create icon element and append as a child to the button
var icon = document.createElement('i'); var icon = document.createElement('i');
for (var iconClassIndex = 0; iconClassIndex < iconClasses.length; iconClassIndex++) { for (var iconClassIndex = 0; iconClassIndex < iconClasses.length; iconClassIndex++) {
var iconClass = iconClasses[iconClassIndex]; var iconClass = iconClasses[iconClassIndex];
icon.classList.add(iconClass); icon.classList.add(iconClass);
}
el.appendChild(icon);
} }
el.appendChild(icon);
if (options.action && enableActions) { if (options.action && enableActions) {
if (typeof options.action === 'function') { if (typeof options.action === 'function') {