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

Commit

Permalink
Remove non-leaf blocks in tree => raw conversion
Browse files Browse the repository at this point in the history
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
  • Loading branch information
niveditc authored and facebook-github-bot committed Aug 30, 2018
1 parent d6a0ac0 commit f5b2acb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/component/utils/exploration/DraftTreeAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ const DraftTreeAdapter = {
if (isListBlock(block)) {
newBlock.depth = newBlock.depth || 0;
addDepthToChildren(block);

// if it's a non-leaf node, we don't do anything else
if (block.children != null && block.children.length > 0) {
return;
}
}

delete newBlock.children;
Expand Down
31 changes: 31 additions & 0 deletions src/component/utils/exploration/__tests__/DraftTreeAdapter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ test('must be able to convert from tree raw state with only root blocks to raw s
});

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

test('must be able to convert from tree raw state with nested list blocks to raw state preserving lists depth', () => {
/**
* 1. Alpha
* a. Beta
* i. Charlie
* b. Delta
*/
const rawState = {
blocks: [
{
key: 'A',
type: 'ordered-list-item',
text: 'Alpha',
children: [],
},
{
key: 'X',
text: '',
type: 'ordered-list-item',
children: [
{
key: 'B',
type: 'ordered-list-item',
text: 'Beta',
children: [],
},
{
key: 'Y',
type: 'ordered-list-item',
text: '',
children: [
{
key: 'C',
Expand Down Expand Up @@ -122,11 +141,23 @@ test('must be able to convert from tree raw state with nested list blocks to raw
key: 'A',
type: 'ordered-list-item',
text: 'Alpha',
children: [],
},
{
key: 'X',
type: 'ordered-list-item',
text: '',
children: [
{
key: 'B',
text: 'Beta',
type: 'ordered-list-item',
children: [],
},
{
key: 'Y',
type: 'ordered-list-item',
text: '',
children: [
{
key: 'C',
Expand Down

0 comments on commit f5b2acb

Please sign in to comment.