[Glitch] Fix character count not ignoring hidden CW field

Port 68775b6039 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
signup-info-prompt
Mélanie Chauvel 2020-11-30 12:09:34 +01:00 committed by Claire
parent f8d867bac4
commit 40e2de3904
1 changed files with 23 additions and 22 deletions

View File

@ -17,6 +17,7 @@ import Publisher from './publisher';
import TextareaIcons from './textarea_icons'; import TextareaIcons from './textarea_icons';
import { maxChars } from 'flavours/glitch/util/initial_state'; import { maxChars } from 'flavours/glitch/util/initial_state';
import CharacterCounter from './character_counter'; import CharacterCounter from './character_counter';
import { length } from 'stringz';
const messages = defineMessages({ const messages = defineMessages({
placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' }, placeholder: { id: 'compose_form.placeholder', defaultMessage: 'What is on your mind?' },
@ -81,30 +82,37 @@ class ComposeForm extends ImmutablePureComponent {
this.props.onChange(e.target.value); this.props.onChange(e.target.value);
} }
getFulltextForCharacterCounting = () => {
return [
this.props.spoiler? this.props.spoilerText: '',
countableText(this.props.text),
this.props.advancedOptions && this.props.advancedOptions.get('do_not_federate') ? ' 👁️' : ''
].join('');
}
canSubmit = () => {
const { isSubmitting, isChangingUpload, isUploading, anyMedia } = this.props;
const fulltext = this.getFulltextForCharacterCounting();
return !(isSubmitting || isUploading || isChangingUpload || length(fulltext) > maxChars || (!fulltext.trim().length && !anyMedia));
}
handleSubmit = (overriddenVisibility = null) => { handleSubmit = (overriddenVisibility = null) => {
const { textarea: { value }, uploadForm } = this;
const { const {
onChange,
onSubmit, onSubmit,
isSubmitting,
isChangingUpload,
isUploading,
media, media,
anyMedia,
text,
mediaDescriptionConfirmation, mediaDescriptionConfirmation,
onMediaDescriptionConfirm, onMediaDescriptionConfirm,
onChangeVisibility, onChangeVisibility,
} = this.props; } = this.props;
// If something changes inside the textarea, then we update the if (this.props.text !== this.textarea.value) {
// state before submitting. // Something changed the text inside the textarea (e.g. browser extensions like Grammarly)
if (onChange && text !== value) { // Update the state to match the current text
onChange(value); this.props.onChange(this.textarea.value);
} }
// Submit disabled: if (!this.canSubmit()) {
if (isSubmitting || isUploading || isChangingUpload || (!text.trim().length && !anyMedia)) {
return; return;
} }
@ -260,13 +268,9 @@ class ComposeForm extends ImmutablePureComponent {
} = this; } = this;
const { const {
advancedOptions, advancedOptions,
anyMedia,
intl, intl,
isSubmitting, isSubmitting,
isChangingUpload,
isUploading,
layout, layout,
media,
onChangeSpoilerness, onChangeSpoilerness,
onChangeVisibility, onChangeVisibility,
onClearSuggestions, onClearSuggestions,
@ -279,13 +283,10 @@ class ComposeForm extends ImmutablePureComponent {
spoiler, spoiler,
spoilerText, spoilerText,
suggestions, suggestions,
text,
spoilersAlwaysOn, spoilersAlwaysOn,
} = this.props; } = this.props;
let disabledButton = isSubmitting || isUploading || isChangingUpload || (!text.trim().length && !anyMedia); const countText = this.getFulltextForCharacterCounting();
const countText = `${spoilerText}${countableText(text)}${advancedOptions && advancedOptions.get('do_not_federate') ? ' 👁️' : ''}`;
return ( return (
<div className='composer'> <div className='composer'>
@ -353,7 +354,7 @@ class ComposeForm extends ImmutablePureComponent {
<Publisher <Publisher
countText={countText} countText={countText}
disabled={disabledButton} disabled={!this.canSubmit()}
onSecondarySubmit={handleSecondarySubmit} onSecondarySubmit={handleSecondarySubmit}
onSubmit={handleSubmit} onSubmit={handleSubmit}
privacy={privacy} privacy={privacy}