Microsoft Monaco Editor in browser get value of line

前端 未结 2 1559
耶瑟儿~
耶瑟儿~ 2021-02-09 23:28

I have been working through using the browser based version of the Microsoft Monaco Editor, I can\'t find any documentation or any examples in the playground that tell you how t

2条回答
  •  Happy的楠姐
    2021-02-10 00:06

    I think there is no such built-in feature in monaco as I do not found it. but i am doing that using following code:

    editor.addAction({
            id: 'some_id',
            label: 'label',
            keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_C],
            run: function(ed) {
                var position = ed.getPosition();
                var text = ed.getValue(position);
                var splitedText=text.split("\n");
                var line = splitedText[position.lineNumber-1];
    
                // now you have current line
                // you can also get any other line
                // and do something with that line
    
                splitedText[position.lineNumber-1]= _newLineText_
                ed.setValue(splitedText.join("\n"));
                ed.setPosition(position); // to return the pointer to the a position before editing text
    
                return null;
            },
            enablement: {
                textFocus: true,
            }
        });
    

    this method is good for small file but for large file the whole editor will re highlights and that a bad thing.

提交回复
热议问题