【问题标题】:Creating a custom Rails tinyMCE plugin: editor.ui is undefined创建自定义 Rails tinyMCE 插件:editor.ui 未定义
【发布时间】:2019-05-01 21:24:28
【问题描述】:

我想将自定义插件添加到支持 rails 的 tinyMCE 编辑器,但是在控制台中我得到 editor.ui is undefined 并且它失败了

如果我在 config.js 文件中注释掉与 editor.ui 相关的任何代码,那么编辑器会加载并使用“帮助”插件,我可以看到我的插件从 getMetadata 函数加载正常

/config/tinymce.yml

toolbar:
    - bold italic underline superscript subscript underline | link | alignleft aligncenter alignright | bullist numlist | hr | forecolor | backcolor | table | myplugin | image | help
plugins:
  - link table lists textcolor myplugin image imagetools help

app/assets/javascripts/tinymce/plugins/myplugin/plugin.js (这只是直接从https://www.tiny.cloud/docs/advanced/creating-a-plugin/ 将“示例”更改为“myplugin”)

$(document).on("ready", function() {
  tinymce.PluginManager.add("myplugin", function(editor, url) {
    var openDialog = function () {
      return editor.windowManager.open({
        title: "Example plugin",
        body: {
          type: "panel",
          items: [
            {
              type: "input",
              name: "title",
              label: "Title"
            }
          ]
        },
        buttons: [
          {
            type: "cancel",
            text: "Close"
          },
          {
            type: "submit",
            text: "Save",
            primary: true
          }
        ],
        onSubmit: function (api) {
          var data = api.getData();
          // Insert content when the window form is submitted
          editor.insertContent("Title: " + data.title);
          api.close();
        }
      });
    };

    // Add a button that opens a window
    editor.ui.registry.addButton("myplugin", {
      text: "My button",
      onAction: function () {
        // Open window
        openDialog();
      }
    });

    // Adds a menu item, which can then be included in any menu via the menu/menubar configuration
    editor.ui.registry.addMenuItem("myplugin", {
      text: "Example plugin",
      onAction: function() {
        // Open window
        openDialog();
      }
    });

    return {
      getMetadata: function () {
        return  {
          name: "Example plugin",
          url: "http://exampleplugindocsurl.com"
        };
      }
    };
  });
});

【问题讨论】:

    标签: ruby-on-rails tinymce


    【解决方案1】:

    您使用的是哪个版本的 TinyMCE?此代码在我们的小提琴网站上运行良好: http://fiddle.tinymce.com/PGgaab

    【讨论】:

    • 是的,谢谢。我用的是v4。我不认为 V5 API 会有如此大的不同——这将教会我正确阅读文档!对于可能遇到此问题的其他任何人 - 我必须通过将此行添加到我的 gemfile gem 'tinymce-rails', '>= 5.0.4' 然后运行 ​​bundle install 来将 tinymce-rails gem 显式更新到 5.0.4。使用bundle update tinymce-rails 以正常方式更新 gem 只更新到 v4.9.0
    • 我们试图保持核心 API 不变,但是 UI 完全重写了。
    【解决方案2】:

    如果没有看到您正在使用的完整代码,很难确定,但在 TinyMCE 本身初始化之前,jQuery 中的文档准备就绪很可能会触发。如果是这种情况,您不能在 TinyMCE 初始化之前使用 TinyMCE API 添加插件。

    正如该文档页面所建议的那样,我会将插件代码放在一个文件中并像所有其他 TinyMCE 插件一样加载它。 TinyMCE 将在其初始化过程中的正确时间获取插件,这将不再是问题。

    【讨论】:

    • 如果 TinyMCE 脚本没有加载,tinymce.PluginManager 也将是未定义的 ?
    • 看起来你从 Spyder 得到了答案。顺便说一句,可以加载 TinyMCE 脚本,因此存在 TinyMCE 全局对象,但如果尚未初始化 TinyMCE,则尝试使用 editor(或 activeEditor)之类的东西可能会失败。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多