Skip to content

Commit 3d90439

Browse files
committed
Better organization
1 parent e2483e9 commit 3d90439

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

src/ReactMde.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,35 +75,16 @@ class ReactMde extends Component {
7575
* @param {function} command
7676
* @memberOf ReactMde
7777
*/
78-
handleCommand(command)
79-
{
80-
let {
81-
value: { text },
82-
onChange
83-
} = this.props;
84-
let textarea = this.refs.textarea;
85-
var newValue = command(text, getSelection(textarea));
86-
onChange(newValue);
87-
}
88-
89-
handleBoldCommand() {
90-
this.handleCommand(ReactMdeCommands.makeBold);
91-
}
92-
93-
handleItalicCommand() {
94-
this.handleCommand(ReactMdeCommands.makeItalic);
95-
}
96-
97-
handleLinkCommand() {
98-
this.handleCommand(ReactMdeCommands.makeLink);
99-
}
100-
101-
componentDidMount() {
102-
this.handleSelection();
103-
}
104-
105-
componentDidUpdate() {
106-
this.handleSelection();
78+
getCommandHandler(command) {
79+
return function () {
80+
let {
81+
value: { text },
82+
onChange
83+
} = this.props;
84+
let textarea = this.refs.textarea;
85+
var newValue = command(text, getSelection(textarea));
86+
onChange(newValue);
87+
}
10788
}
10889

10990
render() {
@@ -117,11 +98,11 @@ class ReactMde extends Component {
11798
<div className="react-mde">
11899
<div className="mde-header">
119100
<HeaderGroup>
120-
<HeaderItem icon="bold" onClick={this.handleBoldCommand.bind(this)} />
121-
<HeaderItem icon="italic" onClick={this.handleItalicCommand.bind(this)} />
101+
<HeaderItem icon="bold" onClick={this.getCommandHandler(ReactMdeCommands.makeBold).bind(this)} />
102+
<HeaderItem icon="italic" onClick={this.getCommandHandler(ReactMdeCommands.makeItalic).bind(this)} />
122103
</HeaderGroup>
123104
<HeaderGroup>
124-
<HeaderItem icon="link" onClick={this.handleLinkCommand.bind(this)} />
105+
<HeaderItem icon="link" onClick={this.getCommandHandler(ReactMdeCommands.makeLink).bind(this)} />
125106
<HeaderItem icon="quote-right" />
126107
<HeaderItem icon="picture-o" />
127108
</HeaderGroup>
@@ -133,6 +114,9 @@ class ReactMde extends Component {
133114
<HeaderItem icon="at" />
134115
<HeaderItem icon="bookmark" />
135116
</HeaderGroup>
117+
<HeaderGroup>
118+
<HeaderItem icon="star" />
119+
</HeaderGroup>
136120
</div>
137121
<div className="mde-text">
138122
<textarea onChange={this.handleValueChange.bind(this)} value={text} ref="textarea" />
@@ -141,6 +125,14 @@ class ReactMde extends Component {
141125
</div>
142126
);
143127
}
128+
129+
componentDidMount() {
130+
this.handleSelection();
131+
}
132+
133+
componentDidUpdate() {
134+
this.handleSelection();
135+
}
144136
}
145137

146138
export default ReactMde;

src/ReactMdeCommands.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
/**
2-
* Inserts text in the given position
3-
*
4-
* @param {any} text
5-
* @param {any} insertionText
6-
* @param {any} position
7-
* @returns
8-
*/
9-
function insertText(text, insertionText, position) {
10-
return [text.slice(0, position), insertionText, text.slice(position)].join('');
11-
}
1+
import { insertText } from './TextHelper';
122

133
export default {
144

@@ -53,6 +43,15 @@ export default {
5343
*/
5444
makeLink: function (text, selection) {
5545

46+
var newText = insertText(text, '[', selection[0]);
47+
newText = insertText(newText, '](url)', selection[1] + 1);
48+
return {
49+
text: newText,
50+
selection: [selection[0] + 1, selection[1] + 1]
51+
}
52+
},
53+
54+
makeQuote: function (text, selection) {
5655
var newText = insertText(text, '[', selection[0]);
5756
newText = insertText(newText, '](url)', selection[1] + 1);
5857
return {

src/TextHelper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Inserts text in the given position
3+
*
4+
* @param {any} text
5+
* @param {any} insertionText
6+
* @param {any} position
7+
* @returns
8+
*/
9+
export function insertText(text, insertionText, position) {
10+
return [text.slice(0, position), insertionText, text.slice(position)].join('');
11+
}

0 commit comments

Comments
 (0)