简体   繁体   English

WP - JS - 获取Tinymce编辑器的实例

[英]WP - JS - Get Instance of Tinymce Editor

I'm creating a Wordpress plugin, which adds a metabox right under the post editor containing a button. 我正在创建一个Wordpress插件,它在包含按钮的帖子编辑器下添加了一个元数据metabox The plugin also loads a Javascript file right below the closing </body> tag. 该插件还会在结束</body>标记的正下方加载一个Javascript文件。

PURPOSE 目的

At the moment, what I am trying to achieve with the plugin is simple. 目前,我想用插件实现的目标很简单。 When a user enters content to the editor and then clicks the button inside the metabox, I want to modify the editor's content. 当用户向编辑器输入内容然后单击元框内的按钮时,我想修改编辑器的内容。

JS CODE JS代码

In its simplest form: 最简单的形式:

jQuery(document).ready(function($) {
   $('#button').on('click', function(e) {
      e.preventDefault();

      var editor = tinyMCE.get("content");
      editor.setContent(some_content);
   });
});

PROBLEM 问题

The problem is that editor variable returns undefined . 问题是editor变量返回undefined

FIREBUG (when trying to set var editor ) FIREBUG (尝试设置var editor

wpActiveEditor : "content" wpActiveEditor :“内容”
editors : [ ] editors :[]
activeEditor : null activeEditor :null

WHAT HAVE I TRIED 我做了什么

I have tried many, many things (also many small tweaks) found on Tinymce's documentation and here on Stackoverflow but the problem still remains the same. 我在Tinymce的文档和Stackoverflow上找到了很多很多东西(也有很多小的调整),但问题仍然存在。

Any help would be very appreciated. 任何帮助将非常感激。

PS. PS。 The content textarea is visible when running my tests. 运行我的测试时,内容textarea是可见的。

When the Editor first loads with the "Text" mode active, tinymce does not get initialized, therefore you cannot use tinyMCE.get() , as opposed to the "Visual" mode. 当编辑器首次加载“文本”模式激活时,tinymce不会被初始化,因此您不能使用tinyMCE.get() ,而不是“Visual”模式。

(I hadn't noticed that it actually works on the "Visual" mode, as I was keep testing it on the "Text" mode) (我没有注意到它实际上在“可视”模式下工作,因为我一直在“文本”模式下测试它)

So, a conditional statement is necessary to determine first which tab is active. 因此,必须使用条件语句来确定哪个选项卡处于活动状态。 I solved my problem with this method: 我用这个方法解决了我的问题:

function setEditorContent(val) {
  var $_editorTextArea = $('#content');
  $_editorTextArea.is(':visible') ? $_editorTextArea.val(val) : tinyMCE.get('content').setContent(val);
}

Hope this answer will prevent some headaches :) 希望这个答案可以防止一些头痛:)

Well, a live example would help a lot. 好吧,一个现实的例子会有很多帮助。 This way i can only guess: It looks a bit as if you cannot get the editor you want. 这种方式我只能猜测:它看起来有点像你无法得到你想要的编辑器。 There are two possible reasons that come into my mind: 我想到了两个可能的原因:

  1. The editor id you are using is not the id of your editor To verify this you check the id of your editors soure html element (in most cases a textarea).If there is no id set tinymce will use "content" as default. 您正在使用的编辑器ID不是编辑器的ID要验证这一点,请检查您的编辑器soure html元素的ID(在大多数情况下是textarea)。如果没有id设置,tinymce将使用“content”作为默认值。

  2. There iy no editor initialized at all To verify this you can use console.log(tinymce.editors) in your javascript console. 根本没有编辑器初始化要验证这一点你可以在你的javascript控制台中使用console.log(tinymce.editors)。 If no editor is initialized then you will get an empty array. 如果没有初始化编辑器,那么你将获得一个空数组。

Many years later but maybe this will help someone... 许多年后,但也许这将有助于某人......

In addition to everything said above some consideration needs to be paid to the JS event model. 除了上面提到的所有内容之外,还需要考虑JS事件模型。 Consider: 考虑:

  1. TinyMCE may not initialize (and the tinymce global may not be available) until the document is done loading. TinyMCE的可能不会初始化(与tinymce全球可能不可用),直到文件加载完成。 The OP correctly wrapped calls in jQuery(fn) , which will solve this. OP正确包装了jQuery(fn)调用,这将解决这个问题。 This is relevant if you're using an added framework that initializes and tries to manipulate the editors (like AngularJS directives). 如果您正在使用添加的框架初始化并尝试操作编辑器(如AngularJS指令),则这是相关的。
  2. Parts of initialization seem to be asynchronous so even if you wrap everything in jQuery(fn) the editors may not be available until later. 初始化的部分似乎是异步的,所以即使你在jQuery(fn)包装所有内容,编辑器可能在以后才可用。 WP loads Underscore as part of Backbone so wrapping initial attempts to locate editors in _.defer(fn) seems to get me lots of mileage. WP加载Underscore作为Backbone的一部分,所以包装初始尝试在_.defer(fn)找到编辑器似乎给了我很多里程。 This could be done with the native JS setTimeout as well. 这也可以使用本机JS setTimeout来完成。
  3. Beyond the fantastic answer by @m.spyratos, it may be helpful to note that you can hook mode change events (Visual/Text) by adding a jQuery click event handler to button.switch-tmce[data-wp-editor="my_editor_id"] and button.switch-html[data-wp-editor="my_editor_id"] for when the user selects Visual or Text, respectively. 除了@ m.spyratos的精彩答案之外,注意你可以通过向button.switch-tmce[data-wp-editor="my_editor_id"]添加jQuery click事件处理程序来挂钩模式更改事件(Visual / Text)可能会有所帮助button.switch-tmce[data-wp-editor="my_editor_id"]当用户分别选择“视觉”或“文本button.switch-html[data-wp-editor="my_editor_id"]时, button.switch-tmce[data-wp-editor="my_editor_id"]button.switch-html[data-wp-editor="my_editor_id"] Your version may vary but I found that the textarea goes away when switching to Visual mode and the tinymce.editor instance goes away when switching to Text mode. 您的版本可能会有所不同,但我发现切换到可视模式时textarea会消失,而当切换到文本模式时, tinymce.editor实例会消失。 Hooking to these events gives a persistent means to re-hook when the user decides to change modes. 当用户决定改变模式时,钩住这些事件提供了一种持久的方式来重新挂钩。
  4. As a quick reference, you can attach to the editor object ( activeEditor or something in editors[] , which is keyed by editor ID) to receive any and all changes in visual editor content with by hooking to the editor with editor.on('NodeChange keyup', fn) and a single event callback. 作为快速参考,可以附加到编辑器的对象( activeEditor或东西在editors[]其由编辑器ID键控)通过挂钩与编辑器来接收在可视化编辑的内容的任何和所有变化editor.on('NodeChange keyup', fn)和单个事件回调。 I included blur in my solution as well, for posterity. 对于后人,我在我的解决方案中也包含blur The text editor content can be hooked with jQuery('textarea#my_editor_id').on('keyup', fn) . 文本编辑器内容可以与jQuery('textarea#my_editor_id').on('keyup', fn)挂钩。

I have successfully managed multiple editors on a page that are entirely two-way bound entirely through JS; 我已经在一个完全通过JS完全双向约束的页面上成功管理了多个编辑器; the editors are created with wp_editor and no initial content then loaded asynchronously (via AJAX in my case) and managed through multiple edit cycles without a server round-trip. 编辑器是用wp_editor创建的,没有初始内容然后异步加载(在我的情况下通过AJAX)并通过多个编辑周期进行管理而无需服务器往返。 This is possible, if not slightly convoluted. 如果不是稍微复杂的话,这是可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM