Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'context menu' items (not CSS) #1567

Closed
vingeek-dev opened this issue Aug 27, 2019 · 16 comments
Closed

Remove 'context menu' items (not CSS) #1567

vingeek-dev opened this issue Aug 27, 2019 · 16 comments
Labels
contextmenu duplicate feature-request Request for new features or functionality
Milestone

Comments

@vingeek-dev
Copy link

How can we remove items from the context menu? Is there a native mode supported by Monaco?

monaco-editor version: 0.17.1 (latest)
Browser: Google Chrome 76
OS: Windows 10

@alexdima alexdima added the feature-request Request for new features or functionality label Sep 18, 2019
@alexdima alexdima added this to the Backlog milestone Sep 18, 2019
@toolgood
Copy link

function removeMenu(menuId){
	for (var key in menus) {
		if (menus[key]==undefined) { continue; }
		var menu = menus[key];
		for (let index = 0; index < menu.length; index++) {
			var m = menu[index];
			if(m.command.id==menuId){
				menu.splice(index,1);
				return;
			}
		}
	}
}

@toolgood
Copy link

	window.menus= require('vs/platform/actions/common/actions').MenuRegistry._menuItems;

@KevinWuWon
Copy link

+1. The context menu has items like "Go to definition", "Peek definition", etc., which don't work. I'd like to remove them.

@KyleMit
Copy link

KyleMit commented Dec 22, 2020

You can remove menu items from the context menu like this:

import * as actions from "monaco-editor/esm/vs/platform/actions/common/actions";

let menus = actions.MenuRegistry._menuItems;
let contextMenuEntry = [...menus].find(entry => entry[0]._debugName == "EditorContext");
let contextMenuLinks = contextMenuEntry[1];

let removableIds = ["editor.action.goToTypeDefinition"];

let removeById = (list, ids) => {
  let node = list._first;
  do {
    let shouldRemove = ids.includes(node.element?.command?.id);
    if (shouldRemove) { list._remove(node)  }
  } while ((node = node.next));
};

removeById(contextMenuLinks, removableIds);

Further Reading

@codebykat
Copy link

@KyleMit You just saved me so many hours of aggravation, thank you for that walkthrough ❤️

@qsc-jhndnn
Copy link

Did something change in later versions that would have affected the solution by @KyleMit? I'm using 0.27.0 and actions.MenuRegistry._menuItems; is returning a map with 0 entries in it.

@Obapelumi
Copy link

Having the same issue too

@SwatDoge
Copy link

Same issue persists

@SwatDoge
Copy link

This is the current fix for this issue: https://stackoverflow.com/a/73858820/11121992

A hack for sure

@jseparovic
Copy link

This worked for me to remove everything (Peek submenu too)

import * as actions from "monaco-editor/esm/vs/platform/actions/common/actions";
let menus = actions.MenuRegistry._menuItems
let contextMenuEntry = [...menus].find(entry => entry[0]._debugName == "EditorContext")
let contextMenuLinks = contextMenuEntry[1]
let removableIds = [
    "editor.action.clipboardCutAction",
    "editor.action.clipboardCopyAction",
    "editor.action.clipboardPasteAction",
    "editor.action.refactor",
    "editor.action.sourceAction",
    "editor.action.revealDefinition",
    "editor.action.revealDeclaration",
    "editor.action.goToTypeDefinition",
    "editor.action.goToImplementation",
    "editor.action.goToReferences",
    "editor.action.formatDocument",
    "editor.action.formatSelection",
    "editor.action.changeAll",
    "editor.action.rename",
    "editor.action.quickOutline",
    "editor.action.quickCommand",
    "Peek",
]
let removeById = (list: any, ids: any) => {
    let node = list._first
    do {
        let shouldRemove = ids.includes(node.element?.command?.id) || ids.includes(node.element?.title)
        if (shouldRemove) { list._remove(node) }
    } while ((node = node.next))
}
removeById(contextMenuLinks, removableIds)

@SwatDoge
Copy link

This worked for me to remove everything (Peek submenu too)

import * as actions from "monaco-editor/esm/vs/platform/actions/common/actions";
let menus = actions.MenuRegistry._menuItems
let contextMenuEntry = [...menus].find(entry => entry[0]._debugName == "EditorContext")
let contextMenuLinks = contextMenuEntry[1]
let removableIds = [
    "editor.action.clipboardCutAction",
    "editor.action.clipboardCopyAction",
    "editor.action.clipboardPasteAction",
    "editor.action.refactor",
    "editor.action.sourceAction",
    "editor.action.revealDefinition",
    "editor.action.revealDeclaration",
    "editor.action.goToTypeDefinition",
    "editor.action.goToImplementation",
    "editor.action.goToReferences",
    "editor.action.formatDocument",
    "editor.action.formatSelection",
    "editor.action.changeAll",
    "editor.action.rename",
    "editor.action.quickOutline",
    "editor.action.quickCommand",
    "Peek",
]
let removeById = (list: any, ids: any) => {
    let node = list._first
    do {
        let shouldRemove = ids.includes(node.element?.command?.id) || ids.includes(node.element?.title)
        if (shouldRemove) { list._remove(node) }
    } while ((node = node.next))
}
removeById(contextMenuLinks, removableIds)

actions.MenuRegistry._menuItems gives an empty map on newer versions like mentioned here: #1567 (comment)

@chriswnewman
Copy link

This is the current fix for this issue: https://stackoverflow.com/a/73858820/11121992

A hack for sure

This approach got me most of the way there, I ended up posting my modified solution. https://stackoverflow.com/a/75149195/4325849

@lennybakkalian
Copy link

@kylewith solution no longer works in the newer Monaco versions. An updated version would be:

let menus = actions.MenuRegistry._menuItems as Map<any, any>
const contextMenuEntry = Array.from(menus, ([key, value]) => ({key, value})).find(entry => entry.key.id == 'EditorContext')

let removableIds = ["editor.action.clipboardCopyAction", "editor.action.clipboardPasteAction"]
let removeById = (list, ids) => {
  let node = list._first
  do {
    let shouldRemove = ids.includes(node.element?.command?.id)
    if (shouldRemove) {
      list._remove(node)
    }
  } while ((node = node.next))
}
removeById(contextMenuEntry.value, removableIds)

@baiyuxiong
Copy link

I got this import error:
image

@eveloki

This comment was marked as outdated.

@hediet
Copy link
Member

hediet commented Mar 7, 2023

Duplicate of #1280

@hediet hediet marked this as a duplicate of #1280 Mar 7, 2023
@hediet hediet closed this as not planned Won't fix, can't repro, duplicate, stale Mar 7, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contextmenu duplicate feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests