Composer: Fix freezing after multiple formatting (#1548)

This commit is contained in:
Alexander Zinchuk 2021-11-17 17:49:46 +03:00
parent c876400014
commit 5e72caa385
2 changed files with 16 additions and 3 deletions

View File

@ -398,6 +398,7 @@ const MessageInput: FC<OwnProps & StateProps & DispatchProps> = ({
isOpen={isTextFormatterOpen} isOpen={isTextFormatterOpen}
anchorPosition={textFormatterAnchorPosition} anchorPosition={textFormatterAnchorPosition}
selectedRange={selectedRange} selectedRange={selectedRange}
setSelectedRange={setSelectedRange}
onClose={handleCloseTextFormatter} onClose={handleCloseTextFormatter}
/> />
{forcedPlaceholder && <span className="forced-placeholder">{renderText(forcedPlaceholder!)}</span>} {forcedPlaceholder && <span className="forced-placeholder">{renderText(forcedPlaceholder!)}</span>}

View File

@ -20,6 +20,7 @@ export type OwnProps = {
isOpen: boolean; isOpen: boolean;
anchorPosition?: IAnchorPosition; anchorPosition?: IAnchorPosition;
selectedRange?: Range; selectedRange?: Range;
setSelectedRange: (range: Range) => void;
onClose: () => void; onClose: () => void;
}; };
@ -46,6 +47,7 @@ const TextFormatter: FC<OwnProps> = ({
isOpen, isOpen,
anchorPosition, anchorPosition,
selectedRange, selectedRange,
setSelectedRange,
onClose, onClose,
}) => { }) => {
// eslint-disable-next-line no-null/no-null // eslint-disable-next-line no-null/no-null
@ -114,6 +116,13 @@ const TextFormatter: FC<OwnProps> = ({
} }
} }
const updateSelectedRange = useCallback(() => {
const selection = window.getSelection();
if (selection) {
setSelectedRange(selection.getRangeAt(0));
}
}, [setSelectedRange]);
const getSelectedText = useCallback(() => { const getSelectedText = useCallback(() => {
if (!selectedRange) { if (!selectedRange) {
return undefined; return undefined;
@ -187,28 +196,31 @@ const TextFormatter: FC<OwnProps> = ({
} }
}); });
updateSelectedRange();
return { return {
...selectedFormats, ...selectedFormats,
bold: !selectedFormats.bold, bold: !selectedFormats.bold,
}; };
}); });
}, []); }, [updateSelectedRange]);
const handleItalicText = useCallback(() => { const handleItalicText = useCallback(() => {
document.execCommand('italic'); document.execCommand('italic');
updateSelectedRange();
setSelectedTextFormats((selectedFormats) => ({ setSelectedTextFormats((selectedFormats) => ({
...selectedFormats, ...selectedFormats,
italic: !selectedFormats.italic, italic: !selectedFormats.italic,
})); }));
}, []); }, [updateSelectedRange]);
const handleUnderlineText = useCallback(() => { const handleUnderlineText = useCallback(() => {
document.execCommand('underline'); document.execCommand('underline');
updateSelectedRange();
setSelectedTextFormats((selectedFormats) => ({ setSelectedTextFormats((selectedFormats) => ({
...selectedFormats, ...selectedFormats,
underline: !selectedFormats.underline, underline: !selectedFormats.underline,
})); }));
}, []); }, [updateSelectedRange]);
const handleStrikethroughText = useCallback(() => { const handleStrikethroughText = useCallback(() => {
if (selectedTextFormats.strikethrough) { if (selectedTextFormats.strikethrough) {