【问题标题】:Ace Editor API: How to select line 2?Ace Editor API:如何选择第 2 行?
【发布时间】:2015-05-26 01:35:33
【问题描述】:

我想在 ACE 中选择第 2 行进行复制和粘贴。有一个方法selectLine(),记录在这里:http://ace.c9.io/#nav=api&api=selection,但我不明白如何使用它。不幸的是,在 stackoverflow.com 上也找不到关于选择的内容,只有关于突出显示的内容,这是不一样的。

// 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");

var select = new Selection(editor.getSession());    // Uncaught TypeError: Illegal constructor
select.selectLine(2);

【问题讨论】:

    标签: javascript ace-editor


    【解决方案1】:

    一旦 ace 被初始化,它就会创建一个 Selection 对象实例,因此您不需要重新创建它。要访问Selection,只需使用editor.selection

    另一个重要的一点是selectLine 选择当前行(它不接受任何参数)。所以要移动光标并选择你必须首先使用moveCursorToPosition函数的行。

    这是一个例子:

    editor.selection.moveCursorToPosition({row: 1, column: 0});
    editor.selection.selectLine();
    

    【讨论】:

    • 好的,谢谢你的帮助。我真正想做的是:当光标改变时,总是选择当前行。并用按键删除该行。我知道如何添加键盘命令。但是我不知道该怎么做onCursorChange(),它似乎与Editor.on("change", function(Object e))不同。而且我不知道如何删除选定的行。
    • 我也尝试过editor.onCursorChange(editor.selection.selectLine()); 之类的东西,但我想这样的东西不能消耗事件。它甚至不起作用。
    • 我为此发布了一个新问题:stackoverflow.com/questions/30455842/…
    • @Lutz 如果这有帮助,请选择它作为答案
    猜你喜欢
    • 1970-01-01
    • 2013-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多