From dcccca48aa65052ad6136cb2c17493d1ff5f7cc9 Mon Sep 17 00:00:00 2001 From: Wildan Muhlis Date: Sat, 20 Apr 2024 07:55:04 +0700 Subject: [PATCH] Move renderEmojisAsTextComponents to a new file --- .../InlineCodeBlock/getCurrentData.ts | 11 ------- .../InlineCodeBlock/index.native.tsx | 8 ++--- src/components/InlineCodeBlock/index.tsx | 30 ++--------------- .../renderEmojisAsTextComponents.tsx | 32 +++++++++++++++++++ 4 files changed, 38 insertions(+), 43 deletions(-) delete mode 100644 src/components/InlineCodeBlock/getCurrentData.ts create mode 100644 src/components/InlineCodeBlock/renderEmojisAsTextComponents.tsx diff --git a/src/components/InlineCodeBlock/getCurrentData.ts b/src/components/InlineCodeBlock/getCurrentData.ts deleted file mode 100644 index 591ec74c885d..000000000000 --- a/src/components/InlineCodeBlock/getCurrentData.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type {TDefaultRendererProps} from 'react-native-render-html'; -import type {TTextOrTPhrasing} from './types'; - -// Create a temporary solution to display when there are emojis in the inline code block -// We can remove this after https://github.com/Expensify/App/issues/14676 is fixed -export default function getCurrentData(defaultRendererProps: TDefaultRendererProps): string { - if ('data' in defaultRendererProps.tnode) { - return defaultRendererProps.tnode.data; - } - return defaultRendererProps.tnode.children.map((child) => ('data' in child ? child.data : '')).join(''); -} diff --git a/src/components/InlineCodeBlock/index.native.tsx b/src/components/InlineCodeBlock/index.native.tsx index 1c8a1bea4312..fe7bfba62947 100644 --- a/src/components/InlineCodeBlock/index.native.tsx +++ b/src/components/InlineCodeBlock/index.native.tsx @@ -1,13 +1,13 @@ import React from 'react'; import useThemeStyles from '@hooks/useThemeStyles'; -import getCurrentData from './getCurrentData'; import type InlineCodeBlockProps from './types'; import type {TTextOrTPhrasing} from './types'; import WrappedText from './WrappedText'; +import { renderEmojisAsTextComponents } from './renderEmojisAsTextComponents'; function InlineCodeBlock({TDefaultRenderer, defaultRendererProps, textStyle, boxModelStyle}: InlineCodeBlockProps) { const styles = useThemeStyles(); - const data = getCurrentData(defaultRendererProps); + const { elements, hasLargeStyle } = renderEmojisAsTextComponents(defaultRendererProps); return ( ({TDefaultRenderer, > - {data} + {elements} ); diff --git a/src/components/InlineCodeBlock/index.tsx b/src/components/InlineCodeBlock/index.tsx index 4dfe30032d47..f092e41fd3fb 100644 --- a/src/components/InlineCodeBlock/index.tsx +++ b/src/components/InlineCodeBlock/index.tsx @@ -4,39 +4,13 @@ import Text from '@components/Text'; import type InlineCodeBlockProps from './types'; import type { TTextOrTPhrasing } from './types'; import useThemeStyles from '@hooks/useThemeStyles'; -import { TDefaultRendererProps } from 'react-native-render-html'; -import { ThemeStyles } from '@styles/index'; - -function renderEmojisAsTextComponents(defaultRendererProps: TDefaultRendererProps, styles: ThemeStyles) { - const elements: (string | React.JSX.Element)[] = []; - - let hasLargeStyle = false; - if ('data' in defaultRendererProps.tnode) { - elements.push(defaultRendererProps.tnode.data); - } else if (defaultRendererProps.tnode.children) { - defaultRendererProps.tnode.children.forEach((child) => { - if ('data' in child) { - if (child.tagName === 'emoji') { - const largeStyle = 'islarge' in child.attributes ? styles.onlyEmojisText : {}; - if (Object.keys(largeStyle).length > 0) { - hasLargeStyle = true; - } - elements.push({child.data}); - } else { - elements.push(child.data); - } - } - }); - } - - return { elements, hasLargeStyle }; -} +import { renderEmojisAsTextComponents } from './renderEmojisAsTextComponents'; function InlineCodeBlock({ TDefaultRenderer, textStyle, defaultRendererProps, boxModelStyle }: InlineCodeBlockProps) { const styles = useThemeStyles(); const flattenTextStyle = StyleSheet.flatten(textStyle); const { textDecorationLine, ...textStyles } = flattenTextStyle; - const { elements, hasLargeStyle } = renderEmojisAsTextComponents(defaultRendererProps, styles); + const { elements, hasLargeStyle } = renderEmojisAsTextComponents(defaultRendererProps); return ( ) { + const elements: (string | React.JSX.Element)[] = []; + const styles = useThemeStyles(); + + let hasLargeStyle = false; + if ('data' in defaultRendererProps.tnode) { + elements.push(defaultRendererProps.tnode.data); + } else if (defaultRendererProps.tnode.children) { + defaultRendererProps.tnode.children.forEach((child) => { + if ('data' in child) { + if (child.tagName === 'emoji') { + const largeStyle = 'islarge' in child.attributes ? styles.onlyEmojisText : {}; + if (Object.keys(largeStyle).length > 0) { + hasLargeStyle = true; + } + elements.push({child.data}); + } else { + elements.push(child.data); + } + } + }); + } + + return { elements, hasLargeStyle }; +}