Move attachment rendering to a helper

Fixes #5
This commit is contained in:
Javan Makhmali 2018-10-03 14:41:47 -04:00
parent 7a99332434
commit 3044312128
5 changed files with 31 additions and 21 deletions

View File

@ -0,0 +1,24 @@
module ActionText
module ContentHelper
def render_action_text_content(content)
content = content.render_attachments do |attachment|
unless attachment.in?(content.gallery_attachments)
attachment.node.tap do |node|
node.inner_html = render(attachment, in_gallery: false).chomp
end
end
end
content = content.render_attachment_galleries do |attachment_gallery|
render(layout: attachment_gallery, object: attachment_gallery) do
attachment_gallery.attachments.map do |attachment|
attachment.node.inner_html = render(attachment, in_gallery: true).chomp
attachment.to_html
end.join("").html_safe
end.chomp
end
content.to_html
end
end
end

View File

@ -1,3 +1,3 @@
<div class="trix-content">
<%= sanitize document %>
<%= sanitize render_action_text_content(content) %>
</div>

View File

@ -1,8 +1,9 @@
<% transformations = { resize: local_assigns[:in_gallery] ? "800x600>" : "1024x768>" } %>
<figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>">
<% if blob.variable? %>
<%= image_tag blob.variant(resize: "1024x768>") %>
<%= image_tag blob.variant(transformations) %>
<% elsif blob.previewable? %>
<%= image_tag blob.preview(resize: "1024x768>") %>
<%= image_tag blob.preview(transformations) %>
<% end %>
<figcaption class="attachment__caption">

View File

@ -81,23 +81,8 @@ module ActionText
fragment.to_html
end
def to_rendered_html
render_attachments do |attachment|
attachment.node.tap do |node|
node.inner_html = ActionText.renderer.render(attachment)
end
end.render_attachment_galleries do |attachment_gallery|
ActionText.renderer.render(layout: attachment_gallery, object: attachment_gallery, formats: "html") do
attachment_gallery.attachments.map do |attachment|
attachment.node.inner_html = ActionText.renderer.render(attachment)
attachment.to_html
end.join("").html_safe
end
end.to_html
end
def to_rendered_html_with_layout
ActionText.renderer.render(partial: "action_text/content/layout", locals: { document: to_rendered_html })
ActionText.renderer.render(partial: "action_text/content/layout", locals: { content: self })
end
def to_s

View File

@ -25,7 +25,7 @@ module ActionText
initializer "action_text.helper" do
ActiveSupport.on_load(:action_controller_base) do
helper ActionText::TagHelper
helper ActionText::Engine.helpers
end
end
@ -33,7 +33,7 @@ module ActionText
config.after_initialize do |app|
ActionText.renderer ||= ApplicationController.renderer
# FIXME: ApplicationController should have a per-request specific renderer
# FIXME: ApplicationController should have a per-request specific renderer
# that's been set with the request.env env, and ActionText should just piggyback off
# that by default rather than doing this work directly.
ApplicationController.before_action do