MDL-75268 editor_tiny: Add items after specified node

This commit is contained in:
David Woloszyn
2023-08-28 12:06:06 +10:00
parent 9bfcd77d51
commit cd064a40eb
3 changed files with 27 additions and 6 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+25 -4
View File
@@ -58,9 +58,10 @@ export const displayFilepicker = (editor, filetype) => new Promise((resolve, rej
* @param {object} toolbar
* @param {string} section
* @param {string} button
* @param {string|null} [after=null]
* @returns {object} The toolbar configuration
*/
export const addToolbarButton = (toolbar, section, button) => {
export const addToolbarButton = (toolbar, section, button, after = null) => {
if (!toolbar) {
return [{
name: section,
@@ -71,7 +72,16 @@ export const addToolbarButton = (toolbar, section, button) => {
const mutatedToolbar = JSON.parse(JSON.stringify(toolbar));
return mutatedToolbar.map((item) => {
if (item.name === section) {
item.items.push(button);
if (after) {
// Insert new button after the specified button.
let index = item.items.findIndex(value => value == after);
if (index !== -1) {
item.items.splice(index + 1, 0, button);
}
} else {
// Append button to end of button section.
item.items.push(button);
}
}
return item;
@@ -148,9 +158,10 @@ export const addToolbarSection = (toolbar, name, relativeTo, append = true) => {
* @param {object} menubar
* @param {string} section
* @param {string} menuitem
* @param {string|null} [after=null]
* @returns {object}
*/
export const addMenubarItem = (menubar, section, menuitem) => {
export const addMenubarItem = (menubar, section, menuitem, after = null) => {
if (!menubar) {
const emptyMenubar = {};
emptyMenubar[section] = {
@@ -162,7 +173,17 @@ export const addMenubarItem = (menubar, section, menuitem) => {
const mutatedMenubar = JSON.parse(JSON.stringify(menubar));
Array.from(Object.entries(mutatedMenubar)).forEach(([name, menu]) => {
if (name === section) {
menu.items = `${menu.items} ${menuitem}`;
if (after) {
// Insert new item after the specified menu item.
let index = menu.items.indexOf(after);
if (index !== -1) {
index += after.length;
menu.items = menu.items.slice(0, index) + ` ${menuitem}` + menu.items.slice(index);
}
} else {
// Append item to end of the menu section.
menu.items = `${menu.items} ${menuitem}`;
}
}
});