# frozen_string_literal: true
module EditorJs
module Blocks
# attaches block
class AttachesBlock < Base
include ActiveSupport::NumberHelper
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
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: '',
custom_file_icon: '',
download_icon: ''
}
def render(_options = {})
file_info = data['file']
title = data['title']
extension = file_info['name']&.split('.')&.last
extension = '' unless EXTENSIONS.key?(extension)
content_tag :div, class: css_name do
html_str = content_tag :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 += content_tag :div, class: "#{css_name}__file-info" do
[
content_tag(:div, title, class: "#{css_name}__title"),
content_tag(:div, number_to_human_size(file_info['size']), class: "#{css_name}__size")
].join().html_safe
end
html_str += content_tag :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
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
def plain
decode_html data['title'].strip
end
end
end
end