Skip to content

Commit

Permalink
Move renderEmojisAsTextComponents to a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
wildan-m committed Apr 20, 2024
1 parent 06e0038 commit dcccca4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 43 deletions.
11 changes: 0 additions & 11 deletions src/components/InlineCodeBlock/getCurrentData.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/components/InlineCodeBlock/index.native.tsx
Original file line number Diff line number Diff line change
@@ -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<TComponent extends TTextOrTPhrasing>({TDefaultRenderer, defaultRendererProps, textStyle, boxModelStyle}: InlineCodeBlockProps<TComponent>) {
const styles = useThemeStyles();
const data = getCurrentData(defaultRendererProps);
const { elements, hasLargeStyle } = renderEmojisAsTextComponents(defaultRendererProps);

return (
<TDefaultRenderer
Expand All @@ -16,9 +16,9 @@ function InlineCodeBlock<TComponent extends TTextOrTPhrasing>({TDefaultRenderer,
>
<WrappedText
textStyles={textStyle}
wordStyles={[boxModelStyle, styles.codeWordStyle]}
wordStyles={[boxModelStyle, styles.codeWordStyle, hasLargeStyle ? styles.onlyEmojisText : {}]}
>
{data}
{elements}
</WrappedText>
</TDefaultRenderer>
);
Expand Down
30 changes: 2 additions & 28 deletions src/components/InlineCodeBlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<TTextOrTPhrasing>, 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(<Text style={[largeStyle, styles.emojiDefault]} key={child.data}>{child.data}</Text>);
} else {
elements.push(child.data);
}
}
});
}

return { elements, hasLargeStyle };
}
import { renderEmojisAsTextComponents } from './renderEmojisAsTextComponents';

function InlineCodeBlock<TComponent extends TTextOrTPhrasing>({ TDefaultRenderer, textStyle, defaultRendererProps, boxModelStyle }: InlineCodeBlockProps<TComponent>) {
const styles = useThemeStyles();
const flattenTextStyle = StyleSheet.flatten(textStyle);
const { textDecorationLine, ...textStyles } = flattenTextStyle;
const { elements, hasLargeStyle } = renderEmojisAsTextComponents(defaultRendererProps, styles);
const { elements, hasLargeStyle } = renderEmojisAsTextComponents(defaultRendererProps);

return (
<TDefaultRenderer
Expand Down
32 changes: 32 additions & 0 deletions src/components/InlineCodeBlock/renderEmojisAsTextComponents.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import Text from '@components/Text';
import type { TTextOrTPhrasing } from './types';
import { TDefaultRendererProps } from 'react-native-render-html';
import useThemeStyles from '@hooks/useThemeStyles';


export function renderEmojisAsTextComponents(defaultRendererProps: TDefaultRendererProps<TTextOrTPhrasing>) {
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(<Text style={[largeStyle, styles.emojiDefault]} key={child.data}>{child.data}</Text>);
} else {
elements.push(child.data);
}
}
});
}

return { elements, hasLargeStyle };
}

0 comments on commit dcccca4

Please sign in to comment.