Class: EditorJs::Blocks::AttachesBlock

Inherits:
Base
  • Object
show all
Includes:
ActiveSupport::NumberHelper
Defined in:
lib/editor_js/blocks/attaches_block.rb

Overview

attaches block

Constant Summary collapse

EXTENSIONS =
{
  'doc' => '#3e74da',
  'docx' => '#3e74da',
  'odt' => '#3e74da',
  'pdf' => '#d47373',
  'rtf' => '#656ecd',
  'tex' => '#5a5a5b',
  'txt' => '#5a5a5b',
  'pptx' => '#e07066',
  'ppt' => '#e07066',
  'mp3' => '#eab456',
  'mp4' => '#f676a6',
  'xls' => '#3f9e64',
  'html' => '#2988f0',
  'htm' => '#2988f0',
  'png' => '#f676a6',
  'jpg' => '#f67676',
  'jpeg' => '#f67676',
  'gif' => '#f6af76',
  'zip' => '#4f566f',
  'rar' => '#4f566f',
  'exe' => '#e26f6f',
  'svg' => '#bf5252',
  'key' => '#e07066',
  'sketch' => '#df821c',
  'ai' => '#df821c',
  'psd' => '#388ae5',
  'dmg' => '#e26f6f',
  'json' => '#2988f0',
  'csv' => '#3f9e64'
}
ICONS =
{
  file_icon: '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="40"><path d="M17 0l15 14V3v34a3 3 0 0 1-3 3H3a3 3 0 0 1-3-3V3a3 3 0 0 1 3-3h20-6zm0 2H3a1 1 0 0 0-1 1v34a1 1 0 0 0 1 1h26a1 1 0 0 0 1-1V14H17V2zm2 10h7.926L19 4.602V12z"></path></svg>',
  custom_file_icon: '<svg xmlns="http://www.w3.org/2000/svg" width="32" height="40"><g fill="#A8ACB8" fill-rule="evenodd"><path fill-rule="nonzero" d="M17 0l15 14V3v34a3 3 0 0 1-3 3H3a3 3 0 0 1-3-3V3a3 3 0 0 1 3-3h20-6zm0 2H3a1 1 0 0 0-1 1v34a1 1 0 0 0 1 1h26a1 1 0 0 0 1-1V14H17V2zm2 10h7.926L19 4.602V12z"></path><path d="M7 22h18v2H7zm0 4h18v2H7zm0 4h18v2H7z"></path></g></svg>',
  download_icon: '<svg xmlns="http://www.w3.org/2000/svg" width="17pt" height="17pt" viewBox="0 0 17 17"><path d="M9.457 8.945V2.848A.959.959 0 0 0 8.5 1.89a.959.959 0 0 0-.957.957v6.097L4.488 5.891a.952.952 0 0 0-1.351 0 .952.952 0 0 0 0 1.351l4.687 4.688a.955.955 0 0 0 1.352 0l4.687-4.688a.952.952 0 0 0 0-1.351.952.952 0 0 0-1.351 0zM3.59 14.937h9.82a.953.953 0 0 0 .953-.957.952.952 0 0 0-.953-.953H3.59a.952.952 0 0 0-.953.953c0 .532.425.957.953.957zm0 0" fill-rule="evenodd"></path></svg>'
}

Constants inherited from Base

Base::InvalidBlockDataError, Base::InvalidBlockTypeError

Instance Attribute Summary

Attributes inherited from Base

#output_buffer, #raw

Instance Method Summary collapse

Methods inherited from Base

#css_name, #data, #decode_html, inherited, #initialize, load, #output, registry, type, #type, #valid?

Constructor Details

This class inherits a constructor from EditorJs::Blocks::Base

Instance Method Details

#plainObject



107
108
109
# File 'lib/editor_js/blocks/attaches_block.rb', line 107

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

#render(_options = {}) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/editor_js/blocks/attaches_block.rb', line 71

def render(_options = {})
  file_info = data['file']
  title = data['title']
  extension = file_info['name']&.split('.')&.last
  extension = '' unless EXTENSIONS.key?(extension)

   :div, class: css_name do
    html_str =  :div, class: "#{css_name}__file-icon", data: {extension: extension}, style: "color: #{EXTENSIONS[extension]};" do
      (EXTENSIONS[extension] ? ICONS[:file_icon] : ICONS[:custom_file_icon]).html_safe
    end

    html_str +=  :div, class: "#{css_name}__file-info" do
      [
        (:div, title, class: "#{css_name}__title"),
        (:div, number_to_human_size(file_info['size']), class: "#{css_name}__size")
      ].join().html_safe
    end

    html_str +=  :a, class: "#{css_name}__download-button", href: file_info['url'], target: '_blank', rel: 'nofollow noindex noreferrer' do
      ICONS[:download_icon].html_safe
    end
    html_str.html_safe
  end
end

#sanitize!Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/editor_js/blocks/attaches_block.rb', line 96

def sanitize!
  data['title'] = Sanitize.fragment(data['title'], remove_contents: true).strip
  file_info = data['file'] || {}
  file_info.each do |key, val|
    break if key == 'size'

    file_info[key] = Sanitize.fragment(val, remove_contents: true).strip
  end
  data['file'] = file_info
end

#schemaObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/editor_js/blocks/attaches_block.rb', line 9

def schema
  YAML.safe_load(<<~YAML)
    type: object
    additionalProperties: false
    properties:
      file:
        type: object
        additionalProperties: false
        properties:
          url:
            type: string
          name:
            type: string
          type:
            type: string
          size:
            type: number
      title:
        type: string
    required:
    - file
  YAML
end