Sha256: ffd0d3c7fed4cf55f6a2d72690605932361f9b5828779f181914428df9f1a754

Contents?: true

Size: 1.18 KB

Versions: 5

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

5 entries across 5 versions & 1 rubygems

Version Path
ckeditor5-1.16.2 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.16.1 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.16.0 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.15.10 lib/ckeditor5/rails/editor/editable_height_normalizer.rb
ckeditor5-1.15.9 lib/ckeditor5/rails/editor/editable_height_normalizer.rb