Sha256: e09d326d0bfeb9aeeae7e80b462e5c821c540aa40cd7c3940658c882a1ec617c

Contents?: true

Size: 1.49 KB

Versions: 17

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require_relative 'props_plugin'
require_relative 'editable_height_normalizer'

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

    def initialize(
      type, config,
      bundle: nil,
      watchdog: true,
      editable_height: nil
    )
      raise ArgumentError, "Invalid editor type: #{type}" unless Props.valid_editor_type?(type)

      @bundle = bundle
      @watchdog = watchdog
      @type = type
      @config = config
      @editable_height = EditableHeightNormalizer.new(type).normalize(editable_height)
    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 :bundle, :watchdog, :type, :config, :editable_height

    def serialized_attributes
      {
        bundle: bundle.to_json,
        plugins: serialize_plugins,
        config: serialize_config,
        watchdog: watchdog
      }
        .merge(editable_height ? { 'editable-height' => editable_height } : {})
    end

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

    def serialize_config
      config
        .except(:plugins)
        .to_json
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/ckeditor5-1.24.9/lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.9 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.8 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.6 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.5 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.4 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.3 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.2 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.1 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.24.0 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.23.5 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.23.4 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.23.2 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.23.1 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.23.0 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.22.0 lib/ckeditor5/rails/editor/props.rb
ckeditor5-1.21.0 lib/ckeditor5/rails/editor/props.rb