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

Commit

Permalink
Fix check for tree blocks
Browse files Browse the repository at this point in the history
Summary: We were wrongly converting from tree to raw whenever a block had a children array, even if no blocks actually have children. This is bad because when we toggle the GK, we clear the children, but we don't necessarily delete the field altogether (and shouldn't). A tree to raw conversion (or vice versa) is needed only when blocks are actually nested.

Reviewed By: agoder

Differential Revision: D10145467

fbshipit-source-id: 10e489e9c3ba3ae0d16c3da4767cd1400345d92e
  • Loading branch information
niveditc authored and facebook-github-bot committed Oct 2, 2018
1 parent 0688fa3 commit 690f7ef
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,174 @@ Object {
}
`;

exports[`ignore empty children array 1`] = `
Object {
"A": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "A",
"nextSibling": "B",
"parent": null,
"prevSibling": null,
"text": "A",
"type": "ordered-list-item",
},
"B": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "B",
"nextSibling": "C",
"parent": null,
"prevSibling": "A",
"text": "B",
"type": "ordered-list-item",
},
"C": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "C",
"nextSibling": null,
"parent": null,
"prevSibling": "B",
"text": "C",
"type": "ordered-list-item",
},
}
`;

exports[`ignore empty children array for tree conversion 1 1`] = `
Object {
"A": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "A",
"nextSibling": "B",
"parent": null,
"prevSibling": null,
"text": "A",
"type": "ordered-list-item",
},
"B": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "B",
"nextSibling": "C",
"parent": null,
"prevSibling": "A",
"text": "B",
"type": "ordered-list-item",
},
"C": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "C",
"nextSibling": null,
"parent": null,
"prevSibling": "B",
"text": "C",
"type": "ordered-list-item",
},
}
`;

exports[`ignore empty children array for tree conversion 2 1`] = `
Object {
"A": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "A",
"nextSibling": "B",
"parent": null,
"prevSibling": null,
"text": "A",
"type": "ordered-list-item",
},
"B": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "B",
"nextSibling": "C",
"parent": null,
"prevSibling": "A",
"text": "B",
"type": "ordered-list-item",
},
"C": Object {
"characterList": Array [
Object {
"entity": null,
"style": Array [],
},
],
"children": Array [],
"data": Object {},
"depth": 0,
"key": "C",
"nextSibling": null,
"parent": null,
"prevSibling": "B",
"text": "C",
"type": "ordered-list-item",
},
}
`;

exports[`must be able to convert content blocks that have list with depth from raw state to tree state when experimentalTreeDataSupport is enabled 1`] = `
Object {
"A": Object {
Expand Down
56 changes: 56 additions & 0 deletions src/model/encoding/__tests__/convertFromRawToDraftState-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,59 @@ test('must be able to convert content blocks that have list with depth from raw

assertDraftState(rawState);
});

test('ignore empty children array', () => {
const rawState = {
blocks: [
{key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'},
{key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'},
{
key: 'C',
type: 'ordered-list-item',
depth: 0,
text: 'C',
children: [],
},
],
entityMap: {},
};

assertDraftState(rawState);
});

test('ignore empty children array for tree conversion 1', () => {
const rawState = {
blocks: [
{key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'},
{key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'},
{
key: 'C',
type: 'ordered-list-item',
depth: 0,
text: 'C',
children: [],
},
],
entityMap: {},
};
assertDraftState(rawState);
});

test('ignore empty children array for tree conversion 2', () => {
toggleExperimentalTreeDataSupport(true);
const rawState = {
blocks: [
{key: 'A', type: 'ordered-list-item', depth: 0, text: 'A'},
{key: 'B', type: 'ordered-list-item', depth: 0, text: 'B'},
{
key: 'C',
type: 'ordered-list-item',
depth: 0,
text: 'C',
children: [],
},
],
entityMap: {},
};
assertDraftState(rawState);
});
4 changes: 3 additions & 1 deletion src/model/encoding/convertFromRawToDraftState.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ const decodeRawBlocks = (
rawState: RawDraftContentState,
entityMap: *,
): BlockMap => {
const isTreeRawBlock = Array.isArray(rawState.blocks[0].children);
const isTreeRawBlock = rawState.blocks.find(
block => Array.isArray(block.children) && block.children.length > 0,
);
const rawBlocks =
experimentalTreeDataSupport && !isTreeRawBlock
? DraftTreeAdapter.fromRawStateToRawTreeState(rawState).blocks
Expand Down

0 comments on commit 690f7ef

Please sign in to comment.