Update editor toolbar.
Unify dropdown handling, refactor crowded toolbar
This commit is contained in:
+8
-68
@@ -124,6 +124,7 @@
|
||||
wikilink: insertWikilink,
|
||||
ul: function () { linePrefix('- '); },
|
||||
ol: function () { linePrefix('1. '); },
|
||||
task: function () { linePrefix('- [ ] '); },
|
||||
hr: function () { wrap('\n\n---\n\n', '', ''); },
|
||||
fmttable: function () { applyTableOp(T.formatTableText); },
|
||||
tblalignleft: function () { applyTableOp(T.setColumnAlignment, 'left'); },
|
||||
@@ -136,6 +137,11 @@
|
||||
dateiso: function () { insertAtCursor(D.isoDate()); },
|
||||
datelong: function () { insertAtCursor(D.longDate()); },
|
||||
movie: function () { M.run(textarea); },
|
||||
wide: function () {
|
||||
var enabled = !document.body.classList.contains('editor-wide');
|
||||
document.body.classList.toggle('editor-wide', enabled);
|
||||
sessionStorage.setItem('editor-wide', enabled ? '1' : '0');
|
||||
},
|
||||
};
|
||||
|
||||
// --- Keyboard shortcut registration ---
|
||||
@@ -151,10 +157,6 @@
|
||||
}
|
||||
});
|
||||
|
||||
keyMap['T'] = actions.fmttable;
|
||||
keyMap['D'] = actions.dateiso;
|
||||
keyMap['W'] = actions.datelong;
|
||||
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (!e.altKey || !e.shiftKey) return;
|
||||
// Shift+digit produces a layout-dependent character in e.key (e.g. "!"
|
||||
@@ -192,69 +194,7 @@
|
||||
applyResult(result);
|
||||
});
|
||||
|
||||
// --- Dropdown helper ---
|
||||
// --- Dropdowns ---
|
||||
|
||||
var openMenus = [];
|
||||
|
||||
function makeDropdown(triggerBtn, items) {
|
||||
var menu = document.createElement('div');
|
||||
menu.className = 'dropdown-menu';
|
||||
items.forEach(function (item) {
|
||||
var btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
btn.className = 'btn btn-tool dropdown-item';
|
||||
btn.textContent = item.label;
|
||||
btn.addEventListener('mousedown', function (e) {
|
||||
e.preventDefault();
|
||||
actions[item.action]();
|
||||
menu.classList.remove('is-open');
|
||||
});
|
||||
menu.appendChild(btn);
|
||||
});
|
||||
triggerBtn.appendChild(menu);
|
||||
openMenus.push(menu);
|
||||
|
||||
triggerBtn.addEventListener('click', function (e) {
|
||||
if (e.target !== triggerBtn) return;
|
||||
var wasOpen = menu.classList.contains('is-open');
|
||||
openMenus.forEach(function (m) { m.classList.remove('is-open'); });
|
||||
if (!wasOpen) menu.classList.add('is-open');
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('click', function (e) {
|
||||
var insideAny = openMenus.some(function (m) {
|
||||
return m.parentElement && m.parentElement.contains(e.target);
|
||||
});
|
||||
if (!insideAny) openMenus.forEach(function (m) { m.classList.remove('is-open'); });
|
||||
});
|
||||
document.addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Escape') openMenus.forEach(function (m) { m.classList.remove('is-open'); });
|
||||
});
|
||||
|
||||
// --- Table dropdown ---
|
||||
|
||||
var tblDropBtn = document.querySelector('[data-action="tbldrop"]');
|
||||
if (tblDropBtn) {
|
||||
makeDropdown(tblDropBtn, [
|
||||
{ label: 'Format table', action: 'fmttable' },
|
||||
{ label: 'Align left', action: 'tblalignleft' },
|
||||
{ label: 'Align center', action: 'tblaligncenter' },
|
||||
{ label: 'Align right', action: 'tblalignright' },
|
||||
{ label: 'Insert column', action: 'tblinsertcol' },
|
||||
{ label: 'Delete column', action: 'tbldeletecol' },
|
||||
{ label: 'Insert row', action: 'tblinsertrow' },
|
||||
{ label: 'Delete row', action: 'tbldeleterow' },
|
||||
]);
|
||||
}
|
||||
|
||||
// --- Date dropdown ---
|
||||
|
||||
var dateDropBtn = document.querySelector('[data-action="datedrop"]');
|
||||
if (dateDropBtn) {
|
||||
makeDropdown(dateDropBtn, [
|
||||
{ label: 'YYYY-MM-DD', action: 'dateiso' },
|
||||
{ label: 'DE Long', action: 'datelong' },
|
||||
]);
|
||||
}
|
||||
document.querySelectorAll('.dropdown-toggle').forEach(wireDropdown);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user