1- import { Component , Element , Event , EventEmitter , h , Prop , State } from '@stencil/core' ;
1+ import { Component , Event , EventEmitter , h , Prop , State } from '@stencil/core' ;
2+
23import { alertController , RangeChangeEventDetail } from '@ionic/core' ;
3- import { ColorUtils , InitStyleColor } from '../../../../utils/editor/color.utils' ;
4+
5+ import { DeckdeckgoHighlightCodeCarbonTheme , DeckdeckgoHighlightCodeTerminal } from '@deckdeckgo/highlight-code' ;
6+
7+ import { ColorUtils , InitStyleColor } from '../../../../../utils/editor/color.utils' ;
48
59enum CodeColorType {
610 COMMENTS ,
@@ -19,8 +23,6 @@ enum CodeColorType {
1923 styleUrl : 'app-color-code.scss' ,
2024} )
2125export class AppColorCode {
22- @Element ( ) el : HTMLElement ;
23-
2426 @Prop ( )
2527 selectedElement : HTMLElement ;
2628
@@ -45,11 +47,18 @@ export class AppColorCode {
4547 @State ( )
4648 private highlightColorOpacity : number = 100 ;
4749
48- @Event ( ) colorChange : EventEmitter < void > ;
50+ @State ( )
51+ private terminal : DeckdeckgoHighlightCodeTerminal = DeckdeckgoHighlightCodeTerminal . CARBON ;
52+
53+ @State ( )
54+ private theme : DeckdeckgoHighlightCodeCarbonTheme = DeckdeckgoHighlightCodeCarbonTheme . DRACULA ;
55+
56+ @Event ( ) codeDidChange : EventEmitter < void > ;
4957
5058 async componentWillLoad ( ) {
51- await this . initColor ( ) ;
52- await this . initCurrentHiglight ( ) ;
59+ const promises : Promise < void > [ ] = [ this . initColor ( ) , this . initCurrentHiglight ( ) , this . initTerminal ( ) ] ;
60+
61+ await Promise . all ( promises ) ;
5362 }
5463
5564 // prettier-ignore
@@ -68,6 +77,18 @@ export class AppColorCode {
6877 } ) ;
6978 }
7079
80+ private async initTerminal ( ) {
81+ this . terminal =
82+ this . selectedElement && this . selectedElement . hasAttribute ( 'terminal' )
83+ ? ( this . selectedElement . getAttribute ( 'terminal' ) as DeckdeckgoHighlightCodeTerminal )
84+ : DeckdeckgoHighlightCodeTerminal . CARBON ;
85+
86+ this . theme =
87+ this . selectedElement && this . selectedElement . hasAttribute ( 'theme' )
88+ ? ( this . selectedElement . getAttribute ( 'theme' ) as DeckdeckgoHighlightCodeCarbonTheme )
89+ : DeckdeckgoHighlightCodeCarbonTheme . DRACULA ;
90+ }
91+
7192 private selectColor ( $event : CustomEvent , colorFunction : Function ) : Promise < void > {
7293 return new Promise < void > ( async ( resolve ) => {
7394 if ( ! this . selectedElement || ! this . selectedElement . parentElement ) {
@@ -165,13 +186,13 @@ export class AppColorCode {
165186 }
166187
167188 // prettier-ignore
168- private initColor ( ) : Promise < string > {
169- return new Promise < string > ( async ( resolve ) => {
189+ private initColor ( ) : Promise < void > {
190+ return new Promise < void > ( async ( resolve ) => {
170191 if ( ! this . selectedElement || ! this . selectedElement . style ) {
171192 this . codeColor = undefined ;
172193 this . codeColorOpacity = 100 ;
173194
174- resolve ( null ) ;
195+ resolve ( ) ;
175196 return ;
176197 }
177198
@@ -248,7 +269,7 @@ export class AppColorCode {
248269 }
249270
250271 private emitColorChange ( ) {
251- this . colorChange . emit ( ) ;
272+ this . codeDidChange . emit ( ) ;
252273 }
253274
254275 private updateOpacity ( $event : CustomEvent < RangeChangeEventDetail > , opacityFunction : Function ) : Promise < void > {
@@ -297,14 +318,40 @@ export class AppColorCode {
297318 } ) ;
298319 }
299320
321+ private toggle ( $event : CustomEvent , attribute : 'terminal' | 'theme' ) : Promise < void > {
322+ return new Promise < void > ( async ( resolve ) => {
323+ if ( ! $event || ! $event . detail ) {
324+ resolve ( ) ;
325+ return ;
326+ }
327+
328+ if ( ! this . selectedElement ) {
329+ resolve ( ) ;
330+ return ;
331+ }
332+
333+ if ( attribute === 'terminal' ) {
334+ this . terminal = $event . detail . value ;
335+ } else if ( attribute === 'theme' ) {
336+ this . theme = $event . detail . value ;
337+ }
338+
339+ this . selectedElement . setAttribute ( attribute , $event . detail . value ) ;
340+
341+ this . codeDidChange . emit ( ) ;
342+
343+ resolve ( ) ;
344+ } ) ;
345+ }
346+
300347 render ( ) {
301- return [ this . renderCategoryColor ( ) , this . renderHighlightLinesColor ( ) ] ;
348+ return [ this . renderTerminal ( ) , this . renderTheme ( ) , this . renderCategoryColor ( ) , this . renderHighlightLinesColor ( ) ] ;
302349 }
303350
304351 private renderCategoryColor ( ) {
305352 return (
306- < app-expansion-panel >
307- < ion-label slot = "title" > Colors </ ion-label >
353+ < app-expansion-panel expanded = { 'close' } >
354+ < ion-label slot = "title" > More colors </ ion-label >
308355 < ion-list >
309356 < ion-item class = "select" >
310357 < ion-label > Apply a color to</ ion-label >
@@ -365,9 +412,70 @@ export class AppColorCode {
365412 ) ;
366413 }
367414
368- private renderHighlightLinesColor ( ) {
415+ private renderTerminal ( ) {
416+ return (
417+ < app-expansion-panel >
418+ < ion-label slot = "title" > Terminal</ ion-label >
419+
420+ < ion-list class = "terminal" >
421+ < ion-item class = "select" >
422+ < ion-label > Terminal</ ion-label >
423+
424+ < ion-select
425+ value = { this . terminal }
426+ placeholder = "Select a terminal"
427+ onIonChange = { ( $event : CustomEvent ) => this . toggle ( $event , 'terminal' ) }
428+ interface = "popover"
429+ mode = "md"
430+ class = "ion-padding-start ion-padding-end ion-text-capitalize" >
431+ { Object . keys ( DeckdeckgoHighlightCodeTerminal ) . map ( ( key : string ) => {
432+ return (
433+ < ion-select-option value = { DeckdeckgoHighlightCodeTerminal [ key ] } >
434+ { DeckdeckgoHighlightCodeTerminal [ key ] . replace ( / ^ \w / , ( c ) => c . toUpperCase ( ) ) }
435+ </ ion-select-option >
436+ ) ;
437+ } ) }
438+ </ ion-select >
439+ </ ion-item >
440+ </ ion-list >
441+ </ app-expansion-panel >
442+ ) ;
443+ }
444+
445+ private renderTheme ( ) {
369446 return (
370447 < app-expansion-panel >
448+ < ion-label slot = "title" > Theme</ ion-label >
449+
450+ < ion-list class = "theme" >
451+ < ion-item class = "select" >
452+ < ion-label > Theme</ ion-label >
453+
454+ < ion-select
455+ value = { this . theme }
456+ placeholder = "Select a theme"
457+ disabled = { this . terminal !== DeckdeckgoHighlightCodeTerminal . CARBON }
458+ onIonChange = { ( $event : CustomEvent ) => this . toggle ( $event , 'theme' ) }
459+ interface = "popover"
460+ mode = "md"
461+ class = "ion-padding-start ion-padding-end ion-text-capitalize" >
462+ { Object . keys ( DeckdeckgoHighlightCodeCarbonTheme ) . map ( ( key : string ) => {
463+ return (
464+ < ion-select-option value = { DeckdeckgoHighlightCodeCarbonTheme [ key ] } >
465+ { DeckdeckgoHighlightCodeCarbonTheme [ key ] . replace ( / ^ \w / , ( c ) => c . toUpperCase ( ) ) }
466+ </ ion-select-option >
467+ ) ;
468+ } ) }
469+ </ ion-select >
470+ </ ion-item >
471+ </ ion-list >
472+ </ app-expansion-panel >
473+ ) ;
474+ }
475+
476+ private renderHighlightLinesColor ( ) {
477+ return (
478+ < app-expansion-panel expanded = { 'close' } >
371479 < ion-label slot = "title" > Highlight lines</ ion-label >
372480 < button slot = "info" class = "info" onClick = { ( $event : UIEvent ) => this . presentHighlightInfo ( $event ) } >
373481 < ion-icon name = "help" > </ ion-icon >
0 commit comments