-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHG migrate text editor to separate component
- Loading branch information
Showing
7 changed files
with
62 additions
and
47 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
56 changes: 56 additions & 0 deletions
56
web/src/components/forums/RichTextEditor/RichTextEditor.jsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, {useState, useRef} from 'react'; | ||
import {EditorState, RichUtils, convertToRaw} from 'draft-js'; | ||
import Editor, {composeDecorators} from '@draft-js-plugins/editor'; | ||
import createResizablePlugin from '@draft-js-plugins/resizeable'; | ||
import EditorToolbar from './Toolbar/EditorToolbar'; | ||
import createImagePlugin from '@draft-js-plugins/image'; | ||
import {makeStyles} from '@mui/styles'; | ||
import 'draft-js/dist/Draft.css'; | ||
import './TextEditor.css'; | ||
const resizeablePlugin = createResizablePlugin(); | ||
const decorator = composeDecorators( | ||
resizeablePlugin.decorator, | ||
); | ||
const imagePlugin = createImagePlugin({decorator}); | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
toolbarContainer: { | ||
borderColor: theme.palette.white, | ||
borderTop: theme.spacing(0.1) + ' solid', | ||
borderBottom: theme.spacing(0.1) + ' solid', | ||
}, | ||
})); | ||
|
||
export default function RichTextEditor({getContent}) { | ||
const [editorState, setEditorState] = useState(EditorState.createEmpty()); | ||
const editor = useRef(null); | ||
const classes = useStyles(); | ||
getContent = () => { | ||
return JSON.stringify(convertToRaw(editorState.getCurrentContent())); | ||
}; | ||
|
||
const handleKeyCommand = (command) => { | ||
const newState = RichUtils.handleKeyCommand(editorState, command); | ||
if (newState) { | ||
setEditorState(newState); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
return ( | ||
<> | ||
<div className={classes.toolbarContainer}> | ||
<EditorToolbar editorState={editorState} setEditorState={setEditorState} imagePlugin={imagePlugin} /> | ||
</div> | ||
<Editor | ||
ref={editor} | ||
handleKeyCommand={handleKeyCommand} | ||
editorState={editorState} | ||
onChange={(editorState) => { | ||
setEditorState(editorState); | ||
}} | ||
plugins={[imagePlugin, resizeablePlugin]} | ||
/> | ||
</> | ||
); | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.