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

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

Overview

Builder class for configuring CKEditor5 toolbar items.

Examples:

Basic toolbar configuration

toolbar = ToolbarBuilder.new([:bold, :italic])
toolbar.append(:link)
toolbar.prepend(:heading)

Direct Known Subclasses

ToolbarGroupItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items) ⇒ ToolbarBuilder

Initialize a new toolbar builder with given items.

Examples:

Create new toolbar

ToolbarBuilder.new([:bold, :italic, :|, :link])

Parameters:

  • items (Array<Symbol>)

    Initial toolbar items



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

def initialize(items)
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



11
12
13
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 11

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



94
95
96
97
98
99
100
101
102
103
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 94

def append(*appended_items, after: nil)
  if after
    index = find_item_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

#break_lineSymbol

Returns toolbar line break symbol

Examples:

Add line break to toolbar

toolbar do
  append :bold, break_line, :italic
end

Returns:

  • (Symbol)

    Line break symbol (-)



29
30
31
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 29

def break_line
  :-
end

#find_group(name) ⇒ ToolbarGroupItem?

Find group by name in toolbar items

Parameters:

  • name (Symbol)

    Group name to find

Returns:



109
110
111
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 109

def find_group(name)
  items.find { |item| item.is_a?(ToolbarGroupItem) && item.name == name }
end

#group(name, **options, &block) ⇒ ToolbarGroupItem

Create and add new group to toolbar

Parameters:

  • name (Symbol)

    Group name

  • options (Hash)

    Group options (label:, icons:)

  • block (Proc)

    Configuration block

Returns:



126
127
128
129
130
131
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 126

def group(name, **options, &block)
  group = ToolbarGroupItem.new(name, [], **options)
  group.instance_eval(&block) if block_given?
  items << group
  group
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



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

def prepend(*prepended_items, before: nil)
  if before
    index = find_item_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



51
52
53
54
55
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 51

def remove(*removed_items)
  items.delete_if do |existing_item|
    removed_items.any? { |item_to_remove| item_matches?(existing_item, item_to_remove) }
  end
end

#remove_group(name) ⇒ Object

Remove group by name from toolbar items

Parameters:

  • name (Symbol)

    Group name to remove



116
117
118
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 116

def remove_group(name)
  items.delete_if { |item| item.is_a?(ToolbarGroupItem) && item.name == name }
end

#separatorSymbol

Returns toolbar separator symbol

Examples:

Add separator to toolbar

toolbar do
  append :bold, separator, :italic
end

Returns:

  • (Symbol)

    Separator symbol (|)



40
41
42
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 40

def separator
  :|
end