microsoft / monaco-editor

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

how to use createContextKey<T>(key: string, defaultValue: T): IContextKey<T> function . i want to set default value based on some condition. how can i achieve this.any one help me out. #1198

Closed reddys11 closed 4 years ago

reddys11 commented 5 years ago

monaco-editor version: 0.X.Y Browser: OS:

Steps or JS usage snippet reproducing the issue:

alexdima commented 5 years ago

Here is a sample showing how you can use context keys:

https://microsoft.github.io/monaco-editor/playground.html#interacting-with-the-editor-adding-a-command-to-an-editor-instance

reddys11 commented 5 years ago

Thanks for the response.its working fine but, for the first right click context menu items appear based on the previous condition(not required items are coming in context menu). when i right click second time context menu items showing exactly what i required.

When I click for the second time context menu items getting correctly.how to resolve this,this is my code.

        editor.onContextMenu(function (e) {
                    var editorContent=[];
                    editorContent = monaco.editor.getModels()[0].getValue().split("\n");
                   myCondition1=editor.createContextKey('myCondition1',editorContent[(editor.getPosition().lineNumber)-1].trim().length==0?false:true);
                   myCondition2=editor.createContextKey('myCondition2', editorContent[(editor.getPosition().lineNumber)-1].trim().length>0?false:true);
                    });

               /* document.oncontextmenu = function() {return false;};

                  $(document).mousedown(function(e){ 
                    if( e.button == 2 ) { 
                        $('#right-menu-click').trigger('Click');
                        var editorContent=[];
                        editorContent = monaco.editor.getModels()[0].getValue().split("\n");
                   // alert(editor.getPosition().lineNumber);
                    myCondition1=editor.createContextKey('myCondition1',editorContent[(editor.getPosition().lineNumber)-1].trim().length==0?false:true);
                    myCondition2=editor.createContextKey('myCondition2', editorContent[(editor.getPosition().lineNumber)-1].trim().length>0?false:true);
                    myCondition3=editor.createContextKey('myCondition3', editorContent.indexOf((editorContent[(editor.getPosition().lineNumber)-1].trim()).substring(1,editorContent[(editor.getPosition().lineNumber)-1].trim().length-1))==-1?false:true);
                    } 
                  });*/
                editor.addAction({
                    id: 'newStatement',
                    label: 'Add Statement',
                    keybindings: [
                        monaco.KeyMod.CtrlCmd | monaco.KeyCode.F10,
                        monaco.KeyMod.chord(monaco.KeyMod.Alt | monaco.KeyCode.KEY_N)
                    ],
                    precondition: 'myCondition2',
                    keybindingContext: null,
                    contextMenuGroupId: 'navigation',
                    contextMenuOrder: 1.5,
                    run: function() {
                        var statuscheck=statusCheckInOut();
                        if(statuscheck){
                            return;
                        }
                        $('#addService').trigger('click');
                        return null;
                    }
                });
                editor.addAction({
                    id: 'modifyStatement',
                    label: 'Modify Statement',
                    keybindings: [
                        monaco.KeyMod.CtrlCmd | monaco.KeyCode.F10,
                        monaco.KeyMod.chord(monaco.KeyMod.Alt | monaco.KeyCode.KEY_M)
                    ],
                    precondition: 'myCondition1',
                    keybindingContext: null,
                    contextMenuGroupId: 'navigation',
                    contextMenuOrder: 1.5,
                    run: function() {
                        var statuscheck=statusCheckInOut();
                        if(statuscheck){
                            return;
                        }
                        //var x=myApp.addServiceReplacerValues();
                        $('#ModifyService').trigger('click');
                        return null;
                    }
                });
alexdima commented 4 years ago

Unfortunately this looks like you need help with programming in general, not with specific editor APIs. Please ask such questions to Stack Overflow. Thank you!

vscodebot[bot] commented 4 years ago

Please ask your question on StackOverflow. We have a great community over there. They have already answered hundreds of questions and are happy to answer yours as well. See also our issue reporting guidelines.

Happy Coding!