Ace Editor API: How to select current line onCursorChange?

余生颓废 提交于 2019-12-11 19:27:27

问题


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();


来源:https://stackoverflow.com/questions/30455842/ace-editor-api-how-to-select-current-line-oncursorchange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!