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.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

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.NETJavaScriptASP.NET Core

Build a Custom Backstage View in ASP.NET Core with TX Text Control

This article shows how to build a custom backstage view with a File tab to load your multi-format documents using TX Text Control for ASP.NET Core applications.


ASP.NETASP.NET CoreDocument Editor

ASP.NET Core Document Editor with Backend via the Text Control Private NuGet…

This article demonstrates how to create a Document Editor ASP.NET Core application using the Text Control Private NuGet Feed. We will build a basic web application that enables users to edit…


ASP.NETASP.NET CoreDocument Automation

Why Document Processing Libraries Require a Document Editor

A document processing library alone cannot guarantee reliable and predictable results. Users need a true WYSIWYG document editor to design and adjust templates to appear exactly as they will after…

Share on this blog post on: