Back to Devexpress

Context Menu

aspnet-7531-components-html-editor-concepts-context-menu.md

latest8.1 KB
Original Source

Context Menu

  • Jan 18, 2022
  • 4 minutes to read

A context menu is a popup menu displayed when a user right-clicks content (text, image, table, etc.) within the editor.

The context menu contains a collection of items (HtmlEditorContextMenuItem objects). To access the collection, use the ASPxHtmlEditor.ContextMenuItems property. The collection is an HtmlEditorContextMenuItemCollection class instance that has methods and properties to access and manipulate (add or delete) menu items.

The ASPxHtmlEditorSettings.AllowContextMenu property allows you to specify the type and availability of the context menu.

AllowContextMenu property valueCorresponding menu type
TrueAn HTML Editor menu that contains common (Cut, Copy, Paste, and Select All) and context-specific (Modify Table, Change Image, and so on) operations.
DefaultA browser’s context menu (for IE, Chrome, and so on) that contains commands specific to a particular browser.
FalseA context menu is not shown.

Note

You can invoke a context menu only in the editor’s Design View and HTML View.

Default Context Menu Items

A context menu contains the following items:

ItemDescription
Items available in Design view
CutCuts the selection to the clipboard. See the note below the table.
CopyCopies the selection to the clipboard. See the note below the table.
PastePastes content from the clipboard to the current cursor position. See the note below the table.
Select AllSelects all editor content.
UnlinkRemoves the current link element.
Change LinkInvokes the Change Link dialog for the current link.
Change ImageInvokes the Change Image dialog for the current image.
Change AudioInvokes the Change Audio dialog for the current audio element.
Change VideoInvokes the Change Video dialog for the current video element.
Change FlashInvokes the Change Flash dialog for the current flash element.
Change YouTube VideoInvokes the Change YouTube Video dialog for the current YouTube video element.
Change PlaceholderInvokes the Change Placeholder dialog for the current placeholder.
Table PropertiesInvokes the Table Properties dialog for the current table.
Row PropertiesInvokes the Row Properties dialog for the current row.
Column PropertiesInvokes the Column Properties dialog for the current column.
Cell PropertiesInvokes the Cell Properties dialog for the current cell.
Insert TableInserts a new table.
Insert Row AboveInserts a table row above the current one.
Insert Row BelowInserts a table row below the current one.
Insert Column to the LeftInserts a table column to the left of the current one.
Insert Column to the RightInserts a table column to the right of the current one.
Split HorizontallySplits the current cell horizontally into two cells.
Split VerticallySplits the current cell vertically into two cells.
Merge RightMerges the current table cell with the right one.
Merge DownMerges the current table cell with the bottom one.
Delete TableDeletes the current table.
Delete RowDeletes the current table row.
Delete ColumnDeletes the current table column.
Items available in HTML view
OutdentDecrease an indent for the current paragraph.
IndentIncrease an indent for the current paragraph.
CommentComments the selected code lines.
UncommentUncomments the selected code section.
Format DocumentFormats the HTML document.
Collapse TagCollapses the current tag.
Expand TagExpands the current tag.

Default item visibility is switched based on the currently selected element.

Note

Some browsers (for example, Firefox, Chrome) do not allow scripts to work with the clipboard for security reasons. Therefore, the Cut, Copy, and Paste items are removed from the context menu for these browsers.

Context Menu Runtime Customization

ASPxHtmlEditor allows you to customize default content menu items and create custom items. To modify an item collection, use members of the HtmlEditorContextMenuItemCollection class.

The code sample below demonstrates how you can handle the ASPxClientHtmlEditor.ContextMenuShowing event to change the content of a context menu (by changing its items visibility).

csharp
protected void Page_Load(object sender, EventArgs e) {
     if (!IsPostBack) {
          MyHtmlEditor.ContextMenuItems.CreateDefaultItems();
          MyHtmlEditor.ContextMenuItems.Insert(0, new HtmlEditorContextMenuItem("Add Title...", "AddTitle"));
          MyHtmlEditor.ContextMenuItems.Insert(1, new HtmlEditorContextMenuItem("Change Title...", "ChangeTitle"));
          MyHtmlEditor.ContextMenuItems.Insert(2, new HtmlEditorContextMenuItem("Remove Title", "RemoveTitle"));
     }
}
javascript
function OnContextMenuShowing(s, e) {
     var contextMenu = s.GetContextMenu();
     var selectedElement = myHtmlEditor.GetSelection().GetSelectedElement();
     var isImageSelected = selectedElement && selectedElement.tagName.toLowerCase() == "img";

     SetContextMenuItemVisible(contextMenu, "AddTitle", isImageSelected && !selectedElement.title);
     SetContextMenuItemVisible(contextMenu, "ChangeTitle", isImageSelected && selectedElement.title);
     SetContextMenuItemVisible(contextMenu, "RemoveTitle", isImageSelected && selectedElement.title);
}

function SetContextMenuItemVisible(contextMenu, itemName, visible) {
     var item = contextMenu.GetItemByName(itemName);
     if(item)
          item.SetVisible(visible);
}
aspx
<dx:ASPxHtmlEditor ID="DemoHtmlEditor" runat="server" ClientInstanceName="myHtmlEditor">
     <ClientSideEvents ContextMenuShowing="OnContextMenuShowing"

     ...

</dx:ASPxHtmlEditor>

Note that if you create custom context menu items within ASPxHtmlEditor, default items are not created automatically. To manually populate a context menu with default items, call the HtmlEditorContextMenuItemCollection.CreateDefaultItems method.

Context Menu Design-time Customization

You can customize a context menu item collection at design time using the ASPxHtmlEditor Designer or directly in markup.