MDL-75268 editor_tiny: Add items after specified node
This commit is contained in:
+1
-1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -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}`;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user