Module: CKEditor5::Rails::Presets::Concerns::PluginMethods

Extended by:
ActiveSupport::Concern
Included in:
Context::PresetBuilder, PresetBuilder
Defined in:
lib/ckeditor5/rails/presets/concerns/plugin_methods.rb

Defined Under Namespace

Classes: DisallowedInlinePluginError, MissingInlinePluginError, UnsupportedESModuleError

Instance Method Summary collapse

Instance Method Details

#external_plugin(name, **kwargs) ⇒ Object

Registers an external plugin loaded from a URL

Examples:

Load plugin from URL

external_plugin :MyPlugin, script: 'https://example.com/plugin.js'

Load with import alias

external_plugin :MyPlugin,
  script: 'https://example.com/plugin.js',
  import_as: 'Plugin'

Parameters:

  • name (Symbol)

    Plugin name

  • kwargs (Hash)

    Plugin options like :script, :import_as, :window_name, :stylesheets



30
31
32
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 30

def external_plugin(name, **kwargs)
  register_plugin(Editor::PropsExternalPlugin.new(name, **kwargs))
end

#inline_plugin(name, code) ⇒ Object

Registers an inline plugin with raw JavaScript code

Examples:

Define custom highlight plugin

inline_plugin :MyCustomPlugin, <<~JS
  const { Plugin } = await import( 'ckeditor5' );

  return class MyCustomPlugin extends Plugin {
    static get pluginName() {
      return 'MyCustomPlugin';
    }

    init() {
      // Plugin initialization code
    }
  }
JS

Parameters:

  • name (Symbol)

    Plugin name

  • code (String)

    JavaScript code defining the plugin



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 52

def inline_plugin(name, code)
  if code.match?(/export default/)
    raise UnsupportedESModuleError,
          'Inline plugins must not use ES module syntax!' \
          'Use async async imports instead!'
  end

  unless code.match?(/return class(\s+\w+)?\s+extends\s+Plugin/)
    raise MissingInlinePluginError,
          'Plugin code must return a class that extends Plugin!'
  end

  plugin = Editor::PropsInlinePlugin.new(name, code)
  plugin.compress! unless disallow_inline_plugin_compression

  register_plugin(plugin)
end

#plugin(name, **kwargs) ⇒ Object

Register a single plugin by name

Examples:

Register standard plugin

plugin :Bold

Register premium plugin

plugin :RealTimeCollaboration, premium: true

Register custom plugin

plugin :MyPlugin, import_name: 'my-custom-plugin'

Parameters:

  • name (Symbol, Editor::PropsBasePlugin)

    Plugin name or instance

  • kwargs (Hash)

    Plugin configuration options



80
81
82
83
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 80

def plugin(name, **kwargs)
  premium(true) if kwargs[:premium] && respond_to?(:premium)
  register_plugin(PluginsBuilder.create_plugin(name, **kwargs))
end

#plugins(*names, **kwargs, &block) ⇒ Object

Register multiple plugins and configure plugin settings

Examples:

Register multiple plugins

plugins :Bold, :Italic, :Underline

Configure plugins with block

plugins do
  remove :Heading
  append :SelectAll, :RemoveFormat
  prepend :SourceEditing
end

Parameters:

  • names (Array<Symbol>)

    Plugin names to register

  • kwargs (Hash)

    Shared plugin configuration



97
98
99
100
101
102
103
104
105
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 97

def plugins(*names, **kwargs, &block)
  config[:plugins] ||= []

  names.each { |name| plugin(name, **kwargs) } unless names.empty?

  builder = PluginsBuilder.new(config[:plugins])
  builder.instance_eval(&block) if block_given?
  builder
end