|
| 1 | +/* |
| 2 | + * TuiEditor |
| 3 | + */ |
| 4 | + |
| 5 | +import React, { useState } from "react"; |
| 6 | +import PropTypes from "prop-types"; |
| 7 | +import cn from "classnames"; |
| 8 | +import { Editor } from "@toast-ui/react-editor"; |
| 9 | +import styles from "./styles.module.scss"; |
| 10 | + |
| 11 | +const TuiEditor = (props) => { |
| 12 | + const [editorElement, setEditorElement] = useState(null); |
| 13 | + const onChange = () => { |
| 14 | + const mk = editorElement.editorInst.getMarkdown(); |
| 15 | + props.onChange(mk); |
| 16 | + }; |
| 17 | + return ( |
| 18 | + <div className={cn(styles["editor-container"], props.className)}> |
| 19 | + <Editor |
| 20 | + {...props} |
| 21 | + ref={setEditorElement} |
| 22 | + onChange={onChange} |
| 23 | + initialValue={props.value} |
| 24 | + /> |
| 25 | + </div> |
| 26 | + ); |
| 27 | +}; |
| 28 | + |
| 29 | +TuiEditor.defaultProps = { |
| 30 | + height: "320px", |
| 31 | + minHeight: "320px", |
| 32 | + initialValue: "", |
| 33 | + previewStyle: "", |
| 34 | + initialEditType: "wysiwyg", |
| 35 | + language: "en-US", |
| 36 | + useCommandShortcut: true, |
| 37 | + customHTMLSanitizer: null, |
| 38 | + frontMatter: false, |
| 39 | + hideModeSwitch: true, |
| 40 | + referenceDefinition: false, |
| 41 | + usageStatistics: false, |
| 42 | + useDefaultHTMLSanitizer: true, |
| 43 | +}; |
| 44 | + |
| 45 | +TuiEditor.propTypes = { |
| 46 | + // Editor's initial value |
| 47 | + value: PropTypes.string, |
| 48 | + className: PropTypes.string, |
| 49 | + // Markdown editor's preview style (tab, vertical) |
| 50 | + previewStyle: PropTypes.string.isRequired, |
| 51 | + // Editor's height style value. Height is applied as border-box ex) '300px', '100%', 'auto' |
| 52 | + height: PropTypes.string, |
| 53 | + // Initial editor type (markdown, wysiwyg) |
| 54 | + initialEditType: PropTypes.string, |
| 55 | + // Editor's min-height style value in pixel ex) '300px' |
| 56 | + minHeight: PropTypes.string, |
| 57 | + // The placeholder text of the editable element. |
| 58 | + placeholder: PropTypes.string, |
| 59 | + // hide mode switch tab bar |
| 60 | + hideModeSwitch: PropTypes.bool, |
| 61 | + // language, 'en-US' |
| 62 | + language: PropTypes.string, |
| 63 | + // whether use keyboard shortcuts to perform commands |
| 64 | + useCommandShortcut: PropTypes.bool, |
| 65 | + // It would be emitted when editor fully load1 |
| 66 | + onLoad: PropTypes.func, |
| 67 | + // It would be emitted when content changed |
| 68 | + onChange: PropTypes.func, |
| 69 | + // It would be emitted when format change by cursor position |
| 70 | + onStateChange: PropTypes.func, |
| 71 | + // It would be emitted when editor get focus |
| 72 | + onFocus: PropTypes.func, |
| 73 | + // It would be emitted when editor loose focus |
| 74 | + onBlur: PropTypes.func, |
| 75 | + // hooks |
| 76 | + hooks: PropTypes.arrayOf(PropTypes.object), |
| 77 | + // send hostname to google analytics |
| 78 | + usageStatistics: PropTypes.bool, |
| 79 | + // use default htmlSanitizer |
| 80 | + useDefaultHTMLSanitizer: PropTypes.bool, |
| 81 | + // toolbar items. |
| 82 | + toolbarItems: PropTypes.arrayOf(PropTypes.object), |
| 83 | + // Array of plugins. A plugin can be either a function or an array in the form of [function, options]. |
| 84 | + plugins: PropTypes.arrayOf(PropTypes.object), |
| 85 | + // Using extended Autolinks specified in GFM spec |
| 86 | + extendedAutolinks: PropTypes.object, |
| 87 | + // convertor extention |
| 88 | + customConvertor: PropTypes.object, |
| 89 | + // Attributes of anchor element that should be rel, target, contenteditable, hreflang, type |
| 90 | + linkAttribute: PropTypes.object, |
| 91 | + // Object containing custom renderer functions correspond to markdown node |
| 92 | + customHTMLRenderer: PropTypes.object, |
| 93 | + // whether use the specification of link reference definition |
| 94 | + referenceDefinition: PropTypes.bool, |
| 95 | + // custom HTML sanitizer |
| 96 | + customHTMLSanitizer: PropTypes.func, |
| 97 | + // whether use the front matter |
| 98 | + frontMatter: PropTypes.bool, |
| 99 | +}; |
| 100 | + |
| 101 | +export default TuiEditor; |
0 commit comments