import type { ContentKitDescendantElement, ContentKitInlineElement, ContentKitRootElement, } from '@gitbook/api'; import React from 'react'; import { ElementBlock } from './ElementBlock'; import { ElementBox } from './ElementBox'; import { ElementButton } from './ElementButton'; import { ElementCard } from './ElementCard'; import { ElementCodeBlock } from './ElementCodeBlock'; import { ElementDivider } from './ElementDivider'; import { ElementIcon } from './ElementIcon'; import { ElementImage } from './ElementImage'; import { ElementInput } from './ElementInput'; import { ElementMarkdown } from './ElementMarkdown'; import { ElementModal } from './ElementModal'; import { ElementStack } from './ElementStack'; import { ElementText } from './ElementText'; import { ElementTextInput } from './ElementTextInput'; import { ElementWebframe } from './ElementWebframe'; import type { ContentKitServerContext } from './types'; export function Element(props: { element: ContentKitDescendantElement | ContentKitRootElement | ContentKitInlineElement; context: ContentKitServerContext; state: object; }) { const { element, context, state } = props; switch (element.type) { case 'text': return ( ); case 'block': return ( ); case 'box': return ( ); case 'divider': return ; case 'markdown': return ; case 'modal': { const content = ( ) : null } > ); if (context.modalWrapper) { const ModalWrapper = context.modalWrapper; return {content}; } return content; } case 'image': { return ; } case 'button': { return ( : null } trailingIcon={ element.trailingIcon ? ( ) : null } /> ); } case 'textinput': { return ; } case 'hstack': case 'vstack': return ( ); case 'card': return ( ) : ( ) ) : null } hint={ element.hint ? ( typeof element.hint === 'string' ? ( element.hint ) : ( ) ) : null } buttons={ element.buttons ? element.buttons.map((button, index) => ( )) : [] } > {element.children ? ( ) : null} ); case 'codeblock': return ; case 'webframe': return ; case 'input': return ; default: return (
                    ContentKit element not implemented "{element.type}":{' '}
                    {JSON.stringify(element, null, 2)}
                
); } } function Elements(props: { elements: string | Array; context: ContentKitServerContext; state: object; }) { const { context, state } = props; const elements = Array.isArray(props.elements) ? props.elements : [props.elements]; return ( <> {elements.map((element, index) => { if (typeof element === 'string') { return {element}; } return ; })} ); } /** * Ensure a set of elements are going to be rendered as a stack (either hstack or vstack). * If the elements are already a stack, return them as is. */ function ensureStackElements( type: 'hstack' | 'vstack', elements: ContentKitDescendantElement[], align?: 'start' | 'center' | 'end' ): ContentKitDescendantElement[] { if (elements.length === 1) { return elements; } return [{ type, align, children: elements }]; }