Skip to content

Commit

Permalink
Fix clear function (niuware#106)
Browse files Browse the repository at this point in the history
* Add clear inline styles function

* Implement clearInlineStyles function

* Update package version
  • Loading branch information
niuware authored Apr 28, 2020
1 parent 7035c22 commit 851f512
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
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",
Expand Down
13 changes: 4 additions & 9 deletions src/MUIRichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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 = () => {
Expand Down
9 changes: 8 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

0 comments on commit 851f512

Please sign in to comment.