From 45460d5b8ec9b3e7b3e59c614b68cb4613e96a0b Mon Sep 17 00:00:00 2001 From: Fernando Rojo Date: Wed, 11 Dec 2024 16:35:44 -0500 Subject: [PATCH] remove stylesheet.flatten --- src/MarkdownTextInput.tsx | 36 ++++++++++++++++++---------------- src/web/utils/webStyleUtils.ts | 6 ++++-- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/MarkdownTextInput.tsx b/src/MarkdownTextInput.tsx index b9601378..d7c85e8b 100644 --- a/src/MarkdownTextInput.tsx +++ b/src/MarkdownTextInput.tsx @@ -2,7 +2,6 @@ import type { TextInput, TextInputSubmitEditingEventData, - TextStyle, NativeSyntheticEvent, TextInputSelectionChangeEventData, TextInputProps, @@ -13,7 +12,6 @@ import type { } from 'react-native'; import React, {useEffect, useRef, useCallback, useMemo, useLayoutEffect} from 'react'; import type {CSSProperties, MutableRefObject, ReactEventHandler, FocusEventHandler, MouseEvent, KeyboardEvent, SyntheticEvent, ClipboardEventHandler, TouchEvent} from 'react'; -import {StyleSheet} from 'react-native'; import {updateInputStructure} from './web/utils/parserUtils'; import InputHistory from './web/InputHistory'; import type {TreeNode} from './web/utils/treeUtils'; @@ -69,6 +67,14 @@ type HTMLMarkdownElement = HTMLElement & { value: string; }; +function flattenStyle(style: React.CSSProperties | React.CSSProperties[] | undefined, finalStyle: CSSProperties = {}) { + if (Array.isArray(style)) { + style.forEach((s) => flattenStyle(s, finalStyle)); + return finalStyle; + } + return Object.assign(finalStyle, style); +} + const MarkdownTextInput = React.forwardRef( ( { @@ -98,7 +104,7 @@ const MarkdownTextInput = React.forwardRef StyleSheet.flatten(style), [style]); + const flattenedStyle = useMemo(() => flattenStyle(style as React.CSSProperties), [style]); // Empty placeholder would collapse the div, so we need to use zero-width space to prevent it const heightSafePlaceholder = useMemo(() => getPlaceholderValue(placeholder), [placeholder]); @@ -193,16 +199,12 @@ const MarkdownTextInput = React.forwardRef - StyleSheet.flatten([ - styles.defaultInputStyles, - flattenedStyle && { - caretColor: (flattenedStyle as TextStyle).color || 'black', - }, - {whiteSpace: multiline ? 'pre-wrap' : 'nowrap'}, - disabled && styles.disabledInputStyles, - parseToReactDOMStyle(flattenedStyle), - ]) as CSSProperties, + () => ({ + ...styles.defaultInputStyles, + ...{whiteSpace: multiline ? 'pre-wrap' : 'nowrap'}, + ...(disabled && styles.disabledInputStyles), + ...parseToReactDOMStyle(flattenedStyle), + }), [flattenedStyle, multiline, disabled], ); @@ -761,23 +763,23 @@ const MarkdownTextInput = React.forwardRef; export default MarkdownTextInput; diff --git a/src/web/utils/webStyleUtils.ts b/src/web/utils/webStyleUtils.ts index 1714536b..dff3bf2b 100644 --- a/src/web/utils/webStyleUtils.ts +++ b/src/web/utils/webStyleUtils.ts @@ -1,5 +1,4 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import type {TextStyle} from 'react-native'; import type {MarkdownStyle} from '../../MarkdownTextInputDecoratorViewNativeComponent'; import {mergeMarkdownStyleWithDefault} from '../../styleUtils'; import type {PartialMarkdownStyle} from '../../styleUtils'; @@ -48,7 +47,10 @@ function processMarkdownStyle(input: PartialMarkdownStyle | undefined): Markdown return processUnitsInMarkdownStyle(mergeMarkdownStyleWithDefault(input)); } -function parseToReactDOMStyle(style: TextStyle): any { +/** + * TODO nando DEPRECATE? + */ +function parseToReactDOMStyle(style: React.CSSProperties): any { return createReactDOMStyle(preprocessStyle(style)); }