Sha256: 62e6a14edd04445b59c7c90ab0849ecbbd566fa0d242341b7ee91a64106b319e

Contents?: true

Size: 1.2 KB

Versions: 9

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module CKEditor5::Rails
  class Presets::PluginsBuilder
    attr_reader :items

    def initialize(plugins)
      @items = plugins
    end

    def self.create_plugin(name, **kwargs)
      if name.is_a?(Editor::PropsBasePlugin)
        name
      else
        Editor::PropsPlugin.new(name, **kwargs)
      end
    end

    def remove(*names)
      names.each { |name| items.delete_if { |plugin| plugin.name == name } }
    end

    def prepend(*names, before: nil, **kwargs)
      new_plugins = names.map { |name| self.class.create_plugin(name, **kwargs) }

      if before
        index = items.index { |p| p.name == before }
        raise ArgumentError, "Plugin '#{before}' not found" unless index

        items.insert(index, *new_plugins)
      else
        items.insert(0, *new_plugins)
      end
    end

    def append(*names, after: nil, **kwargs)
      new_plugins = names.map { |name| self.class.create_plugin(name, **kwargs) }

      if after
        index = items.index { |p| p.name == after }
        raise ArgumentError, "Plugin '#{after}' not found" unless index

        items.insert(index + 1, *new_plugins)
      else
        items.push(*new_plugins)
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ckeditor5-1.19.5 lib/ckeditor5/rails/presets/plugins_builder.rb
ckeditor5-1.19.4 lib/ckeditor5/rails/presets/plugins_builder.rb
ckeditor5-1.19.3 lib/ckeditor5/rails/presets/plugins_builder.rb
ckeditor5-1.19.2 lib/ckeditor5/rails/presets/plugins_builder.rb
ckeditor5-1.19.1 lib/ckeditor5/rails/presets/plugins_builder.rb
ckeditor5-1.19.0 lib/ckeditor5/rails/presets/plugins_builder.rb
ckeditor5-1.18.3 lib/ckeditor5/rails/presets/plugins_builder.rb
ckeditor5-1.18.1 lib/ckeditor5/rails/presets/plugins_builder.rb
ckeditor5-1.18.0 lib/ckeditor5/rails/presets/plugins_builder.rb