WhiteListInput.jsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* eslint-disable max-len */
  2. import React from 'react';
  3. import PropTypes from 'prop-types';
  4. import { withTranslation } from 'react-i18next';
  5. import { createSubscribedElement } from '../../UnstatedUtils';
  6. import { tags, attrs } from '../../../../../lib/service/xss/recommended-whitelist';
  7. import AppContainer from '../../../services/AppContainer';
  8. import MarkDownSettingContainer from '../../../services/MarkDownSettingContainer';
  9. class WhiteListInput extends React.Component {
  10. renderRecommendBtn() {
  11. const { t } = this.props;
  12. return (
  13. <p id="btn-import-tags" className="btn btn-xs btn-primary">
  14. { t('markdown_setting.import_recommended', 'tags') }
  15. </p>
  16. );
  17. }
  18. renderTagValue() {
  19. const { customizable, markDownSettingContainer } = this.props;
  20. if (customizable) {
  21. return markDownSettingContainer.state.tagWhiteList;
  22. }
  23. return tags;
  24. }
  25. renderAttrValue() {
  26. const { customizable, markDownSettingContainer } = this.props;
  27. if (customizable) {
  28. return markDownSettingContainer.state.attrWhiteList;
  29. }
  30. return attrs;
  31. }
  32. render() {
  33. const { t, customizable, markDownSettingContainer } = this.props;
  34. return (
  35. <>
  36. <div className="m-t-15">
  37. <div className="d-flex justify-content-between">
  38. { t('markdown_setting.Tag names') }
  39. {customizable && this.renderRecommendBtn()}
  40. </div>
  41. <textarea className="form-control xss-list" name="recommendedTags" rows="6" cols="40" readOnly={!customizable} value={this.renderTagValue()} onChange={(e) => { markDownSettingContainer.onChangeTagWhiteList(e.target.value) }} />
  42. </div>
  43. <div className="m-t-15">
  44. <div className="d-flex justify-content-between">
  45. { t('markdown_setting.Tag attributes') }
  46. {customizable && this.renderRecommendBtn()}
  47. </div>
  48. <textarea className="form-control xss-list" name="recommendedAttrs" rows="6" cols="40" readOnly={!customizable} value={this.renderAttrValue()} onChange={(e) => { markDownSettingContainer.onChangeAttrWhiteList(e.target.value) }} />
  49. </div>
  50. </>
  51. );
  52. }
  53. }
  54. const WhiteListWrapper = (props) => {
  55. return createSubscribedElement(WhiteListInput, props, [AppContainer, MarkDownSettingContainer]);
  56. };
  57. WhiteListInput.propTypes = {
  58. t: PropTypes.func.isRequired, // i18next
  59. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  60. markDownSettingContainer: PropTypes.instanceOf(MarkDownSettingContainer).isRequired,
  61. customizable: PropTypes.bool.isRequired,
  62. };
  63. export default withTranslation()(WhiteListWrapper);