Module: Cdn::Helpers

Includes:
ActionView::Helpers::TagHelper, CKEditor5::Rails::Cdn::Concerns::BundleBuilder, CKEditor5::Rails::Cdn::Concerns::InlinePluginsTagsBuilder
Included in:
CKEditor5::Rails::Helpers
Defined in:
lib/ckeditor5/rails/cdn/helpers.rb

Defined Under Namespace

Classes: ImportmapAlreadyRenderedError

Instance Method Summary collapse

Methods included from CKEditor5::Rails::Cdn::Concerns::InlinePluginsTagsBuilder

#ckeditor5_inline_plugins_tags

Methods included from CKEditor5::Rails::Cdn::Concerns::BundleBuilder

#create_preset_bundle

Instance Method Details

#ckeditor5_assets(preset: :default, importmap: true, lazy: false, **kwargs) ⇒ Object

The ‘ckeditor5_assets` helper includes CKEditor 5 assets in your application. It’s responsible for generating the necessary JavaScript and CSS imports based on the specified preset and configuration.

Examples:

Basic usage with default preset

<%= ckeditor5_assets %>
<%= ckeditor5_editor %>

Simple editor with custom configuration

<%= ckeditor5_assets preset: :basic %>
<%= ckeditor5_editor toolbar: [:bold, :italic], plugins: [:Bold, :Italic] %>

Using custom preset with translations and language

<%= ckeditor5_assets preset: :custom, translations: [:pl, :es], language: :pl %>

Commercial usage with license key

<%= ckeditor5_assets license_key: 'your-license-key' %>

Using preset builder object

<% @preset = ckeditor5_preset do
  version '44.3.0'
  toolbar :bold, :italic
  plugins :Bold, :Italic
end %>
<%= ckeditor5_assets preset: @preset %>

Editor only configuration with different types

<%= ckeditor5_assets preset: :basic %>
<%= ckeditor5_editor type: :classic %>
<%= ckeditor5_editor type: :inline %>
<%= ckeditor5_editor type: :balloon %>

Parameters:

  • preset (Symbol, PresetBuilder) (defaults to: :default)

    The name of the preset to use (default: :default) or a PresetBuilder object created using ckeditor5_preset helper

  • kwargs (Hash)

    Additional configuration options:

    • version: Specify a custom CKEditor version

    • cdn: Select CDN provider (:jsdelivr, :unpkg, etc.)

    • translations: Array of language codes to include

    • ckbox: Configuration hash for CKBox integration

    • license_key: Commercial license key

    • premium: Enable premium features

    • language: Set editor UI language (e.g. :pl, :es)

    • lazy: Enable lazy loading of dependencies (slower but useful for async partials)

    • importmap: Whether to use importmap for dependencies (default: true)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ckeditor5/rails/cdn/helpers.rb', line 70

def ckeditor5_assets(
  preset: :default,
  importmap: true,
  lazy: false,
  **kwargs
)
  ensure_importmap_not_rendered!

  mapped_preset = merge_with_editor_preset(preset, **kwargs)
  bundle = create_preset_bundle(mapped_preset)

  @__ckeditor_context = {
    license_key: mapped_preset.license_key,
    bundle: bundle,
    preset: mapped_preset
  }

  build_assets_html_tags(bundle, mapped_preset, importmap: importmap, lazy: lazy)
end

#ckeditor5_lazy_javascript_tagsObject

Note:

Do not use this helper if ckeditor5_assets is already included on the page as it will cause duplicate imports.

Helper for dynamically loading CKEditor assets when working with Turbo/Stimulus. Adds importmap containing imports from all presets and includes only web component initialization code. Useful when dynamically adding editors to the page with unknown preset configuration.

Examples:

With Turbo/Stimulus dynamic editor loading

<%= ckeditor5_lazy_javascript_tags %>


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ckeditor5/rails/cdn/helpers.rb', line 101

def ckeditor5_lazy_javascript_tags
  ensure_importmap_not_rendered!

  tags = [
    Assets::WebComponentBundle.instance.to_html(nonce: content_security_policy_nonce),
    ckeditor5_inline_plugins_tags
  ]

  if importmap_available?
    @__ckeditor_context = {
      bundle: combined_bundle
    }
  else
    tags.prepend(
      Assets::AssetsImportMap.new(combined_bundle).to_html(nonce: content_security_policy_nonce)
    )
  end

  safe_join(tags)
end