Skip to content

Commit b07fc88

Browse files
committed
Wire editor save button to state mgmt logic
1 parent f56e959 commit b07fc88

5 files changed

Lines changed: 58 additions & 16 deletions

File tree

locales/en/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
},
6464
"output": "Output",
6565
"instructions": {
66-
"cancel": "Cancel"
66+
"cancel": "Cancel",
67+
"save": "Save"
6768
}
6869
}
6970
},

src/components/Instructions.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ export default function Instructions({
77
instructions,
88
isEditing,
99
isOpen,
10+
projectKey,
1011
onCancelEditing,
12+
onSaveChanges,
1113
}) {
1214
if (!isEditing && !instructions || !isOpen) {
1315
return null;
@@ -22,7 +24,9 @@ export default function Instructions({
2224
isEditing ?
2325
<InstructionsEditor
2426
instructions={instructions}
27+
projectKey={projectKey}
2528
onCancelEditing={onCancelEditing}
29+
onSaveChanges={onSaveChanges}
2630
/> :
2731
markdownToReact(instructions)
2832
}
@@ -35,5 +39,7 @@ Instructions.propTypes = {
3539
instructions: PropTypes.string.isRequired,
3640
isEditing: PropTypes.bool.isRequired,
3741
isOpen: PropTypes.bool.isRequired,
42+
projectKey: PropTypes.string.isRequired,
3843
onCancelEditing: PropTypes.func.isRequired,
44+
onSaveChanges: PropTypes.func.isRequired,
3945
};
Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,50 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33
import {t} from 'i18next';
4+
import bindAll from 'lodash/bindAll';
45

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>
1240
</div>
13-
<pre contentEditable>
14-
{instructions}
15-
</pre>
16-
</div>
17-
);
41+
);
42+
}
1843
}
1944

2045
InstructionsEditor.propTypes = {
2146
instructions: PropTypes.string.isRequired,
47+
projectKey: PropTypes.string.isRequired,
2248
onCancelEditing: PropTypes.func.isRequired,
49+
onSaveChanges: PropTypes.func.isRequired,
2350
};

src/containers/Instructions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ import {connect} from 'react-redux';
22
import Instructions from '../components/Instructions';
33
import {
44
getCurrentProjectInstructions,
5+
getCurrentProjectKey,
56
getHiddenUIComponents,
67
isEditingInstructions,
78
} from '../selectors';
8-
import {stopEditingInstructions} from '../actions';
9+
import {stopEditingInstructions, updateProjectInstructions} from '../actions';
910

1011
function mapStateToProps(state) {
1112
return {
1213
instructions: getCurrentProjectInstructions(state),
1314
isEditing: isEditingInstructions(state),
1415
isOpen: !getHiddenUIComponents(state).includes('instructions'),
16+
projectKey: getCurrentProjectKey(state),
1517
};
1618
}
1719

@@ -20,6 +22,9 @@ function mapDispatchToProps(dispatch) {
2022
onCancelEditing() {
2123
dispatch(stopEditingInstructions());
2224
},
25+
onSaveChanges(projectKey, newValue) {
26+
dispatch(updateProjectInstructions(projectKey, newValue));
27+
},
2328
};
2429
}
2530

src/css/application.css

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,7 @@ body {
351351
margin: 0;
352352
outline: 0;
353353
word-wrap: break-word;
354-
overflow-wrap: break-word;
355-
white-space: normal;
354+
white-space: pre-wrap;
356355
}
357356
}
358357

@@ -365,6 +364,10 @@ body {
365364
margin-bottom: 100%;
366365
text-align: right;
367366
background-color: var(--color-light-gray);
367+
368+
& button:not(:last-child) {
369+
margin-right: .5em;
370+
}
368371
}
369372

370373
/** @define project-preview */

0 commit comments

Comments
 (0)