Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clear function #106

Merged
merged 3 commits into from
Apr 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mui-rte",
"version": "1.16.1",
"version": "1.16.2",
"description": "Material-UI Rich Text Editor and Viewer",
"keywords": [
"material-ui",
13 changes: 4 additions & 9 deletions src/MUIRichTextEditor.tsx
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import Media from './components/Media'
import Blockquote from './components/Blockquote'
import CodeBlock from './components/CodeBlock'
import UrlPopover, { TAlignment, TUrlData, TMediaType } from './components/UrlPopover'
import { getSelectionInfo, getCompatibleSpacing, removeBlockFromMap, atomicBlockExists, isGt } from './utils'
import { getSelectionInfo, getCompatibleSpacing, removeBlockFromMap, atomicBlockExists, isGt, clearInlineStyles } from './utils'

const styles = ({ spacing, typography, palette }: Theme) => createStyles({
root: {
@@ -343,15 +343,10 @@ const MUIRichTextEditor: RefForwardingComponent<any, IMUIRichTextEditorProps> =
}

const handleClearFormat = () => {
const withoutStyles = clearInlineStyles(editorState)
const selectionInfo = getSelectionInfo(editorState)
let newEditorState = editorState
selectionInfo.inlineStyle.forEach((effect) => {
if (effect) {
newEditorState = RichUtils.toggleInlineStyle(newEditorState, effect)
}
})
newEditorState = RichUtils.toggleBlockType(newEditorState, selectionInfo.blockType)
setEditorState(newEditorState)
const newEditorState = EditorState.push(editorState, withoutStyles, 'change-inline-style');
setEditorState(RichUtils.toggleBlockType(newEditorState, selectionInfo.blockType))
}

const handleSave = () => {
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -89,4 +89,11 @@ const isGt = (value: number, maxValue?: number): boolean => {
return value > maxValue
}

export { getSelectionInfo, getCompatibleSpacing, removeBlockFromMap, atomicBlockExists, isGt }
const clearInlineStyles = (editorState: EditorState): ContentState => {
const styles = ['BOLD', 'ITALIC', 'UNDERLINE', 'STRIKETHROUGH']
return styles.reduce((newContentState: ContentState, style: string) => (
Modifier.removeInlineStyle(newContentState, editorState.getSelection(), style)
), editorState.getCurrentContent())
}

export { getSelectionInfo, getCompatibleSpacing, removeBlockFromMap, atomicBlockExists, isGt, clearInlineStyles }