【问题标题】:How to change the mode dynamically in codemirror on encounter of a particular expression?如何在遇到特定表达式时在 codemirror 中动态更改模式?
【发布时间】:2014-07-29 11:18:12
【问题描述】:

在 textarea 中,只要遇到 '

function determineCodeMirrorType(cm) {
  if (cm.getOption('mode') == 'text/ftl') {
    checkAndSwitchToHTML(cm, cm.getValue());
  } else if (cm.getOption('mode') == 'text/html') {
    checkAndSwitchToFTL(cm, cm.getValue());
  }
}

function checkAndSwitchToHTML(cm, val) {
  if (/^\s*</.test(val)) {
    cm.setOption("mode", "text/html");
  }
}

function checkAndSwitchToFTL(cm, val) {
  if (/[<#|<@|$]/.test(val)) {
    cm.setOption("mode", "text/ftl");
  }
}

function buildCMInstance(mode, value) {
  var cm = CodeMirror.fromTextArea(document.getElementById("code"), {
    mode:mode,
    value:value,
    lineNumbers:true,
    onChange:function(cmInstance){ 
      determineCodeMirrorType(cmInstance); //The call to this function is not made.
    })
  });
  return cm;
}

$(document).ready(function(){
  var cm = buildCMInstance("text/ftl")
});

我想知道是否有任何可以通过调用函数“determineCodeMirrorType”来允许代码动态更改的选项。

【问题讨论】:

    标签: html text-editor freemarker codemirror


    【解决方案1】:

    onChange 在最近的 CodeMirror 版本中不存在。您必须通过调用cm.on("change", function(cm, change) { .... }) 来注册事件处理程序。

    【讨论】:

    • 感谢马林的回复。我使用了上述方法,但是在使用 change 事件时,文本编辑器的状态不断变化。您能否建议另一个事件,该事件允许两种模式同时运行,并且每当解析特定模式的适当令牌时,自动完成功能也可以工作。
    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2015-03-19
    • 2013-12-31
    相关资源
    最近更新 更多