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, #plugin, #plugins

Methods included from Concerns::ConfigurationMethods

#configure

Methods included from Editor::Helpers::Config

#ckeditor5_element_ref, #ckeditor5_preset

Constructor Details

#initialize(disallow_inline_plugins: false, &block) ⇒ PresetBuilder

Returns a new instance of PresetBuilder.

Examples:

Basic initialization

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


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

def initialize(disallow_inline_plugins: false, &block)
  @disallow_inline_plugins = disallow_inline_plugins
  @version = nil
  @premium = false
  @cdn = :jsdelivr
  @translations = [:en]
  @license_key = nil
  @type = :classic
  @ckbox = nil
  @editable_height = nil
  @automatic_upgrades = false
  @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



193
194
195
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 193

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

#automatic_upgrades?Boolean

Check if automatic upgrades are enabled

Returns:

  • (Boolean)


199
200
201
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 199

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:



310
311
312
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 310

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:



293
294
295
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 293

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 212

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)



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

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

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

#deconstruct_keys(keys) ⇒ Object



66
67
68
69
70
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 66

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



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

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


146
147
148
149
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 146

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

#gpl?Boolean

Check if preset is using GPL license

Returns:

  • (Boolean)


62
63
64
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 62

def gpl?
  license_key == 'GPL'
end

#initialize_copy(source) ⇒ Object

Examples:

Copy preset and modify it

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


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 41

def initialize_copy(source)
  super

  @translations = source.translations.dup
  @ckbox = source.ckbox.dup if source.ckbox
  @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



328
329
330
331
332
333
334
335
336
337
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 328

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

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

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

#language?Boolean

Check if language is configured

Returns:

  • (Boolean)


316
317
318
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 316

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



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

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



244
245
246
247
248
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 244

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

Check if menubar is visible

Returns:

  • (Boolean)


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

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)


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

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:



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

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



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

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

  @premium = premium
end

#premium?Boolean

Check if preset is using premium features

Returns:

  • (Boolean)


56
57
58
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 56

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



343
344
345
346
347
348
349
350
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 343

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

  plugin(Plugins::SimpleUploadAdapter.new)
  configure(:simpleUpload, { uploadUrl: upload_url })
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:



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 267

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



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

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

  @translations = translations
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



233
234
235
236
237
238
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 233

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 '43.3.1'

Parameters:

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

    Editor version

Returns:

  • (String, nil)

    Current version



178
179
180
181
182
183
184
185
186
187
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 178

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
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



359
360
361
362
363
364
365
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 359

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