Class: CKEditor5::Rails::Presets::PresetBuilder
- Inherits:
-
Object
- Object
- CKEditor5::Rails::Presets::PresetBuilder
- Defined in:
- lib/ckeditor5/rails/presets/preset_builder.rb
Instance Method Summary collapse
-
#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.
- #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(disallow_inline_plugins: false, &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') ⇒ Object
Configure simple upload adapter.
-
#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) ⇒ String?
Set or get editor version.
-
#wproofreader(version: nil, cdn: nil, **config) ⇒ Object
Configure WProofreader plugin.
Methods included from Concerns::PluginMethods
#external_plugin, #inline_plugin, #plugin, #plugins
Methods included from Concerns::ConfigurationMethods
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.
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
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
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
310 311 312 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 310 def (*items, **kwargs, &block) (*items, **kwargs, type: :balloonToolbar, &block) end |
#block_toolbar(*items, **kwargs) { ... } ⇒ ToolbarBuilder
Configure block toolbar items and grouping
293 294 295 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 293 def (*items, **kwargs, &block) (*items, **kwargs, type: :blockToolbar, &block) end |
#cdn(cdn = nil, &block) ⇒ Symbol, Proc
Configure CDN source
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
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
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 |
#gpl ⇒ Object
Set GPL license and disable premium features
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
62 63 64 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 62 def gpl? license_key == 'GPL' end |
#initialize_copy(source) ⇒ Object
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: (source.config[:toolbar]) }.merge( source.config.except(:plugins, :toolbar).deep_dup ) end |
#language(ui = nil, content: ui) ⇒ Hash?
Configure editor language
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
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
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 |
#menubar(visible: true) ⇒ Object
Configure menubar visibility
244 245 246 247 248 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 244 def (visible: true) config[:menuBar] = { isVisible: visible } end |
#menubar? ⇒ Boolean
Check if menubar is visible
252 253 254 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 252 def config.dig(:menuBar, :isVisible) || false end |
#merge_with_hash!(**overrides) ⇒ self
Merge preset with configuration hash
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
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
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
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
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
267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 267 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
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
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
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
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 |