Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mobiledoc renderer for gallery card
no issue - basic renderer for working with Koenig's gallery card
- Loading branch information
1 parent
4c0e275
commit 402d26a
Showing
4 changed files
with
200 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const MAX_IMG_PER_ROW = 3; | ||
|
||
/** | ||
* <figure class="kg-gallery-card kg-width-wide"> | ||
* <div class="kg-gallery-container> | ||
* <div class="kg-gallery-row"> | ||
* <div class="kg-gallery-image"><img width="" height=""></div> | ||
* <div class="kg-gallery-image"><img width="" height=""></div> | ||
* <div class="kg-gallery-image"><img width="" height=""></div> | ||
* </div> | ||
* <div class="kg-gallery-row"> | ||
* <div class="kg-gallery-image"><img></div> | ||
* <div class="kg-gallery-image"><img></div> | ||
* </div> | ||
* </div> | ||
* <figcaption></figcaption> | ||
* </figure> | ||
*/ | ||
|
||
module.exports = { | ||
name: 'gallery', | ||
type: 'dom', | ||
render(opts) { | ||
let payload = opts.payload; | ||
// let version = opts.options.version; | ||
let dom = opts.env.dom; | ||
|
||
if (!payload.images || payload.images.length === 0) { | ||
return ''; | ||
} | ||
|
||
let figure = dom.createElement('figure'); | ||
figure.setAttribute('class', 'kg-gallery-card kg-width-wide'); | ||
|
||
let container = dom.createElement('div'); | ||
container.setAttribute('class', 'kg-gallery-container'); | ||
figure.appendChild(container); | ||
|
||
let buildStructure = function buildStructure(images) { | ||
let rows = []; | ||
let noOfImages = images.length; | ||
|
||
images.forEach((image, idx) => { | ||
let row = image.row; | ||
|
||
if (noOfImages > 1 && (noOfImages % MAX_IMG_PER_ROW === 1) && (idx === (noOfImages - 2))) { | ||
row = row + 1; | ||
} | ||
if (!rows[row]) { | ||
rows[row] = []; | ||
} | ||
|
||
rows[row].push(image); | ||
}); | ||
|
||
return rows; | ||
}; | ||
|
||
let rows = buildStructure(payload.images); | ||
|
||
rows.forEach((row) => { | ||
let rowDiv = dom.createElement('div'); | ||
rowDiv.setAttribute('class', 'kg-gallery-row'); | ||
|
||
row.forEach((image) => { | ||
let imgDiv = dom.createElement('div'); | ||
imgDiv.setAttribute('class', 'kg-gallery-image'); | ||
|
||
let img = dom.createElement('img'); | ||
img.setAttribute('src', image.src); | ||
img.setAttribute('width', image.width); | ||
img.setAttribute('height', image.height); | ||
|
||
if (image.alt) { | ||
img.setAttribute('alt', image.alt); | ||
} | ||
if (image.title) { | ||
img.setAttribute('title', image.title); | ||
} | ||
|
||
imgDiv.appendChild(img); | ||
rowDiv.appendChild(imgDiv); | ||
}); | ||
|
||
container.appendChild(rowDiv); | ||
}); | ||
|
||
if (payload.caption) { | ||
let figcaption = dom.createElement('figcaption'); | ||
figcaption.appendChild(dom.createRawHTMLSection(payload.caption)); | ||
figure.appendChild(figcaption); | ||
} | ||
|
||
return figure; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const should = require('should'); | ||
const card = require('../../../../../server/lib/mobiledoc/cards/gallery'); | ||
const SimpleDom = require('simple-dom'); | ||
const serializer = new SimpleDom.HTMLSerializer(SimpleDom.voidMap); | ||
|
||
describe('Gallery card', function () { | ||
it('renders a gallery', function () { | ||
let opts = { | ||
env: { | ||
dom: new SimpleDom.Document() | ||
}, | ||
payload: { | ||
images: [ | ||
{ | ||
row: 0, | ||
fileName: 'NatGeo01.jpg', | ||
src: '/content/images/2018/08/NatGeo01-9.jpg', | ||
width: 3200, | ||
height: 1600 | ||
}, | ||
{ | ||
row: 0, | ||
fileName: 'NatGeo02.jpg', | ||
src: '/content/images/2018/08/NatGeo02-10.jpg', | ||
width: 3200, | ||
height: 1600 | ||
}, | ||
{ | ||
row: 0, | ||
fileName: 'NatGeo03.jpg', | ||
src: '/content/images/2018/08/NatGeo03-6.jpg', | ||
width: 3200, | ||
height: 1600 | ||
}, | ||
{ | ||
row: 1, | ||
fileName: 'NatGeo04.jpg', | ||
src: '/content/images/2018/08/NatGeo04-7.jpg', | ||
alt: 'Alt test', | ||
width: 3200, | ||
height: 1600 | ||
}, | ||
{ | ||
row: 1, | ||
fileName: 'NatGeo05.jpg', | ||
src: '/content/images/2018/08/NatGeo05-4.jpg', | ||
title: 'Title test', | ||
width: 3200, | ||
height: 1600 | ||
}, | ||
{ | ||
row: 1, | ||
fileName: 'NatGeo06.jpg', | ||
src: '/content/images/2018/08/NatGeo06-6.jpg', | ||
width: 3200, | ||
height: 1600 | ||
}, | ||
{ | ||
row: 2, | ||
fileName: 'NatGeo07.jpg', | ||
src: '/content/images/2018/08/NatGeo07-5.jpg', | ||
width: 3200, | ||
height: 1600 | ||
}, | ||
{ | ||
row: 2, | ||
fileName: 'NatGeo09.jpg', | ||
src: '/content/images/2018/08/NatGeo09-8.jpg', | ||
width: 3200, | ||
height: 1600 | ||
} | ||
], | ||
caption: 'Test caption' | ||
} | ||
}; | ||
|
||
serializer.serialize(card.render(opts)).should.eql('<figure class="kg-gallery-card kg-width-wide"><div class="kg-gallery-container"><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo01-9.jpg" width="3200" height="1600"></div><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo02-10.jpg" width="3200" height="1600"></div><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo03-6.jpg" width="3200" height="1600"></div></div><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo04-7.jpg" width="3200" height="1600" alt="Alt test"></div><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo05-4.jpg" width="3200" height="1600" title="Title test"></div><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo06-6.jpg" width="3200" height="1600"></div></div><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo07-5.jpg" width="3200" height="1600"></div><div class="kg-gallery-image"><img src="/content/images/2018/08/NatGeo09-8.jpg" width="3200" height="1600"></div></div></div><figcaption>Test caption</figcaption></figure>'); | ||
}); | ||
|
||
it('renders nothing with no images', function () { | ||
let opts = { | ||
env: { | ||
dom: new SimpleDom.Document() | ||
}, | ||
payload: { | ||
images: [], | ||
caption: 'Test caption' | ||
} | ||
}; | ||
|
||
serializer.serialize(card.render(opts)).should.eql(''); | ||
}); | ||
}); |
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