From cca37bc9e87e88ccd45599e3c3e01fdca068b98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Sun, 29 Oct 2023 09:05:48 -0400 Subject: [PATCH] Start rendering images --- src/components/DocumentView/Images.tsx | 70 +++++++++++++++++++++++--- src/lib/document.ts | 20 +++++++- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/components/DocumentView/Images.tsx b/src/components/DocumentView/Images.tsx index 995aefaa8..b8da12f51 100644 --- a/src/components/DocumentView/Images.tsx +++ b/src/components/DocumentView/Images.tsx @@ -1,15 +1,71 @@ +import Image from 'next/image' + import { BlockProps } from './Block'; -import { Blocks } from './Blocks'; +import { ContentRefContext, resolveContentRef } from '@/lib/references'; +import { ClassValue, tcls } from '@/lib/tailwind'; +import { getNodeFragmentByName, isNodeEmpty } from '@/lib/document'; +import { Inlines } from './Inlines'; export function Images(props: BlockProps) { const { block, style, ...contextProps } = props; return ( - +
+ {block.nodes.map((node: any, i: number) => ( + 0 && 'mt-4', style]} + siblings={block.nodes.length} + {...contextProps} + /> + ))} +
+ ); +} + +async function ImageBlock(props: { + block: any; + style: ClassValue; + context: ContentRefContext; + siblings: number; +}) { + const { block, context, siblings } = props; + + const [src, darkSrc] = await Promise.all([ + resolveContentRef(block.data.ref, context), + block.data.darkRef ? resolveContentRef(block.data.darkRef, context) : null + ]); + + if (!src) { + return null; + } + + const caption = getNodeFragmentByName(block, 'caption'); + + const image = ( + {block.data.alt} + ); + + if (!caption || isNodeEmpty(caption)) { + return image; + } + + return ( + + {image} +
+ +
+
); } diff --git a/src/lib/document.ts b/src/lib/document.ts index 863a8cf25..058878c16 100644 --- a/src/lib/document.ts +++ b/src/lib/document.ts @@ -9,7 +9,7 @@ export interface DocumentSection { */ export function hasFullWidthBlock(document: any): boolean { return document.nodes.some((node) => { - return node.data.fullWidth || node.type === 'images'; + return node.data.fullWidth; }); } @@ -50,6 +50,7 @@ export function getNodeText(node: any): string { switch (node.object) { case 'text': return node.leaves.map((leaf) => leaf.text).join(''); + case 'fragment': case 'block': case 'inline': return node.nodes.map((child) => getNodeText(child)).join(''); @@ -65,3 +66,20 @@ export function getNodeFragmentByType(node: any, type: string): any { const fragment = node.fragments?.find((child: any) => child.type === type); return fragment; } + +/** + * Get a fragment by its `fragment` name in a node. + */ +export function getNodeFragmentByName(node: any, name: string): any { + const fragment = node.fragments?.find((child: any) => child.fragment === name); + return fragment; +} + +/** + * Test if a node is empty. + */ +export function isNodeEmpty(node: any): boolean { + const text = getNodeText(node); + return text.trim().length === 0; +} +