allow dropdown menu on toolbar to group secondary buttons

dropdown_wip
firm1 4 years ago
parent 661fcc82ab
commit 98fbe56248

@ -346,6 +346,46 @@ var easyMDE = new EasyMDE({
}); });
``` ```
Put some buttons on dropdown menu
```Javascript
var easyMDE = new EasyMDE({
toolbar: [{
name: "heading",
action: EasyMDE.toggleHeadingSmaller,
className: "fa fa-header",
title: "Headers",
},
"|",
{
name: "others",
className: "fa fa-blind",
title: "others buttons",
children: [
{
name: "image",
action: EasyMDE.drawImage,
className: "fa fa-picture-o",
title: "Image",
},
{
name: "quote",
action: EasyMDE.toggleBlockquote,
className: "fa fa-percent",
title: "Quote",
},
{
name: "link",
action: EasyMDE.drawLink,
className: "fa fa-link",
title: "Link",
}
]
},
// [, ...]
]
});
```
### Keyboard shortcuts ### Keyboard shortcuts

@ -22,7 +22,7 @@
right: 0; right: 0;
bottom: 0; bottom: 0;
height: auto; height: auto;
z-index: 9; z-index: 8;
border-right: none !important; border-right: none !important;
border-bottom-right-radius: 0 !important; border-bottom-right-radius: 0 !important;
} }
@ -72,9 +72,6 @@
.editor-toolbar.fullscreen { .editor-toolbar.fullscreen {
width: 100%; width: 100%;
height: 50px; height: 50px;
overflow-x: auto;
overflow-y: hidden;
white-space: nowrap;
padding-top: 10px; padding-top: 10px;
padding-bottom: 10px; padding-bottom: 10px;
box-sizing: border-box; box-sizing: border-box;
@ -119,7 +116,7 @@
padding: 0; padding: 0;
} }
.editor-toolbar button { .editor-toolbar button, .editor-toolbar .easymde-dropdown {
background: transparent; background: transparent;
display: inline-block; display: inline-block;
text-align: center; text-align: center;
@ -316,3 +313,20 @@
color: #7f8c8d; color: #7f8c8d;
font-style: italic; font-style: italic;
} }
.easymde-dropdown {
position: relative;
}
.easymde-dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 8px 8px;
z-index: 2;
}
.easymde-dropdown:hover .easymde-dropdown-content {
display: block;
}

@ -110,15 +110,30 @@ function fixShortcut(name) {
return name; return name;
} }
/**
* Create dropdown block
*/
function createToolbarDropdown(options, enableTooltips, shortcuts, parent) {
var el = createToolbarButton(options, enableTooltips, shortcuts, 'div');
el.className += ' easymde-dropdown';
var content = document.createElement('div');
content.className = 'easymde-dropdown-content';
for (var childrenIndex = 0; childrenIndex < options.children.length; childrenIndex++) {
var child = createToolbarButton(options.children[childrenIndex], enableTooltips, shortcuts, 'button', parent);
content.appendChild(child);
}
el.appendChild(content);
return el;
}
/** /**
* Create button element for toolbar. * Create button element for toolbar.
*/ */
function createToolbarButton(options, enableTooltips, shortcuts) { function createToolbarButton(options, enableTooltips, shortcuts, markup, parent) {
options = options || {}; options = options || {};
var el = document.createElement('button'); var el = document.createElement(markup);
el.className = options.name; el.className = options.name;
el.setAttribute('type', 'button'); el.setAttribute('type', markup);
enableTooltips = (enableTooltips == undefined) ? true : enableTooltips; enableTooltips = (enableTooltips == undefined) ? true : enableTooltips;
// Properly hande custom shortcuts // Properly hande custom shortcuts
@ -167,6 +182,20 @@ function createToolbarButton(options, enableTooltips, shortcuts) {
} }
el.appendChild(icon); el.appendChild(icon);
if (options.action) {
if (typeof options.action === 'function') {
el.onclick = function (e) {
e.preventDefault();
options.action(parent);
};
} else if (typeof options.action === 'string') {
el.onclick = function (e) {
e.preventDefault();
window.open(options.action, '_blank');
};
}
}
return el; return el;
} }
@ -2254,24 +2283,14 @@ EasyMDE.prototype.createToolbar = function (items) {
if (item === '|') { if (item === '|') {
el = createSep(); el = createSep();
} else { } else {
el = createToolbarButton(item, self.options.toolbarTips, self.options.shortcuts); if (item.children) {
} el = createToolbarDropdown(item, self.options.toolbarTips, self.options.shortcuts, self);
} else {
// bind events, special for info el = createToolbarButton(item, self.options.toolbarTips, self.options.shortcuts, 'button', self);
if (item.action) {
if (typeof item.action === 'function') {
el.onclick = function (e) {
e.preventDefault();
item.action(self);
};
} else if (typeof item.action === 'string') {
el.onclick = function (e) {
e.preventDefault();
window.open(item.action, '_blank');
};
} }
} }
toolbarData[item.name || item] = el; toolbarData[item.name || item] = el;
bar.appendChild(el); bar.appendChild(el);

Loading…
Cancel
Save