Skip to content

Commit 26831ae

Browse files
committed
fixes #26 where when multiple instances of the editor exists, a style change (e.g bold, list, italics, etc) on once instance was also updating the others. This fix generates a unique id for each instance that the update event checks to ensure only the correct instance is actually updated
1 parent ec65c33 commit 26831ae

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

src/MarkdownEditor.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ var MarkdownEditor = React.createClass({
3737
},
3838

3939
getInitialState: function() {
40-
return {content: this.props.initialContent, inEditMode: true};
40+
var uniqueInstanceRef = Math.random().toString(36).substring(7)
41+
return {content: this.props.initialContent, inEditMode: true, instanceRef: uniqueInstanceRef};
4142
},
4243

4344
render: function() {
@@ -48,8 +49,9 @@ var MarkdownEditor = React.createClass({
4849
divContent = <MarkdownEditorContent styles={{styleMarkdownTextArea: this.props.styles.styleMarkdownTextArea}}
4950
content={this.state.content} onChangeHandler={this.onChangeHandler}/>;
5051
if (this.props.editorTabs !== false){
52+
5153
editorMenu = <MarkdownEditorMenu styles={{styleMarkdownMenu: this.props.styles.styleMarkdownMenu}}
52-
iconsSet={this.props.iconsSet}/>;
54+
iconsSet={this.props.iconsSet} instanceRef={this.state.instanceRef}/>;
5355
}
5456
} else {
5557
divContent = <MarkdownEditorPreview styles={{styleMarkdownPreviewArea: this.props.styles.styleMarkdownPreviewArea}}
@@ -92,7 +94,7 @@ var MarkdownEditor = React.createClass({
9294
handleMarkdowEditorStoreUpdated: function(markdownEditorStoreState) {
9395
var currentSelection = markdownEditorStoreState.currentSelection;
9496

95-
if (currentSelection != null) {
97+
if (currentSelection != null && markdownEditorStoreState.instanceRef === this.state.instanceRef) {
9698
this.updateText(this.state.content, currentSelection, markdownEditorStoreState.action);
9799
}
98100
},
@@ -105,6 +107,7 @@ var MarkdownEditor = React.createClass({
105107
},
106108

107109
updateText: function(text, selection, actionType) {
110+
debugger;
108111
var token = this.generateMarkdownToken(actionType);
109112
var beforeSelectionContent = text.slice(0, selection.selectionStart);
110113
var afterSelectionContent = text.slice(selection.selectionEnd, text.length);

src/components/MarkdownEditorMenu.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,36 @@ var MarkdownEditorMenu = React.createClass({
5959
}
6060
},
6161

62-
handleBoldButtonClick: function() {
63-
MarkdownEditorActions.makeBold();
62+
handleBoldButtonClick: function() {
63+
MarkdownEditorActions.makeBold(this.props.instanceRef);
6464
},
6565

6666
handleImageButtonClick: function() {
67-
MarkdownEditorActions.makeImage();
67+
MarkdownEditorActions.makeImage(this.props.instanceRef);
6868
},
6969

7070
handleItalicButtonClick: function() {
71-
MarkdownEditorActions.makeItalic();
71+
MarkdownEditorActions.makeItalic(this.props.instanceRef);
7272
},
7373

7474
handleUnderlineButtonClick: function() {
75-
MarkdownEditorActions.makeUnderline();
75+
MarkdownEditorActions.makeUnderline(this.props.instanceRef);
7676
},
7777

7878
handleHeaderButtonClick: function() {
79-
MarkdownEditorActions.makeHeader();
79+
MarkdownEditorActions.makeHeader(this.props.instanceRef);
8080
},
8181

8282
handleSubHeaderButtonClick: function() {
83-
MarkdownEditorActions.makeSubHeader();
83+
MarkdownEditorActions.makeSubHeader(this.props.instanceRef);
8484
},
8585

8686
handleLinkButtonClick: function() {
87-
MarkdownEditorActions.makeLink();
87+
MarkdownEditorActions.makeLink(this.props.instanceRef);
8888
},
8989

9090
handleListButtonClick: function() {
91-
MarkdownEditorActions.makeList();
91+
MarkdownEditorActions.makeList(this.props.instanceRef);
9292
}
9393
});
9494

src/stores/MarkdownEditorStore.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var MarkdownSelectionActions = require('../actions/MarkdownSelectionActions');
44

55
var MarkdownEditorStore = Reflux.createStore({
66
init: function() {
7+
78
this.currentSelection = null;
89
this.listenTo(MarkdownEditorActions.makeBold, this.handleMakeBold);
910
this.listenTo(MarkdownEditorActions.makeItalic, this.handleMakeItalic);
@@ -17,36 +18,36 @@ var MarkdownEditorStore = Reflux.createStore({
1718
this.listenTo(MarkdownEditorActions.setSelection, this.handleSetSelection);
1819
},
1920

20-
handleMakeBold: function() {
21-
this.trigger({action: 'bold', currentSelection: this.currentSelection});
21+
handleMakeBold: function(instanceRef) {
22+
this.trigger({action: 'bold', currentSelection: this.currentSelection, instanceRef: instanceRef});
2223
},
2324

24-
handleMakeItalic: function() {
25-
this.trigger({action: 'italic', currentSelection: this.currentSelection});
25+
handleMakeItalic: function(instanceRef) {
26+
this.trigger({action: 'italic', currentSelection: this.currentSelection, instanceRef: instanceRef});
2627
},
2728

28-
handleMakeLink: function() {
29-
this.trigger({action: 'link', currentSelection: this.currentSelection});
29+
handleMakeLink: function(instanceRef) {
30+
this.trigger({action: 'link', currentSelection: this.currentSelection, instanceRef: instanceRef});
3031
},
3132

32-
handleMakeUnderline: function() {
33-
this.trigger({action: 'underline', currentSelection: this.currentSelection});
33+
handleMakeUnderline: function(instanceRef) {
34+
this.trigger({action: 'underline', currentSelection: this.currentSelection, instanceRef: instanceRef});
3435
},
3536

36-
handleMakeHeader: function() {
37-
this.trigger({action: 'header', currentSelection: this.currentSelection});
37+
handleMakeHeader: function(instanceRef) {
38+
this.trigger({action: 'header', currentSelection: this.currentSelection, instanceRef: instanceRef});
3839
},
3940

40-
handleMakeSubHeader: function() {
41-
this.trigger({action: 'subheader', currentSelection: this.currentSelection});
41+
handleMakeSubHeader: function(instanceRef) {
42+
this.trigger({action: 'subheader', currentSelection: this.currentSelection, instanceRef: instanceRef});
4243
},
4344

44-
handleMakeList: function() {
45-
this.trigger({action: 'list', currentSelection: this.currentSelection});
45+
handleMakeList: function(instanceRef) {
46+
this.trigger({action: 'list', currentSelection: this.currentSelection, instanceRef: instanceRef});
4647
},
4748

48-
handleMakeImage: function() {
49-
this.trigger({action: 'image', currentSelection: this.currentSelection});
49+
handleMakeImage: function(instanceRef) {
50+
this.trigger({action: 'image', currentSelection: this.currentSelection, instanceRef: instanceRef});
5051
},
5152

5253
handleClearSelection: function() {

0 commit comments

Comments
 (0)