Skip to content

Commit

Permalink
fix selection
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymczakJ committed Nov 18, 2024
1 parent b1be58e commit 1d1fc86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
18 changes: 8 additions & 10 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@ export default function App() {
};
}, [textColorState, textFontSizeState]);

const markdownStyle = React.useMemo(() => {
return {
emoji: {
fontSize: emojiFontSizeState ? 15 : 20,
},
link: {
color: linkColorState ? 'red' : 'blue',
},
};
}, [emojiFontSizeState, linkColorState]);
const markdownStyle = {
emoji: {
fontSize: emojiFontSizeState ? 15 : 20,
},
link: {
color: linkColorState ? 'red' : 'blue',
},
};

const ref = React.useRef<MarkdownTextInput>(null);

Expand Down
15 changes: 13 additions & 2 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {getElementHeight, getPlaceholderValue, isEventComposing, normalizeValue,
import {parseToReactDOMStyle, processMarkdownStyle} from './web/utils/webStyleUtils';
import {forceRefreshAllImages} from './web/inputElements/inlineImage';
import type {InlineImagesInputProps} from './commonTypes';
import {deepCompareMarkdownStyles} from './styleUtils';

require('../parser/react-native-live-markdown-parser.js');

Expand Down Expand Up @@ -126,6 +127,16 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
}

const flattenedStyle = useMemo(() => StyleSheet.flatten(style), [style]);
const prevMarkdownStyle = useRef<MarkdownStyle | undefined>(undefined);
const memoizedMarkdownStyle = useMemo(() => {
if (prevMarkdownStyle.current && deepCompareMarkdownStyles(prevMarkdownStyle.current ?? {}, markdownStyle ?? {})) {
return prevMarkdownStyle.current;
}
return markdownStyle;
}, [markdownStyle]);
useEffect(() => {
prevMarkdownStyle.current = memoizedMarkdownStyle;
}, [memoizedMarkdownStyle]); // Runs after state is updated

// Empty placeholder would collapse the div, so we need to use zero-width space to prevent it
const heightSafePlaceholder = useMemo(() => getPlaceholderValue(placeholder), [placeholder]);
Expand Down Expand Up @@ -177,12 +188,12 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
);

const processedMarkdownStyle = useMemo(() => {
const newMarkdownStyle = processMarkdownStyle(markdownStyle);
const newMarkdownStyle = processMarkdownStyle(memoizedMarkdownStyle);
if (divRef.current) {
parseText(divRef.current, divRef.current.value, newMarkdownStyle, null, false, false);
}
return newMarkdownStyle;
}, [markdownStyle, parseText]);
}, [memoizedMarkdownStyle, parseText]);

const inputStyles = useMemo(
() =>
Expand Down

0 comments on commit 1d1fc86

Please sign in to comment.