使用 tinymce 在 HTML 预览和纯文本之间切换

Toggle between HTML preview and plain text with tinymce

在使用 Wordpress 的 tinymce 编辑器时,我注意到他们能够让用户在富文本模式下编辑,他们可以在其中查看所有格式,以及切换回纯文本模式,用户可以在其中进行编辑可以编辑 html/script/css 或任何其他代码。

我是tinymce的新手,不是很了解。我能够使用默认 javascript 和这个:

让 tinymce 工作
tinymce.init({ selector:'#id_content' });

谷歌搜索时,我首先发现的是:

http://archive.tinymce.com/tryit/3_x/toggle_editor.php

这正是我想要的,但没有说明如何在任何地方做到这一点。该示例中有太多选项和插件,我不知道发生了什么。查看源代码,单独的源代码甚至无法实现功能。有一个 "fiddle with this example" ( http://fiddle.tinymce.com/paaaab ) 但即使 fiddle 也不起作用。

经过更多谷歌搜索,我找到了这个 question/answer:Plain and Rich text editor options in TinyMce editor

这个问题似乎问的和我问的是一样的(这个人在他发布问题前 6 天)但是接受的答案现在已经过时并且没有多大意义。我尝试了第二个答案,没有任何改变的确切代码,但它根本不起作用。

如果有人可以用这个 fiddle 工作,或者告诉我为什么我的努力没有奏效,我会欣喜若狂!

您链接到的 Fiddle 没有工作,因为它正在调用最新的 TinyMCE javascript 库。我将其更改为使用 TinyMCE v.3.5.11:

http://fiddle.tinymce.com/Cnfaab

切换格式现在可用。

<a class="btn" href="javascript:;" onclick="tinymce.execCommand('mceToggleEditor',false,'content');"><span>Toggle TinyMCE</span></a>

在 TinyMCE4 中:

不确定这是否是正确的方法,但我相信这可以满足您的要求: http://codepen.io/anon/pen/ZQXLBo

JS:

  tinymce.init({
  selector: 'textarea',
  height: 500,
  toolbar: 'mybutton',
  menubar: false,
  setup: function(editor) {
    editor.addButton('mybutton', {
      text: 'My button',
      icon: false,
      onclick: function(e) {
        var $ = tinymce.dom.DomQuery;
        var myTextarea = $('textarea');
        var myIframe = $(editor.iframeElement);
        myTextarea.value = editor.getContent({
          source_view: true
        });
        myIframe.toggleClass("hidden");
        myTextarea.toggleClass("visible");
        if ($('iframe.hidden').length > 0) {
          myTextarea.prependTo(".mce-edit-area");
        } else {
          myIframe.value = myTextarea.value;
          myTextarea.appendTo('body');
        }
      }
    });
  },
  content_css: [
    '//fast.fonts.net/cssapi/e6dc9b99-64fe-4292-ad98-6974f93cd2a2.css',
    '//www.tinymce.com/css/codepen.min.css'
  ]
});

HTML:

<textarea>
      <p style="text-align: center; font-size: 15px;"><img title="TinyMCE Logo" src="//www.tinymce.com/images/glyph-tinymce@2x.png" alt="TinyMCE Logo" width="110" height="97" />
      </p>
      <h1 style="text-align: center;">Welcome to the TinyMCE editor demo!</h1>
      <p>Select a menu item from the listbox above and it will insert contents into the editor at the caret position.</p>

      <h2>Got questions or need help?</h2>
      <ul>
        <li>Our <a href="http://www.tinymce.com/docs/">documentation</a> is a great resource for learning how to configure TinyMCE.</li>
        <li>Have a specific question? Visit the <a href="http://community.tinymce.com/forum/">Community Forum</a>.</li>
        <li>We also offer enterprise grade support as part of <a href="http://www.tinymce.com/pricing">TinyMCE Enterprise</a>.</li>
      </ul>

      <h2>Found a bug?</h2>
      <p>If you think you have found a bug please create an issue on the <a href="https://github.com/tinymce/tinymce/issues">GitHub repo</a> to report it to the developers.</p>

      <h2>Finally ...</h2>
      <p>Don't forget to check out our other product <a href="http://www.plupload.com" target="_blank">Plupload</a>, your ultimate upload solution featuring HTML5 upload support.</p>
      <p>Thanks for supporting TinyMCE! We hope it helps you and your users create great content.
        <br>All the best from the TinyMCE team.</p>
    </textarea>

CSS:

    textarea {
  height: 500px !important;
  width: 100% !important;
  position: absolute;
  z-index: 100;
}

.hidden {
  visibility: hidden !important;
  display: none !important;
}

.visible {
  visibility: visible !important;
  display: block !important;
}

这适用于 Tinymce 4。使用设置配置参数

setup: function(ed)
{
  ed.on('click', function(e){
    tinymce.execCommand('mceToggleEditor', false, 'your editor_id');
  });
}

在编辑器中单击它会切换。 这是一个工作的 tinymce fiddle: http://fiddle.tinymce.com/Fnfaab

如果您特别希望使用丰富的代码编辑器寻找类似 WordPress 的代码切换功能,那么您可能需要查看我创建的 TinyMCE Ace code editor 插件。它创建了一个可以放置在任何工具栏中的切换按钮,并使用 Ace 编辑器(如果可用)(或文本区域作为后备)