Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit f5b2acb

Browse files
niveditcfacebook-github-bot
authored andcommitted
Remove non-leaf blocks in tree => raw conversion
Summary: The raw => tree data conversion creates new parent blocks because nodes with content on them (leaf nodes) aren't allowed to have children. When we convert to back to raw mode, we need to get rid of these extra non-leaf nodes, otherwise they get rendered. Also fixed the test cases to respect the tree invariants. Reviewed By: mitermayer Differential Revision: D9567760 fbshipit-source-id: 6b53fba057dedeabad63a2492748fc1958b71a96
1 parent d6a0ac0 commit f5b2acb

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/component/utils/exploration/DraftTreeAdapter.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ const DraftTreeAdapter = {
8585
if (isListBlock(block)) {
8686
newBlock.depth = newBlock.depth || 0;
8787
addDepthToChildren(block);
88+
89+
// if it's a non-leaf node, we don't do anything else
90+
if (block.children != null && block.children.length > 0) {
91+
return;
92+
}
8893
}
8994

9095
delete newBlock.children;

src/component/utils/exploration/__tests__/DraftTreeAdapter-test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ test('must be able to convert from tree raw state with only root blocks to raw s
5151
});
5252

5353
test('must be able to convert from tree raw state with nested blocks to raw state', () => {
54+
// Right now, we ignore non-list nested blocks
5455
const rawState = {
5556
blocks: [
5657
{
@@ -80,17 +81,35 @@ test('must be able to convert from tree raw state with nested blocks to raw stat
8081
});
8182

8283
test('must be able to convert from tree raw state with nested list blocks to raw state preserving lists depth', () => {
84+
/**
85+
* 1. Alpha
86+
* a. Beta
87+
* i. Charlie
88+
* b. Delta
89+
*/
8390
const rawState = {
8491
blocks: [
8592
{
8693
key: 'A',
8794
type: 'ordered-list-item',
8895
text: 'Alpha',
96+
children: [],
97+
},
98+
{
99+
key: 'X',
100+
text: '',
101+
type: 'ordered-list-item',
89102
children: [
90103
{
91104
key: 'B',
105+
type: 'ordered-list-item',
92106
text: 'Beta',
107+
children: [],
108+
},
109+
{
110+
key: 'Y',
93111
type: 'ordered-list-item',
112+
text: '',
94113
children: [
95114
{
96115
key: 'C',
@@ -122,11 +141,23 @@ test('must be able to convert from tree raw state with nested list blocks to raw
122141
key: 'A',
123142
type: 'ordered-list-item',
124143
text: 'Alpha',
144+
children: [],
145+
},
146+
{
147+
key: 'X',
148+
type: 'ordered-list-item',
149+
text: '',
125150
children: [
126151
{
127152
key: 'B',
128153
text: 'Beta',
129154
type: 'ordered-list-item',
155+
children: [],
156+
},
157+
{
158+
key: 'Y',
159+
type: 'ordered-list-item',
160+
text: '',
130161
children: [
131162
{
132163
key: 'C',

0 commit comments

Comments
 (0)