Products Technologies Demo Docs Blog Support Company

Manipulating the Context Menu in the Document Editor using JavaScript

This article shows how to manipulate the context menu in the Document Editor in JavaScript. The context menu can be customized by adding or removing items or by changing the event handler of existing items.

Manipulating the Context Menu in the Document Editor using JavaScript

A fully customizable shortcut menu is available in the Document Editor since version 31.0 of TX Text Control .NET Server. A new contextMenuOpening event, which allows you to change the items before the menu is rendered, can be used to access the contents of the context menu.

ContextMenuItem objects can be added, removed, and modified, including the click handler associated with them. The location property indicates the context of the menu and whether the input position is a text selection or above a table, image, or header.

The items property returns all available items that can be manipulated.

In this article we will discuss 4 different typical scenarios when you manipulate the context menus:

  • Removing an existing item from the context menu.
  • Removing a sub-item from the context menu.
  • Disabling an existing item.
  • Changing the click handler of an existing item.

Based on the standard context menu for inserting a footnote (Insert -> Footnote), the examples should show the scenarios that are typical for this.

Context menu

Removing Items

Removing an existing item from the context menu is very simple. The following code removes the Insert item from the context menu:

// remove the "Insert" menu item
TXTextControl.addEventListener("contextMenuOpening", e => { 
        
        // check if e.items contains an entry where property text == "Insert"
        const index = e.items.findIndex(i => i.text === "Insert");

        // if so, remove it
        if (index > -1) {
                e.items.splice(index, 1);
        }

})

Context menu

Removing Sub-Items

Removing a sub-item from the context menu is a little bit more complex. The following code removes the Footnote item from the Insert item:

// remove the "Footnote" menu item
TXTextControl.addEventListener("contextMenuOpening", (e) => {

        // check if e.items contains an entry where property text == "Insert"
        const index = e.items.findIndex((i) => i.text === "Insert");

        // check if e.items[index].items contains an entry where property text == "Footnote"
        const index2 = e.items[index].items.findIndex((i) => i.text === "Footnote");

        // if so, remove it
        if (index2 > -1) {
                e.items[index].items.splice(index2, 1);
        }

})

Context menu

First, it searches for the Insert menu item, and then it searches for the Footnote item in its item collection. If found, the item is removed from the array.

Disabling the Item

Disabling an existing item is also very simple. The following code disables the Footnote item:

// disable the "Footnote" menu item
TXTextControl.addEventListener("contextMenuOpening", (e) => {

        // check if e.items contains an entry where property text == "Insert"
        const index = e.items.findIndex((i) => i.text === "Insert");

        // check if e.items[index].items contains an entry where property text == "Footnote"
        const index2 = e.items[index].items.findIndex((i) => i.text === "Footnote");

        // if so, disable it
        e.items[index].items[index2].isEnabled = false;

})

Context menu

First, it searches for the Footnote item and then it sets the isEnabled property to false.

Changing the Click Handler

The click handler can also be subject to manipulation. This sample code overrides the default action and adds an alert to the function.

// cancel the click handler of the "Footnote" menu item
TXTextControl.addEventListener("contextMenuOpening", (e) => {

        // check if e.items contains an entry where property text == "Insert"
        const index = e.items.findIndex((i) => i.text === "Insert");

        // check if e.items[index].items contains an entry where property text == "Footnote"
        const index2 = e.items[index].items.findIndex((i) => i.text === "Footnote");

        // if so, cancel the click handler
        e.items[index].items[index2].clickHandler = function(e) { 
                e.cancel = true; alert("Footnote is disabled"); 
        }
        
})

Context menu

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETDocument EditorEvents

Document Editor: Initialization Events

There are several events that the TX Text Control provides during the initialization phase. These events and the order in which they are fired are explained in this article.


ASP.NETJavaScriptDocument Editor

Detect Toggle Button Changes Using a MutationObserver

This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…


ASP.NETASP.NET CoreDocument Editor

Getting Started Video Tutorial: Document Editor in ASP.NET Core C# on Linux

This video tutorial shows how to use the Document Editor in an ASP.NET Core application using C# and deploy on Linux using Docker. This tutorial is part of the TX Text Control Getting Started…


ASP.NETDocument EditorDocument Protection

Document Protection in ASP.NET with TX Text Control: Healthcare Use Cases

This article explores document protection use cases in the healthcare domain. It also explains how to assign usernames, configure edit modes, and use editable regions based on user roles using the…


ASP.NETApp ServicesASP.NET Core

Deploying the TX Text Control Document Editor in an ASP.NET Core Web App to…

This tutorial shows how to deploy the TX Text Control Document Editor to Azure App Services using an ASP.NET Core Web App. The Document Editor is a powerful word processing component that can be…