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