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

Commit 02e0e00

Browse files
niveditcfacebook-github-bot
authored andcommitted
Bug Fix - Remove deleted block from its parent's children
Summary: Forgot to update this parent-child pointer & didn't have a test for it, so the invariant threw an error for untab in production. https://pxl.cl/hFG6 Reviewed By: vdurmont Differential Revision: D9982548 fbshipit-source-id: 7d97bdcea15d23f3d4fcd1a64d00e12587b76549
1 parent fbe2267 commit 02e0e00

File tree

3 files changed

+182
-1
lines changed

3 files changed

+182
-1
lines changed

src/model/modifier/exploration/DraftTreeOperations.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,17 @@ const moveChildUp = (blockMap: BlockMap, key: string): BlockMap => {
426426
newBlockMap.get(prevSiblingKey).merge({nextSibling: null}),
427427
);
428428
}
429+
if (grandparentKey != null) {
430+
const grandparent = newBlockMap.get(grandparentKey);
431+
const oldChildren = grandparent.getChildKeys();
432+
newBlockMap = newBlockMap.set(
433+
grandparentKey,
434+
grandparent.merge({
435+
children: oldChildren.delete(oldChildren.indexOf(parentKey)),
436+
}),
437+
);
438+
}
439+
429440
newBlockMap = newBlockMap.delete(parentKey);
430441
}
431442

src/model/modifier/exploration/__tests__/DraftTreeOperations-test.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,53 @@ test('test moving only child up deletes parent 3', () => {
546546
});
547547

548548
const blockMap11 = Immutable.OrderedMap({
549+
A: new ContentBlockNode({
550+
key: 'A',
551+
parent: null,
552+
text: 'alpha',
553+
children: Immutable.List([]),
554+
prevSibling: null,
555+
nextSibling: 'X',
556+
}),
557+
X: new ContentBlockNode({
558+
key: 'X',
559+
parent: null,
560+
text: '',
561+
children: Immutable.List(['B', 'Y']),
562+
prevSibling: 'A',
563+
nextSibling: null,
564+
}),
565+
B: new ContentBlockNode({
566+
key: 'B',
567+
parent: 'X',
568+
text: 'beta',
569+
children: Immutable.List([]),
570+
prevSibling: null,
571+
nextSibling: 'Y',
572+
}),
573+
Y: new ContentBlockNode({
574+
key: 'Y',
575+
parent: 'X',
576+
text: '',
577+
children: Immutable.List(['C']),
578+
prevSibling: 'B',
579+
nextSibling: null,
580+
}),
581+
C: new ContentBlockNode({
582+
key: 'C',
583+
parent: 'Y',
584+
text: 'charlie',
585+
children: Immutable.List([]),
586+
prevSibling: null,
587+
nextSibling: null,
588+
}),
589+
});
590+
591+
test('test moving only child up deletes parent 4', () => {
592+
expect(DraftTreeOperations.moveChildUp(blockMap11, 'C')).toMatchSnapshot();
593+
});
594+
595+
const blockMap12 = Immutable.OrderedMap({
549596
A: new ContentBlockNode({
550597
key: 'A',
551598
parent: null,
@@ -605,5 +652,5 @@ const blockMap11 = Immutable.OrderedMap({
605652
});
606653

607654
test('test merging blocks', () => {
608-
expect(DraftTreeOperations.mergeBlocks(blockMap11, 'X')).toMatchSnapshot();
655+
expect(DraftTreeOperations.mergeBlocks(blockMap12, 'X')).toMatchSnapshot();
609656
});

src/model/modifier/exploration/__tests__/__snapshots__/DraftTreeOperations-test.js.snap

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2947,6 +2947,129 @@ Immutable.OrderedMap {
29472947
}
29482948
`;
29492949

2950+
exports[`test moving only child up deletes parent 4 1`] = `
2951+
Immutable.OrderedMap {
2952+
"A": Immutable.Record {
2953+
"parent": null,
2954+
"characterList": Immutable.List [
2955+
Immutable.Record {
2956+
"style": Immutable.OrderedSet [],
2957+
"entity": null,
2958+
},
2959+
Immutable.Record {
2960+
"style": Immutable.OrderedSet [],
2961+
"entity": null,
2962+
},
2963+
Immutable.Record {
2964+
"style": Immutable.OrderedSet [],
2965+
"entity": null,
2966+
},
2967+
Immutable.Record {
2968+
"style": Immutable.OrderedSet [],
2969+
"entity": null,
2970+
},
2971+
Immutable.Record {
2972+
"style": Immutable.OrderedSet [],
2973+
"entity": null,
2974+
},
2975+
],
2976+
"data": Immutable.Map {},
2977+
"depth": 0,
2978+
"key": "A",
2979+
"text": "alpha",
2980+
"type": "unstyled",
2981+
"children": Immutable.List [],
2982+
"prevSibling": null,
2983+
"nextSibling": "X",
2984+
},
2985+
"X": Immutable.Record {
2986+
"parent": null,
2987+
"characterList": Immutable.List [],
2988+
"data": Immutable.Map {},
2989+
"depth": 0,
2990+
"key": "X",
2991+
"text": "",
2992+
"type": "unstyled",
2993+
"children": Immutable.List [
2994+
"B",
2995+
"C",
2996+
],
2997+
"prevSibling": "A",
2998+
"nextSibling": null,
2999+
},
3000+
"B": Immutable.Record {
3001+
"parent": "X",
3002+
"characterList": Immutable.List [
3003+
Immutable.Record {
3004+
"style": Immutable.OrderedSet [],
3005+
"entity": null,
3006+
},
3007+
Immutable.Record {
3008+
"style": Immutable.OrderedSet [],
3009+
"entity": null,
3010+
},
3011+
Immutable.Record {
3012+
"style": Immutable.OrderedSet [],
3013+
"entity": null,
3014+
},
3015+
Immutable.Record {
3016+
"style": Immutable.OrderedSet [],
3017+
"entity": null,
3018+
},
3019+
],
3020+
"data": Immutable.Map {},
3021+
"depth": 0,
3022+
"key": "B",
3023+
"text": "beta",
3024+
"type": "unstyled",
3025+
"children": Immutable.List [],
3026+
"prevSibling": null,
3027+
"nextSibling": "C",
3028+
},
3029+
"C": Immutable.Record {
3030+
"parent": "X",
3031+
"characterList": Immutable.List [
3032+
Immutable.Record {
3033+
"style": Immutable.OrderedSet [],
3034+
"entity": null,
3035+
},
3036+
Immutable.Record {
3037+
"style": Immutable.OrderedSet [],
3038+
"entity": null,
3039+
},
3040+
Immutable.Record {
3041+
"style": Immutable.OrderedSet [],
3042+
"entity": null,
3043+
},
3044+
Immutable.Record {
3045+
"style": Immutable.OrderedSet [],
3046+
"entity": null,
3047+
},
3048+
Immutable.Record {
3049+
"style": Immutable.OrderedSet [],
3050+
"entity": null,
3051+
},
3052+
Immutable.Record {
3053+
"style": Immutable.OrderedSet [],
3054+
"entity": null,
3055+
},
3056+
Immutable.Record {
3057+
"style": Immutable.OrderedSet [],
3058+
"entity": null,
3059+
},
3060+
],
3061+
"data": Immutable.Map {},
3062+
"depth": 0,
3063+
"key": "C",
3064+
"text": "charlie",
3065+
"type": "unstyled",
3066+
"children": Immutable.List [],
3067+
"prevSibling": "B",
3068+
"nextSibling": null,
3069+
},
3070+
}
3071+
`;
3072+
29503073
exports[`test replacing a parent's child 1`] = `
29513074
Immutable.OrderedMap {
29523075
"A": Immutable.Record {

0 commit comments

Comments
 (0)