CodeMirror OnSelect事件不起作用



请参阅末尾的编辑


我刚开始在一个项目中使用CodeMirror,我面临着一个我根本不理解的问题。

问题是处理CodeMirror的事件,特别是"select"事件,因为我处理"change"事件没有问题,所以我无法猜测发生了什么。

让我们看看一些代码;我正在将一个CodeMirror对象包装到另一个对象中,它看起来像这样:

function expressionEditor(config)
{
    jQuery('#divEditor').append('<textarea id="expression"/>');
    this.editor = CodeMirror.fromTextArea(document.getElementById('expression'),
    {
      mode:        'custom',
      lineNumbers: true,
      extraKeys:   {'Ctrl-Space': 'autocomplete'}
    });
    this.OnChange = function(instance, object)
    {
      if (object.text[0] === '.')
      {
        CodeMirror.showHint(this.editor, CodeMirror.hint.custom);
      }
    };
    this.OnSelectHint = function(completion, Element)
    {
      alert(completion + ' : ' + Element);
    };
    CodeMirror.on(this.editor, 'change', jQuery.proxy(this.OnChange, this));
    CodeMirror.on(this.editor, 'select', jQuery.proxy(this.OnSelectHint, this));
}

正如我所说,事件"change"按预期工作,当我在编辑器中写入一个点时,它会调用调用提示列表的函数this.OnChange,但当我浏览建议的提示时,this.OnSelectHint函数永远不会被调用(导航器上不会弹出alert(。

根据关于"select"事件的CodeMirror文档,回调必须是"0">选择完成时激发。传递了完成值(字符串或对象(和在菜单中表示它的DOM节点"。

由于其他活动正在进行,我完全迷失了。。。有什么线索吗?


编辑

正如Marijn所说,CodeMirror中没有"select"事件;CCD_ 9事件来自CCD_。只是为了知道:

  • 如何处理CodeMirror加载项引发的事件

CodeMirror API中没有"select"事件。因此,听它绝对没有任何作用。

问题是show-hint中的"select"事件是在自动完成建议数组上触发的,而不是在CodeMirror编辑器本身上。

如上所述,您可以通过包装show-hint函数在该对象上设置事件处理程序。例如,使用sql-hint附加组件:

    const sqlHint = CodeMirror.hint.sql;
    CodeMirror.hint.sql = (cm, options) => {
      const result = sqlHint(cm, options);
      if (result) {
        CodeMirror.on(result, 'shown', onAutocompleteShown);
        CodeMirror.on(result, 'select', onAutocompleteSelect);
      }
      return result;
    }

这里onAutocompleteShownonAutocompleteSelect是事件处理程序。

最新更新