Created
March 13, 2020 14:19
-
-
Save isabellachen/623a6c9587f5437b1c189d09945e6bd8 to your computer and use it in GitHub Desktop.
sidra draftToMarkdown custom style handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { convertFromRaw, convertToRaw, Editor, EditorState, RichUtils } from 'draft-js'; | |
import { draftToMarkdown, markdownToDraft } from 'markdown-draft-js'; | |
import React, { useEffect, useState } from 'react'; | |
import { useIsFirstRender } from '~/core/utils'; | |
import './markdown-editor.scss'; | |
import { BlockTypeControls } from './MarkdownEditorBlockTypeControls'; | |
import { InlineStyleControls } from './MarkdownEditorInlineStyleControls'; | |
type MarkdownEditorProps = { | |
markdown: string; | |
updateMarkdownEdit: React.Dispatch<React.SetStateAction<string>>; | |
}; | |
export const MarkdownEditor = ({ markdown, updateMarkdownEdit }: MarkdownEditorProps) => { | |
let domEditor: Editor; | |
const [editorState, updateEditorState] = useState(EditorState.createEmpty()); | |
const isFirstRender = useIsFirstRender(); | |
useEffect(() => { | |
domEditor.focus(); | |
if (markdown) { | |
const rawData = markdownToDraft(markdown); | |
const contentState = convertFromRaw(rawData); | |
const editorStateFromExistingMarkdown = EditorState.createWithContent(contentState); | |
updateEditorState(editorStateFromExistingMarkdown); | |
} | |
}, []); | |
const toggleInlineStyle = (inlineStyle: string) => { | |
const newEditorState = RichUtils.toggleInlineStyle(editorState, inlineStyle); | |
updateEditorState(newEditorState); | |
}; | |
const toggleBlockType = (blockType: string) => { | |
const newEditorState = RichUtils.toggleBlockType(editorState, blockType); | |
updateEditorState(newEditorState); | |
}; | |
const setDomEditorRef = (ref: Editor) => { | |
domEditor = ref; | |
}; | |
const focusEditor = () => { | |
domEditor.focus(); | |
}; | |
const customStyleHandlersToMarkdown = { | |
styleItems: { | |
UNDERLINE: { | |
open: function() { | |
return '<u>'; | |
}, | |
close: function() { | |
return '</u>'; | |
}, | |
}, | |
}, | |
}; | |
const onChangeInEditor = (e: EditorState) => { | |
if (isFirstRender) { | |
return; | |
} | |
updateEditorState(e); | |
const contentState = editorState.getCurrentContent(); | |
const rawObject = convertToRaw(contentState); | |
const markdownString = draftToMarkdown(rawObject, customStyleHandlersToMarkdown); | |
updateMarkdownEdit(markdownString); | |
}; | |
return ( | |
<> | |
<BlockTypeControls editorState={editorState} onToggle={toggleBlockType} /> | |
<InlineStyleControls editorState={editorState} onToggle={toggleInlineStyle} /> | |
<div className="markdown-editor__container"> | |
<div className="markdown-editor__inner" onClick={focusEditor}> | |
<Editor editorState={editorState} onChange={onChangeInEditor} ref={setDomEditorRef} /> | |
</div> | |
</div> | |
</> | |
); | |
}; |
Author
isabellachen
commented
Mar 13, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment