import React, { MutableRefObject, useCallback, useEffect, useRef, useState } from 'react'; import { TextInput as TextInputType } from 'react-native'; import { TextInput } from 'components/layout'; import { Text } from 'components/text'; import { Maybe, MessageFragment, RichTextContentSegmentString, RichTextContentSegmentTypeEnum, RichTextContentStringStyle, } from 'expo-schema'; import { localStyle } from 'utils/richText'; import { debounce, uniqueId } from 'lodash'; import { metrics } from 'config'; import { useTheme } from 'hooks'; import { COLOR_VALUES } from 'theme/color'; import { EditorState } from 'lexical'; const { INPUT_DEBOUNCE } = metrics.RICH_TEXT_EDITOR; export const Editor: React.FC = ({ placeholder, richTextContent, setRichTextContent, }) => { const { activeThemeMode } = useTheme(); const [localStrings, setLocalStrings] = useState([]); const richRef = useRef() as MutableRefObject; const { id: segmentId, strings } = richTextContent; const getTextDecoration = (style: Maybe | undefined) => { const isUnderLine = style?.underline ? 'underline' : 'none'; return style?.strikethrough ? 'line-through' : isUnderLine; }; useEffect(() => { if (setRichTextContent) { if (localStrings.length > 0) { setRichTextContent({ order: 1, id: uniqueId(), type: RichTextContentSegmentTypeEnum.Paragraph, strings: localStrings, variant: 'input', }); } } }, [localStrings, setRichTextContent]); const handleChange = useCallback((value: string) => { const segments: string[] = value.trim().split(' '); const parsedSegments: RichTextContentSegmentString[] = segments.map((segment, index) => { return { order: index, value: ' ' + segment, style: localStyle, }; }) as RichTextContentSegmentString[]; setLocalStrings(parsedSegments); }, []); // eslint-disable-next-line react-hooks/exhaustive-deps const debouncedChangeHandler = useCallback(debounce(handleChange, INPUT_DEBOUNCE), []); const placeholderTextColor = activeThemeMode === 'dark' ? COLOR_VALUES.dark['grey-08'] : COLOR_VALUES.light['grey-08']; return ( <> {strings.map(({ id, style, value }) => { return ( {value} ); })} ); }; interface EditorProps { initialState: any; draft: MessageFragment | undefined; message: MessageFragment | undefined; handleChange: (editorState: EditorState) => void; handleSendMessage: (editorState?: EditorState) => void; }