-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmd-plugin.js
More file actions
62 lines (54 loc) · 1.76 KB
/
md-plugin.js
File metadata and controls
62 lines (54 loc) · 1.76 KB
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
import React, { Component, PropTypes } from 'react';
import { autobind } from 'core-decorators';
import pluginConstructor from './plugin-constructor';
class MDPlugin extends Component {
static propTypes = {
pluginData: PropTypes.object,
isPreviewing: PropTypes.bool,
toggleEditMode: PropTypes.func,
updatePluginData: PropTypes.func,
className: PropTypes.string,
style: PropTypes.object,
width: PropTypes.string,
editMode: PropTypes.bool
};
@autobind
updateMarkdown(markdown) {
const marked = require('marked');
const html = marked(markdown);
const pluginData = { markdown, html };
this.props.updatePluginData({ pluginData, editMode: false });
}
renderContent() {
const { html = '', markdown = '' } = this.props.pluginData;
const { padding } = this.props.style;
let content = (
<div style={{ padding }} onClick={this.props.toggleEditMode}>
{(html !== '')
? <div dangerouslySetInnerHTML={{ __html: html }} />
: 'Click to edit markdown'}
</div>
);
if (this.props.editMode) {
const MarkdownEditor = require('./md-plugin/markdown-editor').default;
content = (<MarkdownEditor
markdown={markdown}
onUpdateMarkdown={this.updateMarkdown}
/>);
}
return content;
}
render() {
const { className = '', style, width, editMode, pluginData: { markdown = '' } } = this.props;
const classNames = `md-plugin ${className}`;
const pluginStyle = Object.assign({}, style, {
textAlign: (editMode || markdown !== '' ? 'left' : 'center'),
width,
padding: 0
});
return (<div className={ classNames } style={ pluginStyle }>
{this.renderContent()}
</div>);
}
}
export default pluginConstructor(MDPlugin);