2022-09-10 14:52:06 -07:00
|
|
|
import classNames from 'clsx';
|
|
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
2022-09-20 15:12:52 -07:00
|
|
|
import { defineMessages, FormattedMessage, MessageDescriptor, useIntl } from 'react-intl';
|
2022-09-10 14:52:06 -07:00
|
|
|
import { Link, useHistory } from 'react-router-dom';
|
|
|
|
import { length } from 'stringz';
|
|
|
|
|
|
|
|
import {
|
|
|
|
changeCompose,
|
|
|
|
submitCompose,
|
|
|
|
clearComposeSuggestions,
|
|
|
|
fetchComposeSuggestions,
|
|
|
|
selectComposeSuggestion,
|
|
|
|
insertEmojiCompose,
|
|
|
|
uploadCompose,
|
|
|
|
} from 'soapbox/actions/compose';
|
2022-11-15 08:11:42 -08:00
|
|
|
import AutosuggestInput, { AutoSuggestion } from 'soapbox/components/autosuggest-input';
|
|
|
|
import AutosuggestTextarea from 'soapbox/components/autosuggest-textarea';
|
2022-11-25 09:04:11 -08:00
|
|
|
import { Button, HStack, Stack } from 'soapbox/components/ui';
|
2022-11-26 08:38:16 -08:00
|
|
|
import { useAppDispatch, useAppSelector, useCompose, useFeatures, useInstance, usePrevious } from 'soapbox/hooks';
|
2022-11-15 12:48:54 -08:00
|
|
|
import { isMobile } from 'soapbox/is-mobile';
|
2022-09-10 14:52:06 -07:00
|
|
|
|
2022-11-15 09:23:36 -08:00
|
|
|
import QuotedStatusContainer from '../containers/quoted-status-container';
|
|
|
|
import ReplyIndicatorContainer from '../containers/reply-indicator-container';
|
|
|
|
import ScheduleFormContainer from '../containers/schedule-form-container';
|
|
|
|
import UploadButtonContainer from '../containers/upload-button-container';
|
|
|
|
import WarningContainer from '../containers/warning-container';
|
2022-09-10 14:52:06 -07:00
|
|
|
import { countableText } from '../util/counter';
|
|
|
|
|
2022-11-15 09:23:36 -08:00
|
|
|
import EmojiPickerDropdown from './emoji-picker/emoji-picker-dropdown';
|
|
|
|
import MarkdownButton from './markdown-button';
|
|
|
|
import PollButton from './poll-button';
|
|
|
|
import PollForm from './polls/poll-form';
|
|
|
|
import PrivacyDropdown from './privacy-dropdown';
|
|
|
|
import ReplyMentions from './reply-mentions';
|
|
|
|
import ScheduleButton from './schedule-button';
|
|
|
|
import SpoilerButton from './spoiler-button';
|
2022-10-31 14:19:51 -07:00
|
|
|
import SpoilerInput from './spoiler-input';
|
2022-11-15 09:23:36 -08:00
|
|
|
import TextCharacterCounter from './text-character-counter';
|
|
|
|
import UploadForm from './upload-form';
|
|
|
|
import VisualCharacterCounter from './visual-character-counter';
|
|
|
|
import Warning from './warning';
|
2022-09-10 14:52:06 -07:00
|
|
|
|
2022-11-15 08:11:42 -08:00
|
|
|
import type { Emoji } from 'soapbox/components/autosuggest-emoji';
|
2022-09-10 14:52:06 -07:00
|
|
|
|
|
|
|
const allowedAroundShortCode = '><\u0085\u0020\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\u2028\u2029\u0009\u000a\u000b\u000c\u000d';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What\'s on your mind?' },
|
2022-09-20 15:12:52 -07:00
|
|
|
pollPlaceholder: { id: 'compose_form.poll_placeholder', defaultMessage: 'Add a poll topic…' },
|
|
|
|
eventPlaceholder: { id: 'compose_form.event_placeholder', defaultMessage: 'Post to this event' },
|
2022-10-31 14:19:51 -07:00
|
|
|
spoiler_placeholder: { id: 'compose_form.spoiler_placeholder', defaultMessage: 'Write your warning here (optional)' },
|
2022-09-10 14:52:06 -07:00
|
|
|
publish: { id: 'compose_form.publish', defaultMessage: 'Post' },
|
|
|
|
publishLoud: { id: 'compose_form.publish_loud', defaultMessage: '{publish}!' },
|
|
|
|
message: { id: 'compose_form.message', defaultMessage: 'Message' },
|
|
|
|
schedule: { id: 'compose_form.schedule', defaultMessage: 'Schedule' },
|
|
|
|
saveChanges: { id: 'compose_form.save_changes', defaultMessage: 'Save changes' },
|
|
|
|
});
|
|
|
|
|
2022-09-16 11:18:12 -07:00
|
|
|
interface IComposeForm<ID extends string> {
|
|
|
|
id: ID extends 'default' ? never : ID,
|
2022-09-10 14:52:06 -07:00
|
|
|
shouldCondense?: boolean,
|
|
|
|
autoFocus?: boolean,
|
|
|
|
clickableAreaRef?: React.RefObject<HTMLDivElement>,
|
2022-09-30 14:20:58 -07:00
|
|
|
event?: string,
|
2022-09-10 14:52:06 -07:00
|
|
|
}
|
|
|
|
|
2022-09-30 14:20:58 -07:00
|
|
|
const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickableAreaRef, event }: IComposeForm<ID>) => {
|
2022-09-10 14:52:06 -07:00
|
|
|
const history = useHistory();
|
|
|
|
const intl = useIntl();
|
|
|
|
const dispatch = useAppDispatch();
|
2022-11-26 08:38:16 -08:00
|
|
|
const { configuration } = useInstance();
|
2022-09-10 14:52:06 -07:00
|
|
|
|
2022-09-14 11:01:00 -07:00
|
|
|
const compose = useCompose(id);
|
2022-09-10 14:52:06 -07:00
|
|
|
const showSearch = useAppSelector((state) => state.search.submitted && !state.search.hidden);
|
|
|
|
const isModalOpen = useAppSelector((state) => !!(state.modals.size && state.modals.last()!.modalType === 'COMPOSE'));
|
2022-11-26 08:38:16 -08:00
|
|
|
const maxTootChars = configuration.getIn(['statuses', 'max_characters']) as number;
|
2022-12-25 15:31:07 -08:00
|
|
|
const scheduledStatusCount = useAppSelector((state) => state.scheduled_statuses.size);
|
2022-09-10 14:52:06 -07:00
|
|
|
const features = useFeatures();
|
|
|
|
|
|
|
|
const { text, suggestions, spoiler, spoiler_text: spoilerText, privacy, focusDate, caretPosition, is_submitting: isSubmitting, is_changing_upload: isChangingUpload, is_uploading: isUploading, schedule: scheduledAt } = compose;
|
2022-09-19 11:55:08 -07:00
|
|
|
const prevSpoiler = usePrevious(spoiler);
|
2022-09-10 14:52:06 -07:00
|
|
|
|
|
|
|
const hasPoll = !!compose.poll;
|
|
|
|
const isEditing = compose.id !== null;
|
|
|
|
const anyMedia = compose.media_attachments.size > 0;
|
|
|
|
|
|
|
|
const [composeFocused, setComposeFocused] = useState(false);
|
|
|
|
|
|
|
|
const formRef = useRef(null);
|
|
|
|
const spoilerTextRef = useRef<AutosuggestInput>(null);
|
|
|
|
const autosuggestTextareaRef = useRef<AutosuggestTextarea>(null);
|
|
|
|
|
|
|
|
const handleChange: React.ChangeEventHandler<HTMLTextAreaElement> = (e) => {
|
|
|
|
dispatch(changeCompose(id, e.target.value));
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleKeyDown: React.KeyboardEventHandler = (e) => {
|
|
|
|
if (e.keyCode === 13 && (e.ctrlKey || e.metaKey)) {
|
|
|
|
handleSubmit();
|
|
|
|
e.preventDefault(); // Prevent bubbling to other ComposeForm instances
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getClickableArea = () => {
|
|
|
|
return clickableAreaRef ? clickableAreaRef.current : formRef.current;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isEmpty = () => {
|
|
|
|
return !(text || spoilerText || anyMedia);
|
|
|
|
};
|
|
|
|
|
|
|
|
const isClickOutside = (e: MouseEvent | React.MouseEvent) => {
|
|
|
|
return ![
|
|
|
|
// List of elements that shouldn't collapse the composer when clicked
|
|
|
|
// FIXME: Make this less brittle
|
|
|
|
getClickableArea(),
|
|
|
|
document.querySelector('.privacy-dropdown__dropdown'),
|
|
|
|
document.querySelector('.emoji-picker-dropdown__menu'),
|
|
|
|
document.getElementById('modal-overlay'),
|
|
|
|
].some(element => element?.contains(e.target as any));
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClick = useCallback((e: MouseEvent | React.MouseEvent) => {
|
|
|
|
if (isEmpty() && isClickOutside(e)) {
|
|
|
|
handleClickOutside();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleClickOutside = () => {
|
|
|
|
setComposeFocused(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleComposeFocus = () => {
|
|
|
|
setComposeFocused(true);
|
|
|
|
};
|
|
|
|
|
2022-11-05 03:46:15 -07:00
|
|
|
const handleSubmit = (e?: React.FormEvent<Element>) => {
|
2022-09-10 14:52:06 -07:00
|
|
|
if (text !== autosuggestTextareaRef.current?.textarea?.value) {
|
|
|
|
// Something changed the text inside the textarea (e.g. browser extensions like Grammarly)
|
|
|
|
// Update the state to match the current text
|
|
|
|
dispatch(changeCompose(id, autosuggestTextareaRef.current!.textarea!.value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submit disabled:
|
|
|
|
const fulltext = [spoilerText, countableText(text)].join('');
|
|
|
|
|
2022-11-05 03:46:15 -07:00
|
|
|
if (e) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
|
2022-09-10 14:52:06 -07:00
|
|
|
if (isSubmitting || isUploading || isChangingUpload || length(fulltext) > maxTootChars || (fulltext.length !== 0 && fulltext.trim().length === 0 && !anyMedia)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch(submitCompose(id, history));
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSuggestionsClearRequested = () => {
|
2022-09-14 13:05:40 -07:00
|
|
|
dispatch(clearComposeSuggestions(id));
|
2022-09-10 14:52:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const onSuggestionsFetchRequested = (token: string | number) => {
|
2022-09-14 13:05:40 -07:00
|
|
|
dispatch(fetchComposeSuggestions(id, token as string));
|
2022-09-10 14:52:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const onSuggestionSelected = (tokenStart: number, token: string | null, value: string | undefined) => {
|
2022-09-14 13:05:40 -07:00
|
|
|
if (value) dispatch(selectComposeSuggestion(id, tokenStart, token, value, ['text']));
|
2022-09-10 14:52:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const onSpoilerSuggestionSelected = (tokenStart: number, token: string | null, value: AutoSuggestion) => {
|
2022-09-14 13:05:40 -07:00
|
|
|
dispatch(selectComposeSuggestion(id, tokenStart, token, value, ['spoiler_text']));
|
2022-09-10 14:52:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const setCursor = (start: number, end: number = start) => {
|
|
|
|
if (!autosuggestTextareaRef.current?.textarea) return;
|
|
|
|
autosuggestTextareaRef.current.textarea.setSelectionRange(start, end);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleEmojiPick = (data: Emoji) => {
|
|
|
|
const position = autosuggestTextareaRef.current!.textarea!.selectionStart;
|
|
|
|
const needsSpace = data.custom && position > 0 && !allowedAroundShortCode.includes(text[position - 1]);
|
|
|
|
|
|
|
|
dispatch(insertEmojiCompose(id, position, data, needsSpace));
|
|
|
|
};
|
|
|
|
|
|
|
|
const onPaste = (files: FileList) => {
|
|
|
|
dispatch(uploadCompose(id, files, intl));
|
|
|
|
};
|
|
|
|
|
|
|
|
const focusSpoilerInput = () => {
|
|
|
|
spoilerTextRef.current?.input?.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
const focusTextarea = () => {
|
|
|
|
autosuggestTextareaRef.current?.textarea?.focus();
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const length = text.length;
|
|
|
|
document.addEventListener('click', handleClick, true);
|
|
|
|
|
|
|
|
if (length > 0) {
|
|
|
|
setCursor(length); // Set cursor at end
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener('click', handleClick, true);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-09-19 11:55:08 -07:00
|
|
|
if (spoiler && !prevSpoiler) {
|
|
|
|
focusSpoilerInput();
|
|
|
|
} else if (!spoiler && prevSpoiler) {
|
|
|
|
focusTextarea();
|
2022-09-10 14:52:06 -07:00
|
|
|
}
|
|
|
|
}, [spoiler]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (typeof caretPosition === 'number') {
|
|
|
|
setCursor(caretPosition);
|
|
|
|
}
|
|
|
|
}, [focusDate]);
|
|
|
|
|
2022-09-14 11:01:00 -07:00
|
|
|
const renderButtons = useCallback(() => (
|
2022-11-25 09:04:11 -08:00
|
|
|
<HStack alignItems='center' space={2}>
|
2022-09-14 11:01:00 -07:00
|
|
|
{features.media && <UploadButtonContainer composeId={id} />}
|
|
|
|
<EmojiPickerDropdown onPickEmoji={handleEmojiPick} />
|
|
|
|
{features.polls && <PollButton composeId={id} />}
|
|
|
|
{features.privacyScopes && <PrivacyDropdown composeId={id} />}
|
|
|
|
{features.scheduledStatuses && <ScheduleButton composeId={id} />}
|
|
|
|
{features.spoilers && <SpoilerButton composeId={id} />}
|
|
|
|
{features.richText && <MarkdownButton composeId={id} />}
|
2022-11-25 09:04:11 -08:00
|
|
|
</HStack>
|
2022-09-14 11:01:00 -07:00
|
|
|
), [features, id]);
|
|
|
|
|
2022-09-10 14:52:06 -07:00
|
|
|
const condensed = shouldCondense && !composeFocused && isEmpty() && !isUploading;
|
|
|
|
const disabled = isSubmitting;
|
|
|
|
const countedText = [spoilerText, countableText(text)].join('');
|
|
|
|
const disabledButton = disabled || isUploading || isChangingUpload || length(countedText) > maxTootChars || (countedText.length !== 0 && countedText.trim().length === 0 && !anyMedia);
|
|
|
|
const shouldAutoFocus = autoFocus && !showSearch && !isMobile(window.innerWidth);
|
|
|
|
|
2023-01-13 08:31:48 -08:00
|
|
|
let publishText: string = '';
|
|
|
|
let publishIcon: string | undefined;
|
2022-09-20 15:12:52 -07:00
|
|
|
let textareaPlaceholder: MessageDescriptor;
|
2022-09-10 14:52:06 -07:00
|
|
|
|
|
|
|
if (isEditing) {
|
|
|
|
publishText = intl.formatMessage(messages.saveChanges);
|
|
|
|
} else if (privacy === 'direct') {
|
2023-01-13 08:31:48 -08:00
|
|
|
publishText = intl.formatMessage(messages.message);
|
|
|
|
publishIcon = require('@tabler/icons/mail.svg');
|
2022-09-10 14:52:06 -07:00
|
|
|
} else if (privacy === 'private') {
|
2023-01-13 08:31:48 -08:00
|
|
|
publishText = intl.formatMessage(messages.publish);
|
|
|
|
publishIcon = require('@tabler/icons/lock.svg');
|
2022-09-10 14:52:06 -07:00
|
|
|
} else {
|
|
|
|
publishText = privacy !== 'unlisted' ? intl.formatMessage(messages.publishLoud, { publish: intl.formatMessage(messages.publish) }) : intl.formatMessage(messages.publish);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scheduledAt) {
|
|
|
|
publishText = intl.formatMessage(messages.schedule);
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:20:58 -07:00
|
|
|
if (event) {
|
2022-09-20 15:12:52 -07:00
|
|
|
textareaPlaceholder = messages.eventPlaceholder;
|
|
|
|
} else if (hasPoll) {
|
|
|
|
textareaPlaceholder = messages.pollPlaceholder;
|
|
|
|
} else {
|
|
|
|
textareaPlaceholder = messages.placeholder;
|
|
|
|
}
|
|
|
|
|
2022-09-10 14:52:06 -07:00
|
|
|
return (
|
2022-11-05 03:46:15 -07:00
|
|
|
<Stack className='w-full' space={4} ref={formRef} onClick={handleClick} element='form' onSubmit={handleSubmit}>
|
2022-09-30 14:20:58 -07:00
|
|
|
{scheduledStatusCount > 0 && !event && (
|
2022-09-10 14:52:06 -07:00
|
|
|
<Warning
|
|
|
|
message={(
|
|
|
|
<FormattedMessage
|
|
|
|
id='compose_form.scheduled_statuses.message'
|
|
|
|
defaultMessage='You have scheduled posts. {click_here} to see them.'
|
|
|
|
values={{ click_here: (
|
|
|
|
<Link to='/scheduled_statuses'>
|
|
|
|
<FormattedMessage
|
|
|
|
id='compose_form.scheduled_statuses.click_here'
|
|
|
|
defaultMessage='Click here'
|
|
|
|
/>
|
|
|
|
</Link>
|
|
|
|
) }}
|
|
|
|
/>)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<WarningContainer composeId={id} />
|
|
|
|
|
2022-09-30 14:20:58 -07:00
|
|
|
{!shouldCondense && !event && <ReplyIndicatorContainer composeId={id} />}
|
2022-09-10 14:52:06 -07:00
|
|
|
|
2022-09-30 14:20:58 -07:00
|
|
|
{!shouldCondense && !event && <ReplyMentions composeId={id} />}
|
2022-09-10 14:52:06 -07:00
|
|
|
|
|
|
|
<AutosuggestTextarea
|
|
|
|
ref={(isModalOpen && shouldCondense) ? undefined : autosuggestTextareaRef}
|
2022-09-20 15:12:52 -07:00
|
|
|
placeholder={intl.formatMessage(textareaPlaceholder)}
|
2022-09-10 14:52:06 -07:00
|
|
|
disabled={disabled}
|
|
|
|
value={text}
|
|
|
|
onChange={handleChange}
|
|
|
|
suggestions={suggestions}
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
onFocus={handleComposeFocus}
|
|
|
|
onSuggestionsFetchRequested={onSuggestionsFetchRequested}
|
|
|
|
onSuggestionsClearRequested={onSuggestionsClearRequested}
|
|
|
|
onSuggestionSelected={onSuggestionSelected}
|
|
|
|
onPaste={onPaste}
|
|
|
|
autoFocus={shouldAutoFocus}
|
|
|
|
condensed={condensed}
|
|
|
|
id='compose-textarea'
|
|
|
|
>
|
|
|
|
{
|
|
|
|
!condensed &&
|
2022-10-31 14:19:51 -07:00
|
|
|
<Stack space={4} className='compose-form__modifiers'>
|
2022-09-10 14:52:06 -07:00
|
|
|
<UploadForm composeId={id} />
|
|
|
|
<PollForm composeId={id} />
|
|
|
|
<ScheduleFormContainer composeId={id} />
|
2022-10-31 14:19:51 -07:00
|
|
|
|
|
|
|
<SpoilerInput
|
|
|
|
composeId={id}
|
|
|
|
onSuggestionsFetchRequested={onSuggestionsFetchRequested}
|
|
|
|
onSuggestionsClearRequested={onSuggestionsClearRequested}
|
|
|
|
onSuggestionSelected={onSpoilerSuggestionSelected}
|
2022-10-31 14:23:17 -07:00
|
|
|
ref={spoilerTextRef}
|
2022-10-31 14:19:51 -07:00
|
|
|
/>
|
|
|
|
</Stack>
|
2022-09-10 14:52:06 -07:00
|
|
|
}
|
|
|
|
</AutosuggestTextarea>
|
|
|
|
|
|
|
|
<QuotedStatusContainer composeId={id} />
|
|
|
|
|
|
|
|
<div
|
|
|
|
className={classNames('flex flex-wrap items-center justify-between', {
|
|
|
|
'hidden': condensed,
|
|
|
|
})}
|
|
|
|
>
|
2022-09-14 11:01:00 -07:00
|
|
|
{renderButtons()}
|
2022-09-10 14:52:06 -07:00
|
|
|
|
2022-11-25 09:04:11 -08:00
|
|
|
<HStack space={4} alignItems='center' className='ml-auto rtl:ml-0 rtl:mr-auto'>
|
2022-09-10 14:52:06 -07:00
|
|
|
{maxTootChars && (
|
2022-11-25 09:04:11 -08:00
|
|
|
<HStack space={1} alignItems='center'>
|
2022-09-10 14:52:06 -07:00
|
|
|
<TextCharacterCounter max={maxTootChars} text={text} />
|
|
|
|
<VisualCharacterCounter max={maxTootChars} text={text} />
|
2022-11-25 09:04:11 -08:00
|
|
|
</HStack>
|
2022-09-10 14:52:06 -07:00
|
|
|
)}
|
|
|
|
|
2023-01-13 08:31:48 -08:00
|
|
|
<Button type='submit' theme='primary' text={publishText} icon={publishIcon} disabled={disabledButton} />
|
2022-11-25 09:04:11 -08:00
|
|
|
</HStack>
|
|
|
|
{/* <HStack alignItems='center' space={4}>
|
|
|
|
</HStack> */}
|
2022-09-10 14:52:06 -07:00
|
|
|
</div>
|
|
|
|
</Stack>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default ComposeForm;
|