Add captions to embedded media
This commit is contained in:
parent
27faf59549
commit
853e9ad50d
5 changed files with 68 additions and 3 deletions
|
@ -37,6 +37,44 @@ describe EmbeddedConverter do
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "and a caption exists" do
|
||||||
|
it "returns an EmbeddedContent node with caption" do
|
||||||
|
store = GistStore.new
|
||||||
|
paragraph = PostResponse::Paragraph.from_json <<-JSON
|
||||||
|
{
|
||||||
|
"name": "ab12",
|
||||||
|
"text": "Caption",
|
||||||
|
"type": "IFRAME",
|
||||||
|
"href": null,
|
||||||
|
"layout": "INSET_CENTER",
|
||||||
|
"markups": [],
|
||||||
|
"iframe": {
|
||||||
|
"mediaResource": {
|
||||||
|
"id": "abc123",
|
||||||
|
"href": "https://twitter.com/user/status/1",
|
||||||
|
"iframeSrc": "https://cdn.embedly.com/widgets/...",
|
||||||
|
"iframeWidth": 500,
|
||||||
|
"iframeHeight": 281
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"metadata": null
|
||||||
|
}
|
||||||
|
JSON
|
||||||
|
caption = FigureCaption.new(children: [Text.new("Caption")] of Child)
|
||||||
|
|
||||||
|
result = EmbeddedConverter.convert(paragraph, store)
|
||||||
|
|
||||||
|
result.should eq(
|
||||||
|
EmbeddedContent.new(
|
||||||
|
src: "https://cdn.embedly.com/widgets/...",
|
||||||
|
originalWidth: 500,
|
||||||
|
originalHeight: 281,
|
||||||
|
caption: caption,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when the mediaResource has a blank iframeSrc value" do
|
context "when the mediaResource has a blank iframeSrc value" do
|
||||||
|
|
|
@ -249,6 +249,7 @@ describe PageContent do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "renders embedded content" do
|
it "renders embedded content" do
|
||||||
|
caption_children = [Text.new("Caption")] of Child
|
||||||
page = Page.new(
|
page = Page.new(
|
||||||
title: "Title",
|
title: "Title",
|
||||||
author: user_anchor_factory,
|
author: user_anchor_factory,
|
||||||
|
@ -258,6 +259,7 @@ describe PageContent do
|
||||||
src: "https://example.com",
|
src: "https://example.com",
|
||||||
originalWidth: 1000,
|
originalWidth: 1000,
|
||||||
originalHeight: 600,
|
originalHeight: 600,
|
||||||
|
caption: FigureCaption.new(children: caption_children)
|
||||||
),
|
),
|
||||||
] of Child
|
] of Child
|
||||||
)
|
)
|
||||||
|
@ -268,6 +270,11 @@ describe PageContent do
|
||||||
<figure>
|
<figure>
|
||||||
<iframe src="https://example.com" width="800" height="480" frameborder="0" allowfullscreen="true">
|
<iframe src="https://example.com" width="800" height="480" frameborder="0" allowfullscreen="true">
|
||||||
</iframe>
|
</iframe>
|
||||||
|
<label class="margin-toggle" for="#{caption_children.hash}">✍︎</label>
|
||||||
|
<input class="margin-toggle" type="checkbox" id="#{caption_children.hash}">
|
||||||
|
<span class="marginnote">
|
||||||
|
Caption
|
||||||
|
</span>
|
||||||
</figure>
|
</figure>
|
||||||
HTML
|
HTML
|
||||||
end
|
end
|
||||||
|
|
|
@ -34,11 +34,19 @@ class EmbeddedConverter
|
||||||
EmbeddedContent.new(
|
EmbeddedContent.new(
|
||||||
src: media.iframeSrc,
|
src: media.iframeSrc,
|
||||||
originalWidth: media.iframeWidth,
|
originalWidth: media.iframeWidth,
|
||||||
originalHeight: media.iframeHeight
|
originalHeight: media.iframeHeight,
|
||||||
|
caption: caption
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def caption : FigureCaption?
|
||||||
|
if !paragraph.text.blank?
|
||||||
|
children = [Text.new(paragraph.text || "")] of Child
|
||||||
|
FigureCaption.new(children: children)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private def custom_embed(media : PostResponse::MediaResource) : Embedded
|
private def custom_embed(media : PostResponse::MediaResource) : Embedded
|
||||||
if media.href.starts_with?(GIST_HOST_AND_SCHEME)
|
if media.href.starts_with?(GIST_HOST_AND_SCHEME)
|
||||||
GithubGist.new(href: media.href, gist_store: gist_store)
|
GithubGist.new(href: media.href, gist_store: gist_store)
|
||||||
|
|
|
@ -40,6 +40,9 @@ class PageContent < BaseComponent
|
||||||
frameborder: "0",
|
frameborder: "0",
|
||||||
allowfullscreen: true,
|
allowfullscreen: true,
|
||||||
)
|
)
|
||||||
|
if caption = child.caption
|
||||||
|
render_child(caption)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -147,8 +147,14 @@ module Nodes
|
||||||
MAX_WIDTH = 800
|
MAX_WIDTH = 800
|
||||||
|
|
||||||
getter src : String
|
getter src : String
|
||||||
|
getter caption : FigureCaption?
|
||||||
|
|
||||||
def initialize(@src : String, @originalWidth : Int32, @originalHeight : Int32)
|
def initialize(
|
||||||
|
@src : String,
|
||||||
|
@originalWidth : Int32,
|
||||||
|
@originalHeight : Int32,
|
||||||
|
@caption : FigureCaption? = nil
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def width
|
def width
|
||||||
|
@ -168,7 +174,10 @@ module Nodes
|
||||||
end
|
end
|
||||||
|
|
||||||
def ==(other : EmbeddedContent)
|
def ==(other : EmbeddedContent)
|
||||||
other.src == src && other.width == width && other.height == height
|
other.src == src &&
|
||||||
|
other.width == width &&
|
||||||
|
other.height == height &&
|
||||||
|
other.caption == caption
|
||||||
end
|
end
|
||||||
|
|
||||||
def empty?
|
def empty?
|
||||||
|
|
Loading…
Reference in a new issue