Skip to content

Commit 5340597

Browse files
Add navigation service
1 parent 2259be4 commit 5340597

File tree

17 files changed

+113
-60
lines changed

17 files changed

+113
-60
lines changed

apps/codelab/src/app/admin/content/presentation-editor/content.component.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Component } from '@angular/core';
2-
import { ContentService } from './content.service';
2+
import { ContentService } from './services/content.service';
33
import { ActivatedRoute } from '@angular/router';
44
import { map } from 'rxjs/operators';
55
import { combineLatest } from 'rxjs';
66
import { ContentPresentation } from './types';
7+
import { NavigationService } from './services/navigation.service';
78

89
declare const require;
910

@@ -14,31 +15,36 @@ declare const require;
1415
})
1516
export class ContentComponent {
1617
readonly presentation$ = combineLatest([
17-
this.activeRoute.params,
18+
this.navigationService.selectedPresentationId$,
1819
this.contentService.state$
1920
]).pipe(
20-
map(([params, presentations]) => {
21+
map(([presentationId, presentations]) => {
22+
debugger;
2123
return presentations.find(
22-
(p: ContentPresentation) => p.id === params.presentation
24+
(p: ContentPresentation) => p.id === presentationId
2325
);
2426
})
2527
);
2628

2729
selectedSlide$ = this.contentService.selectedSlide$;
2830

2931
currentSlide$ = combineLatest([
30-
this.activeRoute.params,
32+
this.navigationService.selectedSlide$,
3133
this.presentation$
3234
]).pipe(
33-
map(([params, presentation]) => {
34-
return presentation.slides[params.slide || 0];
35+
map(([slide, presentation]) => {
36+
debugger;
37+
return presentation.slides[slide || 0];
3538
})
3639
);
3740

3841
constructor(
3942
readonly contentService: ContentService,
43+
readonly navigationService: NavigationService,
4044
readonly activeRoute: ActivatedRoute
41-
) {}
45+
) {
46+
this.contentService.state$.subscribe(a => console.log());
47+
}
4248

4349
addSlide(presentationId: string) {
4450
this.contentService.addSlide(presentationId);

apps/codelab/src/app/admin/content/presentation-editor/content.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { AngularFirestoreModule } from '@angular/fire/firestore';
66
import { DragDropModule } from '@angular/cdk/drag-drop';
77
import { RouterModule } from '@angular/router';
88
import { SidePanelModule } from './side-panel/side-panel.module';
9-
import { ContentService } from './content.service';
9+
import { ContentService } from './services/content.service';
1010
import { MatIconModule } from '@angular/material/icon';
1111
import { MatButtonModule } from '@angular/material/button';
1212
import { Observable, OperatorFunction } from 'rxjs';
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { Component, HostListener } from '@angular/core';
2-
import { ContentService } from '../content.service';
2+
import { NavigationService } from '../services/navigation.service';
3+
import { ContentService } from '../services/content.service';
34

45
@Component({
56
selector: 'slides-preview',
67
templateUrl: './preview.component.html',
78
styleUrls: ['./preview.component.css']
89
})
910
export class PreviewComponent {
10-
constructor(readonly contentService: ContentService) {}
11+
constructor(
12+
readonly navigationService: NavigationService,
13+
readonly contentService: ContentService
14+
) {}
15+
1116
presentationId = 'TBD';
1217

1318
@HostListener('window:keydown.arrowleft')
1419
previousSlide() {
15-
this.contentService.previousSlide(this.presentationId);
20+
this.navigationService.previousSlide(this.presentationId);
1621
}
1722

1823
@HostListener('window:keydown.arrowright')
1924
nextSlide() {
20-
this.contentService.nextSlide(this.presentationId);
25+
this.navigationService.nextSlide(this.presentationId);
2126
}
2227
}

apps/codelab/src/app/admin/content/presentation-editor/content.service.ts renamed to apps/codelab/src/app/admin/content/presentation-editor/services/content.service.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ import { Injectable, OnDestroy } from '@angular/core';
22
import { AngularFirestore } from '@angular/fire/firestore';
33
import { ActivatedRoute, Router } from '@angular/router';
44
import { Location } from '@angular/common';
5-
import { BehaviorSubject, combineLatest, merge, Subject } from 'rxjs';
6-
import { auditTime, map, scan, share, takeUntil } from 'rxjs/operators';
7-
import { ContentBlock, ContentPresentation } from './types';
5+
import { BehaviorSubject, merge, Subject } from 'rxjs';
6+
import {
7+
auditTime,
8+
map,
9+
scan,
10+
share,
11+
shareReplay,
12+
takeUntil
13+
} from 'rxjs/operators';
814
import { nanoid } from 'nanoid';
915
import * as firebase from 'firebase';
10-
import { reducer } from './reducer';
16+
import { ContentBlock, ContentPresentation } from '../types';
17+
import { reducer } from '../reducer';
1118

1219
const DOC_KEY = 'presentations';
1320

@@ -31,8 +38,8 @@ export class ContentService implements OnDestroy {
3138
private readonly localActions$ = new Subject();
3239

3340
readonly appliedActions = new Set();
34-
const;
35-
allActions2$ = merge(
41+
42+
readonly allActions$ = merge(
3643
this.presentations$.pipe(
3744
map((presentations: ContentPresentation[] = []) => ({
3845
type: 'init',
@@ -42,11 +49,11 @@ export class ContentService implements OnDestroy {
4249
this.localActions$
4350
);
4451

45-
public readonly state$ = this.allActions2$.pipe(
52+
public readonly state$ = this.allActions$.pipe(
4653
scan((state: ContentPresentation[], action: any) => {
4754
return reducer(state, action);
4855
}, {}),
49-
share()
56+
shareReplay(1)
5057
);
5158

5259
constructor(
@@ -75,16 +82,6 @@ export class ContentService implements OnDestroy {
7582
this.onDestroy.complete();
7683
}
7784

78-
// TODO: Move this out
79-
goToSlide(presentationId: string, index: number) {
80-
if (index >= 0) {
81-
this.selectedSlideSubject.next(index);
82-
this.location.replaceState(
83-
'admin/content/' + presentationId + '/' + index
84-
);
85-
}
86-
}
87-
8885
dispatch(presentationId, action) {
8986
const a = {
9087
...action,
@@ -96,16 +93,6 @@ export class ContentService implements OnDestroy {
9693
this.localActions$.next(a);
9794
}
9895

99-
// TODO: Move out
100-
nextSlide(presentationId: string) {
101-
this.goToSlide(presentationId, this.selectedSlide$.value + 1);
102-
}
103-
104-
// TODO: Move out
105-
previousSlide(presentationId: string) {
106-
this.goToSlide(presentationId, this.selectedSlide$.value - 1);
107-
}
108-
10996
addSlide(presentationId: string) {
11097
const index = this.selectedSlide$.value;
11198
const action = {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Injectable } from '@angular/core';
2+
import { BehaviorSubject } from 'rxjs';
3+
import { ActivatedRoute, Router } from '@angular/router';
4+
import { Location } from '@angular/common';
5+
import { take } from 'rxjs/operators';
6+
7+
@Injectable({
8+
providedIn: 'root'
9+
})
10+
export class NavigationService {
11+
private readonly selectedSlideSubject = new BehaviorSubject(0);
12+
private readonly selectedPresentationIdSubject = new BehaviorSubject<
13+
string | undefined
14+
>(undefined);
15+
public selectedSlide$ = this.selectedSlideSubject.asObservable();
16+
public selectedPresentationId$ = this.selectedPresentationIdSubject.asObservable();
17+
18+
constructor(
19+
readonly route: ActivatedRoute,
20+
readonly router: Router,
21+
readonly location: Location
22+
) {
23+
// TODO(kirjs): need a better way
24+
const params =
25+
router.routerState.snapshot.root.firstChild.firstChild.firstChild.params;
26+
this.selectedPresentationIdSubject.next(params.presentation);
27+
this.selectedSlideSubject.next(params.slide);
28+
}
29+
30+
goToPresentation(presentationId: string) {
31+
this.selectedPresentationIdSubject.next(presentationId);
32+
this.router.navigate(['admin', 'content', presentationId, '0']);
33+
}
34+
35+
goToSlide(presentationId: string, index: number) {
36+
if (index >= 0) {
37+
this.selectedSlideSubject.next(index);
38+
this.location.replaceState(
39+
'admin/content/' + presentationId + '/' + index
40+
);
41+
}
42+
}
43+
44+
nextSlide(presentationId: string) {
45+
this.goToSlide(presentationId, this.selectedSlideSubject.value + 1);
46+
}
47+
48+
previousSlide(presentationId: string) {
49+
this.goToSlide(presentationId, this.selectedSlideSubject.value - 1);
50+
}
51+
}

apps/codelab/src/app/admin/content/presentation-editor/side-panel/side-panel.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*ngFor="let slide of slides; let i = index"
88
[class.selected]="i === selectedSlide"
99
inert="true"
10-
(click)="this.contentService.goToSlide(presentationId, i)"
10+
(click)="this.navigationService.goToSlide(presentationId, i)"
1111
>
1212
<div
1313
cdkDrag

apps/codelab/src/app/admin/content/presentation-editor/side-panel/side-panel.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
import { Location } from '@angular/common';
99

1010
import { ActivatedRoute, Router } from '@angular/router';
11-
import { ContentService } from '../content.service';
11+
import { ContentService } from '../services/content.service';
12+
import { NavigationService } from '../services/navigation.service';
1213

1314
@Component({
1415
selector: 'slides-side-panel',
@@ -25,7 +26,8 @@ export class SidePanelComponent {
2526
readonly location: Location,
2627
readonly route: ActivatedRoute,
2728
readonly router: Router,
28-
readonly contentService: ContentService
29+
readonly contentService: ContentService,
30+
readonly navigationService: NavigationService
2931
) {}
3032

3133
addSlide() {
@@ -34,11 +36,11 @@ export class SidePanelComponent {
3436

3537
@HostListener('keydown.arrowdown')
3638
nextSlide() {
37-
this.contentService.nextSlide(this.presentationId);
39+
this.navigationService.nextSlide(this.presentationId);
3840
}
3941

4042
@HostListener('keydown.arrowup')
4143
prevSlide() {
42-
this.contentService.previousSlide(this.presentationId);
44+
this.navigationService.previousSlide(this.presentationId);
4345
}
4446
}

apps/codelab/src/app/admin/content/presentation-editor/slide-editor/action-bar/action-bar.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, EventEmitter, Input, Output } from '@angular/core';
22
import { ContentBlock, ContentSlide } from '../../types';
3-
import { ContentService } from '../../content.service';
3+
import { ContentService } from '../../services/content.service';
44

55
@Component({
66
selector: 'slides-action-bar',

apps/codelab/src/app/admin/content/presentation-editor/slide-editor/slide-editor.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, EventEmitter, Input, Output } from '@angular/core';
2-
import { ContentService } from '../content.service';
2+
import { ContentService } from '../services/content.service';
33
import { assertIsHtmlBlock, ContentBlock, ContentSlide } from '../types';
44

55
@Component({

apps/codelab/src/app/admin/content/presentation-editor/slide-editor/slide-meta-editor/slide-meta-editor.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, Input, OnInit } from '@angular/core';
2-
import { ContentService } from '../../content.service';
2+
import { ContentService } from '../../services/content.service';
33

44
@Component({
55
selector: 'slides-slide-meta-editor',

0 commit comments

Comments
 (0)