// Copyright 2019-2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import * as React from 'react'; import { get, noop } from 'lodash'; import classNames from 'classnames'; import { EmojiButton, Props as EmojiButtonProps } from './emoji/EmojiButton'; import { Props as StickerButtonProps, StickerButton, } from './stickers/StickerButton'; import { CompositionInput, InputApi, Props as CompositionInputProps, } from './CompositionInput'; import { MessageRequestActions, Props as MessageRequestActionsProps, } from './conversation/MessageRequestActions'; import { MandatoryProfileSharingActions } from './conversation/MandatoryProfileSharingActions'; import { countStickers } from './stickers/lib'; import { LocalizerType } from '../types/Util'; import { EmojiPickDataType } from './emoji/EmojiPicker'; export type OwnProps = { readonly i18n: LocalizerType; readonly areWePending?: boolean; readonly groupVersion?: 1 | 2; readonly isMissingMandatoryProfileSharing?: boolean; readonly messageRequestsEnabled?: boolean; readonly acceptedMessageRequest?: boolean; readonly compositionApi?: React.MutableRefObject<{ focusInput: () => void; isDirty: () => boolean; setDisabled: (disabled: boolean) => void; setShowMic: (showMic: boolean) => void; setMicActive: (micActive: boolean) => void; attSlotRef: React.RefObject; reset: InputApi['reset']; resetEmojiResults: InputApi['resetEmojiResults']; }>; readonly micCellEl?: HTMLElement; readonly attCellEl?: HTMLElement; readonly attachmentListEl?: HTMLElement; onChooseAttachment(): unknown; }; export type Props = Pick< CompositionInputProps, | 'members' | 'onSubmit' | 'onEditorStateChange' | 'onTextTooLong' | 'draftText' | 'draftBodyRanges' | 'clearQuotedMessage' | 'getQuotedMessage' > & Pick< EmojiButtonProps, 'onPickEmoji' | 'onSetSkinTone' | 'recentEmojis' | 'skinTone' > & Pick< StickerButtonProps, | 'knownPacks' | 'receivedPacks' | 'installedPack' | 'installedPacks' | 'blessedPacks' | 'recentStickers' | 'clearInstalledStickerPack' | 'onClickAddPack' | 'onPickSticker' | 'clearShowIntroduction' | 'showPickerHint' | 'clearShowPickerHint' > & MessageRequestActionsProps & OwnProps; const emptyElement = (el: HTMLElement) => { // Necessary to deal with Backbone views // eslint-disable-next-line no-param-reassign el.innerHTML = ''; }; export const CompositionArea = ({ i18n, attachmentListEl, micCellEl, onChooseAttachment, // CompositionInput onSubmit, compositionApi, onEditorStateChange, onTextTooLong, draftText, draftBodyRanges, clearQuotedMessage, getQuotedMessage, members, // EmojiButton onPickEmoji, onSetSkinTone, recentEmojis, skinTone, // StickerButton knownPacks, receivedPacks, installedPack, installedPacks, blessedPacks, recentStickers, clearInstalledStickerPack, onClickAddPack, onPickSticker, clearShowIntroduction, showPickerHint, clearShowPickerHint, // Message Requests acceptedMessageRequest, areWePending, conversationType, groupVersion, isBlocked, isMissingMandatoryProfileSharing, messageRequestsEnabled, name, onAccept, onBlock, onBlockAndDelete, onDelete, onUnblock, phoneNumber, profileName, title, }: Props): JSX.Element => { const [disabled, setDisabled] = React.useState(false); const [showMic, setShowMic] = React.useState(!draftText); const [micActive, setMicActive] = React.useState(false); const [dirty, setDirty] = React.useState(false); const [large, setLarge] = React.useState(false); const inputApiRef = React.useRef(); const handleForceSend = React.useCallback(() => { setLarge(false); if (inputApiRef.current) { inputApiRef.current.submit(); } }, [inputApiRef, setLarge]); const handleSubmit = React.useCallback( (...args) => { setLarge(false); onSubmit(...args); }, [setLarge, onSubmit] ); const focusInput = React.useCallback(() => { if (inputApiRef.current) { inputApiRef.current.focus(); } }, [inputApiRef]); const withStickers = countStickers({ knownPacks, blessedPacks, installedPacks, receivedPacks, }) > 0; // A ref to grab a slot where backbone can insert link previews and attachments const attSlotRef = React.useRef(null); if (compositionApi) { // Using a React.MutableRefObject, so we need to reassign this prop. // eslint-disable-next-line no-param-reassign compositionApi.current = { isDirty: () => dirty, focusInput, setDisabled, setShowMic, setMicActive, attSlotRef, reset: () => { if (inputApiRef.current) { inputApiRef.current.reset(); } }, resetEmojiResults: () => { if (inputApiRef.current) { inputApiRef.current.resetEmojiResults(); } }, }; } const insertEmoji = React.useCallback( (e: EmojiPickDataType) => { if (inputApiRef.current) { inputApiRef.current.insertEmoji(e); onPickEmoji(e); } }, [inputApiRef, onPickEmoji] ); const handleToggleLarge = React.useCallback(() => { setLarge(l => !l); }, [setLarge]); // The following is a work-around to allow react to lay-out backbone-managed // dom nodes until those functions are in React const micCellRef = React.useRef(null); React.useLayoutEffect(() => { const { current: micCellContainer } = micCellRef; if (micCellContainer && micCellEl) { emptyElement(micCellContainer); micCellContainer.appendChild(micCellEl); } return noop; }, [micCellRef, micCellEl, large, dirty, showMic]); React.useLayoutEffect(() => { const { current: attSlot } = attSlotRef; if (attSlot && attachmentListEl) { attSlot.appendChild(attachmentListEl); } return noop; }, [attSlotRef, attachmentListEl]); const emojiButtonFragment = (
); const micButtonFragment = showMic ? (
) : null; const attButton = (
); const sendButtonFragment = (
); const stickerButtonPlacement = large ? 'top-start' : 'top-end'; const stickerButtonFragment = withStickers ? (
) : null; // Listen for cmd/ctrl-shift-x to toggle large composition mode React.useEffect(() => { const handler = (e: KeyboardEvent) => { const { key, shiftKey, ctrlKey, metaKey } = e; // When using the ctrl key, `key` is `'X'`. When using the cmd key, `key` is `'x'` const xKey = key === 'x' || key === 'X'; const commandKey = get(window, 'platform') === 'darwin' && metaKey; const controlKey = get(window, 'platform') !== 'darwin' && ctrlKey; const commandOrCtrl = commandKey || controlKey; // cmd/ctrl-shift-x if (xKey && shiftKey && commandOrCtrl) { e.preventDefault(); setLarge(x => !x); } }; document.addEventListener('keydown', handler); return () => { document.removeEventListener('keydown', handler); }; }, [setLarge]); if ( isBlocked || areWePending || (messageRequestsEnabled && !acceptedMessageRequest) ) { return ( ); } // If no message request, but we haven't shared profile yet, we show profile-sharing UI if ( (conversationType === 'direct' || (conversationType === 'group' && groupVersion === 1)) && isMissingMandatoryProfileSharing ) { return ( ); } return (
{!large ? emojiButtonFragment : null}
{!large ? ( <> {stickerButtonFragment} {!dirty ? micButtonFragment : null} {attButton} ) : null}
{large ? (
{emojiButtonFragment} {stickerButtonFragment} {attButton} {!dirty ? micButtonFragment : null} {dirty || !showMic ? sendButtonFragment : null}
) : null}
); };