-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9640 from alphagov/content-modelling/659-preview-…
…with-embedded Content modelling/659 Preview Host Documents via iFrame
- Loading branch information
Showing
18 changed files
with
288 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...r/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class ContentBlockManager::ContentBlock::Editions::HostContentController < ContentBlockManager::BaseController | ||
def preview | ||
host_content_id = params[:host_content_id] | ||
content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id]) | ||
@preview_content = ContentBlockManager::GetPreviewContent.for_content_id(content_id: host_content_id, content_block_edition:) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
lib/engines/content_block_manager/app/models/content_block_manager/preview_content.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module ContentBlockManager | ||
class PreviewContent < Data.define(:title, :html) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
require "net/http" | ||
require "json" | ||
require "uri" | ||
|
||
module ContentBlockManager | ||
class GetPreviewContent | ||
def self.for_content_id(content_id:, content_block_edition:) | ||
new(content_id:, content_block_edition:).for_content_id | ||
end | ||
|
||
def for_content_id | ||
ContentBlockManager::PreviewContent.new(title: content_item["title"], html:) | ||
end | ||
|
||
private | ||
|
||
def initialize(content_id:, content_block_edition:) | ||
@content_id = content_id | ||
@content_block_edition = content_block_edition | ||
end | ||
|
||
def html | ||
@html ||= preview_html | ||
end | ||
|
||
def content_item | ||
@content_item ||= begin | ||
response = Services.publishing_api.get_content(@content_id) | ||
response.parsed_content | ||
end | ||
end | ||
|
||
def frontend_base_path | ||
Rails.env.development? ? Plek.external_url_for("government-frontend") : Plek.website_root | ||
end | ||
|
||
def frontend_path | ||
frontend_base_path + content_item["base_path"] | ||
end | ||
|
||
def preview_html | ||
uri = URI(frontend_path) | ||
nokogiri_html = html_snapshot_from_frontend(uri) | ||
replace_existing_content_blocks(nokogiri_html) | ||
end | ||
|
||
def replace_existing_content_blocks(nokogiri_html) | ||
replace_blocks(nokogiri_html) | ||
style_blocks(nokogiri_html) | ||
nokogiri_html | ||
end | ||
|
||
def replace_blocks(nokogiri_html) | ||
@preview_content_block_render ||= @content_block_edition.render | ||
content_block_spans(nokogiri_html).each do |span| | ||
span.replace @preview_content_block_render | ||
end | ||
end | ||
|
||
BLOCK_STYLE = "background-color: yellow;".freeze | ||
|
||
def style_blocks(nokogiri_html) | ||
content_block_spans(nokogiri_html).each do |span| | ||
span["style"] = BLOCK_STYLE | ||
end | ||
end | ||
|
||
def content_block_spans(nokogiri_html) | ||
nokogiri_html.css("span[data-content-id=\"#{@content_block_edition.document.content_id}\"]") | ||
end | ||
|
||
ERROR_HTML = "<html><body><p>Preview not found</p></body></html>".freeze | ||
|
||
def html_snapshot_from_frontend(uri) | ||
begin | ||
raw_html = Net::HTTP.get(uri) | ||
rescue StandardError | ||
raw_html = ERROR_HTML | ||
end | ||
Nokogiri::HTML.parse(raw_html) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...ager/app/views/content_block_manager/content_block/editions/host_content/preview.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<% content_for :page_title, "Preview content block in host document" %> | ||
<% content_for :context, "Preview content block" %> | ||
<% content_for :title, @preview_content.title %> | ||
<% content_for :title_margin_bottom, 0 %> | ||
|
||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-two-thirds govuk-body govuk-!-margin-bottom-0"> | ||
<p><strong>Document title: </strong><%= @preview_content.title %></p> | ||
</div> | ||
</div> | ||
<hr class="govuk-section-break govuk-!-margin-bottom-8 govuk-section-break--visible"> | ||
<div class="govuk-grid-row"> | ||
<div class="govuk-grid-column-full"> | ||
<iframe id="preview" style="width:100%;height:80vh;" srcdoc="<%= @preview_content.html %>"></iframe> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.