diff --git a/projects/editor/src/app/components/unit-view/page-view/properties/page-properties.component.ts b/projects/editor/src/app/components/unit-view/page-view/properties/page-properties.component.ts index a93387685b23a16513ecd167942eaf414cc52d6e..2bacd6d70f5aa9d924959b188db92b53c3120db9 100644 --- a/projects/editor/src/app/components/unit-view/page-view/properties/page-properties.component.ts +++ b/projects/editor/src/app/components/unit-view/page-view/properties/page-properties.component.ts @@ -15,11 +15,13 @@ import { MessageService } from '../../../../../../../common/message.service'; <div class="input-group" fxLayoutAlign="space-between center"> Reihenfolge <button mat-icon-button matSuffix color="accent" - [style.margin-left.px]="50"> + [style.margin-left.px]="50" + (click)="movePage('up')"> <mat-icon>north</mat-icon> </button> <button mat-icon-button color="accent" - [style.margin-right.px]="20"> + [style.margin-right.px]="20" + (click)="movePage('down')"> <mat-icon>south</mat-icon> </button> </div> @@ -85,6 +87,10 @@ export class PagePropertiesComponent implements OnInit, OnDestroy { }); } + movePage(direction: 'up' | 'down'): void { + this.unitService.movePage(this.selectedPage, direction); + } + updateModel(property: string, value: number | boolean, isInputValid: boolean | null = true): void { if (isInputValid && value != null) { this.unitService.updatePageProperty(this.selectedPage, property, value); diff --git a/projects/editor/src/app/components/unit-view/unit-view.component.html b/projects/editor/src/app/components/unit-view/unit-view.component.html index e2ca3d85b8075f452b685e7b881953f82f7d8ed9..97776e3773fab6d2c431d60229bdbb1c87c8ba93 100644 --- a/projects/editor/src/app/components/unit-view/unit-view.component.html +++ b/projects/editor/src/app/components/unit-view/unit-view.component.html @@ -11,8 +11,9 @@ </mat-drawer> <mat-drawer-content> - <mat-tab-group [style.height.%]="100" mat-align-tabs="start" - [selectedIndex]="selectedPageIndex" + <mat-tab-group *ngIf="pagesLoaded" + [style.height.%]="100" mat-align-tabs="start" + [selectedIndex]="selectionService.selectedPageIndex | async" (selectedIndexChange)="selectPage($event)"> <mat-tab *ngFor="let page of unit.pages; let i = index"> <ng-template mat-tab-label> diff --git a/projects/editor/src/app/components/unit-view/unit-view.component.ts b/projects/editor/src/app/components/unit-view/unit-view.component.ts index ad011b98113ade244e4d17cc1deba8b7bad9944a..b4822387aac777b9413343f9b56840ad5cc39732 100644 --- a/projects/editor/src/app/components/unit-view/unit-view.component.ts +++ b/projects/editor/src/app/components/unit-view/unit-view.component.ts @@ -1,6 +1,6 @@ import { Component, OnDestroy, OnInit } from '@angular/core'; import { Observable, Subject } from 'rxjs'; -import { takeUntil } from 'rxjs/operators'; +import { take, takeUntil } from 'rxjs/operators'; import { UnitService } from '../../unit.service'; import { DialogService } from '../../dialog.service'; import { SelectionService } from '../../selection.service'; @@ -24,8 +24,8 @@ import { Unit } from '../../../../../common/unit'; }) export class UnitViewComponent implements OnInit, OnDestroy { unit!: Unit; - selectedPageIndex: number = 0; private ngUnsubscribe = new Subject<void>(); + pagesLoaded = true; constructor(public selectionService: SelectionService, public unitService: UnitService, @@ -37,25 +37,36 @@ export class UnitViewComponent implements OnInit, OnDestroy { .subscribe((unit: Unit) => { this.unit = unit; }); + // The following is a hack. The tab element gets bugged when changing the underlying array. + // With this we can temporarily remove it from the DOM and then add it again, re-initializing it. + this.unitService.pageMoved + .pipe(takeUntil(this.ngUnsubscribe)) + .subscribe(() => { + this.pagesLoaded = false; + setTimeout(() => { + this.pagesLoaded = true; + }); + }); } selectPage(newIndex: number): void { - this.selectedPageIndex = newIndex; - this.selectionService.selectPage(this.unit.pages[this.selectedPageIndex]); + this.selectionService.selectPageIndex(newIndex); } addPage(): void { this.unitService.addPage(); - this.selectionService.selectPage(this.unit.pages[this.unit.pages.length - 1]); - this.selectedPageIndex = this.unit.pages.length - 1; + this.selectionService.selectPageIndex(this.unit.pages.length - 1); } deletePage(): void { this.showConfirmDialog().pipe(takeUntil(this.ngUnsubscribe)).subscribe((result: boolean) => { if (result) { - this.unitService.deletePage(this.selectedPageIndex); - this.selectedPageIndex -= 1; - this.selectionService.selectPage(this.unit.pages[this.selectedPageIndex]); + this.selectionService.selectedPageIndex + .pipe(take(1)) + .subscribe(index => { + this.unitService.deletePage(index); + this.selectionService.selectPageIndex(index - 1); + }).unsubscribe(); } }); } diff --git a/projects/editor/src/app/selection.service.ts b/projects/editor/src/app/selection.service.ts index 576164a5413e0aba245778d62e72cff743fd0757..e2e176d0ca31f591d32f2c6fcb67a5c3defe2b04 100644 --- a/projects/editor/src/app/selection.service.ts +++ b/projects/editor/src/app/selection.service.ts @@ -12,6 +12,7 @@ export class SelectionService { private unit!: Unit; private _selectedPage!: Subject<UnitPage>; + private _selectedPageIndex: BehaviorSubject<number>; private _selectedPageSection!: BehaviorSubject<UnitPageSection>; private selectedPageSectionComponent!: any; @@ -26,19 +27,25 @@ export class SelectionService { this.unit = unit; }); this._selectedPage = new BehaviorSubject(this.unit.pages[0]); + this._selectedPageIndex = new BehaviorSubject(0); this._selectedPageSection = new BehaviorSubject(this.unit.pages[0].sections[0]); this._selectedElements = new BehaviorSubject([] as UnitUIElement[]); } // === PAGE ======= - selectPage(page: UnitPage): void { - this._selectedPage.next(page); - this._selectedPageSection.next(page.sections[0]); + selectPageIndex(index: number): void { + this._selectedPage.next(this.unit.pages[index]); + this._selectedPageSection.next(this.unit.pages[index].sections[0]); + this._selectedPageIndex.next(index); } get selectedPage(): Observable<UnitPage> { return this._selectedPage.asObservable(); } + + get selectedPageIndex(): Observable<number> { + return this._selectedPageIndex.asObservable(); + } // ### PAGE ###### // === SECTION ===== diff --git a/projects/editor/src/app/unit.service.ts b/projects/editor/src/app/unit.service.ts index 91d6710dc5f8cca375bc5318be8fc201f8cab54b..9506367e23f3a62d2417632e698ffe23073d51ef 100644 --- a/projects/editor/src/app/unit.service.ts +++ b/projects/editor/src/app/unit.service.ts @@ -17,6 +17,7 @@ export class UnitService { private _unit: BehaviorSubject<Unit>; elementPropertyUpdated: Subject<void> = new Subject<void>(); + pageMoved: Subject<void> = new Subject<void>(); selectedPageIndex: number = 0; // TODO weg refactorn constructor(private veronaApiService: VeronaAPIService, @@ -57,6 +58,20 @@ export class UnitService { this.veronaApiService.sendVoeDefinitionChangedNotification(); } + /* reorder page in page array */ + movePage(selectedPage: UnitPage, direction: 'up' | 'down'): void { + const oldPageIndex = this._unit.value.pages.indexOf(selectedPage); + if ((this._unit.value.pages.length > 1) && + !(direction === 'down' && oldPageIndex + 1 === this._unit.value.pages.length) && + !(direction === 'up' && oldPageIndex === 0)) { + const newPageIndex = direction === 'up' ? oldPageIndex - 1 : oldPageIndex + 1; + const page = this._unit.value.pages.splice(oldPageIndex, 1); + this._unit.value.pages.splice(newPageIndex, 0, page[0]); + this._unit.next(this._unit.value); + this.pageMoved.next(); + } + } + updatePageProperty(page: UnitPage, property: string, value: number | boolean): void { if (property === 'alwaysVisible' && value === true && !this.isSetPageAlwaysVisibleAllowed()) { this.messageService.showError('Kann nur für eine Seite gesetzt werden');