Module: CKEditor5::Rails::Presets::Concerns::PluginMethods
- Extended by:
- ActiveSupport::Concern
- Included in:
- Context::PresetBuilder, PresetBuilder
- Defined in:
- lib/ckeditor5/rails/presets/concerns/plugin_methods.rb
Defined Under Namespace
Classes: CompressionDisabledError, InvalidPatchPluginError, MissingInlinePluginError, UnsupportedESModuleError
Instance Method Summary collapse
-
#compression(enabled: false) ⇒ void
Sets compression of inline plugin code.
-
#compression? ⇒ Boolean
Check if compression is enabled.
-
#external_plugin(name, **kwargs) ⇒ Object
Registers an external plugin loaded from a URL.
-
#inline_plugin(name, code, compress: !@disallow_inline_plugin_compression)) ⇒ Object
Registers an inline plugin with raw JavaScript code.
-
#patch_plugin(plugin) ⇒ Editor::PropsPatchPlugin?
Registers a patch plugin that modifies CKEditor behavior for specific versions.
-
#plugin(name, **kwargs) ⇒ Object
Register a single plugin by name.
-
#plugins(*names, **kwargs, &block) ⇒ Object
Register multiple plugins and configure plugin settings.
-
#try_compress_inline_plugins! ⇒ void
Compresses inline plugins to reduce bundle size.
Instance Method Details
#compression(enabled: false) ⇒ void
This method is useful for debugging purposes, as it allows you to see the uncompressed code.
This method returns an undefined value.
Sets compression of inline plugin code. Make sure that it is called before setting the version or adding plugins.
27 28 29 |
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 27 def compression(enabled: false) @disallow_inline_plugin_compression = !enabled end |
#compression? ⇒ Boolean
Check if compression is enabled
35 36 37 |
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 35 def compression? !@disallow_inline_plugin_compression end |
#external_plugin(name, **kwargs) ⇒ Object
Registers an external plugin loaded from a URL
49 50 51 |
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 49 def external_plugin(name, **kwargs) register_plugin(Editor::PropsExternalPlugin.new(name, **kwargs)) end |
#inline_plugin(name, code, compress: !@disallow_inline_plugin_compression)) ⇒ Object
Registers an inline plugin with raw JavaScript code
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 72 def inline_plugin(name, code, compress: !@disallow_inline_plugin_compression) if code.match?(/export default/) raise UnsupportedESModuleError, 'Inline plugins must not use ES module syntax!' \ 'Use async async imports instead!' end unless code.match?(/return class(\s+\w+)?\s+extends\s+Plugin/) raise MissingInlinePluginError, 'Plugin code must return a class that extends Plugin!' end plugin = Editor::PropsInlinePlugin.new(name, code, compress: compress) register_plugin(plugin) end |
#patch_plugin(plugin) ⇒ Editor::PropsPatchPlugin?
Registers a patch plugin that modifies CKEditor behavior for specific versions
96 97 98 99 100 101 102 103 104 |
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 96 def patch_plugin(plugin) unless plugin.is_a?(Editor::PropsPatchPlugin) raise InvalidPatchPluginError, 'Provided plugin must be a PropsPatchPlugin instance' end return unless !@version || plugin.applicable_for_version?(@version) register_plugin(plugin) end |
#plugin(name, **kwargs) ⇒ Object
Register a single plugin by name
116 117 118 119 |
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 116 def plugin(name, **kwargs) premium(true) if kwargs[:premium] && respond_to?(:premium) register_plugin(PluginsBuilder.create_plugin(name, **kwargs)) end |
#plugins(*names, **kwargs, &block) ⇒ Object
Register multiple plugins and configure plugin settings
133 134 135 136 137 138 139 140 141 |
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 133 def plugins(*names, **kwargs, &block) config[:plugins] ||= [] names.each { |name| plugin(name, **kwargs) } unless names.empty? builder = PluginsBuilder.new(config[:plugins]) builder.instance_eval(&block) if block_given? builder end |
#try_compress_inline_plugins! ⇒ void
This method is called automatically when defining a preset
This method returns an undefined value.
Compresses inline plugins to reduce bundle size
150 151 152 153 154 155 156 157 158 |
# File 'lib/ckeditor5/rails/presets/concerns/plugin_methods.rb', line 150 def try_compress_inline_plugins! raise CompressionDisabledError if @disallow_inline_plugin_compression config[:plugins].each do |plugin| next unless plugin.is_a?(Editor::PropsInlinePlugin) plugin.try_compress! end end |