|
| 1 | +import React, { useState } from "react"; |
| 2 | +import { Editor } from "react-draft-wysiwyg"; |
| 3 | +import "../../../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css"; |
| 4 | +// import draftToMarkdown from "draftjs-to-markdown"; |
| 5 | +import Button from "../button/Button"; |
| 6 | +import DashboardLayout from "../../layout/dashboardLayout/DashboardLayout"; |
| 7 | +import { RichUtils } from 'draft-js'; |
| 8 | +import { draftToMarkdown, markdownToDraft } from 'markdown-draft-js'; |
| 9 | +import { EditorState, convertToRaw, convertFromRaw } from 'draft-js'; |
| 10 | + |
| 11 | +import './MarkDownEditor.scss' |
| 12 | + |
| 13 | +const MarkDownEditor = () => { |
| 14 | + const [editorState, setEditorState] = useState(EditorState.createEmpty()); |
| 15 | + const [markDown, setMarkDown] = useState(false); |
| 16 | + const [markDownText, setMarkDownText] = useState(null); |
| 17 | + |
| 18 | + function onEditorStateChange(editorState) { |
| 19 | + setEditorState(editorState); |
| 20 | + } |
| 21 | + |
| 22 | + function changeToMarkDown() { |
| 23 | + const rawContentState = convertToRaw(editorState.getCurrentContent()); |
| 24 | + setMarkDownText(draftToMarkdown(rawContentState)); |
| 25 | + setMarkDown(true) |
| 26 | + } |
| 27 | + |
| 28 | + function changeToEditor() { |
| 29 | + |
| 30 | + const markDownData = markdownToDraft(markDownText); |
| 31 | + const rawMarkDown = convertFromRaw(markDownData); |
| 32 | + |
| 33 | + setEditorState(EditorState.createWithContent(rawMarkDown)) |
| 34 | + setMarkDown(false) |
| 35 | + } |
| 36 | + |
| 37 | + function uploadImageCallBack(file) { |
| 38 | + // return new Promise( |
| 39 | + // (resolve, reject) => { |
| 40 | + // const xhr = new XMLHttpRequest(); // eslint-disable-line no-undef |
| 41 | + // xhr.open('POST', 'https://api.imgur.com/3/image'); |
| 42 | + // xhr.setRequestHeader('Authorization', 'Client-ID 8d26ccd12712fca'); |
| 43 | + // const data = new FormData(); // eslint-disable-line no-undef |
| 44 | + // data.append('image', file); |
| 45 | + // xhr.send(data); |
| 46 | + // xhr.addEventListener('load', () => { |
| 47 | + // const response = JSON.parse(xhr.responseText); |
| 48 | + // resolve(response); |
| 49 | + // }); |
| 50 | + // xhr.addEventListener('error', () => { |
| 51 | + // const error = JSON.parse(xhr.responseText); |
| 52 | + // reject(error); |
| 53 | + // }); |
| 54 | + // }, |
| 55 | + // ); |
| 56 | + } |
| 57 | + |
| 58 | + const toolbarOptions = { |
| 59 | + options: ['inline', 'blockType', 'fontSize', 'fontFamily', 'list', 'textAlign', 'link', 'image', 'remove', 'history'], |
| 60 | + image: { |
| 61 | + className: 'button-color', |
| 62 | + uploadCallback: uploadImageCallBack, |
| 63 | + previewImage: true |
| 64 | + }, |
| 65 | + // blockType: { |
| 66 | + // className: 'button-color' |
| 67 | + // }, |
| 68 | + // inline: { |
| 69 | + // className: 'button-color' |
| 70 | + // }, |
| 71 | + // history: { |
| 72 | + // className: 'button-color' |
| 73 | + // }, |
| 74 | + // remove: { |
| 75 | + // className: 'button-color' |
| 76 | + // }, |
| 77 | + // fontSize: { |
| 78 | + // className: 'button-color' |
| 79 | + // }, |
| 80 | + // fontFamily: { |
| 81 | + // className: 'button-color' |
| 82 | + // }, |
| 83 | + // list: { |
| 84 | + // className: 'button-color' |
| 85 | + // }, |
| 86 | + // textAlign: { |
| 87 | + // className: 'button-color' |
| 88 | + // }, |
| 89 | + // link: { |
| 90 | + // className: 'button-color' |
| 91 | + // }, |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + // const styleButton = (array) => { |
| 96 | + |
| 97 | + // const customStyle = array.map(item => Object.prototype.constructor({`${item ":" "button-color"}`})) |
| 98 | + // return [...customStyle] |
| 99 | + // } |
| 100 | + |
| 101 | + // const styledButtonClass = styleButton(toolbarOptions.options); |
| 102 | + // console.log(styledButtonClass) |
| 103 | + |
| 104 | + //styling mark down editor |
| 105 | + const editorStyle = { |
| 106 | + border: "0.5px solid var(--dropdowns)", |
| 107 | + backgroundColor: "var(--cards)", |
| 108 | + height: "200px", |
| 109 | + borderRadius: '4px', |
| 110 | + color: 'var(--primary-white)', |
| 111 | + marginBottom: "40px", |
| 112 | + padding: '8px', |
| 113 | + overflowY: "auto" |
| 114 | + } |
| 115 | + |
| 116 | + const toolbarStyle = { |
| 117 | + background: 'var(--dropdowns)', |
| 118 | + marginBottom: "14px", |
| 119 | + border: "0.5px solid var(--dropdowns)", |
| 120 | + borderRadius: '4px', |
| 121 | + padding: '8px' |
| 122 | + } |
| 123 | + |
| 124 | + return ( |
| 125 | + <> |
| 126 | + <div className="mark-down-container"> |
| 127 | + {!markDown ? <Editor |
| 128 | + toolbarStyle={toolbarStyle} |
| 129 | + editorStyle={editorStyle} |
| 130 | + editorState={editorState} |
| 131 | + onEditorStateChange={onEditorStateChange} |
| 132 | + toolbar={toolbarOptions} |
| 133 | + // toolbarCustomButtons={[<CustomOption />]} |
| 134 | + /> |
| 135 | + : <textarea style={editorStyle} value={markDownText} onChange={e => setMarkDownText(e.target.value)} /> |
| 136 | + } |
| 137 | + { |
| 138 | + !markDown |
| 139 | + ? <button className="secondary-btn markdown-button" onClick={changeToMarkDown}>Mark down</button> |
| 140 | + : <button className="secondary-btn markdown-button" onClick={changeToEditor}>Editor</button> |
| 141 | + } |
| 142 | + </div> |
| 143 | + <div> |
| 144 | + </div> |
| 145 | + </> |
| 146 | + ) |
| 147 | +} |
| 148 | + |
| 149 | +// const CustomOption = ({editorState, onChange}) => { |
| 150 | + |
| 151 | +// const toggleMarkDown = () => { |
| 152 | +// const newState = RichUtils.toggleInlineStyle( |
| 153 | +// editorState, |
| 154 | +// 'Mark Down', |
| 155 | +// ); |
| 156 | +// if (newState) { |
| 157 | +// onChange(newState); |
| 158 | +// } |
| 159 | +// }; |
| 160 | +// return(<div className="rdw-storybook-custom-option" onClick={toggleMarkDown}>MD</div>) |
| 161 | +// } |
| 162 | + |
| 163 | +export default MarkDownEditor |
0 commit comments