Skip to content

Commit 3ebc903

Browse files
author
Dobromir Hristov
committed
refactor: center captions that are below figures
1 parent 2361a14 commit 3ebc903

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

src/components/ContentNode.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ function renderNode(createElement, references) {
197197
if ((title && abstract.length) || abstract.length) {
198198
// if there is a `title`, it should be above, otherwise below
199199
figureContent.splice(title ? 0 : 1, 0,
200-
createElement(FigureCaption, { props: { title } }, renderChildren(abstract)));
200+
createElement(FigureCaption, {
201+
props: { title, centered: !title },
202+
}, renderChildren(abstract)));
201203
}
202204
return createElement(Figure, { props: { anchor } }, figureContent);
203205
};

src/components/ContentNode/FigureCaption.vue

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
-->
1010

1111
<template>
12-
<figcaption class="caption">
12+
<figcaption class="caption" :class="{ centered }">
1313
<strong v-if="title">{{ title }}</strong>&nbsp;<slot />
1414
</figcaption>
1515
</template>
@@ -22,6 +22,10 @@ export default {
2222
type: String,
2323
required: false,
2424
},
25+
centered: {
26+
type: Boolean,
27+
default: false,
28+
},
2529
},
2630
};
2731
</script>
@@ -35,6 +39,10 @@ export default {
3539
&:last-child {
3640
margin-top: $stacked-margin-xlarge;
3741
}
42+
43+
&.centered {
44+
text-align: center;
45+
}
3846
}
3947
4048
/deep/ p {

tests/unit/components/ContentNode.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,13 @@ describe('ContentNode', () => {
455455
expect(caption.exists()).toBe(true);
456456
expect(caption.contains('p')).toBe(true);
457457
expect(caption.props('title')).toBeFalsy();
458+
expect(caption.props('centered')).toBe(true);
458459
expect(caption.text()).toContain('blah');
459460
// assert figurerecaption is below the image
460461
expect(figure.html()).toMatchInlineSnapshot(`
461462
<figure-stub>
462463
<inlineimage-stub alt="" variants="[object Object],[object Object]"></inlineimage-stub>
463-
<figurecaption-stub>
464+
<figurecaption-stub centered="true">
464465
<p>blah</p>
465466
</figurecaption-stub>
466467
</figure-stub>
@@ -567,6 +568,7 @@ describe('ContentNode', () => {
567568
expect(caption.exists()).toBe(true);
568569
expect(caption.contains('p')).toBe(true);
569570
expect(caption.props('title')).toBe(metadata.title);
571+
expect(caption.props('centered')).toBe(true);
570572
expect(caption.text()).toContain('blah');
571573
});
572574

@@ -592,12 +594,13 @@ describe('ContentNode', () => {
592594
expect(caption.exists()).toBe(true);
593595
expect(caption.contains('p')).toBe(true);
594596
expect(caption.props('title')).toBeFalsy();
597+
expect(caption.props('centered')).toBe(true);
595598
expect(caption.text()).toContain('blah');
596599
// assert figurerecaption is below the image
597600
expect(figure.html()).toMatchInlineSnapshot(`
598601
<figure-stub>
599602
<inlinevideo-stub identifier="video.mp4"></inlinevideo-stub>
600-
<figurecaption-stub>
603+
<figurecaption-stub centered="true">
601604
<p>blah</p>
602605
</figurecaption-stub>
603606
</figure-stub>
@@ -1043,6 +1046,7 @@ describe('ContentNode', () => {
10431046
const caption = figure.find(FigureCaption);
10441047
expect(caption.exists()).toBe(true);
10451048
expect(caption.props('title')).toBe(metadata.title);
1049+
expect(caption.props('centered')).toBe(false);
10461050
expect(caption.contains('p')).toBe(true);
10471051
expect(caption.text()).toContain('blah');
10481052
});

tests/unit/components/ContentNode/FigureCaption.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ describe('FigureCaption', () => {
2727
expect(wrapper.is('figcaption')).toBe(true);
2828
expect(wrapper.text()).toBe('Blah');
2929
});
30+
31+
it('renders a <figcaption> centered', () => {
32+
const slots = { default: '<p>Blah</p>' };
33+
const wrapper = shallowMount(FigureCaption, { slots, propsData: { centered: true } });
34+
expect(wrapper.classes()).toContain('centered');
35+
});
3036
});

0 commit comments

Comments
 (0)