This repository has been archived by the owner on Aug 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
editor.jsx
79 lines (70 loc) · 1.97 KB
/
editor.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
MediumEditorComp = React.createClass({
//Get the default Properties
//We are setting edit by default to true
getDefaultProps () {
return {
edit : true
};
},
//Init the medium editor and create a reference
//Within our component instance
initMedium () {
let instance = this;
instance.medium = new MediumEditor(instance.refs.editor, {
toolbar: this.props.toolbar,
anchor: this.props.anchor,
anchorPreview: this.props.anchorPreview,
placeholder: this.props.placeholder,
paste: this.props.paste,
keyboardCommands: this.props.keyboardCommands
});
//Subscribe to changes and if there is a callback call it
instance.medium.subscribe('editableInput', function (event, editor) {
if(typeof instance.props.onChange === 'function') {
instance.props.onChange(event, instance.refs.editor.innerHTML);
}
});
},
//Allow us to toggle the medium editor
toggleMediumEditor () {
if(this.props.edit) {
this.initMedium();
} else if(this.medium) {
this.medium.destroy();
}
},
//On mount toggle the editor based on the edit prop
componentDidMount() {
this.toggleMediumEditor();
},
//When the component is unmounted destroy the
//Non-isomorphic component
componentWillUnmount () {
if(this.medium)
this.medium.destroy();
},
//Check if we should update
shouldComponentUpdate (nextProps, nextState) {
return nextProps.edit !== this.props.edit;
},
//Toggle the editor when something updates
componentDidUpdate (prevProps, prevState) {
this.toggleMediumEditor();
},
//Render our component
render() {
let instance = this;
return(
<div
ref="editor"
className="editable"
spellCheck="true"
role="textbox"
aria-multiline="true"
dangerouslySetInnerHTML={ { __html : instance.props.defaultValue } }
style={ this.props.style }
{...this.props.other}
/>
);
}
});