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