|
1 | 1 | import React from 'react'; |
2 | 2 | import PropTypes from 'prop-types'; |
3 | 3 | import {t} from 'i18next'; |
| 4 | +import bindAll from 'lodash/bindAll'; |
4 | 5 |
|
5 | | -export default function InstructionsEditor({instructions, onCancelEditing}) { |
6 | | - return ( |
7 | | - <div className="instructions-editor"> |
8 | | - <div className="instructions-editor-menu"> |
9 | | - <button onClick={onCancelEditing}> |
10 | | - {t('workspace.components.instructions.cancel')} |
11 | | - </button> |
| 6 | +export default class InstructionsEditor extends React.Component { |
| 7 | + constructor({instructions}) { |
| 8 | + super(); |
| 9 | + this.state = {instructions}; |
| 10 | + bindAll(this, '_handleCancelEditing', '_handleSaveChanges', '_ref'); |
| 11 | + } |
| 12 | + |
| 13 | + _handleCancelEditing() { |
| 14 | + this.props.onCancelEditing(); |
| 15 | + } |
| 16 | + |
| 17 | + _handleSaveChanges() { |
| 18 | + const newValue = this.editor.innerText.trim(); |
| 19 | + this.props.onSaveChanges(this.props.projectKey, newValue); |
| 20 | + } |
| 21 | + |
| 22 | + _ref(preElement) { |
| 23 | + this.editor = preElement; |
| 24 | + } |
| 25 | + |
| 26 | + render() { |
| 27 | + return ( |
| 28 | + <div className="instructions-editor"> |
| 29 | + <div className="instructions-editor-menu"> |
| 30 | + <button onClick={this._handleSaveChanges}> |
| 31 | + {t('workspace.components.instructions.save')} |
| 32 | + </button> |
| 33 | + <button onClick={this._handleCancelEditing}> |
| 34 | + {t('workspace.components.instructions.cancel')} |
| 35 | + </button> |
| 36 | + </div> |
| 37 | + <pre contentEditable ref={this._ref}> |
| 38 | + {this.state.instructions} |
| 39 | + </pre> |
12 | 40 | </div> |
13 | | - <pre contentEditable> |
14 | | - {instructions} |
15 | | - </pre> |
16 | | - </div> |
17 | | - ); |
| 41 | + ); |
| 42 | + } |
18 | 43 | } |
19 | 44 |
|
20 | 45 | InstructionsEditor.propTypes = { |
21 | 46 | instructions: PropTypes.string.isRequired, |
| 47 | + projectKey: PropTypes.string.isRequired, |
22 | 48 | onCancelEditing: PropTypes.func.isRequired, |
| 49 | + onSaveChanges: PropTypes.func.isRequired, |
23 | 50 | }; |
0 commit comments