text-panel.component.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { Component, ElementRef, Input, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
  2. import { BehaviorSubject, combineLatest, Observable, Subject, Subscription } from 'rxjs';
  3. import { delay, distinctUntilChanged, filter, map, shareReplay, take } from 'rxjs/operators';
  4. import { AppConfig, EditionLevel, EditionLevelType, TextFlow } from '../../app.config';
  5. import { EntitiesSelectItem } from '../../components/entities-select/entities-select.component';
  6. import { LemsSelectItem } from '../../components/lems-select/lems-select.component';
  7. import { Page } from '../../models/evt-models';
  8. import { EVTModelService } from '../../services/evt-model.service';
  9. import { EVTStatusService } from '../../services/evt-status.service';
  10. import { EvtIconInfo } from '../../ui-components/icon/icon.component';
  11. @Component({
  12. selector: 'evt-text-panel',
  13. templateUrl: './text-panel.component.html',
  14. styleUrls: ['./text-panel.component.scss'],
  15. })
  16. export class TextPanelComponent implements OnInit, OnDestroy {
  17. @ViewChild('mainContent') mainContent: ElementRef;
  18. @Input() hideEditionLevelSelector: boolean;
  19. @Input() pageID: string;
  20. public currentPage$ = new BehaviorSubject<Page>(undefined);
  21. public currentPageId$ = this.currentPage$.pipe(
  22. map(p => p?.id),
  23. );
  24. @Output() pageChange: Observable<Page> = this.currentPage$.pipe(
  25. filter(p => !!p),
  26. distinctUntilChanged(),
  27. );
  28. @Input() editionLevelID: EditionLevelType;
  29. public currentEdLevel$ = new BehaviorSubject<EditionLevel>(undefined);
  30. public currentEdLevelId$ = this.currentEdLevel$.pipe(
  31. map(e => e?.id),
  32. );
  33. @Output() editionLevelChange: Observable<EditionLevel> = this.currentEdLevel$.pipe(
  34. filter(e => !!e),
  35. distinctUntilChanged(),
  36. );
  37. public currentStatus$ = combineLatest([
  38. this.evtModelService.pages$,
  39. this.currentPage$,
  40. this.currentEdLevel$,
  41. this.evtStatus.currentViewMode$,
  42. ]).pipe(
  43. delay(0),
  44. filter(([pages, currentPage, editionLevel, currentViewMode]) => !!pages && !!currentPage && !!editionLevel && !!currentViewMode),
  45. map(([pages, currentPage, editionLevel, currentViewMode]) => ({ pages, currentPage, editionLevel, currentViewMode })),
  46. distinctUntilChanged((a, b) => JSON.stringify(a) === JSON.stringify(b)),
  47. shareReplay(1),
  48. );
  49. public itemsToHighlight$ = new Subject<EntitiesSelectItem[]>();
  50. public itemsLemsToHighlight$ = new Subject<LemsSelectItem[]>();
  51. public secondaryContent = '';
  52. private showSecondaryContent = false;
  53. public selectedPage;
  54. public textFlow: TextFlow = AppConfig.evtSettings.edition.defaultTextFlow || 'prose';
  55. public enableProseVersesToggler = AppConfig.evtSettings.edition.proseVersesToggler;
  56. public get proseVersesTogglerIcon(): EvtIconInfo {
  57. return { icon: this.textFlow === 'prose' ? 'align-left' : 'align-justify', iconSet: 'fas' };
  58. }
  59. public isMultiplePageFlow$ = this.currentStatus$.pipe(
  60. map((x) => x.editionLevel.id === 'critical' && x.currentViewMode.id !== 'imageText'),
  61. shareReplay(1),
  62. );
  63. private updatingPageFromScroll = false;
  64. private subscriptions: Subscription[] = [];
  65. constructor(
  66. public evtModelService: EVTModelService,
  67. public evtStatus: EVTStatusService,
  68. ) {
  69. }
  70. ngOnInit() {
  71. if (this.editionLevelID === 'critical') {
  72. this.textFlow = AppConfig.evtSettings.edition.defaultTextFlow || 'verses';
  73. }
  74. if (!this.enableProseVersesToggler) {
  75. this.textFlow = undefined;
  76. }
  77. this.subscriptions.push(
  78. this.currentStatus$.pipe(
  79. map(currentStatus => currentStatus.currentPage),
  80. filter(page => !!page),
  81. delay(0), // Needed to have the HTML pb el available
  82. ).subscribe((page) => this._scrollToPage(page.id)),
  83. );
  84. }
  85. getSecondaryContent(): string {
  86. return this.secondaryContent;
  87. }
  88. isSecondaryContentOpened(): boolean {
  89. return this.showSecondaryContent;
  90. }
  91. toggleSecondaryContent(newContent: string) {
  92. if (this.secondaryContent !== newContent) {
  93. this.showSecondaryContent = true;
  94. this.secondaryContent = newContent;
  95. }
  96. else {
  97. this.showSecondaryContent = false;
  98. this.secondaryContent = '';
  99. }
  100. }
  101. toggleProseVerses() {
  102. this.textFlow = this.textFlow === 'prose' ? 'verses' : 'prose';
  103. }
  104. ngOnDestroy() {
  105. this.subscriptions.forEach(subscription => subscription.unsubscribe());
  106. }
  107. updatePage() {
  108. if (this.mainContent && this.editionLevelID === 'critical') {
  109. const mainContentEl: HTMLElement = this.mainContent.nativeElement;
  110. const pbs = mainContentEl.querySelectorAll('evt-page');
  111. let pbCount = 0;
  112. let pbVisible = false;
  113. let pbId = '';
  114. const docViewTop = mainContentEl.scrollTop;
  115. const docViewBottom = docViewTop + mainContentEl.parentElement.clientHeight;
  116. while (pbCount < pbs.length && !pbVisible) {
  117. pbId = pbs[pbCount].getAttribute('data-id');
  118. const pbElem = mainContentEl.querySelector<HTMLElement>(`evt-page[data-id="${pbId}"]`);
  119. const pbRect = pbElem.getBoundingClientRect();
  120. if (pbRect.top && (pbRect.top <= docViewBottom) && (pbRect.top >= docViewTop)) {
  121. pbVisible = true;
  122. } else {
  123. pbCount++;
  124. }
  125. }
  126. combineLatest([this.evtModelService.pages$, this.currentPageId$])
  127. .pipe(take(1)).subscribe(([pages, currentPageId]) => {
  128. if (pbVisible && currentPageId !== pbId) {
  129. this.updatingPageFromScroll = true;
  130. this.evtStatus.updatePage$.next(pages.find(p => p.id === pbId));
  131. }
  132. });
  133. }
  134. }
  135. private _scrollToPage(pageId: string) {
  136. if (this.updatingPageFromScroll) {
  137. this.updatingPageFromScroll = false;
  138. } else if (this.mainContent) {
  139. const mainContentEl: HTMLElement = this.mainContent.nativeElement;
  140. const pageEl = mainContentEl.querySelector<HTMLElement>(`[data-id="${pageId}"]`);
  141. if (pageEl) {
  142. pageEl.scrollIntoView();
  143. } else {
  144. mainContentEl.parentElement.scrollTop = 0;
  145. }
  146. }
  147. }
  148. }