Skip to content

Commit e2483e9

Browse files
committed
Implementing the part that inserts a link
1 parent e15ac85 commit e2483e9

2 files changed

Lines changed: 57 additions & 10 deletions

File tree

src/ReactMde.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,32 @@ class ReactMde extends Component {
7070
}
7171
}
7272

73-
handleBoldCommand() {
73+
/**
74+
* Handles the execution of a command
75+
* @param {function} command
76+
* @memberOf ReactMde
77+
*/
78+
handleCommand(command)
79+
{
7480
let {
7581
value: { text },
7682
onChange
7783
} = this.props;
7884
let textarea = this.refs.textarea;
79-
var newValue = ReactMdeCommands.makeBold(text, getSelection(textarea));
85+
var newValue = command(text, getSelection(textarea));
8086
onChange(newValue);
8187
}
8288

89+
handleBoldCommand() {
90+
this.handleCommand(ReactMdeCommands.makeBold);
91+
}
92+
8393
handleItalicCommand() {
84-
let {
85-
value: { text },
86-
onChange
87-
} = this.props;
88-
let textarea = this.refs.textarea;
89-
var newValue = ReactMdeCommands.makeItalic(text, getSelection(textarea));
90-
onChange(newValue);
94+
this.handleCommand(ReactMdeCommands.makeItalic);
95+
}
96+
97+
handleLinkCommand() {
98+
this.handleCommand(ReactMdeCommands.makeLink);
9199
}
92100

93101
componentDidMount() {
@@ -113,7 +121,7 @@ class ReactMde extends Component {
113121
<HeaderItem icon="italic" onClick={this.handleItalicCommand.bind(this)} />
114122
</HeaderGroup>
115123
<HeaderGroup>
116-
<HeaderItem icon="link" />
124+
<HeaderItem icon="link" onClick={this.handleLinkCommand.bind(this)} />
117125
<HeaderItem icon="quote-right" />
118126
<HeaderItem icon="picture-o" />
119127
</HeaderGroup>

src/ReactMdeCommands.js

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

513
export default {
614

15+
/**
16+
* Makes the text bold
17+
*
18+
* @param {any} text
19+
* @param {any} selection
20+
* @returns
21+
*/
722
makeBold: function (text, selection) {
823
var newText = insertText(text, '**', selection[0]);
924
newText = insertText(newText, '**', selection[1] + 2);
@@ -13,12 +28,36 @@ export default {
1328
}
1429
},
1530

31+
/**
32+
* Makes the text italic
33+
*
34+
* @param {any} text
35+
* @param {any} selection
36+
* @returns
37+
*/
1638
makeItalic: function (text, selection) {
1739
var newText = insertText(text, '*', selection[0]);
1840
newText = insertText(newText, '*', selection[1] + 1);
1941
return {
2042
text: newText,
2143
selection: [selection[0] + 1, selection[1] + 1]
2244
}
45+
},
46+
47+
/**
48+
* Makes a link
49+
*
50+
* @param {any} text
51+
* @param {any} selection
52+
* @returns
53+
*/
54+
makeLink: function (text, selection) {
55+
56+
var newText = insertText(text, '[', selection[0]);
57+
newText = insertText(newText, '](url)', selection[1] + 1);
58+
return {
59+
text: newText,
60+
selection: [selection[0] + 1, selection[1] + 1]
61+
}
2362
}
2463
}

0 commit comments

Comments
 (0)