diff --git a/src/renderers/offprint.ts b/src/renderers/offprint.ts index c1e2337..89f5605 100644 --- a/src/renderers/offprint.ts +++ b/src/renderers/offprint.ts @@ -178,17 +178,35 @@ type ImageLike = { aspectRatio?: app.offprint.block.image.AspectRatio } +function imageGridAspectRatioStyle(aspectRatio?: "landscape" | "portrait" | "square" | "mosaic") { + switch (aspectRatio) { + case "landscape": + return "aspect-ratio:16/9" + case "portrait": + return "aspect-ratio:3/4" + case "square": + return "aspect-ratio:1/1" + default: + return "aspect-ratio:4/3" + } +} + function renderGridImages(images: ImageLike[], opts: StandardSiteDocumentRendererOptions) { const imagePaths: string[] = [] const html = images .map((image) => { const src = resolveBlobSrc(getImageBlob(image), opts) - const aspectRatio = image.aspectRatio - ? ` style="aspect-ratio:${image.aspectRatio.width}/${image.aspectRatio.height}"` - : "" - if (!src) return `
` + const styles = [ + "display:block", + "width:100%", + "height:100%", + "object-fit:cover", + image.aspectRatio ? `aspect-ratio:${image.aspectRatio.width}/${image.aspectRatio.height}` : undefined, + ].filter((style) => !!style) + const style = ` style="${styles.join(";")}"` + if (!src) return `
` imagePaths.push(src) - return `${escapeHtml(image.alt ?? ` + return `${escapeHtml(image.alt ?? ` }) .join("") @@ -447,9 +465,10 @@ export const blockImageCarouselRenderer: RendererFunction< > = async (entry, opts) => { const { html, imagePaths } = renderGridImages(entry.images as ImageLike[], opts) const caption = entry.caption ? `
${escapeHtml(entry.caption)}
` : "" + const gridStyle = `display:grid;grid-template-columns:repeat(${Math.min(entry.images.length, 3)}, minmax(0, 1fr));gap:8px` return { - html: ``, + html: ``, metadata: { imagePaths, }, @@ -468,9 +487,10 @@ export const blockImageDiffRenderer: RendererFunction< entry.alignment === "right" ? "margin-left:auto" : undefined, ].filter((style) => !!style) const style = styles.length > 0 ? ` style="${styles.join(";")}"` : "" + const gridStyle = "display:grid;grid-template-columns:repeat(2, minmax(0, 1fr));gap:8px" return { - html: `
${html}
${caption}
`, + html: `
${html}
${caption}
`, metadata: { imagePaths, }, @@ -483,11 +503,16 @@ export const blockImageGridRenderer: RendererFunction< > = async (entry, opts) => { const { html, imagePaths } = renderGridImages(entry.images as ImageLike[], opts) const caption = entry.caption ? `
${escapeHtml(entry.caption)}
` : "" - const rows = entry.gridRows ? `--grid-rows:${entry.gridRows}` : undefined - const style = rows ? ` style="${rows}"` : "" + const columns = Math.ceil(entry.images.length / (entry.gridRows ?? 2)) + const gridStyle = [ + "display:grid", + `grid-template-columns:repeat(${columns}, minmax(0, 1fr))`, + "gap:8px", + imageGridAspectRatioStyle(entry.aspectRatio), + ].join(";") return { - html: `
${html}
${caption}
`, + html: `
${html}
${caption}
`, metadata: { imagePaths, },