Class: CKEditor5::Rails::Presets::PresetBuilder
- Inherits:
-
Object
- Object
- CKEditor5::Rails::Presets::PresetBuilder
- Defined in:
- lib/ckeditor5/rails/presets/preset_builder.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Method Summary collapse
-
#apply_integration_patches(compress: !@disallow_inline_plugin_compression)) ⇒ void
Apply integration patches for the current version.
-
#automatic_upgrades(enabled: true) ⇒ Object
Enable or disable automatic version upgrades.
-
#automatic_upgrades? ⇒ Boolean
Check if automatic upgrades are enabled.
-
#balloon_toolbar(*items, **kwargs) { ... } ⇒ ToolbarBuilder
Configure balloon toolbar items and grouping.
-
#block_toolbar(*items, **kwargs) { ... } ⇒ ToolbarBuilder
Configure block toolbar items and grouping.
-
#cdn(cdn = nil, &block) ⇒ Symbol, Proc
Configure CDN source.
-
#ckbox(version = nil, theme: :lark) ⇒ Object
Configure CKBox integration.
-
#custom_translations(lang_code = nil, translations = {}, compress: !@disallow_inline_plugin_compression)) ⇒ void
Configure custom translations for the editor.
- #deconstruct_keys(keys) ⇒ Object
-
#editable_height(height = nil) ⇒ Integer?
Set or get editable height in pixels.
-
#gpl ⇒ Object
Set GPL license and disable premium features.
-
#gpl? ⇒ Boolean
Check if preset is using GPL license.
-
#initialize(&block) ⇒ PresetBuilder
constructor
A new instance of PresetBuilder.
- #initialize_copy(source) ⇒ Object
-
#language(ui = nil, content: ui) ⇒ Hash?
Configure editor language.
-
#language? ⇒ Boolean
Check if language is configured.
-
#license_key(license_key = nil) ⇒ String?
Set or get license key.
-
#menubar(visible: true) ⇒ Object
Configure menubar visibility.
-
#menubar? ⇒ Boolean
Check if menubar is visible.
-
#merge_with_hash!(**overrides) ⇒ self
Merge preset with configuration hash.
-
#override(&block) ⇒ PresetBuilder
Create a new preset by overriding current configuration.
-
#premium(premium = nil) ⇒ Boolean
Enable or check premium features.
-
#premium? ⇒ Boolean
Check if preset is using premium features.
-
#simple_upload_adapter(upload_url = '/uploads', compress: !@disallow_inline_plugin_compression)) ⇒ Object
Configure simple upload adapter.
-
#special_characters(compress: !@disallow_inline_plugin_compression,) { ... } ⇒ Object
Configure special characters plugin.
-
#toolbar(*items, should_group_when_full: true, type: :toolbar, &block) ⇒ ToolbarBuilder
Configure toolbar items and grouping.
-
#translations(*translations) ⇒ Array<Symbol>
Set or get translations.
-
#type(type = nil) ⇒ Symbol
Set or get editor type.
-
#version(version = nil, apply_patches: true) ⇒ String?
Set or get editor version.
-
#wproofreader(version: nil, cdn: nil, compress: !@disallow_inline_plugin_compression,, **config) ⇒ Object
Configure WProofreader plugin.
Methods included from Concerns::PluginMethods
#compression, #compression?, #external_plugin, #inline_plugin, #patch_plugin, #plugin, #plugins, #try_compress_inline_plugins!
Methods included from Concerns::ConfigurationMethods
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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 @disallow_inline_plugin_compression = false @custom_translations = {} @config = { plugins: [], toolbar: [] } instance_eval(&block) if block_given? end |
Instance Method Details
#apply_integration_patches(compress: !@disallow_inline_plugin_compression)) ⇒ void
This method returns an undefined value.
Apply integration patches for the current version
204 205 206 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 204 def apply_integration_patches(compress: !@disallow_inline_plugin_compression) patch_plugin(Plugins::Patches::FixColorPickerRaceCondition.new(compress: compress)) end |
#automatic_upgrades(enabled: true) ⇒ Object
Enable or disable automatic version upgrades
212 213 214 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 212 def automatic_upgrades(enabled: true) @automatic_upgrades = enabled end |
#automatic_upgrades? ⇒ Boolean
Check if automatic upgrades are enabled
218 219 220 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 218 def automatic_upgrades? @automatic_upgrades end |
#balloon_toolbar(*items, **kwargs) { ... } ⇒ ToolbarBuilder
Configure balloon toolbar items and grouping
329 330 331 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 329 def (*items, **kwargs, &block) (*items, **kwargs, type: :balloonToolbar, &block) end |
#block_toolbar(*items, **kwargs) { ... } ⇒ ToolbarBuilder
Configure block toolbar items and grouping
312 313 314 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 312 def (*items, **kwargs, &block) (*items, **kwargs, type: :blockToolbar, &block) end |
#cdn(cdn = nil, &block) ⇒ Symbol, Proc
Configure CDN source
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 231 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
124 125 126 127 128 129 130 131 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 124 def ckbox(version = nil, theme: :lark) return @ckbox if version.nil? @ckbox = { version: version, theme: theme } end |
#custom_translations(lang_code = nil, translations = {}, compress: !@disallow_inline_plugin_compression)) ⇒ void
This method returns an undefined value.
Configure custom translations for the editor
361 362 363 364 365 366 367 368 369 370 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 361 def custom_translations(lang_code = nil, translations = {}, compress: !@disallow_inline_plugin_compression) 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, compress: compress)) end |
#deconstruct_keys(keys) ⇒ Object
40 41 42 43 44 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 40 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
113 114 115 116 117 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 113 def editable_height(height = nil) return @editable_height if height.nil? @editable_height = height end |
#gpl ⇒ Object
Set GPL license and disable premium features
149 150 151 152 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 149 def gpl license_key('GPL') premium(false) end |
#gpl? ⇒ Boolean
Check if preset is using GPL license
71 72 73 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 71 def gpl? license_key == 'GPL' end |
#initialize_copy(source) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 49 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: (source.config[:toolbar]) }.merge( source.config.except(:plugins, :toolbar).deep_dup ) end |
#language(ui = nil, content: ui) ⇒ Hash?
Configure editor language
380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 380 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
335 336 337 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 335 def language? config[:language].present? end |
#license_key(license_key = nil) ⇒ String?
Set or get license key
138 139 140 141 142 143 144 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 138 def license_key(license_key = nil) return @license_key if license_key.nil? @license_key = license_key cdn(:cloud) unless gpl? end |
#menubar(visible: true) ⇒ Object
Configure menubar visibility
263 264 265 266 267 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 263 def (visible: true) config[:menuBar] = { isVisible: visible } end |
#menubar? ⇒ Boolean
Check if menubar is visible
271 272 273 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 271 def config.dig(:menuBar, :isVisible) || false end |
#merge_with_hash!(**overrides) ⇒ self
Merge preset with configuration hash
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 93 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
84 85 86 87 88 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 84 def override(&block) clone.tap do |preset| preset.instance_eval(&block) end end |
#premium(premium = nil) ⇒ Boolean
Enable or check premium features
159 160 161 162 163 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 159 def premium(premium = nil) return @premium if premium.nil? @premium = premium end |
#premium? ⇒ Boolean
Check if preset is using premium features
65 66 67 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 65 def premium? @premium end |
#simple_upload_adapter(upload_url = '/uploads', compress: !@disallow_inline_plugin_compression)) ⇒ Object
Configure simple upload adapter
400 401 402 403 404 405 406 407 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 400 def simple_upload_adapter(upload_url = '/uploads', compress: !@disallow_inline_plugin_compression) plugins do remove(:Base64UploadAdapter) end plugin(Plugins::SimpleUploadAdapter.new(compress: compress)) configure(:simpleUpload, { uploadUrl: upload_url }) end |
#special_characters(compress: !@disallow_inline_plugin_compression,) { ... } ⇒ Object
Configure special characters plugin
453 454 455 456 457 458 459 460 461 462 463 464 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 453 def special_characters(compress: !@disallow_inline_plugin_compression, &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(compress: compress)) end configure(:specialCharactersBootstrap, builder.to_h) end |
#toolbar(*items, should_group_when_full: true, type: :toolbar, &block) ⇒ ToolbarBuilder
Configure toolbar items and grouping
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 286 def (*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
170 171 172 173 174 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 170 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
252 253 254 255 256 257 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 252 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, apply_patches: true) ⇒ String?
Set or get editor version
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 186 def version(version = nil, apply_patches: true) 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 apply_integration_patches if apply_patches end |
#wproofreader(version: nil, cdn: nil, compress: !@disallow_inline_plugin_compression,, **config) ⇒ Object
Configure WProofreader plugin
417 418 419 420 421 422 423 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 417 def wproofreader(version: nil, cdn: nil, compress: !@disallow_inline_plugin_compression, **config) configure :wproofreader, config plugins do prepend(Plugins::WProofreaderSync.new(compress: compress)) append(Plugins::WProofreader.new(version: version, cdn: cdn)) end end |