2018-10-08 17:44:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-01 05:43:25 -04:00
|
|
|
require "active_support/core_ext/object/try"
|
|
|
|
|
2018-04-13 19:23:04 -04:00
|
|
|
module ActionText
|
2018-02-07 19:26:19 -05:00
|
|
|
class Attachment
|
|
|
|
include Attachments::TrixConversion, Attachments::Minification, Attachments::Caching
|
|
|
|
|
2021-03-05 16:57:36 -05:00
|
|
|
mattr_accessor :tag_name, default: "action-text-attachment"
|
|
|
|
|
2018-10-03 12:11:39 -04:00
|
|
|
ATTRIBUTES = %w( sgid content-type url href filename filesize width height previewable presentation caption )
|
2018-02-07 19:26:19 -05:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def fragment_by_canonicalizing_attachments(content)
|
|
|
|
fragment_by_minifying_attachments(fragment_by_converting_trix_attachments(content))
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_node(node, attachable = nil)
|
2018-04-13 19:23:04 -04:00
|
|
|
new(node, attachable || ActionText::Attachable.from_node(node))
|
2018-02-07 19:26:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def from_attachables(attachables)
|
2021-04-22 21:08:34 -04:00
|
|
|
Array(attachables).filter_map { |attachable| from_attachable(attachable) }
|
2018-02-07 19:26:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def from_attachable(attachable, attributes = {})
|
2018-04-13 17:10:18 -04:00
|
|
|
if node = node_from_attributes(attachable.to_rich_text_attributes(attributes))
|
2018-02-07 19:26:19 -05:00
|
|
|
new(node, attachable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def from_attributes(attributes, attachable = nil)
|
|
|
|
if node = node_from_attributes(attributes)
|
|
|
|
from_node(node, attachable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def node_from_attributes(attributes)
|
|
|
|
if attributes = process_attributes(attributes).presence
|
2021-03-05 16:57:36 -05:00
|
|
|
ActionText::HtmlConversion.create_element(tag_name, attributes)
|
2018-02-07 19:26:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_attributes(attributes)
|
|
|
|
attributes.transform_keys { |key| key.to_s.underscore.dasherize }.slice(*ATTRIBUTES)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :node, :attachable
|
|
|
|
|
|
|
|
delegate :to_param, to: :attachable
|
|
|
|
delegate_missing_to :attachable
|
|
|
|
|
|
|
|
def initialize(node, attachable)
|
|
|
|
@node = node
|
|
|
|
@attachable = attachable
|
|
|
|
end
|
|
|
|
|
|
|
|
def caption
|
|
|
|
node_attributes["caption"].presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_attributes
|
|
|
|
node_attributes.merge(attachable_attributes).merge(sgid_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_full_attributes
|
|
|
|
self.class.from_attributes(full_attributes, attachable)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_plain_text
|
|
|
|
if respond_to?(:attachable_plain_text_representation)
|
|
|
|
attachable_plain_text_representation(caption)
|
|
|
|
else
|
|
|
|
caption.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_html
|
2018-02-13 16:00:14 -05:00
|
|
|
HtmlConversion.node_to_html(node)
|
2018-02-07 19:26:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
to_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
|
|
|
"#<#{self.class.name} attachable=#{attachable.inspect}>"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def node_attributes
|
|
|
|
@node_attributes ||= ATTRIBUTES.map { |name| [ name.underscore, node[name] ] }.to_h.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachable_attributes
|
2018-04-13 17:10:18 -04:00
|
|
|
@attachable_attributes ||= (attachable.try(:to_rich_text_attributes) || {}).stringify_keys
|
2018-02-07 19:26:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def sgid_attributes
|
|
|
|
@sgid_attributes ||= node_attributes.slice("sgid").presence || attachable_attributes.slice("sgid")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|