@@ -97,6 +97,19 @@ export const getShadowColor = (attribute: string): HTMLInputElement =>
97
97
export const getShadowPreview = ( attribute : string ) : HTMLInputElement =>
98
98
document . getElementById ( `${ attribute } -preview` ) as HTMLInputElement ;
99
99
100
+ export const getParentElementOfColors = ( attribute : string ) : HTMLElement =>
101
+ document . querySelector ( `[data-content=${ attribute } ] .colors` ) as HTMLElement ;
102
+
103
+ export const getNewColorButton = ( attribute : string ) : HTMLElement =>
104
+ document . querySelector (
105
+ `[data-content=${ attribute } ] .addNewColor`
106
+ ) as HTMLElement ;
107
+
108
+ export const removeNewColorButton = ( attribute : string ) : HTMLElement =>
109
+ document . querySelector (
110
+ `[data-content=${ attribute } ] .removeNewColor`
111
+ ) as HTMLElement ;
112
+
100
113
export const setGradientDegreeValue = ( degreeElement : HTMLElement ) : void =>
101
114
degreeElement . addEventListener ( 'input' , ( e ) => {
102
115
const target = e . target as HTMLInputElement ;
@@ -142,15 +155,15 @@ export function slideIn(slider: HTMLElement, isOpen: boolean) {
142
155
}
143
156
144
157
export const createGradientPreview = (
145
- color1 : HTMLInputElement ,
146
- color2 : HTMLInputElement ,
147
158
range : HTMLInputElement ,
148
- preview : HTMLElement
159
+ attribute : string
149
160
) => {
150
- const colorFrom = color1 ?. value ;
151
- const colorTo = color2 ?. value ;
152
161
const fill = range ?. value ;
153
- preview . style . background = `linear-gradient(${ fill } deg, ${ colorFrom } , ${ colorTo } )` ;
162
+ gradientPreview (
163
+ attribute
164
+ ) . style . background = `linear-gradient(${ fill } deg, ${ getColorsValue (
165
+ attribute
166
+ ) . join ( ', ' ) } )`;
154
167
} ;
155
168
156
169
function createDownloadLink ( fileName : string , url : string ) {
@@ -173,6 +186,21 @@ export function copyCodeToClipboard(
173
186
actOnGenerator ( attribute , outputElement ) ;
174
187
}
175
188
189
+ export const addRule = ( function ( style ) {
190
+ const sheet = document . head . appendChild ( style ) . sheet ;
191
+ return function ( selector : string , css : { [ x : string ] : any } ) {
192
+ const propText =
193
+ typeof css === 'string'
194
+ ? css
195
+ : Object . keys ( css )
196
+ . map ( function ( p ) {
197
+ return p + ':' + ( p === 'content' ? "'" + css [ p ] + "'" : css [ p ] ) ;
198
+ } )
199
+ . join ( ';' ) ;
200
+ sheet ?. insertRule ( selector + '{' + propText + '}' , sheet . cssRules . length ) ;
201
+ } ;
202
+ } ) ( document . createElement ( 'style' ) ) ;
203
+
176
204
/**
177
205
* what should copy when the copy css button is clicked
178
206
*
@@ -368,3 +396,81 @@ export function triggerEmptyAnimation(inputElement: HTMLInputElement): void {
368
396
inputElement . style . borderColor = 'white' ;
369
397
} , 1000 ) ;
370
398
}
399
+
400
+ export function addNewColorPicker ( attribute : string ) : void {
401
+ const getParentElementToAddTo = getParentElementOfColors ( attribute ) ;
402
+ const numberOfChildren = getParentElementToAddTo ?. childElementCount ;
403
+
404
+ if ( numberOfChildren === undefined || numberOfChildren === 4 ) return ;
405
+
406
+ const colorNumber = numberOfChildren + 1 ;
407
+
408
+ getParentElementToAddTo ?. appendChild ( createLabelForNewColor ( ) ) ;
409
+ whatColorButtonShouldShow ( attribute ) ;
410
+
411
+ // create label element
412
+ function createLabelForNewColor ( ) : Node {
413
+ const labelWrapperForColor = document . createElement ( 'label' ) ;
414
+ labelWrapperForColor . setAttribute ( 'for' , 'color' ) ;
415
+ labelWrapperForColor . className = 'color' ;
416
+
417
+ labelWrapperForColor . appendChild ( createNewColor ( ) ) ;
418
+
419
+ return labelWrapperForColor ;
420
+ }
421
+
422
+ // create color pickter
423
+ function createNewColor ( ) : Node {
424
+ const newColorCreated = document . createElement ( 'input' ) ;
425
+ newColorCreated . setAttribute ( 'type' , 'text' ) ;
426
+ newColorCreated . setAttribute ( 'data-coloris' , '' ) ;
427
+ newColorCreated . placeholder = 'Tap to pick a color' ;
428
+ newColorCreated . id = `${ attribute } -color${ colorNumber } ` ;
429
+ newColorCreated . className = `${ attribute } -inputs` ;
430
+
431
+ return newColorCreated ;
432
+ }
433
+ }
434
+
435
+ export function getColorsValue ( attribute : string ) : Array < string > {
436
+ const colorValues : string [ ] = [ ] ;
437
+
438
+ const colorInput = document . querySelectorAll (
439
+ `[data-content=${ attribute } ] .color input`
440
+ ) ;
441
+ colorInput . forEach ( ( value ) => {
442
+ const colorValue = value as HTMLInputElement ;
443
+ colorValues . push ( colorValue . value ) ;
444
+ } ) ;
445
+
446
+ return colorValues ;
447
+ }
448
+
449
+ export function removeColorPicker ( attribute : string ) : void {
450
+ const getParentElementToRemoveFrom = getParentElementOfColors ( attribute ) ;
451
+ const numberOfChildren = getParentElementToRemoveFrom ?. childElementCount ;
452
+
453
+ if ( numberOfChildren === undefined || numberOfChildren === 2 ) return ;
454
+ getParentElementToRemoveFrom ?. lastChild ?. remove ( ) ;
455
+
456
+ whatColorButtonShouldShow ( attribute ) ;
457
+ }
458
+
459
+ export function whatColorButtonShouldShow ( attribute : string ) : void {
460
+ const getNumberOfChildren =
461
+ getParentElementOfColors ( attribute ) . childElementCount ;
462
+
463
+ // display add new color button
464
+ if ( getNumberOfChildren === 2 || getNumberOfChildren !== 4 ) {
465
+ getNewColorButton ( attribute ) . style . display = 'flex' ;
466
+ } else {
467
+ getNewColorButton ( attribute ) . style . display = 'none' ;
468
+ }
469
+
470
+ // display remove color button
471
+ if ( getNumberOfChildren > 2 ) {
472
+ removeNewColorButton ( attribute ) . style . display = 'flex' ;
473
+ } else {
474
+ removeNewColorButton ( attribute ) . style . display = 'none' ;
475
+ }
476
+ }
0 commit comments