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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePresetBuilder

Returns a new instance of PresetBuilder.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 8

def initialize
  @version = nil
  @premium = false
  @cdn = :jsdelivr
  @translations = []
  @license_key = nil
  @type = :classic
  @ckbox = nil
  @config = {
    plugins: [],
    toolbar: []
  }
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 6

def config
  @config
end

Instance Method Details

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 70

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



35
36
37
38
39
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 35

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

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

#configure(key, value) ⇒ Object



92
93
94
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 92

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

#gplObject



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

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

#inline_plugin(name, code) ⇒ Object



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

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

#language(ui, content: ui) ⇒ Object

rubocop:disable Naming/MethodParameterName



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

def language(ui, content: ui) # rubocop:disable Naming/MethodParameterName
  @config[:language] = {
    ui: ui,
    content: content
  }
end

#license_key(license_key = nil) ⇒ Object



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

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

  @license_key = license_key
end


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

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

#plugin(name, **kwargs) ⇒ Object



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

def plugin(name, **kwargs)
  @config[:plugins] << Editor::PropsPlugin.new(name, **kwargs)
end

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



124
125
126
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 124

def plugins(*names, **kwargs)
  names.each { |name| plugin(name, **kwargs) }
end

#premium(premium = nil) ⇒ Object



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

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

  @premium = premium
end

#to_h_with_overrides(**overrides) ⇒ Object



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

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



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

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 = ToolbarBuilder.new(@config[:toolbar])
  builder.instance_eval(&block)
end

#translations(*translations) ⇒ Object



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

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

  @translations = translations
end

#type(type = nil) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

def version(version = nil)
  return @version.to_s if version.nil?

  @version = Semver.new(version)
end