From 79207a41fe741b6c81b08f549fa8275adf36d024 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 11 Dec 2023 23:36:55 -0800 Subject: [PATCH] Consolidate preview components --- .../preview-popup.component.html | 33 ++++------ .../preview-popup.component.spec.ts | 66 ++++++++++++++----- .../preview-popup/preview-popup.component.ts | 43 ++++++++---- .../saved-view-widget.component.html | 7 +- .../saved-view-widget.component.ts | 18 +---- .../document-card-large.component.html | 7 +- .../document-card-large.component.spec.ts | 21 ------ .../document-card-large.component.ts | 11 ---- .../document-card-small.component.html | 7 +- .../document-card-small.component.spec.ts | 21 ------ .../document-card-small.component.ts | 11 ---- 11 files changed, 101 insertions(+), 144 deletions(-) diff --git a/src-ui/src/app/components/common/preview-popup/preview-popup.component.html b/src-ui/src/app/components/common/preview-popup/preview-popup.component.html index e81ff9a40..71c3faf1b 100644 --- a/src-ui/src/app/components/common/preview-popup/preview-popup.component.html +++ b/src-ui/src/app/components/common/preview-popup/preview-popup.component.html @@ -3,25 +3,20 @@

Error loading preview

- -
{{previewText}}
-
- - - -
- - - -
- - -
+ + +
+ + + +
+ +
diff --git a/src-ui/src/app/components/common/preview-popup/preview-popup.component.spec.ts b/src-ui/src/app/components/common/preview-popup/preview-popup.component.spec.ts index 945ed1572..edcf63f51 100644 --- a/src-ui/src/app/components/common/preview-popup/preview-popup.component.spec.ts +++ b/src-ui/src/app/components/common/preview-popup/preview-popup.component.spec.ts @@ -4,52 +4,84 @@ import { PreviewPopupComponent } from './preview-popup.component' import { PdfViewerComponent } from '../pdf-viewer/pdf-viewer.component' import { By } from '@angular/platform-browser' import { SafeUrlPipe } from 'src/app/pipes/safeurl.pipe' +import { SettingsService } from 'src/app/services/settings.service' +import { SETTINGS_KEYS } from 'src/app/data/paperless-uisettings' +import { HttpClientTestingModule } from '@angular/common/http/testing' +import { DocumentService } from 'src/app/services/rest/document.service' + +const doc = { + id: 10, + title: 'Document 10', + content: 'Cupcake ipsum dolor sit amet ice cream.', + original_file_name: 'sample.pdf', +} describe('PreviewPopupComponent', () => { let component: PreviewPopupComponent let fixture: ComponentFixture + let settingsService: SettingsService + let documentService: DocumentService beforeEach(() => { TestBed.configureTestingModule({ declarations: [PreviewPopupComponent, PdfViewerComponent, SafeUrlPipe], + imports: [HttpClientTestingModule], }) + settingsService = TestBed.inject(SettingsService) + documentService = TestBed.inject(DocumentService) + jest + .spyOn(documentService, 'getPreviewUrl') + .mockImplementation((id) => doc.original_file_name) fixture = TestBed.createComponent(PreviewPopupComponent) component = fixture.componentInstance + component.document = doc fixture.detectChanges() }) - it('should render object if use native PDF viewer', () => { - component.useNativePdfViewer = true - component.previewURL = 'sample.pdf' + it('should guess if file is pdf by file name', () => { + expect(component.isPdf).toBeTruthy() + component.document.archived_file_name = 'sample.pdf' + expect(component.isPdf).toBeTruthy() + component.document.archived_file_name = undefined + component.document.original_file_name = 'sample.txt' + expect(component.isPdf).toBeFalsy() + component.document.original_file_name = 'sample.pdf' + }) + + it('should return settings for native PDF viewer', () => { + settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, false) + expect(component.useNativePdfViewer).toBeFalsy() + settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, true) + expect(component.useNativePdfViewer).toBeTruthy() + }) + + it('should render object if native PDF viewer enabled', () => { + settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, true) fixture.detectChanges() expect(fixture.debugElement.query(By.css('object'))).not.toBeNull() }) - it('should render pngx viewer if not use native PDF viewer', () => { - component.useNativePdfViewer = false - component.previewURL = 'sample.pdf' + it('should render pngx viewer if native PDF viewer disabled', () => { + settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, false) fixture.detectChanges() expect(fixture.debugElement.query(By.css('object'))).toBeNull() expect(fixture.debugElement.query(By.css('pngx-pdf-viewer'))).not.toBeNull() }) - it('should render plain text if needed', () => { - component.renderAsPlainText = true - component.previewText = 'Hello world' - fixture.detectChanges() - expect(fixture.debugElement.query(By.css('object'))).toBeNull() - expect(fixture.debugElement.query(By.css('pngx-pdf-viewer'))).toBeNull() - expect(fixture.debugElement.nativeElement.textContent).toContain( - 'Hello world' - ) - }) - it('should show lock icon on password error', () => { + settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, false) component.onError({ name: 'PasswordException' }) fixture.detectChanges() + expect(component.requiresPassword).toBeTruthy() expect(fixture.debugElement.query(By.css('svg'))).not.toBeNull() }) + it('should fall back to object for non-pdf', () => { + component.document.original_file_name = 'sample.png' + fixture.detectChanges() + expect(fixture.debugElement.query(By.css('object'))).not.toBeNull() + }) + it('should show message on error', () => { component.onError({}) fixture.detectChanges() diff --git a/src-ui/src/app/components/common/preview-popup/preview-popup.component.ts b/src-ui/src/app/components/common/preview-popup/preview-popup.component.ts index ef34db9f2..014e31e8b 100644 --- a/src-ui/src/app/components/common/preview-popup/preview-popup.component.ts +++ b/src-ui/src/app/components/common/preview-popup/preview-popup.component.ts @@ -1,4 +1,8 @@ import { Component, Input } from '@angular/core' +import { PaperlessDocument } from 'src/app/data/paperless-document' +import { SETTINGS_KEYS } from 'src/app/data/paperless-uisettings' +import { DocumentService } from 'src/app/services/rest/document.service' +import { SettingsService } from 'src/app/services/settings.service' @Component({ selector: 'pngx-preview-popup', @@ -7,21 +11,38 @@ import { Component, Input } from '@angular/core' }) export class PreviewPopupComponent { @Input() - renderAsPlainText: boolean = false - - @Input() - previewText: string - - @Input() - previewURL: string - - @Input() - useNativePdfViewer: boolean = false + document: PaperlessDocument error = false + requiresPassword: boolean = false - onError(event) { + get renderAsObject(): boolean { + return (this.isPdf && this.useNativePdfViewer) || !this.isPdf + } + + get previewURL() { + return this.documentService.getPreviewUrl(this.document.id) + } + + get useNativePdfViewer(): boolean { + return this.settingsService.get(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER) + } + + get isPdf(): boolean { + // We dont have time to retrieve metadata, make a best guess by file name + return ( + this.document?.original_file_name?.endsWith('.pdf') || + this.document?.archived_file_name?.endsWith('.pdf') + ) + } + + constructor( + private settingsService: SettingsService, + private documentService: DocumentService + ) {} + + onError(event: any) { if (event.name == 'PasswordException') { this.requiresPassword = true } else { diff --git a/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.html b/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.html index 695936ad9..b1c8515d5 100644 --- a/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.html +++ b/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.html @@ -36,12 +36,7 @@ - - + diff --git a/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.ts b/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.ts index 176392574..39dec7e35 100644 --- a/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.ts +++ b/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.ts @@ -22,8 +22,6 @@ import { DocumentListViewService } from 'src/app/services/document-list-view.ser import { ComponentWithPermissions } from 'src/app/components/with-permissions/with-permissions.component' import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' import { queryParamsFromFilterRules } from 'src/app/utils/query-params' -import { SettingsService } from 'src/app/services/settings.service' -import { SETTINGS_KEYS } from 'src/app/data/paperless-uisettings' @Component({ selector: 'pngx-saved-view-widget', @@ -42,8 +40,7 @@ export class SavedViewWidgetComponent private list: DocumentListViewService, private consumerStatusService: ConsumerStatusService, public openDocumentsService: OpenDocumentsService, - public documentListViewService: DocumentListViewService, - private settingsService: SettingsService + public documentListViewService: DocumentListViewService ) { super() } @@ -113,10 +110,6 @@ export class SavedViewWidgetComponent ]) } - get useNativePdfViewer(): boolean { - return this.settingsService.get(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER) - } - getPreviewUrl(document: PaperlessDocument): string { return this.documentService.getPreviewUrl(document.id) } @@ -125,13 +118,6 @@ export class SavedViewWidgetComponent return this.documentService.getDownloadUrl(document.id) } - isPdf(document: PaperlessDocument) { - return ( - document.original_file_name?.endsWith('.pdf') || - document.archived_file_name?.endsWith('.pdf') - ) - } - mouseEnterPreview(doc: PaperlessDocument) { this.popover = this.popovers.get(this.documents.indexOf(doc)) this.mouseOnPreview = true @@ -155,6 +141,8 @@ export class SavedViewWidgetComponent } mouseLeaveCard() { + console.log('leave card') + this.popover?.close() } diff --git a/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.html b/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.html index 1cac5b76e..b59c934f2 100644 --- a/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.html +++ b/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.html @@ -56,12 +56,7 @@  View - - + diff --git a/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.spec.ts b/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.spec.ts index d2971936b..7bdd8422c 100644 --- a/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.spec.ts +++ b/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.spec.ts @@ -19,9 +19,7 @@ import { DocumentTitlePipe } from 'src/app/pipes/document-title.pipe' import { SafeUrlPipe } from 'src/app/pipes/safeurl.pipe' import { DocumentCardLargeComponent } from './document-card-large.component' import { IsNumberPipe } from 'src/app/pipes/is-number.pipe' -import { SettingsService } from 'src/app/services/settings.service' import { PreviewPopupComponent } from '../../common/preview-popup/preview-popup.component' -import { SETTINGS_KEYS } from 'src/app/data/paperless-uisettings' const doc = { id: 10, @@ -43,7 +41,6 @@ const doc = { describe('DocumentCardLargeComponent', () => { let component: DocumentCardLargeComponent let fixture: ComponentFixture - let settingsService: SettingsService beforeEach(async () => { TestBed.configureTestingModule({ @@ -67,7 +64,6 @@ describe('DocumentCardLargeComponent', () => { }).compileComponents() fixture = TestBed.createComponent(DocumentCardLargeComponent) - settingsService = TestBed.inject(SettingsService) component = fixture.componentInstance component.document = doc fixture.detectChanges() @@ -93,23 +89,6 @@ describe('DocumentCardLargeComponent', () => { expect(component.popover.isOpen()).toBeFalsy() })) - it('should guess if file is pdf by file name', () => { - component.document.original_file_name = 'sample.pdf' - expect(component.isPdf).toBeTruthy() - component.document.archived_file_name = 'sample.pdf' - expect(component.isPdf).toBeTruthy() - component.document.archived_file_name = undefined - component.document.original_file_name = 'sample.txt' - expect(component.isPdf).toBeFalsy() - }) - - it('should return settings for native PDF viewer', () => { - settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, false) - expect(component.useNativePdfViewer).toBeFalsy() - settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, true) - expect(component.useNativePdfViewer).toBeTruthy() - }) - it('should trim content', () => { expect(component.contentTrimmed).toHaveLength(503) // includes ... }) diff --git a/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.ts b/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.ts index 31cc445ff..deb855449 100644 --- a/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.ts +++ b/src-ui/src/app/components/document-list/document-card-large/document-card-large.component.ts @@ -103,17 +103,6 @@ export class DocumentCardLargeComponent extends ComponentWithPermissions { return this.documentService.getPreviewUrl(this.document.id) } - get useNativePdfViewer(): boolean { - return this.settingsService.get(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER) - } - - get isPdf(): boolean { - return ( - this.document?.original_file_name?.endsWith('.pdf') || - this.document?.archived_file_name?.endsWith('.pdf') - ) - } - mouseEnterPreview() { this.mouseOnPreview = true if (!this.popover.isOpen()) { diff --git a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.html b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.html index 528fd2f2b..c945384d6 100644 --- a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.html +++ b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.html @@ -94,12 +94,7 @@ - - + diff --git a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.spec.ts b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.spec.ts index df333786b..28db4502e 100644 --- a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.spec.ts +++ b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.spec.ts @@ -22,8 +22,6 @@ import { By } from '@angular/platform-browser' import { TagComponent } from '../../common/tag/tag.component' import { PaperlessTag } from 'src/app/data/paperless-tag' import { IsNumberPipe } from 'src/app/pipes/is-number.pipe' -import { SettingsService } from 'src/app/services/settings.service' -import { SETTINGS_KEYS } from 'src/app/data/paperless-uisettings' import { PreviewPopupComponent } from '../../common/preview-popup/preview-popup.component' const doc = { @@ -56,7 +54,6 @@ const doc = { describe('DocumentCardSmallComponent', () => { let component: DocumentCardSmallComponent let fixture: ComponentFixture - let settingsService: SettingsService beforeEach(async () => { TestBed.configureTestingModule({ @@ -81,7 +78,6 @@ describe('DocumentCardSmallComponent', () => { }).compileComponents() fixture = TestBed.createComponent(DocumentCardSmallComponent) - settingsService = TestBed.inject(SettingsService) component = fixture.componentInstance component.document = Object.assign({}, doc) fixture.detectChanges() @@ -111,23 +107,6 @@ describe('DocumentCardSmallComponent', () => { ).toHaveLength(6) }) - it('should guess if file is pdf by file name', () => { - component.document.original_file_name = 'sample.pdf' - expect(component.isPdf).toBeTruthy() - component.document.archived_file_name = 'sample.pdf' - expect(component.isPdf).toBeTruthy() - component.document.archived_file_name = undefined - component.document.original_file_name = 'sample.txt' - expect(component.isPdf).toBeFalsy() - }) - - it('should return settings for native PDF viewer', () => { - settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, false) - expect(component.useNativePdfViewer).toBeFalsy() - settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, true) - expect(component.useNativePdfViewer).toBeTruthy() - }) - it('should show preview on mouseover after delay to preload content', fakeAsync(() => { component.mouseEnterPreview() expect(component.popover.isOpen()).toBeTruthy() diff --git a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.ts b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.ts index 39b3e9b8b..41ef958fb 100644 --- a/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.ts +++ b/src-ui/src/app/components/document-list/document-card-small/document-card-small.component.ts @@ -77,17 +77,6 @@ export class DocumentCardSmallComponent extends ComponentWithPermissions { return $localize`Private` } - get useNativePdfViewer(): boolean { - return this.settingsService.get(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER) - } - - get isPdf(): boolean { - return ( - this.document?.original_file_name?.endsWith('.pdf') || - this.document?.archived_file_name?.endsWith('.pdf') - ) - } - getTagsLimited$() { const limit = this.document.notes.length > 0 ? 6 : 7 return this.document.tags$.pipe(