Sha256: 560d9a1848dffcfb99581976c66d458c7fbd2b7080b913eb8b54f1c237452196

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module EditorJs
  module Blocks
    class ImageBlock < Base
      def schema
        YAML.safe_load(<<~YAML)
          type: object
          additionalProperties: false
          properties:
            caption:
              type: string
            url:
              type: string
            stretched:
              type: boolean
            withBackground:
              type: boolean
            withBorder:
              type: boolean
          required:
          - url
        YAML
      end

      def render(_options = {})
        content_tag :div, class: css_name do
          url = data['url']
          caption = data['caption']
          withBorder = data['withBorder']
          withBackground = data['withBackground']
          stretched = data['stretched']

          html_class = "#{css_name}__picture"
          html_class += " #{css_name}__picture--stretched" if stretched
          html_class += " #{css_name}__picture--with-background" if withBackground
          html_class += " #{css_name}__picture--with-border" if withBorder

          html_str =  content_tag :div, class: html_class do
                        content_tag :img, '', src: url
                      end
          html_str << content_tag(:div, caption.html_safe, class: "#{css_name}__caption").html_safe
        end
      end

      def sanitize!
        data['caption'] = Sanitize.fragment(data['caption'], remove_contents: true)
      end

      def plain
        decode_html data['caption'].strip
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
editor_js-0.2.2 lib/editor_js/blocks/image_block.rb
editor_js-0.2.1 lib/editor_js/blocks/image_block.rb
editor_js-0.2.0 lib/editor_js/blocks/image_block.rb
editor_js-0.1.0 lib/editor_js/blocks/image_block.rb