You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.4 KiB

  1. // Package imports.
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { defineMessages, FormattedMessage } from 'react-intl';
  5. // Utils.
  6. import {
  7. assignHandlers,
  8. hiddenComponent,
  9. } from 'flavours/glitch/util/react_helpers';
  10. // Messages.
  11. const messages = defineMessages({
  12. placeholder: {
  13. defaultMessage: 'Write your warning here',
  14. id: 'compose_form.spoiler_placeholder',
  15. },
  16. });
  17. // Handlers.
  18. const handlers = {
  19. // Handles a keypress.
  20. handleKeyDown ({
  21. ctrlKey,
  22. keyCode,
  23. metaKey,
  24. altKey,
  25. }) {
  26. const { onSubmit, onSecondarySubmit } = this.props;
  27. // We submit the status on control/meta + enter.
  28. if (onSubmit && keyCode === 13 && (ctrlKey || metaKey)) {
  29. onSubmit();
  30. }
  31. // Submit the status with secondary visibility on alt + enter.
  32. if (onSecondarySubmit && keyCode === 13 && altKey) {
  33. onSecondarySubmit();
  34. }
  35. },
  36. handleRefSpoilerText (spoilerText) {
  37. this.spoilerText = spoilerText;
  38. },
  39. // When the escape key is released, we focus the UI.
  40. handleKeyUp ({ key }) {
  41. if (key === 'Escape') {
  42. document.querySelector('.ui').parentElement.focus();
  43. }
  44. },
  45. };
  46. // The component.
  47. export default class ComposerSpoiler extends React.PureComponent {
  48. // Constructor.
  49. constructor (props) {
  50. super(props);
  51. assignHandlers(this, handlers);
  52. }
  53. // Rendering.
  54. render () {
  55. const { handleKeyDown, handleKeyUp, handleRefSpoilerText } = this.handlers;
  56. const {
  57. hidden,
  58. intl,
  59. onChange,
  60. text,
  61. } = this.props;
  62. // The result.
  63. return (
  64. <div className={`composer--spoiler ${hidden ? '' : 'composer--spoiler--visible'}`}>
  65. <label>
  66. <span {...hiddenComponent}>
  67. <FormattedMessage {...messages.placeholder} />
  68. </span>
  69. <input
  70. id='glitch.composer.spoiler.input'
  71. onChange={onChange}
  72. onKeyDown={handleKeyDown}
  73. onKeyUp={handleKeyUp}
  74. placeholder={intl.formatMessage(messages.placeholder)}
  75. type='text'
  76. value={text}
  77. ref={handleRefSpoilerText}
  78. />
  79. </label>
  80. </div>
  81. );
  82. }
  83. }
  84. // Props.
  85. ComposerSpoiler.propTypes = {
  86. hidden: PropTypes.bool,
  87. intl: PropTypes.object.isRequired,
  88. onChange: PropTypes.func,
  89. onSubmit: PropTypes.func,
  90. onSecondarySubmit: PropTypes.func,
  91. text: PropTypes.string,
  92. };