在使用rails_admin时添加插件到ckeditor

Adding plugins to ckeditor while using rails_admin

本文关键字:添加 插件 ckeditor admin rails      更新时间:2023-09-26

我已经为这件事绞尽脑汁好几个小时了。也许有人能帮我。

我有一个rails应用程序。我使用rails_admin gem,版本0.6.2。

按照rails_admin的指示,我在几个文本字段上启用了ckeditor,它工作得很好。

接下来我需要自定义一些工具栏图标,所以我在app/assets/javascripts/ckeditor/

中创建了一个自定义的config.js文件

这工作得很好,我可以改变工具栏按钮(config.js的示例代码如下)

CKEDITOR.config.toolbar_Custom = [
  { name: 'document',    items : [ 'Source','NewPage','Preview','-','Templates' ] }
  // other toolbars removed for brevity
];
CKEDITOR.config.toolbar = 'Custom';

现在我想添加一些ckeditor插件-特别是codesnippet插件。

我把插件文件放在app/assets/javascripts/ckeditor/plugins中,并包括它们如下:

CKEDITOR.config.extraPlugins = 'codesnippet'

现在ckeditor不会加载,控制台抱怨 ckeditor。编辑器CKEDITOR。style是未定义的。这些错误来自插件js文件,所以我知道他们正在加载,但他们似乎在ckeditor有时间初始化之前被引用?

我试过把我的配置设置打包成

之类的东西
CKEDITOR.on('instanceReady', function(){
    // initialize config stuff here
});

但这没有任何作用。

认为问题涉及rails_admin的这个coffeescript文件的第159行,它正在设置编辑器并应用设置。但无论如何,我迷失了方向,这些文件只会让我感到困惑。

将以下内容添加到config.js文件中,该文件将位于app/assets/javascripts/ckeditor/

CKEDITOR.editorConfig = function (config) {
  config.extraPlugins = 'widget,dialog,codesnippet,widgetselection,lineutils';
}

从以下链接下载插件(zip格式)。提取它们并放在app/assets/javascripts/ckeditor/plugins,这是代码片段所需的。

  1. https://ckeditor.com/cke4/addon/widgetselection
  2. https://ckeditor.com/cke4/addon/lineutils
  3. https://ckeditor.com/cke4/addon/dialog
  4. https://ckeditor.com/cke4/addon/widget
  5. https://ckeditor.com/cke4/addon/codesnippet

将下面一行添加到assets.rb .

Rails.application.config.assets.precompile += %w( ckeditor/* )

为rails_admin启用它,尝试下面的任何特定模型。

config.model 'Problem' do
    # Your code here
    edit do
      field :code_snippet, :ck_editor
      # Rest columns
    end
  end