import { Scroll, Text } from 'folds'; import React from 'react'; import { RenderElementProps, RenderLeafProps, useFocused, useSelected, useSlate, } from 'slate-react'; import * as css from '../../styles/CustomHtml.css'; import { CommandElement, EmoticonElement, LinkElement, MentionElement } from './slate'; import { useMatrixClient } from '../../hooks/useMatrixClient'; import { getBeginCommand } from './utils'; import { BlockType } from './types'; import { mxcUrlToHttp } from '../../utils/matrix'; import { useMediaAuthentication } from '../../hooks/useMediaAuthentication'; // Put this at the start and end of an inline component to work around this Chromium bug: // https://bugs.chromium.org/p/chromium/issues/detail?id=1249405 function InlineChromiumBugfix() { return ( {String.fromCodePoint(160) /* Non-breaking space */} ); } function RenderMentionElement({ attributes, element, children, }: { element: MentionElement } & RenderElementProps) { const selected = useSelected(); const focused = useFocused(); return ( {element.name} {children} ); } function RenderCommandElement({ attributes, element, children, }: { element: CommandElement } & RenderElementProps) { const selected = useSelected(); const focused = useFocused(); const editor = useSlate(); return ( {`/${element.command}`} {children} ); } function RenderEmoticonElement({ attributes, element, children, }: { element: EmoticonElement } & RenderElementProps) { const mx = useMatrixClient(); const useAuthentication = useMediaAuthentication(); const selected = useSelected(); const focused = useFocused(); return ( {element.key.startsWith('mxc://') ? ( {element.shortcode} ) : ( element.key )} {children} ); } function RenderLinkElement({ attributes, element, children, }: { element: LinkElement } & RenderElementProps) { return ( {children} ); } export function RenderElement({ attributes, element, children }: RenderElementProps) { switch (element.type) { case BlockType.Paragraph: return ( {children} ); case BlockType.Heading: if (element.level === 1) return ( {children} ); if (element.level === 2) return ( {children} ); if (element.level === 3) return ( {children} ); return ( {children} ); case BlockType.CodeLine: return
{children}
; case BlockType.CodeBlock: return (
{children}
); case BlockType.QuoteLine: return
{children}
; case BlockType.BlockQuote: return ( {children} ); case BlockType.ListItem: return ( {children} ); case BlockType.OrderedList: return (
    {children}
); case BlockType.UnorderedList: return ( ); case BlockType.Mention: return ( {children} ); case BlockType.Emoticon: return ( {children} ); case BlockType.Link: return ( {children} ); case BlockType.Command: return ( {children} ); default: return ( {children} ); } } export function RenderLeaf({ attributes, leaf, children }: RenderLeafProps) { let child = children; if (leaf.bold) child = ( {child} ); if (leaf.italic) child = ( {child} ); if (leaf.underline) child = ( {child} ); if (leaf.strikeThrough) child = ( {child} ); if (leaf.code) child = ( {child} ); if (leaf.spoiler) child = ( {child} ); if (child !== children) return child; return {child}; }