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) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 11

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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 36

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



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ckeditor5/rails/presets/plugins_builder.rb', line 23

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



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

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