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(&block) ⇒ PresetBuilder

Returns a new instance of PresetBuilder.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 13

def initialize(&block)
  @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



130
131
132
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 130

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

#automatic_upgrades?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 134

def automatic_upgrades?
  @automatic_upgrades
end

#cdn(cdn = nil, &block) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 138

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



85
86
87
88
89
90
91
92
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 85

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

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

#deconstruct_keys(keys) ⇒ Object



52
53
54
55
56
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 52

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

#editable_height(height = nil) ⇒ Object



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

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

  @editable_height = height
end

#gplObject



102
103
104
105
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 102

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

#gpl?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 48

def gpl?
  license_key == 'GPL'
end

#initialize_copy(source) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 31

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) ⇒ Object

rubocop:disable Naming/MethodParameterName



187
188
189
190
191
192
193
194
195
196
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 187

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

Returns:

  • (Boolean)


183
184
185
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 183

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

#license_key(license_key = nil) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 94

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

  @license_key = license_key

  cdn(:cloud) unless gpl?
end


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

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

Returns:

  • (Boolean)


166
167
168
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 166

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

#merge_with_hash!(**overrides) ⇒ Object

rubocop:disable Metrics/AbcSize



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 64

def merge_with_hash!(**overrides) # rubocop:disable Metrics/AbcSize
  @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) ⇒ Object



58
59
60
61
62
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 58

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

#premium(premium = nil) ⇒ Object



107
108
109
110
111
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 107

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

  @premium = premium
end

#premium?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 44

def premium?
  @premium
end

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



198
199
200
201
202
203
204
205
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 198

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, &block) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 170

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

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

#translations(*translations) ⇒ Object



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

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

  @translations = translations
end

#type(type = nil) ⇒ Object

Raises:

  • (ArgumentError)


153
154
155
156
157
158
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 153

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) ⇒ Object



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

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



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

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