Context Menus
The menus that open on a right-click in the Document Editor can carry commands of your own application, lose built-in commands users shouldn't use or be turned off. You react to the contextMenuOpening event, add items with click handlers and change built-in items.
The examples extend the view of the ASP.NET Core tutorial, whose Insert Table button inserts a table with the ID 10.
Change Built-In Items
Built-in items are ContextMenuItem objects in the same array. Find them by their text and change them before the menu opens. The texts of the built-in items aren't listed in the reference, and the items changed in version 34.0, so log e.items in the browser console, as the code above does, to see the items of your version.
The following handler cancels the menu in the page margins and disables the Footnote item of the Insert sub menu. It shows two more changes in comments:
function findItem(items, text) {
return (items || []).find(function (item) { return item.text === text; });
}
TXTextControl.addEventListener("textControlLoaded", function () {
TXTextControl.addEventListener("contextMenuOpening", function (e) {
// No context menu in the top and bottom page margins
if ((e.location & TXTextControl.ContextMenuLocation.PageMargin) !== 0) {
e.cancel = true;
return;
}
var insert = findItem(e.items, "Insert");
if (!insert) {
return;
}
var footnote = findItem(insert.items, "Footnote");
if (footnote) {
footnote.isEnabled = false;
// To replace the built-in action instead:
// footnote.clickHandler = function (args) { args.cancel = true; console.log("Footnotes are not available."); };
}
// To remove the complete "Insert" item:
// e.items.splice(e.items.indexOf(insert), 1);
});
});
- ContextMenuEventArgs.cancel with
truesuppresses the menu. - isEnabled with
falseshows the item disabled. - A click handler set on a built-in item runs when the item is clicked. ContextMenuItemClickEventArgs.cancel with
truekeeps the click from being sent to the backend, so the built-in command isn't carried out. spliceremoves an item from the array, so that the menu doesn't show it.
Find items by their text rather than by their position in the array: the order of the built-in items isn't documented either. The blog article Manipulating the Context Menu in the Document Editor using JavaScript shows these changes step by step.
Next Steps
- Ribbon: change the ribbon of the editor.
- Localization: the language of the user interface.
- Events: all events of the editor.
