Skip to content

Commit 6227e52

Browse files
committed
Fixed bug where you would nest already created element
1 parent d317236 commit 6227e52

File tree

1 file changed

+48
-25
lines changed

1 file changed

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

33
const separator = {
4-
id: 11,
5-
tag: 'div',
6-
name: 'Div',
7-
style: {},
8-
placeHolderShort: 'div',
4+
id: 1000,
5+
tag: 'separator',
6+
name: 'separator',
7+
style: { border: 'none' },
8+
placeHolderShort: '',
99
placeHolderLong: '',
10-
// icon: HeaderIcon,
11-
framework: 'reactClassic',
10+
icon: '',
11+
framework: '',
1212
nestable: true
13-
}
13+
};
1414
const manageSeparators = {};
1515
manageSeparators.nextTopSeparatorId = 1000;
1616
// this function checks for two separators in a row or missing separators and adds/removes as needed
1717
manageSeparators.handleSeparators = (arr: object[], str: string) => {
18-
if ((str === 'delete' || str === 'change position') && arr.length === 1 && arr[0].name === 'separator') {
18+
if (
19+
(str === 'delete' || str === 'change position') &&
20+
arr.length === 1 &&
21+
arr[0].name === 'separator'
22+
) {
1923
arr.splice(0, 1);
2024
}
2125
for (let index = 0; index < arr.length; index++) {
22-
if (arr[index].name === 'separator' && arr[index + 1].name === 'separator') {
26+
if (
27+
arr[index].name === 'separator' &&
28+
arr[index + 1].name === 'separator'
29+
) {
2330
arr.splice(index, 1); // removes extra separator from array
24-
}
31+
}
2532
// check for duplicated separator at the end of array and remove it if separator is at the last index
2633
if (arr[arr.length - 1].name === 'separator') arr.splice(arr.length - 1, 1);
2734
// 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
35+
if (
36+
arr[index].name !== 'separator' &&
37+
(index === 0 || arr[index - 1].name !== 'separator')
38+
) {
39+
// initialize topSeparator inside the if condition so that every time this condition evaluated to true,
40+
// a new topSeparator with incremented id will be created
3141
const topSeparator: ChildElement = {
3242
type: 'HTML Element',
3343
typeId: separator.id,
@@ -37,14 +47,20 @@ manageSeparators.handleSeparators = (arr: object[], str: string) => {
3747
children: []
3848
};
3949
// add a topSeparator before the element that does not have one
40-
arr.splice(index, 0, topSeparator)
50+
arr.splice(index, 0, topSeparator);
4151
// update this value in state
4252
manageSeparators.nextTopSeparatorId += 1;
4353
}
4454
// 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);
55+
if (
56+
arr[index].name !== 'input' &&
57+
arr[index].name !== 'img' &&
58+
arr[index].children.length
59+
) {
60+
// recursive call if children array
61+
str === 'delete' || str === 'change position'
62+
? manageSeparators.handleSeparators(arr[index].children, str)
63+
: manageSeparators.handleSeparators(arr[index].children);
4864
}
4965
}
5066
return manageSeparators.nextTopSeparatorId;
@@ -53,14 +69,21 @@ manageSeparators.handleSeparators = (arr: object[], str: string) => {
5369
manageSeparators.mergeSeparator = (arr: object[], index: number) => {
5470
return arr.map((child) => {
5571
// 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) {
72+
if (
73+
(child.name === 'div' ||
74+
child.name === 'form' ||
75+
child.name === 'ol' ||
76+
child.name === 'ul') &&
77+
child.children.length
78+
) {
79+
const divContents = manageSeparators.mergeSeparator(
80+
child.children,
81+
index
82+
);
83+
return { ...child, children: divContents };
84+
} else if (child.name === 'separator' && child.children.length) {
6185
return child.children[index];
62-
}
63-
else return child;
86+
} else return child;
6487
});
6588
};
6689
export default manageSeparators;

0 commit comments

Comments
 (0)