Class: CKEditor5::Rails::Presets::ToolbarBuilder
- Inherits:
-
Object
- Object
- CKEditor5::Rails::Presets::ToolbarBuilder
- Defined in:
- lib/ckeditor5/rails/presets/toolbar_builder.rb
Overview
Builder class for configuring CKEditor5 toolbar items.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
-
#append(*appended_items, after: nil) ⇒ Object
Append items to the editor toolbar.
-
#break_line ⇒ Symbol
Returns toolbar line break symbol.
-
#find_group(name) ⇒ ToolbarGroupItem?
Find group by name in toolbar items.
-
#group(name, **options, &block) ⇒ ToolbarGroupItem
Create and add new group to toolbar.
-
#initialize(items) ⇒ ToolbarBuilder
constructor
Initialize a new toolbar builder with given items.
-
#prepend(*prepended_items, before: nil) ⇒ Object
Prepend items to the editor toolbar.
-
#remove(*removed_items) ⇒ Object
Remove items from the editor toolbar.
-
#remove_group(name) ⇒ Object
Remove group by name from toolbar items.
-
#separator ⇒ Symbol
Returns toolbar separator symbol.
Constructor Details
#initialize(items) ⇒ ToolbarBuilder
Initialize a new toolbar builder with given items.
18 19 20 |
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 18 def initialize(items) @items = items end |
Instance Attribute Details
#items ⇒ Object (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.
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_line ⇒ Symbol
Returns toolbar 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
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
126 127 128 129 130 131 |
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 126 def group(name, **, &block) group = ToolbarGroupItem.new(name, [], **) group.instance_eval(&block) if block_given? items << group group end |
#prepend(*prepended_items, before: nil) ⇒ Object
Prepend items to the editor toolbar.
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.
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
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 |
#separator ⇒ Symbol
Returns toolbar separator symbol
40 41 42 |
# File 'lib/ckeditor5/rails/presets/toolbar_builder.rb', line 40 def separator :| end |