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

Commit 7bb8e7c

Browse files
committed
keep the position of caret after toggle checkable-list
1 parent f5a2ea1 commit 7bb8e7c

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

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

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,50 @@
11
// @flow
22

33
import React, { Component, Fragment } from 'react'
4-
import { EditorState, ContentBlock, DefaultDraftBlockRenderMap, RichUtils } from 'draft-js'
4+
import {
5+
EditorState,
6+
ContentBlock,
7+
DefaultDraftBlockRenderMap,
8+
RichUtils,
9+
} from 'draft-js'
510
import { withPluginContext } from '@djsp/core'
611
import type { PluginProps } from '@djsp/core'
712
import { mergeBlockData } from '@djsp/utils'
8-
import { CheckableListItem, CheckableListItemBlock, CHECKABLE_LIST_ITEM, blockRenderMap } from 'draft-js-checkable-list-item'
13+
import {
14+
CheckableListItem,
15+
CheckableListItemBlock,
16+
CHECKABLE_LIST_ITEM,
17+
blockRenderMap,
18+
} from 'draft-js-checkable-list-item'
919

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

23+
updateEditorState = (newEditorState: EditorState) => {
24+
const { setEditorState, editorState } = this.props
25+
setEditorState(
26+
EditorState.forceSelection(newEditorState, editorState.getSelection())
27+
)
28+
}
29+
1330
onClick = event => {
1431
event.stopPropagation()
1532

16-
const { setEditorState, editorState } = this.props
17-
setEditorState(RichUtils.toggleBlockType(editorState, CHECKABLE_LIST_ITEM))
33+
const { editorState } = this.props
34+
const newEditorState = RichUtils.toggleBlockType(
35+
editorState,
36+
CHECKABLE_LIST_ITEM
37+
)
38+
this.updateEditorState(newEditorState)
1839
}
1940

2041
toggleChecked = (block: ContentBlock) => {
21-
const { setEditorState, editorState } = this.props
22-
23-
let newEditorState = mergeBlockData(editorState, block, { checked: !block.getData().get('checked') })
24-
setEditorState(EditorState.forceSelection(
25-
newEditorState,
26-
editorState.getSelection()
27-
));
42+
const { editorState } = this.props
43+
44+
let newEditorState = mergeBlockData(editorState, block, {
45+
checked: !block.getData().get('checked'),
46+
})
47+
this.updateEditorState(newEditorState)
2848
}
2949

3050
componentDidMount() {
@@ -47,16 +67,34 @@ class CheckableList extends Component<PluginProps> {
4767
if (block.getType() === CHECKABLE_LIST_ITEM) {
4868
return CHECKABLE_LIST_ITEM
4969
}
50-
}
70+
},
5171
})
5272
}
5373

74+
getStyle = (): object => {
75+
const { editorState } = this.props
76+
const selection = editorState.getSelection()
77+
const currentBlockType = editorState
78+
.getCurrentContent()
79+
.getBlockForKey(selection.getStartKey())
80+
.getType()
81+
82+
return currentBlockType === CHECKABLE_LIST_ITEM
83+
? {
84+
fontWeight: 'BOLD',
85+
cursor: 'pointer',
86+
}
87+
: { cursor: 'pointer' }
88+
}
89+
5490
render() {
55-
return (<Fragment>
56-
<button type="button" onClick={this.onClick}>
57-
Toggle checkable-list
58-
</button>
59-
</Fragment>)
91+
return (
92+
<Fragment>
93+
<span style={this.getStyle()} onClick={this.onClick}>
94+
95+
</span>
96+
</Fragment>
97+
)
6098
}
6199
}
62100

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const rawContent = {
1111
blocks: [
1212
{
1313
text: 'Hey there',
14-
}
14+
},
1515
],
1616
entityMap: {},
1717
}
@@ -29,8 +29,8 @@ class App extends Component {
2929
<EditorContainer
3030
editorState={this.state.editorState}
3131
onChange={this.onChange}>
32-
<Editor />
3332
<CheckableList />
33+
<Editor />
3434
</EditorContainer>
3535
</div>
3636
)

0 commit comments

Comments
 (0)