|
| 1 | +/* |
| 2 | + * TuiEditor |
| 3 | + * wrap toast-ui editor with react and support react 15 |
| 4 | + */ |
| 5 | + |
| 6 | +import React from 'react' |
| 7 | +import PropTypes from 'prop-types' |
| 8 | +import Editor from '@toast-ui/editor' |
| 9 | +import styles from './TuiEditor.scss' |
| 10 | +import cn from 'classnames' |
| 11 | +import 'codemirror/lib/codemirror.css' |
| 12 | +import '@toast-ui/editor/dist/toastui-editor.css' |
| 13 | + |
| 14 | +class TuiEditor extends React.Component { |
| 15 | + constructor(props) { |
| 16 | + super(props) |
| 17 | + this.handleValueChange = this.handleValueChange.bind(this) |
| 18 | + } |
| 19 | + |
| 20 | + getRootElement() { |
| 21 | + return this.refs.rootEl |
| 22 | + } |
| 23 | + |
| 24 | + getInstance() { |
| 25 | + return this.editorInst |
| 26 | + } |
| 27 | + |
| 28 | + bindEventHandlers(props) { |
| 29 | + Object.keys(props) |
| 30 | + .filter(key => /^on[A-Z][a-zA-Z]+/.test(key)) |
| 31 | + .forEach(key => { |
| 32 | + const eventName = key[2].toLowerCase() + key.slice(3) |
| 33 | + this.editorInst.off(eventName) |
| 34 | + this.editorInst.on(eventName, props[key]) |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + componentDidMount() { |
| 39 | + const props = { |
| 40 | + ...this.props, |
| 41 | + onChange: this.handleValueChange |
| 42 | + } |
| 43 | + this.editorInst = new Editor({ |
| 44 | + el: this.refs.rootEl, |
| 45 | + ...props |
| 46 | + }) |
| 47 | + this.bindEventHandlers(props) |
| 48 | + } |
| 49 | + |
| 50 | + handleValueChange(){ |
| 51 | + if (this.props.onChange) { |
| 52 | + this.props.onChange(this.getInstance().getMarkdown()) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + shouldComponentUpdate(nextProps) { |
| 57 | + const instance = this.getInstance() |
| 58 | + const { height, previewStyle, className } = nextProps |
| 59 | + |
| 60 | + if (this.props.height !== height) { |
| 61 | + instance.height(height) |
| 62 | + } |
| 63 | + |
| 64 | + if (this.props.previewStyle !== previewStyle) { |
| 65 | + instance.changePreviewStyle(previewStyle) |
| 66 | + } |
| 67 | + |
| 68 | + if (this.props.className !== className) { |
| 69 | + return true |
| 70 | + } |
| 71 | + // this.bindEventHandlers(nextProps, this.props) |
| 72 | + |
| 73 | + return false |
| 74 | + } |
| 75 | + |
| 76 | + render() { |
| 77 | + return <div ref="rootEl" className={cn(styles.editor, this.props.className)} /> |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +TuiEditor.defaultProps = { |
| 83 | + height: '300px', |
| 84 | + minHeight: '300px', |
| 85 | + initialValue: '', |
| 86 | + previewStyle: '', |
| 87 | + initialEditType: 'wysiwyg', |
| 88 | + language: 'en-US', |
| 89 | + useCommandShortcut: true, |
| 90 | + customHTMLSanitizer: null, |
| 91 | + frontMatter: false, |
| 92 | + hideModeSwitch: false, |
| 93 | + referenceDefinition:false, |
| 94 | + usageStatistics: false, |
| 95 | + useDefaultHTMLSanitizer: true |
| 96 | +} |
| 97 | + |
| 98 | +TuiEditor.propTypes = { |
| 99 | + className: PropTypes.string, |
| 100 | + // Markdown editor's preview style (tab, vertical) |
| 101 | + previewStyle: PropTypes.string.isRequired, |
| 102 | + // Editor's height style value. Height is applied as border-box ex) '300px', '100%', 'auto' |
| 103 | + height: PropTypes.string, |
| 104 | + // Initial editor type (markdown, wysiwyg) |
| 105 | + initialEditType: PropTypes.string, |
| 106 | + // Editor's initial value |
| 107 | + initialValue: PropTypes.string, |
| 108 | + // Editor's min-height style value in pixel ex) '300px' |
| 109 | + minHeight: PropTypes.string, |
| 110 | + // The placeholder text of the editable element. |
| 111 | + placeholder: PropTypes.string, |
| 112 | + // hide mode switch tab bar |
| 113 | + hideModeSwitch: PropTypes.bool, |
| 114 | + // language, 'en-US' |
| 115 | + language: PropTypes.string, |
| 116 | + // whether use keyboard shortcuts to perform commands |
| 117 | + useCommandShortcut: PropTypes.bool, |
| 118 | + // It would be emitted when editor fully load1 |
| 119 | + onLoad: PropTypes.func, |
| 120 | + // It would be emitted when content changed |
| 121 | + onChange: PropTypes.func, |
| 122 | + // It would be emitted when format change by cursor position |
| 123 | + onStateChange: PropTypes.func, |
| 124 | + // It would be emitted when editor get focus |
| 125 | + onFocus: PropTypes.func, |
| 126 | + // It would be emitted when editor loose focus |
| 127 | + onBlur: PropTypes.func, |
| 128 | + // hooks |
| 129 | + hooks: PropTypes.arrayOf(PropTypes.object), |
| 130 | + // send hostname to google analytics |
| 131 | + usageStatistics: PropTypes.bool, |
| 132 | + // use default htmlSanitizer |
| 133 | + useDefaultHTMLSanitizer: PropTypes.bool, |
| 134 | + // toolbar items. |
| 135 | + toolbarItems: PropTypes.arrayOf(PropTypes.object), |
| 136 | + // Array of plugins. A plugin can be either a function or an array in the form of [function, options]. |
| 137 | + plugins: PropTypes.arrayOf(PropTypes.object), |
| 138 | + // Using extended Autolinks specified in GFM spec |
| 139 | + extendedAutolinks: PropTypes.object, |
| 140 | + // convertor extention |
| 141 | + customConvertor: PropTypes.object, |
| 142 | + // Attributes of anchor element that should be rel, target, contenteditable, hreflang, type |
| 143 | + linkAttribute: PropTypes.object, |
| 144 | + // Object containing custom renderer functions correspond to markdown node |
| 145 | + customHTMLRenderer: PropTypes.object, |
| 146 | + // whether use the specification of link reference definition |
| 147 | + referenceDefinition: PropTypes.bool, |
| 148 | + // custom HTML sanitizer |
| 149 | + customHTMLSanitizer: PropTypes.func, |
| 150 | + // whether use the front matter |
| 151 | + frontMatter: PropTypes.bool |
| 152 | +} |
| 153 | + |
| 154 | +export default TuiEditor |
0 commit comments