Module: CKEditor5::Rails::Editor::Helpers::Config

Included in:
Editor, Helpers, Presets::PresetBuilder
Defined in:
lib/ckeditor5/rails/editor/helpers/config_helpers.rb

Instance Method Summary collapse

Instance Method Details

#ckeditor5_element_ref(selector) ⇒ Hash

Creates a reference to a DOM element that will be used by CKEditor’s features. This is particularly useful for features that need to check element presence or operate on specific DOM elements.

Examples:

Referencing an element in plugin configuration

configure :yourPlugin, {
  element: ckeditor5_element_ref("body")
}

Parameters:

  • selector (String)

    CSS selector for the target element

Returns:

  • (Hash)

    A hash with the element reference in CKEditor’s format



16
17
18
# File 'lib/ckeditor5/rails/editor/helpers/config_helpers.rb', line 16

def ckeditor5_element_ref(selector)
  { '$element': selector }
end

#ckeditor5_preset(name = nil) { ... } ⇒ PresetBuilder

Creates or retrieves a preset configuration for CKEditor. When called with a name, finds and returns an existing preset. When called with a block, creates a new preset with the given configuration.

Examples:

Finding an existing preset

@preset = ckeditor5_preset(:default)

Creating a custom preset in controller

@preset = ckeditor5_preset do
  version '43.3.1'
  toolbar :sourceEditing, :|, :bold, :italic
  plugins :Essentials, :Paragraph, :Bold, :Italic
end

Using preset in view

<%= ckeditor5_assets preset: @preset %>
<%= ckeditor5_editor %>

Overriding existing preset

@preset = ckeditor5_preset(:default).override do
  toolbar do
    remove :underline, :heading
  end
end

Parameters:

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

    The name of an existing preset to retrieve

Yields:

  • Block for configuring a new preset

Returns:

  • (PresetBuilder)

    The preset configuration object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
# File 'lib/ckeditor5/rails/editor/helpers/config_helpers.rb', line 48

def ckeditor5_preset(name = nil, &block)
  return CKEditor5::Rails::Engine.find_preset(name) if name

  raise ArgumentError, 'Configuration block is required for preset definition' unless block_given?

  CKEditor5::Rails::Presets::PresetBuilder.new(
    disallow_inline_plugins: true,
    &block
  )
end