import React from 'react' import { BaseContentBlock, Block } from 'notion-types' import { getTextContent } from 'notion-utils' import { useNotionContext } from '../context' import { LazyImage } from './lazy-image' const isServer = typeof window === 'undefined' const types = [ 'video', 'image', 'embed', 'figma', 'typeform', 'excalidraw', 'maps', 'tweet', 'pdf', 'gist', 'codepen', 'drive' ] export const Asset: React.FC<{ block: BaseContentBlock }> = ({ block }) => { const { recordMap, mapImageUrl, components } = useNotionContext() if (!block || !types.includes(block.type)) { return null } const style: React.CSSProperties = { position: 'relative', display: 'flex', justifyContent: 'center', alignSelf: 'center', width: '100%', maxWidth: '100%' } const assetStyle: React.CSSProperties = {} // console.log('asset', block) if (block.format) { const { block_aspect_ratio, block_height, block_width, block_full_width, block_page_width, block_preserve_scale } = block.format if (block_full_width || block_page_width) { if (block_full_width) { style.width = '100vw' } else { style.width = '100%' } if (block_aspect_ratio && block.type !== 'image') { // console.log(block.type, block) style.paddingBottom = `${block_aspect_ratio * 100}%` } else if (block_height) { style.height = block_height } else if (block_preserve_scale) { if (block.type === 'image') { style.height = '100%' } else { // TODO: this is just a guess style.paddingBottom = '75%' style.minHeight = 100 } } } else { if (block_width) { style.width = block_width } if (block_preserve_scale && block.type !== 'image') { style.paddingBottom = '50%' style.minHeight = 100 } else { if (block_height && block.type !== 'image') { style.height = block_height } } } if (block_preserve_scale || block.type === 'image') { assetStyle.objectFit = 'contain' } } const source = block.properties?.source?.[0]?.[0] let content = null if (block.type === 'tweet') { const src = source if (!src) return null const id = src.split('?')[0].split('/').pop() if (!id) return null content = (