-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown-editor.js
50 lines (41 loc) · 1.39 KB
/
markdown-editor.js
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
var App = App || {};
(function() {
'use strict';
var e = React.createElement;
App.MarkdownEditor = React.createClass({
componentDidMount: function() {
var editor = CodeMirror.fromTextArea(this.refs.editor.getDOMNode(), {
mode: 'markdown',
lineWrapping: true,
autofocus: true
})
editor.on('change', function() {
this.props.onChange(editor.getValue());
}.bind(this));
editor.on('scroll', this.handleScroll);
},
handleScroll: function(e) {
var editorViewport = e.getScrollInfo()
, previewViewport = document.getElementsByClassName('preview')[0]
, editorContent = document.getElementsByClassName('CodeMirror-sizer')[0]
, previewContent = document.getElementsByClassName('rendered-markdown')[0]
, editorHeight = editorContent.clientHeight - editorViewport.clientHeight
, previewHeight = previewContent.clientHeight - previewViewport.clientHeight
, ratio = previewHeight / editorHeight;
this.props.syncScroll(editorViewport.top * ratio);
},
render: function() {
return (
e('div', {className: 'editor scrollthis'},
e('div', {className: 'floatingheader'},
e('small', null, 'Markdown')
),
e('textarea', {
className: 'form-control',
ref: 'editor'
})
)
)
}
});
})();