Skip to content

Commit 6ab2949

Browse files
committed
Merge branch 'dev' of https://github.com/oslabs-beta/ReacType-v15 into garrett/stylecontext
2 parents 32df9e6 + 2747447 commit 6ab2949

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed
Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ChildElement } from '../interfaces/Interfaces';
22

3-
const separator = {
3+
4+
const separator = {
45
id: 1000,
56
tag: 'separator',
67
name: 'separator',
@@ -10,24 +11,34 @@ const separator = {
1011
icon: '',
1112
framework: '',
1213
nestable: true
13-
}
14+
};
1415
const manageSeparators = {};
1516
manageSeparators.nextTopSeparatorId = 1000;
1617
// this function checks for two separators in a row or missing separators and adds/removes as needed
1718
manageSeparators.handleSeparators = (arr: object[], str: string) => {
18-
if ((str === 'delete' || str === 'change position') && arr.length === 1 && arr[0].name === 'separator') {
19+
if (
20+
(str === 'delete' || str === 'change position') &&
21+
arr.length === 1 &&
22+
arr[0].name === 'separator'
23+
) {
1924
arr.splice(0, 1);
2025
}
2126
for (let index = 0; index < arr.length; index++) {
22-
if (arr[index].name === 'separator' && arr[index + 1].name === 'separator') {
27+
if (
28+
arr[index].name === 'separator' &&
29+
arr[index + 1].name === 'separator'
30+
) {
2331
arr.splice(index, 1); // removes extra separator from array
24-
}
32+
}
2533
// check for duplicated separator at the end of array and remove it if separator is at the last index
2634
if (arr[arr.length - 1].name === 'separator') arr.splice(arr.length - 1, 1);
2735
// check for missing separators
28-
if (arr[index].name !== 'separator' && (index === 0 || arr[index - 1].name !== 'separator')) {
29-
// initialize topSeparator inside the if condition so that every time this condition evaluated to true,
30-
// a new topSeparator with incremented id will be created
36+
if (
37+
arr[index].name !== 'separator' &&
38+
(index === 0 || arr[index - 1].name !== 'separator')
39+
) {
40+
// initialize topSeparator inside the if condition so that every time this condition evaluated to true,
41+
// a new topSeparator with incremented id will be created
3142
const topSeparator: ChildElement = {
3243
type: 'HTML Element',
3344
typeId: separator.id,
@@ -37,14 +48,20 @@ manageSeparators.handleSeparators = (arr: object[], str: string) => {
3748
children: []
3849
};
3950
// add a topSeparator before the element that does not have one
40-
arr.splice(index, 0, topSeparator)
51+
arr.splice(index, 0, topSeparator);
4152
// update this value in state
4253
manageSeparators.nextTopSeparatorId += 1;
4354
}
4455
// check is length is > 0 or it is a nested element
45-
if ((arr[index].name !== 'input' && arr[index].name !== 'img') && arr[index].children.length) {
46-
// recursive call if children array
47-
(str === 'delete' || str === 'change position') ? manageSeparators.handleSeparators(arr[index].children, str) : manageSeparators.handleSeparators(arr[index].children);
56+
if (
57+
arr[index].name !== 'input' &&
58+
arr[index].name !== 'img' &&
59+
arr[index].children.length
60+
) {
61+
// recursive call if children array
62+
str === 'delete' || str === 'change position'
63+
? manageSeparators.handleSeparators(arr[index].children, str)
64+
: manageSeparators.handleSeparators(arr[index].children);
4865
}
4966
}
5067
return manageSeparators.nextTopSeparatorId;
@@ -53,14 +70,21 @@ manageSeparators.handleSeparators = (arr: object[], str: string) => {
5370
manageSeparators.mergeSeparator = (arr: object[], index: number) => {
5471
return arr.map((child) => {
5572
// Added additional nested types for lists
56-
if ((child.name === 'div' || child.name === 'form' || child.name === 'ol' || child.name === 'ul') && child.children.length) {
57-
const divContents = manageSeparators.mergeSeparator(child.children, index);
58-
return { ...child, children: divContents }
59-
}
60-
else if (child.name === 'separator' && child.children.length) {
73+
if (
74+
(child.name === 'div' ||
75+
child.name === 'form' ||
76+
child.name === 'ol' ||
77+
child.name === 'ul') &&
78+
child.children.length
79+
) {
80+
const divContents = manageSeparators.mergeSeparator(
81+
child.children,
82+
index
83+
);
84+
return { ...child, children: divContents };
85+
} else if (child.name === 'separator' && child.children.length) {
6186
return child.children[index];
62-
}
63-
else return child;
87+
} else return child;
6488
});
6589
};
6690
export default manageSeparators;

0 commit comments

Comments
 (0)