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

Commit 4b2dd19

Browse files
feat: add new slide as next slide not last (#1297)
1 parent d62be56 commit 4b2dd19

File tree

4 files changed

+17
-39
lines changed

4 files changed

+17
-39
lines changed

studio/src/app/components/editor/actions/app-slides-aside/app-slides-aside.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {debounce} from '@deckdeckgo/utils';
66

77
import {isSlide} from '../../../../../../../utils/deck/src';
88
import {deckSelector, slideTo} from '../../../../utils/editor/deck.utils';
9+
import {SlideUtils} from '../../../../utils/editor/slide.utils';
910

1011
@Component({
1112
tag: 'app-slides-aside',
@@ -76,21 +77,17 @@ export class AppSlidesAside {
7677
}
7778

7879
private async updateSlide(updatedSlide: HTMLElement) {
79-
const slideIndex: number = this.slideIndex(updatedSlide);
80+
const slideIndex: number = SlideUtils.slideIndex(updatedSlide);
8081

8182
this.slides = [...this.slides.map((slide: HTMLElement, index: number) => (slideIndex === index ? (updatedSlide.cloneNode(true) as HTMLElement) : slide))];
8283
}
8384

8485
private async deleteSlide(deletedSlide: HTMLElement) {
85-
const slideIndex: number = this.slideIndex(deletedSlide);
86+
const slideIndex: number = SlideUtils.slideIndex(deletedSlide);
8687

8788
this.slides = [...this.slides.filter((_slide: HTMLElement, index: number) => slideIndex !== index)];
8889
}
8990

90-
private slideIndex(slide: HTMLElement): number {
91-
return Array.from(slide.parentNode.children).indexOf(slide);
92-
}
93-
9491
private async updateAllSlides() {
9592
const slides: NodeListOf<HTMLElement> = document.querySelectorAll(`${deckSelector} > *`);
9693

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

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,8 @@ export class DeckEventsHandler {
116116
}
117117
};
118118

119-
private onSlidesDidLoad = async ($event: CustomEvent) => {
120-
if ($event) {
121-
await this.initSlideSize();
122-
await this.slideToLastSlide();
123-
}
119+
private onSlidesDidLoad = async () => {
120+
await this.initSlideSize();
124121
};
125122

126123
private onDeckChange = async ($event: CustomEvent) => {
@@ -227,7 +224,7 @@ export class DeckEventsHandler {
227224

228225
// Because of the offline mode, is kind of handy to handle the list on the client side too.
229226
// But maybe in the future it is something which could be moved to the cloud.
230-
await this.updateDeckSlideList(deckStore.state.deck, persistedSlide);
227+
await this.updateDeckSlideList(deckStore.state.deck, persistedSlide, slide);
231228

232229
busyStore.state.deckBusy = false;
233230

@@ -299,7 +296,7 @@ export class DeckEventsHandler {
299296
});
300297
}
301298

302-
private updateDeckSlideList(deck: Deck, slide: Slide): Promise<void> {
299+
private updateDeckSlideList(deck: Deck, slide: Slide, slideElement: HTMLElement): Promise<void> {
303300
return new Promise<void>(async (resolve, reject) => {
304301
try {
305302
if (!deck && !deck.data) {
@@ -316,7 +313,8 @@ export class DeckEventsHandler {
316313
deck.data.slides = [];
317314
}
318315

319-
deck.data.slides.push(slide.id);
316+
const slideIndex: number = SlideUtils.slideIndex(slideElement);
317+
deck.data.slides = [...deck.data.slides.slice(0, slideIndex), slide.id, ...deck.data.slides.slice(slideIndex)];
320318

321319
const updatedDeck: Deck = await this.deckService.update(deck);
322320
deckStore.state.deck = {...updatedDeck};
@@ -891,30 +889,6 @@ export class DeckEventsHandler {
891889
};
892890
}
893891

894-
private async slideToLastSlide(): Promise<void> {
895-
const deck: HTMLDeckgoDeckElement = this.mainRef.querySelector('deckgo-deck');
896-
897-
if (!deck || !deck.children || deck.children.length <= 0) {
898-
return;
899-
}
900-
901-
const slides: Element[] = Array.from(deck.children).filter((slide: Element) => {
902-
return isSlide(slide as HTMLElement);
903-
});
904-
905-
if (!slides || slides.length <= 0) {
906-
return;
907-
}
908-
909-
const lastSlide: Element = slides[slides.length - 1];
910-
911-
if (!lastSlide || lastSlide.getAttribute('slide_id')) {
912-
return;
913-
}
914-
915-
await deck.slideTo(slides.length - 1);
916-
}
917-
918892
async initSlideSize() {
919893
const deck: HTMLDeckgoDeckElement = this.mainRef.querySelector('deckgo-deck');
920894
await deck?.initSlideSize();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,12 @@ export class AppEditor {
332332
}
333333

334334
private async concatSlide(extraSlide: JSX.IntrinsicElements) {
335-
this.slides = [...this.slides, extraSlide];
335+
const slideIndex: number = this.activeIndex + 1;
336+
this.slides = [...this.slides.slice(0, slideIndex), extraSlide, ...this.slides.slice(slideIndex)];
336337

337338
await ParseDeckSlotsUtils.stickLastChildren(this.mainRef);
339+
340+
await this.deckRef?.slideTo(slideIndex);
338341
}
339342

340343
private async replaceSlide(slide: JSX.IntrinsicElements) {

studio/src/app/utils/editor/slide.utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@ export class SlideUtils {
88
static slideScope(element: HTMLElement | undefined): SlideScope {
99
return element?.hasAttribute('scope') ? <SlideScope>element.getAttribute('scope') : SlideScope.DEFAULT;
1010
}
11+
12+
static slideIndex(slide: HTMLElement): number {
13+
return Array.from(slide.parentNode.children).indexOf(slide);
14+
}
1115
}

0 commit comments

Comments
 (0)