Sha256: ffd0d3c7fed4cf55f6a2d72690605932361f9b5828779f181914428df9f1a754

Contents?: true

Size: 1.18 KB

Versions: 24

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

module CKEditor5::Rails::Editor
  class InvalidEditableHeightError < ArgumentError; end

  class EditableHeightNormalizer
    def initialize(editor_type)
      @editor_type = editor_type
    end

    def normalize(value)
      return nil if value.nil?

      validate_editor_type!
      convert_to_pixel_value(value)
    end

    private

    attr_reader :editor_type

    def validate_editor_type!
      return if editor_type == :classic

      raise InvalidEditableHeightError,
            'editable_height can be used only with ClassicEditor'
    end

    def convert_to_pixel_value(value)
      case value
      when Integer then "#{value}px"
      when String then convert_string_to_pixel_value(value)
      else
        raise_invalid_height_error(value)
      end
    end

    def convert_string_to_pixel_value(value)
      return value if value.match?(/^\d+px$/)

      raise_invalid_height_error(value)
    end

    def raise_invalid_height_error(value)
      raise InvalidEditableHeightError,
            "editable_height must be an integer representing pixels or string ending with 'px'\n" \
            "(e.g. 500 or '500px'). Got: #{value.inspect}"
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ckeditor5-1.23.0 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.22.0 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.21.0 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.20.1 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.20.0 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.19.5 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.19.4 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.19.3 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.19.2 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.19.1 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.19.0 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.18.3 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.18.1 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.18.0 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.17.4 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.17.3 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.17.2 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.17.1 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.17.0 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.16.2 lib/ckeditor5/rails/editor/editable_height_normalizer.rb