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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Editor::Helpers::Config

#ckeditor5_element_ref, #ckeditor5_preset

Constructor Details

#initialize(&block) ⇒ PresetBuilder



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

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

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 8

def config
  @config
end

Instance Method Details

#automatic_upgrades(enabled: true) ⇒ Object



117
118
119
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 117

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

#automatic_upgrades?Boolean



121
122
123
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 121

def automatic_upgrades?
  @automatic_upgrades
end

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 125

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



72
73
74
75
76
77
78
79
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 72

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

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

#configure(key, value) ⇒ Object



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

def configure(key, value)
  @config[key] = value
end

#editable_height(height = nil) ⇒ Object



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

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

  @editable_height = height
end

#gplObject



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

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

#gpl?Boolean



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

def gpl?
  license_key == 'GPL'
end

#initialize_copy(source) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 28

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

#inline_plugin(name, code) ⇒ Object



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

def inline_plugin(name, code)
  @config[:plugins] << Editor::PropsInlinePlugin.new(name, code)
end

#language(ui = nil, content: ui) ⇒ Object

rubocop:disable Naming/MethodParameterName



193
194
195
196
197
198
199
200
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 193

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

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

#license_key(license_key = nil) ⇒ Object



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

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

  @license_key = license_key

  cdn(:cloud) unless gpl?
end


151
152
153
154
155
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 151

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


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

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

#plugin(name, **kwargs) ⇒ Object



175
176
177
178
179
180
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 175

def plugin(name, **kwargs)
  plugin_obj = PluginsBuilder.create_plugin(name, **kwargs)

  @config[:plugins] << plugin_obj
  plugin_obj
end

#plugins(*names, **kwargs, &block) ⇒ Object



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

def plugins(*names, **kwargs, &block)
  @config[:plugins] ||= []

  names.each { |name| plugin(name, **kwargs) } unless names.empty?

  return unless block

  builder = PluginsBuilder.new(@config[:plugins])
  builder.instance_eval(&block)
end

#premium(premium = nil) ⇒ Object



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

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

  @premium = premium
end

#premium?Boolean



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

def premium?
  @premium
end

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



202
203
204
205
206
207
208
209
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 202

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

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

#to_h_with_overrides(**overrides) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 53

def to_h_with_overrides(**overrides)
  {
    version: overrides.fetch(:version, 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),
    ckbox: overrides.fetch(:ckbox, ckbox),
    config: config.merge(overrides.fetch(:config, {}))
  }
end

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



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 157

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

  return unless block

  builder = ArrayBuilder.new(@config[:toolbar][:items])
  builder.instance_eval(&block)
end

#translations(*translations) ⇒ Object



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

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

  @translations = translations
end

#type(type = nil) ⇒ Object

Raises:

  • (ArgumentError)


140
141
142
143
144
145
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 140

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



106
107
108
109
110
111
112
113
114
115
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 106

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