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
21
# 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
  @editable_height = 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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 77

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



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

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

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

#configure(key, value) ⇒ Object



99
100
101
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 99

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

#editable_height(height = nil) ⇒ Object



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

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

  @editable_height = height
end

#gplObject



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

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

#inline_plugin(name, code) ⇒ Object



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

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

#language(ui, content: ui) ⇒ Object

rubocop:disable Naming/MethodParameterName



135
136
137
138
139
140
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 135

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

#license_key(license_key = nil) ⇒ Object



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

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

  @license_key = license_key
end


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

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

#plugin(name, **kwargs) ⇒ Object



127
128
129
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 127

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

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



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

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

#premium(premium = nil) ⇒ Object



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

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

  @premium = premium
end

#to_h_with_overrides(**overrides) ⇒ Object



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

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



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 109

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



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

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

  @translations = translations
end

#type(type = nil) ⇒ Object

Raises:

  • (ArgumentError)


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

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



71
72
73
74
75
# File 'lib/ckeditor5/rails/presets/preset_builder.rb', line 71

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

  @version = Semver.new(version)
end