1
1
import { ChildElement } from '../interfaces/Interfaces' ;
2
2
3
- const separator = {
3
+
4
+ const separator = {
4
5
id : 1000 ,
5
6
tag : 'separator' ,
6
7
name : 'separator' ,
@@ -10,24 +11,34 @@ const separator = {
10
11
icon : '' ,
11
12
framework : '' ,
12
13
nestable : true
13
- }
14
+ } ;
14
15
const manageSeparators = { } ;
15
16
manageSeparators . nextTopSeparatorId = 1000 ;
16
17
// this function checks for two separators in a row or missing separators and adds/removes as needed
17
18
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
+ ) {
19
24
arr . splice ( 0 , 1 ) ;
20
25
}
21
26
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
+ ) {
23
31
arr . splice ( index , 1 ) ; // removes extra separator from array
24
- }
32
+ }
25
33
// check for duplicated separator at the end of array and remove it if separator is at the last index
26
34
if ( arr [ arr . length - 1 ] . name === 'separator' ) arr . splice ( arr . length - 1 , 1 ) ;
27
35
// 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
31
42
const topSeparator : ChildElement = {
32
43
type : 'HTML Element' ,
33
44
typeId : separator . id ,
@@ -37,14 +48,20 @@ manageSeparators.handleSeparators = (arr: object[], str: string) => {
37
48
children : [ ]
38
49
} ;
39
50
// add a topSeparator before the element that does not have one
40
- arr . splice ( index , 0 , topSeparator )
51
+ arr . splice ( index , 0 , topSeparator ) ;
41
52
// update this value in state
42
53
manageSeparators . nextTopSeparatorId += 1 ;
43
54
}
44
55
// 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 ) ;
48
65
}
49
66
}
50
67
return manageSeparators . nextTopSeparatorId ;
@@ -53,14 +70,21 @@ manageSeparators.handleSeparators = (arr: object[], str: string) => {
53
70
manageSeparators . mergeSeparator = ( arr : object [ ] , index : number ) => {
54
71
return arr . map ( ( child ) => {
55
72
// 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 ) {
61
86
return child . children [ index ] ;
62
- }
63
- else return child ;
87
+ } else return child ;
64
88
} ) ;
65
89
} ;
66
90
export default manageSeparators ;
0 commit comments