javascript - 带 TinyMCE 文本区域的 Ace 编辑器

标签 javascript jquery html tinymce ace-editor

我正在尝试在我的应用程序中扩展“代码 View ”和“设计 View ”。我可以毫无问题地使用代码 View (Ace Editor)或设计 View (tinymce)。不过,我希望两者能够一起工作,并在开发时更新双方的代码。这将使我只需要发布一个标签而不是发布两个标签,并且可能会出现问题。

我创建了这个fiddle显示我遇到的问题。

在我的 fiddle 中,我显示了一个tinymce文本区域、一个常规文本区域和一个ace编辑器文本区域。 tinymce 和常规文本区域共享相同的名称,并且 Ace 编辑器会在我打字时调用它来更新。您可以看到常规文本区域工作得很好,但是,tinymce 文本区域则不然。

我相信问题可能来自于 TinyMCE 使用 iFrame 并在幕后更新文本区域,但我不确定在 javascript 中调用该 iframe 的最佳方式。

最佳答案

我尝试同步两者,但由于tinyMCE上缺少一些回调/事件,这是我得到的最好的。 ACE 中的内容仅在tinyMCE 上模糊后更新。目前没有直接输入事件:

Fiddle forked

代码:

var editor = ace.edit("edit");
var textarea = $('textarea[name="test"]');
editor.getSession().setValue(textarea.val());
editor.getSession().on('change', function(){
    textarea.val(editor.getSession().getValue());

    // copy ace to tinyMCE
    tinymce.get('test').setContent(editor.getSession().getValue());

});
editor.setTheme("https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.2/theme-terminal.js");
editor.getSession().setMode("https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.2/mode-html.js");

tinymce.init({
    selector: ".test",
    relative_urls: false,
    apply_source_formatting : true,

    setup : function(ed) {
        ed.on('change', function(e) {
              // copy tinyMCE to textarea
              textarea.val(tinymce.activeEditor.getContent({format : 'raw'}));

             // copy tinyMCE to ace
            editor.getSession().setValue(
                tinymce.activeEditor.getContent({format : 'raw'})
            );
        });
    }

});

关于javascript - 带 TinyMCE 文本区域的 Ace 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491401/

相关文章:

html - CSS 下拉菜单相对于表格中的 TD

javascript - 表单提交后返回元素类

c# - 将变量从 Controller 传递到 Devexpress GridView 列 EndCallBack JS

javascript - 充满事件处理程序的巨大文件!如何在 AJAX Web 应用程序中组织大型 jQuery 文件?

php - JQuery .ajax() 关于用户名验证

html - 捕获 div focusout

javascript - Chrome 扩展程序 - 页面更新两次然后在 YouTube 上删除

javascript - Array.sort().filter(...) 在 Javascript 中为零

javascript - 将 'immediately invoking' 函数作为对象属性处理

javascript - 在 $.get 和 $.post 处理程序中全局捕获 javascript 错误