class CKEditor5::Rails::Presets::ToolbarBuilder
toolbar.prepend(:heading)
toolbar.append(:link)
toolbar = ToolbarBuilder.new([:bold, :italic])
@example Basic toolbar configuration
Builder class for configuring CKEditor5 toolbar items.
def append(*appended_items, after: nil)
-
(ArgumentError)
- When the specified 'after' item is not found
Other tags:
- Example: Insert items after specific item -
Example: Append items to toolbar -
Parameters:
-
after
(Symbol, nil
) -- Optional item after which to insert new items -
appended_items
(Array
) -- Toolbar items to be appended
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
def break_line
- Example: Add line break to toolbar -
Returns:
-
(Symbol)
- Line break symbol (-)
def break_line :- end
def find_group(name)
-
(ToolbarGroupItem, nil)
- Found group or nil
Parameters:
-
name
(Symbol
) -- Group name to find
def find_group(name) items.find { |item| item.is_a?(ToolbarGroupItem) && item.name == name } end
def find_item_index(item)
-
(Integer, nil)
- Index of the found item or nil
Parameters:
-
item
(Symbol
) -- Item or group name to find
def find_item_index(item) items.find_index { |existing_item| item_matches?(existing_item, item) } end
def group(name, **options, &block)
-
(ToolbarGroupItem)
- Created group
Parameters:
-
block
(Proc
) -- Configuration block -
options
(Hash
) -- Group options (label:, icons:) -
name
(Symbol
) -- Group name
def group(name, **options, &block) group = ToolbarGroupItem.new(name, [], **options) group.instance_eval(&block) if block_given? items << group group end
def initialize(items)
- Example: Create new toolbar -
Parameters:
-
items
(Array
) -- Initial toolbar items
def initialize(items) @items = items end
def item_matches?(existing_item, item)
- Example: Check if items match -
Returns:
-
(Boolean)
- true if items match, false otherwise
Parameters:
-
item
(Symbol
) -- Item or group name to match against -
existing_item
(Symbol, ToolbarGroupItem
) -- Item to check
def item_matches?(existing_item, item) if existing_item.is_a?(ToolbarGroupItem) existing_item.name == item else existing_item == item end end
def prepend(*prepended_items, before: nil)
-
(ArgumentError)
- When the specified 'before' item is not found
Other tags:
- Example: Insert items before specific item -
Example: Prepend items to toolbar -
Parameters:
-
before
(Symbol, nil
) -- Optional item before which to insert new items -
prepended_items
(Array
) -- Toolbar items to be prepended
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
def remove(*removed_items)
- Example: Remove items from toolbar -
Parameters:
-
removed_items
(Array
) -- Toolbar items to be removed
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
def remove_group(name)
-
name
(Symbol
) -- Group name to remove
def remove_group(name) items.delete_if { |item| item.is_a?(ToolbarGroupItem) && item.name == name } end
def separator
- Example: Add separator to toolbar -
Returns:
-
(Symbol)
- Separator symbol (|)
def separator :| end