【问题标题】:How to disable in ace editor the selection如何在 ace 编辑器中禁用选择
【发布时间】:2014-07-15 11:50:31
【问题描述】:

有谁知道如何禁用 ace.editor 的选择?没有鼠标和手指的单词选择。

我尝试使用editor.selection(false);

但这不起作用。

【问题讨论】:

  • 另外,ace github 中的this issue 似乎表明您不能这样做,但他们有解决方法。

标签: javascript ace-editor


【解决方案1】:
editor.getSession().selection.on('changeSelection', function (e)
{
    editor.getSession().selection.clearSelection();
});

【讨论】:

  • @Unheilig 我无法确切解释原因,因为我三天前刚开始使用 ace,我不知道内部原理,但我可以确认它对我有用。我的猜测是,如果您尝试选择一个字符,它会立即取消选择。
【解决方案2】:

您可以通过将编辑器设置为只读然后覆盖主题中的选择样式来有效地防止选择:

.noselection .ace_marker-layer .ace_selection {
   background: transparent;
}

.noselection .ace_cursor {
   color: transparent;
}

然后只需将 noselection 类添加到您的 ace 容器 div 中。

这实际上不会阻止用户选择任何东西,但不会有视觉指示表明它正在发生;就所有意图和目的而言,这几乎是一回事。

这里有更多详细信息:

https://github.com/ajaxorg/ace/wiki/Creating-or-Extending-an-Edit-Mode

还有这个办法:

https://github.com/ajaxorg/ace/issues/266

【讨论】:

    【解决方案3】:

    editor.setValue(str, -1) // 将光标移到开头

    editor.setValue(str, 1) // 将光标移动到末尾

    【讨论】:

    • 抱歉,但这不会禁用选择。
    • 隐藏光标和行高亮:editor.setOptions({ readOnly: true, highlightActiveLine: false, highlightGutterLine: false }) editor.renderer.$cursorLayer.element.style.opacity=0 制作编辑器non tabbable editor.textInput.getElement().tabIndex=-1 或 editor.textInput.getElement().disabled=true 禁用所有快捷方式 editor.commands.commmandKeyBinding={}
    • 谢谢,但不管是什么原因,这些命令对我不起作用。
    【解决方案4】:

    您可以使用以下代码禁用 ace 编辑器:

    editor.setReadOnly(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-04
      • 2014-12-13
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-22
      相关资源
      最近更新 更多