Draft.jsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { CopyToClipboard } from 'react-copy-to-clipboard';
  5. import {
  6. Collapse,
  7. UncontrolledTooltip,
  8. } from 'reactstrap';
  9. import { withUnstatedContainers } from '../UnstatedUtils';
  10. import AppContainer from '../../services/AppContainer';
  11. import RevisionBody from '../Page/RevisionBody';
  12. class Draft extends React.Component {
  13. constructor(props) {
  14. super(props);
  15. this.state = {
  16. html: '',
  17. isRendered: false,
  18. isPanelExpanded: false,
  19. showCopiedMessage: false,
  20. };
  21. this.growiRenderer = this.props.appContainer.getRenderer('draft');
  22. this.changeToolTipLabel = this.changeToolTipLabel.bind(this);
  23. this.expandPanelHandler = this.expandPanelHandler.bind(this);
  24. this.collapsePanelHandler = this.collapsePanelHandler.bind(this);
  25. this.renderHtml = this.renderHtml.bind(this);
  26. this.renderAccordionTitle = this.renderAccordionTitle.bind(this);
  27. }
  28. changeToolTipLabel() {
  29. this.setState({ showCopiedMessage: true });
  30. setTimeout(() => {
  31. this.setState({ showCopiedMessage: false });
  32. }, 1000);
  33. }
  34. expandPanelHandler() {
  35. this.setState({ isPanelExpanded: true });
  36. if (!this.state.isRendered) {
  37. this.renderHtml();
  38. }
  39. }
  40. collapsePanelHandler() {
  41. this.setState({ isPanelExpanded: false });
  42. }
  43. async renderHtml() {
  44. const context = {
  45. markdown: this.props.markdown,
  46. };
  47. const growiRenderer = this.growiRenderer;
  48. const interceptorManager = this.props.appContainer.interceptorManager;
  49. await interceptorManager.process('prePreProcess', context)
  50. .then(() => {
  51. context.markdown = growiRenderer.preProcess(context.markdown);
  52. })
  53. .then(() => { return interceptorManager.process('postPreProcess', context) })
  54. .then(() => {
  55. const parsedHTML = growiRenderer.process(context.markdown);
  56. context.parsedHTML = parsedHTML;
  57. })
  58. .then(() => { return interceptorManager.process('prePostProcess', context) })
  59. .then(() => {
  60. context.parsedHTML = growiRenderer.postProcess(context.parsedHTML);
  61. })
  62. .then(() => { return interceptorManager.process('postPostProcess', context) })
  63. .then(() => {
  64. this.setState({ html: context.parsedHTML, isRendered: true });
  65. });
  66. }
  67. renderAccordionTitle(isExist) {
  68. const { isPanelExpanded } = this.state;
  69. const { t } = this.props;
  70. const iconClass = isPanelExpanded ? 'fa-rotate-90' : '';
  71. return (
  72. <span>
  73. <span className="mr-2 draft-path" onClick={() => this.setState({ isPanelExpanded: !isPanelExpanded })}>
  74. <i className={`fa fa-fw fa-angle-right mr-2 ${iconClass}`}></i>
  75. {this.props.path}
  76. </span>
  77. { isExist && (
  78. <span className="badge badge-warning">{t('page exists')}</span>
  79. ) }
  80. { !isExist && (
  81. <span className="badge badge-info">draft</span>
  82. ) }
  83. <a className="ml-2" href={this.props.path}><i className="icon icon-login"></i></a>
  84. </span>
  85. );
  86. }
  87. renderControls() {
  88. const { t, path } = this.props;
  89. const encodedPath = path.replace(/\//g, '-');
  90. const tooltipTargetId = `draft-copied-tooltip_${encodedPath}`;
  91. return (
  92. <div className="icon-container">
  93. {this.props.isExist
  94. ? null
  95. : (
  96. <a
  97. href={`${this.props.path}#edit`}
  98. target="_blank"
  99. rel="noopener noreferrer"
  100. data-toggle="tooltip"
  101. title={this.props.t('Edit')}
  102. >
  103. <i className="mx-2 icon-note" />
  104. </a>
  105. )
  106. }
  107. <span id={tooltipTargetId}>
  108. <CopyToClipboard text={this.props.markdown} onCopy={this.changeToolTipLabel}>
  109. <a
  110. className="text-center draft-copy"
  111. >
  112. <i className="mx-2 ti-clipboard" />
  113. </a>
  114. </CopyToClipboard>
  115. </span>
  116. <UncontrolledTooltip placement="top" target={tooltipTargetId} fade={false} trigger="hover">
  117. { this.state.showCopiedMessage && (
  118. <strong>copied!</strong>
  119. ) }
  120. { !this.state.showCopiedMessage && (
  121. <span>{this.props.t('Copy')}</span>
  122. ) }
  123. </UncontrolledTooltip>
  124. <a
  125. className="text-danger text-center"
  126. data-toggle="tooltip"
  127. data-placement="top"
  128. title={t('Delete')}
  129. onClick={() => { return this.props.clearDraft(this.props.path) }}
  130. >
  131. <i className="mx-2 icon-trash" />
  132. </a>
  133. </div>
  134. );
  135. }
  136. render() {
  137. const { isPanelExpanded } = this.state;
  138. return (
  139. <div className="accordion draft-list-item" role="tablist">
  140. <div className="card">
  141. <div className="card-header d-flex" role="tab">
  142. {this.renderAccordionTitle(this.props.isExist)}
  143. <div className="flex-grow-1"></div>
  144. {this.renderControls()}
  145. </div>
  146. <Collapse isOpen={isPanelExpanded} onEntering={this.expandPanelHandler} onExiting={this.collapsePanelHandler}>
  147. <div className="card-body">
  148. {/* loading spinner */}
  149. { this.state.isPanelExpanded && !this.state.isRendered && (
  150. <div className="text-center">
  151. <i className="fa fa-lg fa-spinner fa-pulse mx-auto text-muted"></i>
  152. </div>
  153. ) }
  154. {/* contents */}
  155. { this.state.isPanelExpanded && this.state.isRendered && (
  156. <RevisionBody html={this.state.html} />
  157. ) }
  158. </div>
  159. </Collapse>
  160. </div>
  161. </div>
  162. );
  163. }
  164. }
  165. /**
  166. * Wrapper component for using unstated
  167. */
  168. const DraftWrapper = withUnstatedContainers(Draft, [AppContainer]);
  169. Draft.propTypes = {
  170. t: PropTypes.func.isRequired,
  171. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  172. path: PropTypes.string.isRequired,
  173. markdown: PropTypes.string.isRequired,
  174. isExist: PropTypes.bool.isRequired,
  175. clearDraft: PropTypes.func.isRequired,
  176. };
  177. export default withTranslation()(DraftWrapper);