@@ -24,14 +24,16 @@ export class DeckEventsHandler {
2424 private errorService : ErrorService ;
2525 private deckBusyService : DeckBusyService ;
2626
27- private userSubscription : Subscription ;
2827 private userService : UserService ;
2928
3029 private updateSlideSubscription : Subscription ;
3130 private updateSlideSubject : Subject < HTMLElement > = new Subject ( ) ;
3231
3332 private deckEditorService : DeckEditorService ;
3433
34+ private updateDeckTitleSubscription : Subscription ;
35+ private updateDeckTitleSubject : Subject < string > = new Subject ( ) ;
36+
3537 constructor ( ) {
3638 this . slideService = SlideService . getInstance ( ) ;
3739 this . deckService = DeckService . getInstance ( ) ;
@@ -56,6 +58,10 @@ export class DeckEventsHandler {
5658 this . updateSlideSubscription = this . updateSlideSubject . pipe ( debounceTime ( 500 ) ) . subscribe ( async ( element : HTMLElement ) => {
5759 await this . updateSlide ( element ) ;
5860 } ) ;
61+
62+ this . updateDeckTitleSubscription = this . updateDeckTitleSubject . pipe ( debounceTime ( 500 ) ) . subscribe ( async ( title : string ) => {
63+ await this . updateDeckTitle ( title ) ;
64+ } ) ;
5965 }
6066
6167 destroy ( ) {
@@ -69,8 +75,8 @@ export class DeckEventsHandler {
6975 this . updateSlideSubscription . unsubscribe ( ) ;
7076 }
7177
72- if ( this . userSubscription ) {
73- this . userSubscription . unsubscribe ( ) ;
78+ if ( this . updateDeckTitleSubscription ) {
79+ this . updateDeckTitleSubscription . unsubscribe ( ) ;
7480 }
7581
7682 this . deckEditorService . next ( null ) ;
@@ -87,7 +93,7 @@ export class DeckEventsHandler {
8793 return ;
8894 }
8995
90- await this . updateDeck ( $event . detail ) ;
96+ await this . updateDeckAttributes ( $event . detail ) ;
9197 } ;
9298
9399 private onSlideChange = async ( $event : CustomEvent ) => {
@@ -103,13 +109,20 @@ export class DeckEventsHandler {
103109 return ;
104110 }
105111
106- const parent : HTMLElement = ( $event . target as HTMLElement ) . parentElement ;
112+ const element : HTMLElement = $event . target as HTMLElement ;
113+
114+ const parent : HTMLElement = element . parentElement ;
107115
108116 if ( ! parent || ! parent . nodeName || parent . nodeName . toLowerCase ( ) . indexOf ( 'deckgo-slide' ) <= - 1 ) {
109117 return ;
110118 }
111119
112120 this . updateSlideSubject . next ( parent ) ;
121+
122+ // The first content editable element on the first slide is the title of the presentation
123+ if ( parent && ! parent . previousElementSibling && ! element . previousElementSibling ) {
124+ this . updateDeckTitleSubject . next ( element . textContent ) ;
125+ }
113126 } ;
114127
115128 private onSlideDelete = async ( $event : CustomEvent ) => {
@@ -218,7 +231,7 @@ export class DeckEventsHandler {
218231 } ) ;
219232 }
220233
221- private updateDeck ( deck : HTMLElement ) : Promise < void > {
234+ private updateDeckAttributes ( deck : HTMLElement ) : Promise < void > {
222235 return new Promise < void > ( async ( resolve ) => {
223236 try {
224237 if ( ! deck ) {
@@ -238,6 +251,8 @@ export class DeckEventsHandler {
238251
239252 if ( attributes && Object . keys ( attributes ) . length > 0 ) {
240253 currentDeck . attributes = attributes ;
254+ } else {
255+ currentDeck . attributes = null ;
241256 }
242257
243258 const updatedDeck : Deck = await this . deckService . put ( currentDeck ) ;
@@ -255,6 +270,39 @@ export class DeckEventsHandler {
255270 } ) ;
256271 }
257272
273+ private updateDeckTitle ( title : string ) : Promise < void > {
274+ return new Promise < void > ( async ( resolve ) => {
275+ try {
276+ if ( ! title || title === undefined || title === '' ) {
277+ resolve ( ) ;
278+ return ;
279+ }
280+
281+ this . deckBusyService . busy ( true ) ;
282+
283+ this . deckEditorService . watch ( ) . pipe ( take ( 1 ) ) . subscribe ( async ( currentDeck : Deck ) => {
284+ if ( ! currentDeck ) {
285+ resolve ( ) ;
286+ return ;
287+ }
288+
289+ currentDeck . name = title ;
290+
291+ const updatedDeck : Deck = await this . deckService . put ( currentDeck ) ;
292+ this . deckEditorService . next ( updatedDeck ) ;
293+
294+ this . deckBusyService . busy ( false ) ;
295+
296+ resolve ( ) ;
297+ } ) ;
298+ } catch ( err ) {
299+ this . errorService . error ( err ) ;
300+ this . deckBusyService . busy ( false ) ;
301+ resolve ( ) ;
302+ }
303+ } ) ;
304+ }
305+
258306 private updateSlide ( slide : HTMLElement ) : Promise < void > {
259307 return new Promise < void > ( async ( resolve ) => {
260308 try {
0 commit comments