Ace Editor API: How to select current line onCurso

2019-09-10 20:37发布

问题:

I want to always select the current line, when cursor position changes. And then remove that line with a keypress and append it to a div tag. I know how to add keybord commands. But i don't understand how to do something onCursorChange(), it seems to be different from Editor.on("change", function(Object e)). and also i don't find how remove the selected line. onCursorChange() is mentioned here but not really described how to use it: http://ace.c9.io/#nav=api&api=editor

    // ACE Editor Setup
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/crimson_editor");
    editor.getSession().setMode("ace/mode/html");
    editor.setValue("textline1\n textline2\n textline3");
 // editor.on('onCursorChange', function() { editor.selection.selectLine(); });  // does not work
 // editor.onCursorChange(editor.selection.selectLine()); // or this - does also not work

    editor.commands.addCommand({
           name: 'myCommand',
           bindKey: {win: 'Ctrl-J',  mac: 'Command-J'},
           exec: function(editor) {
           --- DO REMOVE THE SELECTED CONTENT --- 
           }
        });

UPDATE 14.06.2015: I solved it now without onCursorChange() event. Only by defining this keyboard action:

   editor.commands.addCommand({
       name: 'myCommand',
        bindKey: {win: 'Ctrl-Y',  mac: 'Command-Y'},
        exec: function(editor) {
            editor.selection.selectLine();
            myElem = editor.session.getTextRange(editor.getSelectionRange());
            $('#my_output_area').append(myElem);
            editor.removeLines();
       }
    });

回答1:

I think there is a bug on the editor.onCursorChange(); I used editor.on("changeSelection",function(){do whatever}); it work for me, good luck.



回答2:

To select the current line use scrollToLine.

editor.selection.selectLine();