1
- import { ChildElement } from '../interfaces/Interfaces' ;
1
+ import { ChildElement , ManageSeparators } from '../interfaces/Interfaces' ;
2
2
3
3
const separator = {
4
4
id : 1000 ,
@@ -11,79 +11,81 @@ const separator = {
11
11
framework : '' ,
12
12
nestable : true
13
13
} ;
14
- const manageSeparators = { } ;
15
- manageSeparators . nextTopSeparatorId = 1000 ;
16
- // this function checks for two separators in a row or missing separators and adds/removes as needed
17
- manageSeparators . handleSeparators = ( arr : object [ ] , str : string ) => {
18
- if (
19
- ( str === 'delete' || str === 'change position' ) &&
20
- arr . length === 1 &&
21
- arr [ 0 ] . name === 'separator'
22
- ) {
23
- arr . splice ( 0 , 1 ) ;
24
- }
25
- for ( let index = 0 ; index < arr . length ; index ++ ) {
26
- if (
27
- arr [ index ] . name === 'separator' &&
28
- arr [ index + 1 ] . name === 'separator'
29
- ) {
30
- arr . splice ( index , 1 ) ; // removes extra separator from array
31
- }
32
- // check for duplicated separator at the end of array and remove it if separator is at the last index
33
- if ( arr [ arr . length - 1 ] . name === 'separator' ) arr . splice ( arr . length - 1 , 1 ) ;
34
- // check for missing separators
14
+ const manageSeparators : ManageSeparators = {
15
+ nextTopSeparatorId : 1000 ,
16
+ // this function checks for two separators in a row or missing separators and adds/removes as needed
17
+ handleSeparators : ( arr , str ) => {
35
18
if (
36
- arr [ index ] . name !== 'separator' &&
37
- ( index === 0 || arr [ index - 1 ] . name !== 'separator' )
19
+ ( str === 'delete' || str === 'change position' ) &&
20
+ arr . length === 1 &&
21
+ arr . slice ( 0 , 1 ) . name === 'separator'
38
22
) {
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
41
- const topSeparator : ChildElement = {
42
- type : 'HTML Element' ,
43
- typeId : separator . id ,
44
- name : 'separator' ,
45
- childId : manageSeparators . nextTopSeparatorId ,
46
- style : separator . style ,
47
- children : [ ]
48
- } ;
49
- // add a topSeparator before the element that does not have one
50
- arr . splice ( index , 0 , topSeparator ) ;
51
- // update this value in state
52
- manageSeparators . nextTopSeparatorId += 1 ;
23
+ arr . splice ( 0 , 1 ) ;
53
24
}
54
- // check is length is > 0 or it is a nested element
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 ) ;
25
+ for ( let index = 0 ; index < arr . length ; index ++ ) {
26
+ if (
27
+ arr [ index ] . name === 'separator' &&
28
+ arr [ index + 1 ] . name === 'separator'
29
+ ) {
30
+ arr . splice ( index , 1 ) ; // removes extra separator from array
31
+ }
32
+ // check for duplicated separator at the end of array and remove it if separator is at the last index
33
+ if ( arr [ arr . length - 1 ] . name === 'separator' ) arr . splice ( arr . length - 1 , 1 ) ;
34
+ // check for missing separators
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
41
+ const topSeparator : ChildElement = {
42
+ type : 'HTML Element' ,
43
+ typeId : separator . id ,
44
+ name : 'separator' ,
45
+ childId : manageSeparators . nextTopSeparatorId ,
46
+ style : separator . style ,
47
+ children : [ ]
48
+ } ;
49
+ // add a topSeparator before the element that does not have one
50
+ arr . splice ( index , 0 , topSeparator ) ;
51
+ // update this value in state
52
+ manageSeparators . nextTopSeparatorId += 1 ;
53
+ }
54
+ // check is length is > 0 or it is a nested element
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 ) ;
64
+ }
64
65
}
66
+ return manageSeparators . nextTopSeparatorId ;
67
+ } ,
68
+
69
+ // this function replaces separators onto which an element is dropped with the element itself
70
+ mergeSeparator : ( arr : object [ ] , index : number ) => {
71
+ return arr . map ( ( child ) => {
72
+ // Added additional nested types for lists
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 ) {
86
+ return child . children [ index ] ;
87
+ } else return child ;
88
+ } ) ;
65
89
}
66
- return manageSeparators . nextTopSeparatorId ;
67
- } ;
68
- // this function replaces separators onto which an element is dropped with the element itself
69
- manageSeparators . mergeSeparator = ( arr : object [ ] , index : number ) => {
70
- return arr . map ( ( child ) => {
71
- // Added additional nested types for lists
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 ) {
85
- return child . children [ index ] ;
86
- } else return child ;
87
- } ) ;
88
- } ;
90
+ }
89
91
export default manageSeparators ;
0 commit comments