본문 갤러리 이미지 계산 분리
This commit is contained in:
56
lib/content-gallery-images.js
Normal file
56
lib/content-gallery-images.js
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 공개 본문 라이트박스 이미지 목록을 만든다.
|
||||
* @param {Array<Object>} blocks - Markdown 렌더링 블록 목록
|
||||
* @returns {Array<Object>} 라이트박스 이미지 목록
|
||||
*/
|
||||
export const getContentLightboxImages = (blocks) => {
|
||||
const images = []
|
||||
|
||||
for (const block of blocks || []) {
|
||||
if (block?.type === 'image' && block.url) {
|
||||
images.push({
|
||||
...block,
|
||||
lightboxKey: block.id
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
if (block?.type !== 'gallery' || !Array.isArray(block.images)) {
|
||||
continue
|
||||
}
|
||||
|
||||
block.images.forEach((image, imageIndex) => {
|
||||
if (!image?.url) {
|
||||
return
|
||||
}
|
||||
|
||||
images.push({
|
||||
...image,
|
||||
lightboxKey: `${block.id}:${imageIndex}`
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return images
|
||||
}
|
||||
|
||||
/**
|
||||
* 갤러리 이미지를 행 단위로 나눈다.
|
||||
* @param {Array<Object>} images - 갤러리 이미지 목록
|
||||
* @param {number} [rowSize=3] - 행당 이미지 개수
|
||||
* @returns {Array<{ startIndex: number, images: Array<Object> }>} 행 목록
|
||||
*/
|
||||
export const getGalleryRows = (images, rowSize = 3) => {
|
||||
const safeImages = Array.isArray(images) ? images : []
|
||||
const safeRowSize = Number.isInteger(rowSize) && rowSize > 0 ? rowSize : 3
|
||||
const rows = []
|
||||
|
||||
for (let index = 0; index < safeImages.length; index += safeRowSize) {
|
||||
rows.push({
|
||||
startIndex: index,
|
||||
images: safeImages.slice(index, index + safeRowSize)
|
||||
})
|
||||
}
|
||||
|
||||
return rows
|
||||
}
|
||||
Reference in New Issue
Block a user