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

Commit f5a2ea1

Browse files
committed
docs(checkable-list): use contentBlock instead atomicBlock
1 parent 79cb83e commit f5a2ea1

File tree

2 files changed

+29
-80
lines changed

2 files changed

+29
-80
lines changed

examples/checkable-list-example/src/CheckableList.js

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,60 @@
11
// @flow
22

33
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'
78
import { CheckableListItem, CheckableListItemBlock, CHECKABLE_LIST_ITEM, blockRenderMap } from 'draft-js-checkable-list-item'
89

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> {
1911
_unregister: () => void
2012

2113
onClick = event => {
2214
event.stopPropagation()
2315

2416
const { setEditorState, editorState } = this.props
25-
setEditorState(insertEntityBlock(editorState, CHECKABLE_LIST_ITEM, {
26-
checked: false
27-
}))
17+
setEditorState(RichUtils.toggleBlockType(editorState, CHECKABLE_LIST_ITEM))
2818
}
2919

30-
toggleChecked = (block) => {
20+
toggleChecked = (block: ContentBlock) => {
3121
const { setEditorState, editorState } = this.props
32-
const content = editorState.getCurrentContent();
33-
const entityKey = block.getEntityAt(0);
34-
const data = content.getEntity(entityKey).getData();
3522

36-
let newEditorState = mergeEntityData(editorState, entityKey, { checked: !data.checked })
23+
let newEditorState = mergeBlockData(editorState, block, { checked: !block.getData().get('checked') })
3724
setEditorState(EditorState.forceSelection(
3825
newEditorState,
3926
editorState.getSelection()
4027
));
4128
}
4229

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
6049
}
6150
}
62-
}
63-
}
64-
65-
blockStyleFn(block: ContentBlock): ?string {
66-
if (block.getType() === CHECKABLE_LIST_ITEM) {
67-
return CHECKABLE_LIST_ITEM
68-
}
51+
})
6952
}
7053

7154
render() {
7255
return (<Fragment>
73-
<Plugin
74-
blockRendererFn={this.blockRendererFn}
75-
blockRenderMap={DefaultDraftBlockRenderMap.merge(blockRenderMap)}
76-
blockStyleFn={this.blockStyleFn} />
7756
<button type="button" onClick={this.onClick}>
78-
Insert checkable-list
57+
Toggle checkable-list
7958
</button>
8059
</Fragment>)
8160
}

examples/checkable-list-example/src/styles.css

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,3 @@ body {
1212
border: #555;
1313
background: #244a46;
1414
}
15-
16-
17-
.divider {
18-
display: flex;
19-
align-items: center;
20-
justify-content: center;
21-
width: 100%;
22-
height: 100%;
23-
margin: 32px 0;
24-
border: none; /* strip default hr styling */
25-
text-align: center;
26-
}
27-
28-
.divider::after {
29-
margin-left: 48px;
30-
color: rgba(0, 0, 0, 0.26); /* pick a color */
31-
font-size: 2.125rem;
32-
letter-spacing: 48px; /* increase space between dots */
33-
content: '•••';
34-
}
35-
36-
.divider:hover {
37-
border-radius: 2px;
38-
box-shadow: 0 0 0 2px #D2E3F7;
39-
}
40-
41-
.divider.focused {
42-
border-radius: 2px;
43-
box-shadow: 0 0 0 2px #ACCEF7;
44-
}

0 commit comments

Comments
 (0)