|
1 | 1 | // @flow |
2 | 2 |
|
3 | 3 | import React, { Component, Fragment } from 'react' |
4 | | -import { EditorState, ContentBlock, DefaultDraftBlockRenderMap } from 'draft-js' |
5 | | -import { Plugin, withPluginContext } from '@djsp/core' |
6 | | -import { insertEntityBlock, mergeEntityData } from '@djsp/utils' |
| 4 | +import { EditorState, ContentBlock, DefaultDraftBlockRenderMap, RichUtils } from 'draft-js' |
| 5 | +import { withPluginContext } from '@djsp/core' |
| 6 | +import type { PluginProps } from '@djsp/core' |
| 7 | +import { mergeBlockData } from '@djsp/utils' |
7 | 8 | import { CheckableListItem, CheckableListItemBlock, CHECKABLE_LIST_ITEM, blockRenderMap } from 'draft-js-checkable-list-item' |
8 | 9 |
|
9 | | - |
10 | | -type Props = { |
11 | | - block: Object, |
12 | | - blockProps: { |
13 | | - editorState: EditorState, |
14 | | - setEditorState: EditorState => void, |
15 | | - }, |
16 | | -} |
17 | | - |
18 | | -class CheckableList extends Component<Props> { |
| 10 | +class CheckableList extends Component<PluginProps> { |
19 | 11 | _unregister: () => void |
20 | 12 |
|
21 | 13 | onClick = event => { |
22 | 14 | event.stopPropagation() |
23 | 15 |
|
24 | 16 | const { setEditorState, editorState } = this.props |
25 | | - setEditorState(insertEntityBlock(editorState, CHECKABLE_LIST_ITEM, { |
26 | | - checked: false |
27 | | - })) |
| 17 | + setEditorState(RichUtils.toggleBlockType(editorState, CHECKABLE_LIST_ITEM)) |
28 | 18 | } |
29 | 19 |
|
30 | | - toggleChecked = (block) => { |
| 20 | + toggleChecked = (block: ContentBlock) => { |
31 | 21 | const { setEditorState, editorState } = this.props |
32 | | - const content = editorState.getCurrentContent(); |
33 | | - const entityKey = block.getEntityAt(0); |
34 | | - const data = content.getEntity(entityKey).getData(); |
35 | 22 |
|
36 | | - let newEditorState = mergeEntityData(editorState, entityKey, { checked: !data.checked }) |
| 23 | + let newEditorState = mergeBlockData(editorState, block, { checked: !block.getData().get('checked') }) |
37 | 24 | setEditorState(EditorState.forceSelection( |
38 | 25 | newEditorState, |
39 | 26 | editorState.getSelection() |
40 | 27 | )); |
41 | 28 | } |
42 | 29 |
|
43 | | - blockRendererFn = (block: ContentBlock): ?CheckableListItemBlock => { |
44 | | - const { editorState } = this.props; |
45 | | - var content = editorState.getCurrentContent(); |
46 | | - |
47 | | - if (block.getType() === 'atomic') { |
48 | | - var entity = block.getEntityAt(0); |
49 | | - if (!entity) return null; |
50 | | - var entityType = content.getEntity(entity).getType(); |
51 | | - var data = content.getEntity(entity).getData(); |
52 | | - |
53 | | - if (entityType === CHECKABLE_LIST_ITEM) { |
54 | | - return { |
55 | | - component: CheckableListItem, |
56 | | - props: { |
57 | | - onChangeChecked: () => this.toggleChecked(block), |
58 | | - checked: !!data.checked, |
59 | | - }, |
| 30 | + componentDidMount() { |
| 31 | + const { registerPlugin } = this.props |
| 32 | + |
| 33 | + this._unregister = registerPlugin({ |
| 34 | + blockRendererFn: (block: ContentBlock): ?CheckableListItemBlock => { |
| 35 | + if (block.getType() === CHECKABLE_LIST_ITEM) { |
| 36 | + return { |
| 37 | + component: CheckableListItem, |
| 38 | + props: { |
| 39 | + onChangeChecked: () => this.toggleChecked(block), |
| 40 | + checked: !!block.getData().get('checked'), |
| 41 | + }, |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + blockRenderMap: DefaultDraftBlockRenderMap.merge(blockRenderMap), |
| 46 | + blockStyleFn: (block: ContentBlock): ?string => { |
| 47 | + if (block.getType() === CHECKABLE_LIST_ITEM) { |
| 48 | + return CHECKABLE_LIST_ITEM |
60 | 49 | } |
61 | 50 | } |
62 | | - } |
63 | | - } |
64 | | - |
65 | | - blockStyleFn(block: ContentBlock): ?string { |
66 | | - if (block.getType() === CHECKABLE_LIST_ITEM) { |
67 | | - return CHECKABLE_LIST_ITEM |
68 | | - } |
| 51 | + }) |
69 | 52 | } |
70 | 53 |
|
71 | 54 | render() { |
72 | 55 | return (<Fragment> |
73 | | - <Plugin |
74 | | - blockRendererFn={this.blockRendererFn} |
75 | | - blockRenderMap={DefaultDraftBlockRenderMap.merge(blockRenderMap)} |
76 | | - blockStyleFn={this.blockStyleFn} /> |
77 | 56 | <button type="button" onClick={this.onClick}> |
78 | | - Insert checkable-list |
| 57 | + Toggle checkable-list |
79 | 58 | </button> |
80 | 59 | </Fragment>) |
81 | 60 | } |
|
0 commit comments