chuckya/app/javascript/flavours/glitch/features/compose/components/options.jsx

327 lines
8.6 KiB
React
Raw Normal View History

import PropTypes from 'prop-types';
import { defineMessages, injectIntl } from 'react-intl';
import ImmutablePropTypes from 'react-immutable-proptypes';
import ImmutablePureComponent from 'react-immutable-pure-component';
import { connect } from 'react-redux';
import Toggle from 'react-toggle';
import { IconButton } from 'flavours/glitch/components/icon_button';
import { pollLimits } from 'flavours/glitch/initial_state';
import DropdownContainer from '../containers/dropdown_container';
import LanguageDropdown from '../containers/language_dropdown_container';
import PrivacyDropdownContainer from '../containers/privacy_dropdown_container';
import TextIconButton from './text_icon_button';
const messages = defineMessages({
advanced_options_icon_title: {
defaultMessage: 'Advanced options',
id: 'advanced_options.icon_title',
},
attach: {
defaultMessage: 'Attach...',
id: 'compose.attach',
},
content_type: {
defaultMessage: 'Content type',
id: 'content-type.change',
},
doodle: {
defaultMessage: 'Draw something',
id: 'compose.attach.doodle',
},
2021-01-19 01:05:19 +09:00
gif: {
defaultMessage: 'Embed GIF',
id: 'compose.attach.gif',
},
html: {
defaultMessage: 'HTML',
id: 'compose.content-type.html',
},
local_only_long: {
defaultMessage: 'Do not post to other instances',
2018-01-06 11:23:06 +09:00
id: 'advanced_options.local-only.long',
},
local_only_short: {
defaultMessage: 'Local-only',
2018-01-06 11:23:06 +09:00
id: 'advanced_options.local-only.short',
},
markdown: {
defaultMessage: 'Markdown',
id: 'compose.content-type.markdown',
},
plain: {
defaultMessage: 'Plain text',
id: 'compose.content-type.plain',
},
spoiler: {
defaultMessage: 'Hide text behind warning',
id: 'compose_form.spoiler',
},
2018-01-06 11:23:06 +09:00
threaded_mode_long: {
defaultMessage: 'Automatically opens a reply on posting',
id: 'advanced_options.threaded_mode.long',
},
threaded_mode_short: {
defaultMessage: 'Threaded mode',
id: 'advanced_options.threaded_mode.short',
},
upload: {
defaultMessage: 'Upload a file',
id: 'compose.attach.upload',
},
add_poll: {
defaultMessage: 'Add a poll',
id: 'poll_button.add_poll',
},
remove_poll: {
defaultMessage: 'Remove poll',
id: 'poll_button.remove_poll',
},
});
const mapStateToProps = (state, { name }) => ({
checked: state.getIn(['compose', 'advanced_options', name]),
});
class ToggleOptionImpl extends ImmutablePureComponent {
static propTypes = {
name: PropTypes.string.isRequired,
checked: PropTypes.bool,
onChangeAdvancedOption: PropTypes.func.isRequired,
};
handleChange = () => {
this.props.onChangeAdvancedOption(this.props.name);
};
render() {
2022-02-10 01:15:36 +09:00
const { meta, text, checked } = this.props;
return (
<>
<Toggle checked={checked} onChange={this.handleChange} />
<div className='privacy-dropdown__option__content'>
<strong>{text}</strong>
{meta}
</div>
</>
);
}
}
const ToggleOption = connect(mapStateToProps)(ToggleOptionImpl);
class ComposerOptions extends ImmutablePureComponent {
static propTypes = {
acceptContentTypes: PropTypes.string,
advancedOptions: ImmutablePropTypes.map,
disabled: PropTypes.bool,
allowMedia: PropTypes.bool,
hasPoll: PropTypes.bool,
intl: PropTypes.object.isRequired,
onChangeAdvancedOption: PropTypes.func.isRequired,
onChangeContentType: PropTypes.func.isRequired,
onTogglePoll: PropTypes.func.isRequired,
onDoodleOpen: PropTypes.func.isRequired,
onEmbedTenor: PropTypes.func.isRequired,
onModalClose: PropTypes.func.isRequired,
onModalOpen: PropTypes.func.isRequired,
onToggleSpoiler: PropTypes.func,
onUpload: PropTypes.func.isRequired,
contentType: PropTypes.string,
resetFileKey: PropTypes.number,
spoiler: PropTypes.bool,
2019-05-13 04:55:44 +09:00
showContentTypeChoice: PropTypes.bool,
isEditing: PropTypes.bool,
};
handleChangeFiles = ({ target: { files } }) => {
const { onUpload } = this.props;
if (files.length) {
onUpload(files);
}
};
handleClickAttach = (name) => {
const { fileElement } = this;
2022-04-17 09:04:23 +09:00
const { onDoodleOpen, onEmbedTenor } = this.props;
switch (name) {
case 'upload':
if (fileElement) {
fileElement.click();
}
return;
case 'doodle':
onDoodleOpen();
return;
2021-01-19 01:05:19 +09:00
case 'gif':
2022-04-17 09:04:23 +09:00
if (onEmbedTenor) {
onEmbedTenor();
2021-01-19 01:05:19 +09:00
}
return;
}
};
handleRefFileElement = (fileElement) => {
this.fileElement = fileElement;
};
2022-02-10 01:15:36 +09:00
renderToggleItemContents = (item) => {
const { onChangeAdvancedOption } = this.props;
const { name, meta, text } = item;
return <ToggleOption name={name} text={text} meta={meta} onChangeAdvancedOption={onChangeAdvancedOption} />;
};
render () {
const {
acceptContentTypes,
2018-01-06 11:23:06 +09:00
advancedOptions,
contentType,
disabled,
allowMedia,
hasPoll,
2018-01-06 11:23:06 +09:00
onChangeAdvancedOption,
onChangeContentType,
onTogglePoll,
onToggleSpoiler,
resetFileKey,
spoiler,
2019-05-13 04:55:44 +09:00
showContentTypeChoice,
isEditing,
intl: { formatMessage },
} = this.props;
const contentTypeItems = {
plain: {
icon: 'file-text',
name: 'text/plain',
text: formatMessage(messages.plain),
},
html: {
icon: 'code',
name: 'text/html',
text: formatMessage(messages.html),
},
markdown: {
icon: 'arrow-circle-down',
name: 'text/markdown',
text: formatMessage(messages.markdown),
},
};
// The result.
return (
<div className='compose-form__buttons'>
<input
accept={acceptContentTypes}
disabled={disabled || !allowMedia}
key={resetFileKey}
onChange={this.handleChangeFiles}
ref={this.handleRefFileElement}
type='file'
multiple
style={{ display: 'none' }}
/>
<DropdownContainer
disabled={disabled || !allowMedia}
icon='paperclip'
items={[
{
icon: 'cloud-upload',
name: 'upload',
text: formatMessage(messages.upload),
},
{
icon: 'paint-brush',
name: 'doodle',
text: formatMessage(messages.doodle),
},
2021-01-19 01:05:19 +09:00
{
icon: 'file-image-o',
name: 'gif',
2023-02-17 06:12:09 +09:00
text: formatMessage(messages.gif),
2021-01-19 01:05:19 +09:00
}
]}
onChange={this.handleClickAttach}
title={formatMessage(messages.attach)}
/>
{!!pollLimits && (
<IconButton
active={hasPoll}
2023-11-29 14:33:17 +09:00
disabled={disabled}
icon='tasks'
inverted
onClick={onTogglePoll}
size={18}
style={{
height: null,
lineHeight: null,
}}
title={formatMessage(hasPoll ? messages.remove_poll : messages.add_poll)}
/>
)}
<hr />
<PrivacyDropdownContainer disabled={disabled || isEditing} />
2019-05-13 04:55:44 +09:00
{showContentTypeChoice && (
<DropdownContainer
2019-05-13 04:55:44 +09:00
disabled={disabled}
icon={(contentTypeItems[contentType.split('/')[1]] || {}).icon}
items={[
contentTypeItems.plain,
contentTypeItems.html,
contentTypeItems.markdown,
]}
onChange={onChangeContentType}
title={formatMessage(messages.content_type)}
2019-05-13 04:55:44 +09:00
value={contentType}
/>
)}
{onToggleSpoiler && (
<TextIconButton
active={spoiler}
ariaControls='cw-spoiler-input'
label='CW'
onClick={onToggleSpoiler}
title={formatMessage(messages.spoiler)}
/>
)}
<LanguageDropdown />
<DropdownContainer
disabled={disabled || isEditing}
2018-01-15 07:13:24 +09:00
icon='ellipsis-h'
items={advancedOptions ? [
{
meta: formatMessage(messages.local_only_long),
2018-01-15 07:13:24 +09:00
name: 'do_not_federate',
text: formatMessage(messages.local_only_short),
2018-01-15 07:13:24 +09:00
},
{
meta: formatMessage(messages.threaded_mode_long),
2018-01-15 07:13:24 +09:00
name: 'threaded_mode',
text: formatMessage(messages.threaded_mode_short),
2018-01-15 07:13:24 +09:00
},
] : null}
onChange={onChangeAdvancedOption}
renderItemContents={this.renderToggleItemContents}
title={formatMessage(messages.advanced_options_icon_title)}
closeOnChange={false}
2018-01-15 07:13:24 +09:00
/>
</div>
);
}
}
export default injectIntl(ComposerOptions);