microsoft / monaco-editor

A browser based code editor
https://microsoft.github.io/monaco-editor/
MIT License
40.35k stars 3.59k forks source link

[Feature Request] addKeybindingRule(s) for individual editor instances #3623

Open golergka opened 1 year ago

golergka commented 1 year ago

Context

Description

In my project I use several editor instances, and I want to disable certain keybindings only on some of them. Thankfully, 0.34.1 added addKeybindingRules(s), but this function is defined as a global and affects all editors. I'd like to be able to remove keybindings only for selected editors.

Monaco Editor Playground Link

No response

Monaco Editor Playground Code

No response

spahnke commented 1 year ago

For a workaround you could probably use context keys 🤔 Augment the when condition of the keybinding you want to disable in select editors by a custom context key and then set that only in the editors you want the keybinding to be disabled in.

// remove standard keybinding
monaco.editor.addKeybindingRule({keybinding: 0, command: "-editor.action.quickFix"});
// add keybinding again but augment the when condition with a custom context key that would disable the keybinding
monaco.editor.addKeybindingRule({
    keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.Period,
    command: "editor.action.quickFix",
    when: "!disabledKeybinding && editorHasCodeActionsProvider && editorTextFocus && !editorReadonly"
});

...

// set custom context key to disable keybinding in second editor only
editor2.createContextKey("disabledKeybinding", true);

Full example: https://microsoft.github.io/monaco-editor/playground.html?source=v0.36.1#XQAAAAI9BAAAAAAAAABBqQkHQ5NjdQultUhUwcsQIjjaoVayL-wYsMeg24JItvniWevaGyL6ot7GNI6RFj5KIZKnPs8bDABIDSRRqux-oVQTdPebDZ8u0itb93f9WlkNTdWtcwFa0klz2Uze64bXB2v03-EretB3tidfvYGSf4R3--JKiAWo6xaLSDyaa9m7AQ2QOOD0SL4WSkVaM5p4lxmXzzD7USxyTsuKk8lEx2fZLqvK14ygFQO9n03NBLYfaVie45LTJQzof_l-I0n-xaSSYW7xmXVVmqAmViBkQy1_MjxXldo4RIle52xsaNVKPlUm2Fq7whV_C5M6YM606Ug9Xrj7zShENHiQuvyXtYIHRhgYg5tQ39d2h9QYUHJGkNsiLHpGUop5sreVHdFn903eroRdMzu6urS38H6EFqUTV42GZQgyxsFCjFz5a0d9j3MNi1Z5uP7qRiRLMJs8M-h2JYM0mKVJ3gYd0vEbOCkIapmAT3irUJl_7oIPufUSsVesG6ZFoLUTdcOQ2yVhuqzONFmi60CsA3ytEzZ63lVv6SFWQlFWuAenq_poZFTfeefzowAGmo3E5JWDHBQXGeODXZupJa3D2jwQXce4bJPFBMRUo62yWShv29xySxmdbkieZYbZLpJqddYweAaMZ4cO8SnSekVNBVyc4QKqsTVY8We1ByqaF3bYVMO8eXjPlkwVIUndqOSAAhkmKpHnPpF-ae-yrP3_o65bWw