monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

[Bug] Multiple editor instances only latest created instance is working properly

Open steinerjakob opened this issue 3 years ago • 21 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [X] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

/* HTML CONTENT
<div id="container" style="height: 50%"></div>
<div id="container2" style="height: 50%"></div>
*/

const model = monaco.editor.createModel("function hello() {\n\talert('Hello world!');\n}", 'javascript');
const model1 = monaco.editor.createModel("function test() {\n\talert('Hello test!');\n}", 'javascript');
const editor = monaco.editor.create(document.getElementById('container'), {
    model
});
const editor2 = monaco.editor.create(document.getElementById('container2'), {
    model:model1
});

editor.addCommand(monaco.KeyMod.CtrlCmd|monaco.KeyMod.Shift|monaco.KeyCode.KeyF, ()=> {
    const pos = editor.getPosition();
    console.log('pos', pos)
});
editor2.addCommand(monaco.KeyMod.CtrlCmd|monaco.KeyMod.Shift|monaco.KeyCode.KeyF, ()=> {
    const pos = editor2.getPosition();
    console.log('pos2', pos)
});

Actual Behavior

Triggering crtl + shift + f in the first instance, the custom command of the second instance will be triggered.

Expected Behavior

Triggering crtl + shift + f in any editor instance should trigger the command of the correct editor instance.

Additional Context

No response

steinerjakob avatar Feb 07 '22 13:02 steinerjakob

Both editor instances get injected the same instance of StandaloneKeybindingService. Thus, this is currently by design. However, I can see that this looks like a bug.

I'll tag this as feature request to use independent keybinding services for different editors.

hediet avatar Feb 07 '22 14:02 hediet

Ok, thats weird. Because currently I'm using the 0.31.1 version in my project with multiple editor instances and the behaviour is like the expected one.

steinerjakob avatar Feb 07 '22 14:02 steinerjakob

So it is a regression in 0.32.0?

hediet avatar Feb 07 '22 14:02 hediet

From my point of view, yes it is a regression in 0.32.0

steinerjakob avatar Feb 07 '22 14:02 steinerjakob

Same problem here, i can confirm if needed this is a regression between 0.31.1 and 0.32. Reverting to 0.31 makes keybindings stick to their editor instance.

jmbarbier avatar Feb 11 '22 15:02 jmbarbier

Just keeping track of this issue : this is still present on 0.33

jmbarbier avatar Apr 12 '22 22:04 jmbarbier

Hello everyone, Several months have passed. How is this problem fixed?

dingiyan avatar Jul 20 '22 07:07 dingiyan

I have the same problem, with multiple editor instances on the page I need to disable actions.find for some editors. Once disabled, it is disabled for all instances. This worked before on 0.31.1.

ckorherr avatar Jul 21 '22 04:07 ckorherr

Can someone explain how the team is using GitHub milestones? It appears pretty random from an outsiders perspective, given that releases don't even correlate with the months. This issue was added to the July 2022 milestone, which has 18 of 22 issues solved and is closed. Same with some other older milestones. You can see a some of the recent milestones are closed but were never finished and issues were not moved to the next recent milestone https://github.com/microsoft/monaco-editor/milestones?state=closed So from my perspective this was planned for July 2022, but wasn't fixed. But the milestone was closed anyway. So I just want to make sure issues like this aren't forgotten, because I doubt you look into closed milestones.

Prinzhorn avatar Aug 30 '22 13:08 Prinzhorn

Opps... This is breaking our whole application. we're using multiple editor as table cell. I have managed the tab navigation through custom editors key mapping but now the command of last cell is applying for all previous cell, thus no tab works.

image

any other work-around? appreciate the help cc @alexdima

Nishchit14 avatar Nov 04 '22 09:11 Nishchit14

This is taking too long, is there a new estimated date when this will be fixed?

hedgelot avatar Nov 10 '22 09:11 hedgelot

This has also been impacting us for the last few months since updating to a newer release of the editor (we are currently using 0.34.1). In our case a user may have multiple editors open (i.e. editing multiple source file). We use custom key binding CTRL + S to save the model displayed in the "active" editor. In prior versions this worked as expected, now the CTRL + S command is routed to the most-recently-opened editor instance rather than the active/in-focus editor instance, meaning that:

  • users are accidently saving files they do not intend to save, and
  • files that users think they have saved are not actually saved (i.e. they lose work) This is making our users quite unhappy.

ihqtim avatar Nov 24 '22 23:11 ihqtim

Inspired by the workaround in https://github.com/grafana/grafana/pull/60172 which mentioned this issue, here's a more distilled workaround:

function bindAddCommandContext(editor: editor.IStandaloneCodeEditor, context: string): { dispose(): void } {
    const addCommand = editor.addCommand;

    editor.addCommand = function addCommand_hijacked(keybinding, handler, _context) {
        if (_context !== undefined) {
            console.error(
                'editor.addCommand cannot use the context parameter. ' +
                'This is due to a workaround for an upstream bug: ' +
                'https://github.com/microsoft/monaco-editor/issues/2947'
            );
        }
        return addCommand.call(this, keybinding, handler, context);
    };

    return {
        dispose: () => {
            editor.addCommand = addCommand;
        }
    };
}

// Based on workaround in https://github.com/grafana/grafana/pull/60172
const editorFocusedContextKeyName = `__isEditorFocused-${/* unique ID function of your choice */}`;
const isEditorFocused = editor.createContextKey(editorFocusedContextKeyName, false);
const onBlurDisposable = editor.onDidBlurEditorWidget(() => isEditorFocused.set(false));
const onFocusDisposable = editor.onDidFocusEditorText(() => isEditorFocused.set(true));
const addCommandContextBinding = bindAddCommandContext(editor, editorFocusedContextKeyName);

// proceed with configuration

// dispose as needed

Not super pretty, but will hopefully help some of you in the meantime. Confirmed working with @monaco-editor/react (create the binding in onMount).

TheUnlocked avatar Feb 03 '23 19:02 TheUnlocked

@stoanarrr You can re add the command when the editor focus, it works for me:

addCommand () {
    this.editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {
    });
    this.editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyR, () => {
    });
    this.editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyR, () => {
    });
    this.editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Period, () => {
    });
  }
this.editor.onDidFocusEditorText(() => {
        this.addCommand();
    });

yuanzhibang-tool avatar Feb 08 '23 09:02 yuanzhibang-tool

Yes, it's a problem.

See on Playground

cihad avatar Jul 17 '23 23:07 cihad

A simpler workaround than the ones I've seen above might be to use actions instead of commands:

Playground link

spahnke avatar Jul 18 '23 06:07 spahnke

Not related to key bindings but I am having a very similar issue when applying schemas using setDiagnosticsOptions. I have multiple instances of the Editor component and different schemas are applied to each but each subsequent Editor just overrides the schema of all the previous Editors. All the Editor components have unique ids.

beanpot99 avatar Jul 18 '23 21:07 beanpot99

Just tested in the playground with the latest dev build and still seems to be an issue.

The workaround to switch addCommand -> addAction seems to have worked well for us. We were using the context option of addCommand as suggested here, and it appears that precondition of addAction works similarly.

jkillian avatar Feb 13 '24 21:02 jkillian

Hello,The latest version I am currently using still has this issue. Is there a plan to fix it, or is there currently any other solution available

AmamiyaCx avatar Mar 14 '24 08:03 AmamiyaCx