Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
fix(checkable-list-example): use custom mergeBlockData to avoid update
Browse files Browse the repository at this point in the history
issue and add Draft.css
  • Loading branch information
freedomlang committed Apr 16, 2019
1 parent f5279e3 commit 962882e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
52 changes: 32 additions & 20 deletions examples/checkable-list-example/src/CheckableList.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
ContentBlock,
DefaultDraftBlockRenderMap,
RichUtils,
Modifier,
} from 'draft-js'
import { withPluginContext } from '@djsp/core'
import type { PluginProps } from '@djsp/core'
Expand All @@ -18,40 +17,53 @@ import {
CheckableListItemUtils,
} from 'draft-js-checkable-list-item'

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'
)
}

class CheckableList extends Component<PluginProps> {
_unregister: () => void

updateEditorState = (newEditorState: EditorState) => {
const { setEditorState, editorState } = this.props
setEditorState(
EditorState.forceSelection(newEditorState, editorState.getSelection())
)
}

onClick = event => {
event.stopPropagation()

const { setEditorState, editorState } = this.props
const { editorState } = this.props
const newEditorState = RichUtils.toggleBlockType(
editorState,
CHECKABLE_LIST_ITEM
)
setEditorState(
EditorState.forceSelection(newEditorState, editorState.getSelection())
)
this.updateEditorState(newEditorState)
}

toggleChecked = (block: ContentBlock) => {
const { setEditorState, editorState } = this.props
let newContentState = Modifier.mergeBlockData(
editorState.getCurrentContent(),
editorState.getSelection(),
{
checked: !block.getData().get('checked'),
}
)
let newEditorState = EditorState.push(
editorState,
newContentState,
'change-block-data'
)
setEditorState(newEditorState)
const { editorState } = this.props
let newEditorState = mergeBlockData(editorState, block, {
checked: !block.getData().get('checked'),
})
this.updateEditorState(newEditorState)
}

handleTab = (event: SyntheticKeyboardEvent): ?boolean => {
// debugger
if (this.adjustBlockDepth(event)) {
return true
}
Expand Down Expand Up @@ -93,7 +105,7 @@ class CheckableList extends Component<PluginProps> {
return CHECKABLE_LIST_ITEM
}
},
// handleTab: this.handleTab
onTab: this.handleTab,
})
}

Expand Down
1 change: 1 addition & 0 deletions examples/checkable-list-example/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ReactDOM from 'react-dom'
import { EditorState, convertFromRaw } from 'draft-js'
import { EditorContainer, Editor } from '@djsp/core'
import CheckableList from './CheckableList'
import 'draft-js/dist/Draft.css'
import 'draft-js-checkable-list-item/lib/CheckableListItem.css'
import './styles.css'

Expand Down

0 comments on commit 962882e

Please sign in to comment.