Skip to content

Commit 652750f

Browse files
committed
fix(filters): handle removal of all blocks, inserting one unstyled
1 parent 05edf66 commit 652750f

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

src/lib/filters/selection.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ import { ContentState } from "draft-js"
55

66
/**
77
* Applies the new content to the editor state, optionally moving the selection
8-
* to be on a valid block (https://github.com/thibaudcolas/draftjs-filters/issues/27).
8+
* to be on a valid block, inserting one if needed.
9+
* See https://github.com/thibaudcolas/draftjs-filters/issues/27.
910
*/
1011
export const applyContentWithSelection = (
1112
editorState: EditorStateType,
1213
content: ContentState,
1314
nextContent: ContentState,
1415
) => {
16+
// If the block map is empty, insert a new unstyled block and put the selection on it.
17+
if (nextContent.getBlockMap().size === 0) {
18+
return EditorState.moveFocusToEnd(
19+
EditorState.set(editorState, {
20+
currentContent: ContentState.createFromText(""),
21+
}),
22+
)
23+
}
24+
1525
const nextState = EditorState.set(editorState, {
1626
currentContent: nextContent,
1727
})
@@ -29,7 +39,7 @@ export const applyContentWithSelection = (
2939
const nextKeys = nextContent.getBlockMap().keySeq()
3040

3141
// Find the first key whose successor is different in the old content (because a block was removed).
32-
// Starting from the end so the selection is preserved towards the last not-removed block in the filtered region.
42+
// Starting from the end so the selection is preserved towards the last preserved block in the filtered region.
3343
const nextAnchorKey = nextKeys
3444
.reverse()
3545
.find((k) => content.getKeyAfter(k) !== nextContent.getKeyAfter(k))

src/lib/filters/selection.test.js

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,43 @@ describe("selection", () => {
3131
]),
3232
]
3333

34+
it("inserts a block if there is none left, with selection on it", () => {
35+
let editorState = EditorState.createWithContent(
36+
convertFromRaw({
37+
entityMap: {},
38+
blocks: [
39+
{
40+
key: "f8beh",
41+
text: "test",
42+
depth: 3,
43+
},
44+
],
45+
}),
46+
)
47+
48+
const content = editorState.getCurrentContent()
49+
const nextContent = filters.reduce((c, filter) => filter(c), content)
50+
const nextState = applyContentWithSelection(
51+
editorState,
52+
content,
53+
nextContent,
54+
)
55+
const block = nextState.getCurrentContent().getFirstBlock()
56+
57+
expect(block.toJS()).toMatchObject({
58+
text: "",
59+
type: "unstyled",
60+
depth: 0,
61+
})
62+
63+
expect(nextState.getSelection().toJS()).toMatchObject({
64+
anchorKey: block.getKey(),
65+
anchorOffset: 0,
66+
focusKey: block.getKey(),
67+
focusOffset: 0,
68+
})
69+
})
70+
3471
it("does not change selection if valid", () => {
3572
let editorState = EditorState.createWithContent(
3673
convertFromRaw({
@@ -134,45 +171,6 @@ describe("selection", () => {
134171
})
135172
})
136173

137-
it("does not change selection if it cannot be moved elsewhere", () => {
138-
let editorState = EditorState.createWithContent(
139-
convertFromRaw({
140-
entityMap: {},
141-
blocks: [
142-
{
143-
key: "f8beh",
144-
text: "test",
145-
depth: 3,
146-
},
147-
],
148-
}),
149-
)
150-
editorState = EditorState.acceptSelection(
151-
editorState,
152-
editorState.getSelection().merge({
153-
anchorKey: "f8beh",
154-
focusKey: "f8beh",
155-
anchorOffset: 4,
156-
focusOffset: 4,
157-
}),
158-
)
159-
160-
const content = editorState.getCurrentContent()
161-
const nextContent = filters.reduce((c, filter) => filter(c), content)
162-
const nextState = applyContentWithSelection(
163-
editorState,
164-
content,
165-
nextContent,
166-
)
167-
168-
expect(nextState.getSelection().toJS()).toMatchObject({
169-
anchorKey: "f8beh",
170-
anchorOffset: 4,
171-
focusKey: "f8beh",
172-
focusOffset: 4,
173-
})
174-
})
175-
176174
it("works for the last block", () => {
177175
let editorState = EditorState.createWithContent(
178176
convertFromRaw({

0 commit comments

Comments
 (0)