Class: CKEditor5::Rails::Presets::PluginsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ckeditor5/rails/presets/plugins_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugins) ⇒ PluginsBuilder

Returns a new instance of PluginsBuilder.



7
8
9
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 7

def initialize(plugins)
  @items = plugins
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



5
6
7
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 5

def items
  @items
end

Class Method Details

.create_plugin(name, **kwargs) ⇒ Editor::PropsBasePlugin

Creates a plugin instance from a name or returns the plugin if it’s already a PropsBasePlugin

Parameters:

  • name (Symbol, Editor::PropsBasePlugin)

    Plugin name or instance

  • kwargs (Hash)

    Additional plugin configuration

Returns:



16
17
18
19
20
21
22
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 16

def self.create_plugin(name, **kwargs)
  if name.is_a?(Editor::PropsBasePlugin)
    name
  else
    Editor::PropsPlugin.new(name, **kwargs)
  end
end

Instance Method Details

#append(*names, after: nil, **kwargs) ⇒ Object

Appends plugins to the end of the plugins list or after a specific plugin

Examples:

Append plugins to configuration

plugins do
  append :Bold, :Italic, after: :Link
end

Parameters:

  • names (Array<Symbol>)

    Names of plugins to append

  • after (Symbol, nil) (defaults to: nil)

    Optional plugin name after which to insert new plugins

  • kwargs (Hash)

    Additional plugin configuration

Raises:

  • (ArgumentError)

    When the specified ‘after’ plugin is not found



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 68

def append(*names, after: nil, **kwargs)
  new_plugins = names.map { |name| self.class.create_plugin(name, **kwargs) }

  if after
    index = items.index { |p| p.name == after }
    raise ArgumentError, "Plugin '#{after}' not found" unless index

    items.insert(index + 1, *new_plugins)
  else
    items.push(*new_plugins)
  end
end

#prepend(*names, before: nil, **kwargs) ⇒ Object

Prepends plugins to the beginning of the plugins list or before a specific plugin

Examples:

Prepend plugins to configuration

plugins do
  prepend :Bold, :Italic, before: :Link
end

Parameters:

  • names (Array<Symbol>)

    Names of plugins to prepend

  • before (Symbol, nil) (defaults to: nil)

    Optional plugin name before which to insert new plugins

  • kwargs (Hash)

    Additional plugin configuration

Raises:

  • (ArgumentError)

    When the specified ‘before’ plugin is not found



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 45

def prepend(*names, before: nil, **kwargs)
  new_plugins = names.map { |name| self.class.create_plugin(name, **kwargs) }

  if before
    index = items.index { |p| p.name == before }
    raise ArgumentError, "Plugin '#{before}' not found" unless index

    items.insert(index, *new_plugins)
  else
    items.insert(0, *new_plugins)
  end
end

#remove(*names) ⇒ Object

Removes specified plugins from the editor configuration

Examples:

Remove plugins from configuration

plugins do
  remove :Heading, :Link
end

Parameters:

  • names (Array<Symbol>)

    Names of plugins to remove



31
32
33
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 31

def remove(*names)
  names.each { |name| items.delete_if { |plugin| plugin.name == name } }
end