Class: CKEditor5::Rails::Presets::ToolbarBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ ToolbarBuilder

Returns a new instance of ToolbarBuilder.



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

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

Instance Method Details

#append(*appended_items, after: nil) ⇒ Object

Append items to the editor toolbar.

Examples:

Append items to toolbar

toolbar do
  append :selectAll, :|, :selectAll, :selectAll
end

Insert items after specific item

toolbar do
  append :selectAll, after: :bold
end

Parameters:

  • appended_items (Array<Symbol>)

    Toolbar items to be appended

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

    Optional item after which to insert new items

Raises:

  • (ArgumentError)

    When the specified ‘after’ item is not found



59
60
61
62
63
64
65
66
67
68
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 59

def append(*appended_items, after: nil)
  if after
    index = items.index(after)
    raise ArgumentError, "Item '#{after}' not found in array" unless index

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

#prepend(*prepended_items, before: nil) ⇒ Object

Prepend items to the editor toolbar.

Examples:

Prepend items to toolbar

toolbar do
  prepend :selectAll, :|, :selectAll, :selectAll
end

Insert items before specific item

toolbar do
  prepend :selectAll, before: :bold
end

Parameters:

  • prepended_items (Array<Symbol>)

    Toolbar items to be prepended

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

    Optional item before which to insert new items

Raises:

  • (ArgumentError)

    When the specified ‘before’ item is not found



35
36
37
38
39
40
41
42
43
44
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 35

def prepend(*prepended_items, before: nil)
  if before
    index = items.index(before)
    raise ArgumentError, "Item '#{before}' not found in array" unless index

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

#remove(*removed_items) ⇒ Object

Remove items from the editor toolbar.

Examples:

Remove items from toolbar

toolbar do
  remove :underline, :heading
end

Parameters:

  • removed_items (Array<Symbol>)

    Toolbar items to be removed



18
19
20
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 18

def remove(*removed_items)
  removed_items.each { |item| items.delete(item) }
end