-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReactMarkdownMediumEditor.jsx
131 lines (115 loc) · 3.76 KB
/
ReactMarkdownMediumEditor.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React from 'react';
import ReactDOM from 'react-dom';
import toMarkdown from 'to-markdown';
import {Showdown} from 'meteor/markdown';
import i18n from 'meteor/universe:i18n';
const T = i18n.createComponent('universe:react-markdown-wysiwyg');
const converter = new Showdown.converter();
export default React.createClass({
displayName: 'MarkdownMediumEditor',
getInitialState () {
return {
text: converter.makeHtml(this.props.markdown || '')
};
},
getDefaultProps () {
return {
tag: 'div'
};
},
componentDidMount () {
var dom = ReactDOM.findDOMNode(this);
var MediumEditor = require('medium-editor');
this.medium = new MediumEditor (dom, UniUtils.deepExtend(options, this.props.options || {}));
this.medium.subscribe('editableInput', () => {
this._updated = true;
// temporally bugfix of issue #145
$('p > ol, p > ul, p > p', dom).each(function () {
$(this).unwrap();
});
$('li > span ~ br', dom).remove();
$('li > p', dom).each(function () {
let $this = $(this);
$this.parents(':first').html($this.html());
});
this.change(dom.innerHTML);
});
this.medium.subscribe('blur', () => {
this._updated = true;
// temporally bugfix of issue #145
$('p > ol, p > ul, p > p', dom).each(function () {
$(this).unwrap();
});
$('li > span ~ br', dom).remove();
$('li > p', dom).each(function () {
let $this = $(this);
$this.parents(':first').html($this.html());
});
this.blur(dom.innerHTML);
});
},
componentWillUnmount () {
this.medium.destroy();
},
componentWillReceiveProps (nextProps) {
if (nextProps.text !== this.state.text && !this._updated) {
this.setState({text: converter.makeHtml(nextProps.markdown)});
}
if (this._updated) {
this._updated = false;
}
},
render () {
return React.createElement(this.props.tag, {
className: this.props.className,
contentEditable: true,
style: this.props.style,
dangerouslySetInnerHTML: {__html: this.state.text}
});
},
change (text) {
if (this.props.onChange) {
this.props.onChange(getMarkdown(text));
}
},
blur (text) {
if (this.props.onBlur) {
this.props.onBlur(getMarkdown(text));
}
}
});
var options = {
toolbar: {
/* These are the default options for the toolbar,
if nothing is passed this is what is used */
allowMultiParagraphSelection: true,
buttons: ['bold', 'italic', 'underline', 'anchor', 'h1', 'h2', 'h3', 'h4', 'orderedlist', 'unorderedlist', 'indent', 'outdent', 'quote', 'pre'],
diffLeft: 0,
diffTop: -10,
firstButtonClass: 'medium-editor-button-first',
lastButtonClass: 'medium-editor-button-last',
standardizeSelectionStart: false,
static: false,
relativeContainer: null,
buttonLabels: true
},
paste: {
cleanAttrs: ['class', 'style', 'dir'],
cleanTags: ['meta', 'span']
},
placeholder: {
text: T.__('typeYourText')
}
};
var toMarkdownOptions = {converters:[{
filter: 'pre',
replacement: function(content) {
return '`' + content + '`';
}
}]};
function getMarkdown (text) {
text = UniHTML.purify(text, {withoutTags: ['span']});
return (toMarkdown(text, toMarkdownOptions).split('\n').map(function (c) {
return c.trim();
}).join('\n').trim());
}