From 4f632f0aafe1e4e84574c5a080d5832d14668fca Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Thu, 15 Apr 2021 10:33:09 +0300 Subject: [PATCH] Fix Composer: Unable to hide text formatter (#1002) --- .../middle/composer/MessageInput.tsx | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/components/middle/composer/MessageInput.tsx b/src/components/middle/composer/MessageInput.tsx index b7c60bb64..b2034335a 100644 --- a/src/components/middle/composer/MessageInput.tsx +++ b/src/components/middle/composer/MessageInput.tsx @@ -56,6 +56,19 @@ const TEXT_FORMATTER_SAFE_AREA_PX = 90; // For some reason Safari inserts `
` after user removes text from input const SAFARI_BR = '
'; +function clearSelection() { + const selection = window.getSelection(); + if (!selection) { + return; + } + + if (selection.removeAllRanges) { + selection.removeAllRanges(); + } else if (selection.empty) { + selection.empty(); + } +} + const MessageInput: FC = ({ id, editableInputId, @@ -106,6 +119,11 @@ const MessageInput: FC = ({ focusEditableElement(inputRef.current!); }, []); + const handleCloseTextFormatter = useCallback(() => { + closeTextFormatter(); + clearSelection(); + }, [closeTextFormatter]); + function checkSelection() { // Disable the formatter on iOS devices for now. if (IS_IOS) { @@ -326,7 +344,7 @@ const MessageInput: FC = ({ isOpen={isTextFormatterOpen} anchorPosition={textFormatterAnchorPosition} selectedRange={selectedRange} - onClose={closeTextFormatter} + onClose={handleCloseTextFormatter} /> );