Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 2c16ef3

Browse files
feat: save reordering of the slides in database
1 parent e080749 commit 2c16ef3

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

studio/src/app/handlers/editor/events/deck/deck-events.handler.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {ItemReorderEventDetail} from '@ionic/core';
2+
13
import {Subject, Subscription} from 'rxjs';
24
import {debounceTime, filter, take} from 'rxjs/operators';
35

@@ -615,4 +617,33 @@ export class DeckEventsHandler {
615617
resolve();
616618
});
617619
}
620+
621+
updateDeckSlidesOrder(detail: ItemReorderEventDetail): Promise<void> {
622+
return new Promise<void>(async (resolve, reject) => {
623+
try {
624+
if (!detail) {
625+
reject('No new order provided for the slides');
626+
return;
627+
}
628+
629+
if (detail.from < 0 || detail.to < 0 || detail.from === detail.to) {
630+
reject('The new order provided for the slides is not valid');
631+
return;
632+
}
633+
634+
this.deckEditorService.watch().pipe(take(1)).subscribe(async (deck: Deck) => {
635+
if (deck && deck.data && deck.data.slides && detail.to < deck.data.slides.length) {
636+
deck.data.slides.splice(detail.to, 0, ...deck.data.slides.splice(detail.from, 1));
637+
638+
const updatedDeck: Deck = await this.deckService.update(deck);
639+
this.deckEditorService.next(updatedDeck);
640+
}
641+
642+
resolve();
643+
});
644+
} catch (err) {
645+
reject(err);
646+
}
647+
});
648+
}
618649
}

studio/src/app/pages/editor/app-editor/app-editor.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Component, Element, Listen, Prop, State, h} from '@stencil/core';
22

3+
import {ItemReorderEventDetail} from '@ionic/core';
4+
35
import {Subscription} from 'rxjs';
46
import {filter, take} from 'rxjs/operators';
57

@@ -25,7 +27,6 @@ import {AnonymousService} from '../../../services/editor/anonymous/anonymous.ser
2527
import {NavDirection, NavService} from '../../../services/core/nav/nav.service';
2628
import {DeckEditorService} from '../../../services/editor/deck/deck-editor.service';
2729
import {BusyService} from '../../../services/editor/busy/busy.service';
28-
import {ItemReorderEventDetail} from '@ionic/core';
2930

3031
@Component({
3132
tag: 'app-editor',
@@ -490,19 +491,25 @@ export class AppEditor {
490491
}
491492

492493
private reorderSlides(detail: ItemReorderEventDetail): Promise<void> {
493-
return new Promise<void>((resolve) => {
494+
return new Promise<void>(async (resolve) => {
494495
if (!detail) {
495496
resolve();
496497
return;
497498
}
498499

499-
if (detail.from < 0 || detail.to < 0 || !this.slides || detail.to >= this.slides.length || detail.from === detail.to) {
500-
resolve();
501-
return;
502-
}
500+
try {
501+
await this.deckEventsHandler.updateDeckSlidesOrder(detail);
503502

504-
this.slides.splice(detail.to, 0, ...this.slides.splice(detail.from, 1));
505-
this.slides = [...this.slides];
503+
if (detail.from < 0 || detail.to < 0 || !this.slides || detail.to >= this.slides.length || detail.from === detail.to) {
504+
resolve();
505+
return;
506+
}
507+
508+
this.slides.splice(detail.to, 0, ...this.slides.splice(detail.from, 1));
509+
this.slides = [...this.slides];
510+
} catch (err) {
511+
// We ignore the error here
512+
}
506513

507514
// Finish the reorder and position the item in the DOM based on where the gesture ended. This method can also be called directly by the reorder group
508515
detail.complete();

0 commit comments

Comments
 (0)