Class: CKEditor5::Rails::Presets::PresetBuilder

Inherits:
Object
  • Object
show all
Includes:
Editor::Helpers::Config, Concerns::ConfigurationMethods, Concerns::PluginMethods
Defined in:
lib/ckeditor5/rails/presets/preset_builder.rb

Instance Method Summary collapse

Methods included from Concerns::PluginMethods

#external_plugin, #inline_plugin, #patch_plugin, #plugin, #plugins

Methods included from Concerns::ConfigurationMethods

#configure

Methods included from Editor::Helpers::Config

#ckeditor5_element_ref, #ckeditor5_preset, #ckeditor5_translation_ref

Constructor Details

#initialize(&block) ⇒ PresetBuilder

Returns a new instance of PresetBuilder.

Examples:

Basic initialization

PresetBuilder.new do
  version '44.3.0'
  gpl
  type :classic
end


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 20

def initialize(&block)
  @version = nil
  @premium = false
  @cdn = :jsdelivr
  @translations = [:en]
  @license_key = nil
  @type = :classic
  @ckbox = nil
  @editable_height = nil
  @automatic_upgrades = false
  @custom_translations = {}
  @config = {
    plugins: [],
    toolbar: []
  }

  instance_eval(&block) if block_given?
end

Instance Method Details

#automatic_upgrades(enabled: true) ⇒ Object

Enable or disable automatic version upgrades

Examples:

Enable automatic upgrades

automatic_upgrades enabled: true

Parameters:

  • enabled (Boolean) (defaults to: true)

    Enable/disable upgrades



201
202
203
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 201

def automatic_upgrades(enabled: true)
  @automatic_upgrades = enabled
end

#automatic_upgrades?Boolean

Check if automatic upgrades are enabled

Returns:

  • (Boolean)


207
208
209
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 207

def automatic_upgrades?
  @automatic_upgrades
end

#balloon_toolbar(*items, **kwargs) { ... } ⇒ ToolbarBuilder

Configure balloon toolbar items and grouping

Examples:

Configure balloon toolbar items

balloon_toolbar :bold, :italic, :link

Configure with block

balloon_toolbar do
  append :textColor
  remove :italic
end

Parameters:

  • items (Array<Symbol>)

    Toolbar items to include

  • kwargs (Hash)

    Additional toolbar configuration options

Options Hash (**kwargs):

  • :should_group_when_full (Boolean)

    Enable/disable toolbar item grouping

Yields:

  • Optional block for additional toolbar configuration

Returns:



318
319
320
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 318

def balloon_toolbar(*items, **kwargs, &block)
  toolbar(*items, **kwargs, type: :balloonToolbar, &block)
end

#block_toolbar(*items, **kwargs) { ... } ⇒ ToolbarBuilder

Configure block toolbar items and grouping

Examples:

Configure block toolbar items

block_toolbar :heading, :paragraph, :blockQuote

Configure with block

block_toolbar do
  append :table
  remove :paragraph
end

Parameters:

  • items (Array<Symbol>)

    Toolbar items to include

  • kwargs (Hash)

    Additional toolbar configuration options

Options Hash (**kwargs):

  • :should_group_when_full (Boolean)

    Enable/disable toolbar item grouping

Yields:

  • Optional block for additional toolbar configuration

Returns:



301
302
303
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 301

def block_toolbar(*items, **kwargs, &block)
  toolbar(*items, **kwargs, type: :blockToolbar, &block)
end

#cdn(cdn = nil, &block) ⇒ Symbol, Proc

Configure CDN source

Examples:

Use jsDelivr CDN

cdn :jsdelivr

Custom CDN configuration

cdn do |bundle, version, path|
  "https://custom-cdn.com/#{bundle}@#{version}/#{path}"
end

Parameters:

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

    CDN name or custom block

Returns:

  • (Symbol, Proc)

    Current CDN configuration



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 220

def cdn(cdn = nil, &block)
  return @cdn if cdn.nil? && block.nil?

  if block_given?
    unless block.arity == 3
      raise ArgumentError,
            'Block must accept exactly 3 arguments: bundle, version, path'
    end

    @cdn = block
  else
    @cdn = cdn
  end
end

#ckbox(version = nil, theme: :lark) ⇒ Object

Configure CKBox integration

Examples:

Enable CKBox with custom version

ckbox '2.6.0', theme: :lark

Parameters:

  • version (String, nil) (defaults to: nil)

    CKBox version

  • theme (Symbol) (defaults to: :lark)

    Theme name (:lark)



123
124
125
126
127
128
129
130
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 123

def ckbox(version = nil, theme: :lark)
  return @ckbox if version.nil?

  @ckbox = {
    version: version,
    theme: theme
  }
end

#custom_translations(lang_code = nil, translations = {}) ⇒ void

This method returns an undefined value.

Configure custom translations for the editor

Examples:

Add multiple translations

custom_translations :en, {
  'my.button': 'My Button'
}
custom_translations :pl, {
  'my.button': 'Mój przycisk'
}

Use translations in configuration with ckeditor5_translation_ref

custom_translations :en, {
  'custom.heading': 'Custom Section'
}

configure :heading, {
  options: [
    { model: 'heading1', title: ckeditor5_translation_ref('custom.heading') }
  ]
}

Parameters:

  • lang_code (Symbol) (defaults to: nil)

    Language code for translations (e.g. :en, :pl)

  • translations (Hash) (defaults to: {})

    A hash containing translations in format { key => translation }



349
350
351
352
353
354
355
356
357
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 349

def custom_translations(lang_code = nil, translations = {})
  return @custom_translations if lang_code.blank?

  @custom_translations[lang_code.to_sym] ||= {}
  @custom_translations[lang_code.to_sym].merge!(translations)

  plugins.remove(:CustomTranslationsLoader)
  plugins.prepend(Plugins::CustomTranslationsLoader.new(@custom_translations))
end

#deconstruct_keys(keys) ⇒ Object



39
40
41
42
43
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 39

def deconstruct_keys(keys)
  keys.index_with do |key|
    public_send(key)
  end
end

#editable_height(height = nil) ⇒ Integer?

Set or get editable height in pixels

Examples:

Set editor height to 300px

editable_height 300

Parameters:

  • height (Integer, nil) (defaults to: nil)

    Height in pixels

Returns:

  • (Integer, nil)

    Current height value



112
113
114
115
116
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 112

def editable_height(height = nil)
  return @editable_height if height.nil?

  @editable_height = height
end

#gplObject

Set GPL license and disable premium features

Examples:

Enable GPL license

gpl


148
149
150
151
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 148

def gpl
  license_key('GPL')
  premium(false)
end

#gpl?Boolean

Check if preset is using GPL license

Returns:

  • (Boolean)


70
71
72
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 70

def gpl?
  license_key == 'GPL'
end

#initialize_copy(source) ⇒ Object

Examples:

Copy preset and modify it

original = PresetBuilder.new
copied = original.initialize_copy(original)


48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 48

def initialize_copy(source)
  super

  @translations = source.translations.dup
  @ckbox = source.ckbox.dup if source.ckbox
  @custom_translations = source.custom_translations.deep_dup
  @config = {
    plugins: source.config[:plugins].map(&:dup),
    toolbar: deep_copy_toolbar(source.config[:toolbar])
  }.merge(
    source.config.except(:plugins, :toolbar).deep_dup
  )
end

#language(ui = nil, content: ui) ⇒ Hash?

Configure editor language

Examples:

Set Polish UI and content language

language :pl

Different UI and content languages

language :pl, content: :en

Parameters:

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

    UI language code

  • content (Symbol) (defaults to: ui)

    Content language code

Returns:

  • (Hash, nil)

    Language configuration



367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 367

def language(ui = nil, content: ui) # rubocop:disable Naming/MethodParameterName
  return config[:language] if ui.nil?

  # Normalize language codes, as the translation packs used to be in lowercase
  ui = ui.to_sym.downcase
  content = content.to_sym.downcase

  @translations << ui unless @translations.map(&:to_sym).include?(ui)

  config[:language] = {
    ui: ui,
    content: content
  }
end

#language?Boolean

Check if language is configured

Returns:

  • (Boolean)


324
325
326
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 324

def language?
  config[:language].present?
end

#license_key(license_key = nil) ⇒ String?

Set or get license key

Examples:

Set commercial license

license_key 'your-license-key'

Parameters:

  • license_key (String, nil) (defaults to: nil)

    License key

Returns:

  • (String, nil)

    Current license key



137
138
139
140
141
142
143
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 137

def license_key(license_key = nil)
  return @license_key if license_key.nil?

  @license_key = license_key

  cdn(:cloud) unless gpl?
end

Configure menubar visibility

Examples:

Hide menubar

menubar visible: false

Parameters:

  • visible (Boolean) (defaults to: true)

    Show/hide menubar



252
253
254
255
256
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 252

def menubar(visible: true)
  config[:menuBar] = {
    isVisible: visible
  }
end

Check if menubar is visible

Returns:

  • (Boolean)


260
261
262
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 260

def menubar?
  config.dig(:menuBar, :isVisible) || false
end

#merge_with_hash!(**overrides) ⇒ self

Merge preset with configuration hash

Parameters:

  • overrides (Hash)

    Configuration options to merge

Returns:

  • (self)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 92

def merge_with_hash!(**overrides)
  @version = Semver.new(overrides[:version]) if overrides.key?(:version)
  @premium = overrides.fetch(:premium, premium)
  @cdn = overrides.fetch(:cdn, cdn)
  @translations = overrides.fetch(:translations, translations)
  @license_key = overrides.fetch(:license_key, license_key)
  @type = overrides.fetch(:type, type)
  @editable_height = overrides.fetch(:editable_height, editable_height)
  @automatic_upgrades = overrides.fetch(:automatic_upgrades, automatic_upgrades)
  @ckbox = overrides.fetch(:ckbox, ckbox) if overrides.key?(:ckbox) || ckbox
  @config = config.merge(overrides.fetch(:config, {}))

  self
end

#override(&block) ⇒ PresetBuilder

Create a new preset by overriding current configuration

Examples:

Override existing preset

preset.override do
  menubar visible: false
  toolbar do
    remove :underline, :heading
  end
end

Returns:



83
84
85
86
87
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 83

def override(&block)
  clone.tap do |preset|
    preset.instance_eval(&block)
  end
end

#premium(premium = nil) ⇒ Boolean

Enable or check premium features

Examples:

Enable premium features

premium true

Parameters:

  • premium (Boolean, nil) (defaults to: nil)

    Enable/disable premium features

Returns:

  • (Boolean)

    Premium status



158
159
160
161
162
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 158

def premium(premium = nil)
  return @premium if premium.nil?

  @premium = premium
end

#premium?Boolean

Check if preset is using premium features

Returns:

  • (Boolean)


64
65
66
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 64

def premium?
  @premium
end

#simple_upload_adapter(upload_url = '/uploads') ⇒ Object

Configure simple upload adapter

Examples:

Enable upload adapter

simple_upload_adapter '/uploads'

Parameters:

  • upload_url (String) (defaults to: '/uploads')

    Upload endpoint URL



386
387
388
389
390
391
392
393
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 386

def simple_upload_adapter(upload_url = '/uploads')
  plugins do
    remove(:Base64UploadAdapter)
  end

  plugin(Plugins::SimpleUploadAdapter.new)
  configure(:simpleUpload, { uploadUrl: upload_url })
end

#special_characters { ... } ⇒ Object

Configure special characters plugin

Examples:

Basic configuration with block

special_characters do
  group 'Emoji', label: 'Emoticons' do
    item 'smiley', '😊'
    item 'heart', '❤️'
  end
  order :Text, :Emoji
end

Configuration with direct items array

special_characters do
  group 'Arrows',
        items: [
          { title: 'right', character: '' },
          { title: 'left', character: '' }
        ]
end

Mixed configuration

special_characters do
  group 'Mixed',
        items: [{ title: 'star', character: '' }],
        label: 'Mixed Characters' do
    item 'heart', '❤️'
  end
end

Yields:

  • Block for configuring special characters



437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 437

def special_characters(&block)
  builder = SpecialCharactersBuilder.new
  builder.instance_eval(&block) if block_given?

  plugins do
    append(:SpecialCharacters)
    builder.packs_plugins.each { |pack| append(pack) }
    prepend(Plugins::SpecialCharactersBootstrap.new)
  end

  configure(:specialCharactersBootstrap, builder.to_h)
end

#toolbar(*items, should_group_when_full: true, type: :toolbar, &block) ⇒ ToolbarBuilder

Configure toolbar items and grouping

Examples:

Configure toolbar items

toolbar :bold, :italic, :|, :link

Configure with block

toolbar do
  append :selectAll
  remove :heading
end

Parameters:

  • items (Array<Symbol>)

    Toolbar items

  • should_group_when_full (Boolean) (defaults to: true)

    Enable grouping

Returns:



275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 275

def toolbar(*items, should_group_when_full: true, type: :toolbar, &block)
  if @config[type].blank? || !items.empty?
    @config[type] = {
      items: items,
      shouldNotGroupWhenFull: !should_group_when_full
    }
  end

  builder = ToolbarBuilder.new(@config[type][:items])
  builder.instance_eval(&block) if block_given?
  builder
end

#translations(*translations) ⇒ Array<Symbol>

Set or get translations

Examples:

Add Polish and Spanish translations

translations :pl, :es

Parameters:

  • translations (Array<Symbol>)

    Language codes

Returns:

  • (Array<Symbol>)

    Current translations



169
170
171
172
173
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 169

def translations(*translations)
  return @translations if translations.empty?

  @translations = translations.map { |t| t.to_sym.downcase }
end

#type(type = nil) ⇒ Symbol

Set or get editor type

Examples:

Set editor type to inline

type :inline

Parameters:

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

    Editor type (:classic, :inline, :balloon, :decoupled)

Returns:

  • (Symbol)

    Current editor type

Raises:

  • (ArgumentError)

    If invalid type provided



241
242
243
244
245
246
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 241

def type(type = nil)
  return @type if type.nil?
  raise ArgumentError, "Invalid editor type: #{type}" unless Editor::Props.valid_editor_type?(type)

  @type = type
end

#version(version = nil) ⇒ String?

Set or get editor version

Examples:

Set specific version

version '44.3.0'

Get current version

version # => "43.3.1"

Parameters:

  • version (String, nil) (defaults to: nil)

    Editor version to set

Returns:

  • (String, nil)

    Current version string or nil if not set



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 182

def version(version = nil)
  return @version&.to_s if version.nil?

  if @automatic_upgrades && version
    detected = VersionDetector.latest_safe_version(version)
    @version = Semver.new(detected || version)
  else
    @version = Semver.new(version)
  end

  # If there is no license key set, and the version if newer than 44.0.0, switch to GPL
  # as the license key is now required in all versions
  gpl if license_key.nil? && @version.major >= 44
end

#wproofreader(version: nil, cdn: nil, **config) ⇒ Object

Configure WProofreader plugin

Examples:

Basic configuration

wproofreader serviceId: 'your-service-ID',
           srcUrl: 'https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js'

Parameters:

  • version (String, nil) (defaults to: nil)

    Plugin version

  • cdn (String, nil) (defaults to: nil)

    CDN URL

  • config (Hash)

    Plugin configuration



402
403
404
405
406
407
408
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 402

def wproofreader(version: nil, cdn: nil, **config)
  configure :wproofreader, config
  plugins do
    prepend(Plugins::WProofreaderSync.new)
    append(Plugins::WProofreader.new(version: version, cdn: cdn))
  end
end