Sha256: 53056a2fb2334cbfdf5658ad04399fedda31522e76bd360fecce858ad026c55c

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

require_relative 'props_plugin'

module CKEditor5::Rails::Editor
  class Props
    EDITOR_TYPES = {
      classic: 'ClassicEditor',
      inline: 'InlineEditor',
      balloon: 'BalloonEditor',
      decoupled: 'DecoupledEditor',
      multiroot: 'MultiRootEditor'
    }.freeze

    def initialize(context, type, config)
      raise ArgumentError, "Invalid editor type: #{type}" unless Props.valid_editor_type?(type)

      @context = context
      @type = type
      @config = config
    end

    def to_attributes
      {
        type: EDITOR_TYPES[@type],
        **serialized_attributes
      }
    end

    def self.valid_editor_type?(type)
      EDITOR_TYPES.key?(type)
    end

    private

    attr_reader :context, :type, :config

    def serialized_attributes
      {
        translations: serialize_translations,
        plugins: serialize_plugins,
        config: serialize_config
      }
    end

    def serialize_translations
      context[:bundle].translations_scripts.map { |script| script.import_meta.to_h }.to_json
    end

    def serialize_plugins
      (config[:plugins] || []).map { |plugin| PropsPlugin.normalize(plugin).to_h }.to_json
    end

    def serialize_config
      config
        .except(:plugins)
        .merge(licenseKey: context[:license_key] || 'GPL')
        .to_json
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ckeditor5-1.0.5 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.0.4 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.0.3 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.0.2 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.0.1 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.0.0 lib/ckeditor5/rails/editor/props.rb