Attach composition keyboard handlers only when quill has focus
This commit is contained in:
parent
b292568604
commit
cc6b299ab6
2 changed files with 37 additions and 2 deletions
|
@ -54,7 +54,7 @@ import { countStickers } from './stickers/lib';
|
||||||
import {
|
import {
|
||||||
useAttachFileShortcut,
|
useAttachFileShortcut,
|
||||||
useEditLastMessageSent,
|
useEditLastMessageSent,
|
||||||
useKeyboardShortcuts,
|
useKeyboardShortcutsConditionally,
|
||||||
} from '../hooks/useKeyboardShortcuts';
|
} from '../hooks/useKeyboardShortcuts';
|
||||||
import { MediaEditor } from './MediaEditor';
|
import { MediaEditor } from './MediaEditor';
|
||||||
import { isImageTypeSupported } from '../util/GoogleChrome';
|
import { isImageTypeSupported } from '../util/GoogleChrome';
|
||||||
|
@ -416,9 +416,15 @@ export function CompositionArea({
|
||||||
setMessageToEdit,
|
setMessageToEdit,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const [hasFocus, setHasFocus] = useState(false);
|
||||||
|
|
||||||
const attachFileShortcut = useAttachFileShortcut(launchAttachmentPicker);
|
const attachFileShortcut = useAttachFileShortcut(launchAttachmentPicker);
|
||||||
const editLastMessageSent = useEditLastMessageSent(maybeEditMessage);
|
const editLastMessageSent = useEditLastMessageSent(maybeEditMessage);
|
||||||
useKeyboardShortcuts(attachFileShortcut, editLastMessageSent);
|
useKeyboardShortcutsConditionally(
|
||||||
|
hasFocus,
|
||||||
|
attachFileShortcut,
|
||||||
|
editLastMessageSent
|
||||||
|
);
|
||||||
|
|
||||||
// Focus input on first mount
|
// Focus input on first mount
|
||||||
const previousFocusCounter = usePrevious<number | undefined>(
|
const previousFocusCounter = usePrevious<number | undefined>(
|
||||||
|
@ -954,6 +960,8 @@ export function CompositionArea({
|
||||||
large={large}
|
large={large}
|
||||||
linkPreviewLoading={linkPreviewLoading}
|
linkPreviewLoading={linkPreviewLoading}
|
||||||
linkPreviewResult={linkPreviewResult}
|
linkPreviewResult={linkPreviewResult}
|
||||||
|
onBlur={() => setHasFocus(false)}
|
||||||
|
onFocus={() => setHasFocus(true)}
|
||||||
onCloseLinkPreview={onCloseLinkPreview}
|
onCloseLinkPreview={onCloseLinkPreview}
|
||||||
onDirtyChange={setDirty}
|
onDirtyChange={setDirty}
|
||||||
onEditorStateChange={onEditorStateChange}
|
onEditorStateChange={onEditorStateChange}
|
||||||
|
|
|
@ -120,6 +120,8 @@ export type Props = Readonly<{
|
||||||
}): unknown;
|
}): unknown;
|
||||||
onTextTooLong(): unknown;
|
onTextTooLong(): unknown;
|
||||||
onPickEmoji(o: EmojiPickDataType): unknown;
|
onPickEmoji(o: EmojiPickDataType): unknown;
|
||||||
|
onBlur?: () => unknown;
|
||||||
|
onFocus?: () => unknown;
|
||||||
onSubmit(
|
onSubmit(
|
||||||
message: string,
|
message: string,
|
||||||
bodyRanges: DraftBodyRanges,
|
bodyRanges: DraftBodyRanges,
|
||||||
|
@ -159,6 +161,8 @@ export function CompositionInput(props: Props): React.ReactElement {
|
||||||
linkPreviewResult,
|
linkPreviewResult,
|
||||||
moduleClassName,
|
moduleClassName,
|
||||||
onCloseLinkPreview,
|
onCloseLinkPreview,
|
||||||
|
onBlur,
|
||||||
|
onFocus,
|
||||||
onPickEmoji,
|
onPickEmoji,
|
||||||
onScroll,
|
onScroll,
|
||||||
onSubmit,
|
onSubmit,
|
||||||
|
@ -610,6 +614,29 @@ export function CompositionInput(props: Props): React.ReactElement {
|
||||||
quill.focus();
|
quill.focus();
|
||||||
}, [disabled]);
|
}, [disabled]);
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const quill = quillRef.current;
|
||||||
|
|
||||||
|
if (quill === undefined) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleFocus() {
|
||||||
|
onFocus?.();
|
||||||
|
}
|
||||||
|
function handleBlur() {
|
||||||
|
onBlur?.();
|
||||||
|
}
|
||||||
|
|
||||||
|
quill.root.addEventListener('focus', handleFocus);
|
||||||
|
quill.root.addEventListener('blur', handleBlur);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
quill.root.removeEventListener('focus', handleFocus);
|
||||||
|
quill.root.removeEventListener('blur', handleBlur);
|
||||||
|
};
|
||||||
|
}, [onFocus, onBlur]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const emojiCompletion = emojiCompletionRef.current;
|
const emojiCompletion = emojiCompletionRef.current;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue