From 1e5ef1309cfa705a4478ca432dae24889d8d0b08 Mon Sep 17 00:00:00 2001 From: freedomlang Date: Thu, 17 Jan 2019 23:05:48 +0800 Subject: [PATCH] use mergeBlockData of Modifier instead of adding new one in utils --- .../src/CheckableList.js | 25 ++++++++----------- packages/utils/README.md | 6 ----- packages/utils/src/index.js | 16 ------------ 3 files changed, 10 insertions(+), 37 deletions(-) diff --git a/examples/checkable-list-example/src/CheckableList.js b/examples/checkable-list-example/src/CheckableList.js index ad387a4..f7866fd 100644 --- a/examples/checkable-list-example/src/CheckableList.js +++ b/examples/checkable-list-example/src/CheckableList.js @@ -9,7 +9,7 @@ import { } from 'draft-js' import { withPluginContext } from '@djsp/core' import type { PluginProps } from '@djsp/core' -import { mergeBlockData } from '@djsp/utils' +import { Modifier } from 'draft-js' import { CheckableListItem, CheckableListItemBlock, @@ -20,31 +20,26 @@ import { class CheckableList extends Component { _unregister: () => void - updateEditorState = (newEditorState: EditorState) => { - const { setEditorState, editorState } = this.props - setEditorState( - EditorState.forceSelection(newEditorState, editorState.getSelection()) - ) - } - onClick = event => { event.stopPropagation() - const { editorState } = this.props + const { setEditorState, editorState } = this.props const newEditorState = RichUtils.toggleBlockType( editorState, CHECKABLE_LIST_ITEM ) - this.updateEditorState(newEditorState) + setEditorState( + EditorState.forceSelection(newEditorState, editorState.getSelection()) + ) } toggleChecked = (block: ContentBlock) => { - const { editorState } = this.props - - let newEditorState = mergeBlockData(editorState, block, { + const { setEditorState, editorState } = this.props + let newContentState = Modifier.mergeBlockData(editorState.getCurrentContent(), editorState.getSelection(), { checked: !block.getData().get('checked'), - }) - this.updateEditorState(newEditorState) + }); + let newEditorState = EditorState.push(editorState, newContentState, 'change-block-data'); + setEditorState(newEditorState) } componentDidMount() { diff --git a/packages/utils/README.md b/packages/utils/README.md index f0bdac7..6962feb 100644 --- a/packages/utils/README.md +++ b/packages/utils/README.md @@ -149,12 +149,6 @@ getWordCount(editorState: EditorState): number mergeEntityData(editorState: EditorState, entityKey: string, data: Object): EditorState ``` -### mergeBlockData - -```javascript -mergeBlockData(editorState: EditorState, block: ContentBlock, data: { [id: string]: any }): EditorState -``` - ## License MIT © [juliankrispel](https://github.com/juliankrispel) diff --git a/packages/utils/src/index.js b/packages/utils/src/index.js index 464f058..f0899a4 100644 --- a/packages/utils/src/index.js +++ b/packages/utils/src/index.js @@ -278,19 +278,3 @@ export function mergeEntityData( return EditorState.push(editorState, newContentState, 'apply-entity') } - -export function mergeBlockData( - editorState: EditorState, - block: ContentBlock, - data: { [id: string]: any } -): EditorState { - const content = editorState.getCurrentContent() - const updatedBlock = block.mergeIn(['data'], data) - const blockKey = block.getKey() - const blockMap = content.getBlockMap().merge({ [blockKey]: updatedBlock }) - return EditorState.push( - editorState, - content.merge({ blockMap }), - 'change-block-data' - ) -}