2017-12-23 22:16:45 -08:00
|
|
|
// Package imports.
|
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { defineMessages, FormattedMessage } from 'react-intl';
|
|
|
|
|
|
|
|
// Utils.
|
|
|
|
import {
|
|
|
|
assignHandlers,
|
|
|
|
hiddenComponent,
|
|
|
|
} from 'flavours/glitch/util/react_helpers';
|
|
|
|
|
|
|
|
// Messages.
|
|
|
|
const messages = defineMessages({
|
|
|
|
placeholder: {
|
|
|
|
defaultMessage: 'Write your warning here',
|
|
|
|
id: 'compose_form.spoiler_placeholder',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handlers.
|
|
|
|
const handlers = {
|
|
|
|
|
|
|
|
// Handles a keypress.
|
2018-01-03 12:36:21 -08:00
|
|
|
handleKeyDown ({
|
2017-12-23 22:16:45 -08:00
|
|
|
ctrlKey,
|
|
|
|
keyCode,
|
|
|
|
metaKey,
|
2018-10-14 02:38:38 -07:00
|
|
|
altKey,
|
2017-12-23 22:16:45 -08:00
|
|
|
}) {
|
2018-10-14 02:38:38 -07:00
|
|
|
const { onSubmit, onSecondarySubmit } = this.props;
|
2017-12-23 22:16:45 -08:00
|
|
|
|
|
|
|
// We submit the status on control/meta + enter.
|
|
|
|
if (onSubmit && keyCode === 13 && (ctrlKey || metaKey)) {
|
|
|
|
onSubmit();
|
|
|
|
}
|
2018-10-14 02:38:38 -07:00
|
|
|
|
|
|
|
// Submit the status with secondary visibility on alt + enter.
|
|
|
|
if (onSecondarySubmit && keyCode === 13 && altKey) {
|
|
|
|
onSecondarySubmit();
|
|
|
|
}
|
2017-12-23 22:16:45 -08:00
|
|
|
},
|
2018-08-18 11:53:46 -07:00
|
|
|
|
|
|
|
handleRefSpoilerText (spoilerText) {
|
|
|
|
this.spoilerText = spoilerText;
|
|
|
|
},
|
2018-10-20 04:17:02 -07:00
|
|
|
|
|
|
|
// When the escape key is released, we focus the UI.
|
|
|
|
handleKeyUp ({ key }) {
|
|
|
|
if (key === 'Escape') {
|
|
|
|
document.querySelector('.ui').parentElement.focus();
|
|
|
|
}
|
|
|
|
},
|
2017-12-23 22:16:45 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// The component.
|
|
|
|
export default class ComposerSpoiler extends React.PureComponent {
|
|
|
|
|
|
|
|
// Constructor.
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
assignHandlers(this, handlers);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rendering.
|
|
|
|
render () {
|
2018-10-20 04:17:02 -07:00
|
|
|
const { handleKeyDown, handleKeyUp, handleRefSpoilerText } = this.handlers;
|
2017-12-23 22:16:45 -08:00
|
|
|
const {
|
|
|
|
hidden,
|
|
|
|
intl,
|
|
|
|
onChange,
|
|
|
|
text,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
// The result.
|
|
|
|
return (
|
2018-07-27 10:52:16 -07:00
|
|
|
<div className={`composer--spoiler ${hidden ? '' : 'composer--spoiler--visible'}`}>
|
|
|
|
<label>
|
2017-12-23 22:16:45 -08:00
|
|
|
<span {...hiddenComponent}>
|
|
|
|
<FormattedMessage {...messages.placeholder} />
|
|
|
|
</span>
|
|
|
|
<input
|
|
|
|
id='glitch.composer.spoiler.input'
|
|
|
|
onChange={onChange}
|
2018-01-03 12:36:21 -08:00
|
|
|
onKeyDown={handleKeyDown}
|
2018-10-20 04:17:02 -07:00
|
|
|
onKeyUp={handleKeyUp}
|
2017-12-23 22:16:45 -08:00
|
|
|
placeholder={intl.formatMessage(messages.placeholder)}
|
|
|
|
type='text'
|
|
|
|
value={text}
|
2018-08-18 11:53:46 -07:00
|
|
|
ref={handleRefSpoilerText}
|
2017-12-23 22:16:45 -08:00
|
|
|
/>
|
|
|
|
</label>
|
2018-07-27 10:52:16 -07:00
|
|
|
</div>
|
2017-12-23 22:16:45 -08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Props.
|
|
|
|
ComposerSpoiler.propTypes = {
|
|
|
|
hidden: PropTypes.bool,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
onChange: PropTypes.func,
|
|
|
|
onSubmit: PropTypes.func,
|
2018-10-14 02:38:38 -07:00
|
|
|
onSecondarySubmit: PropTypes.func,
|
2017-12-23 22:16:45 -08:00
|
|
|
text: PropTypes.string,
|
|
|
|
};
|