Consolidate preview components

This commit is contained in:
shamoon 2023-12-11 23:36:55 -08:00
parent 0af4638676
commit 79207a41fe
11 changed files with 101 additions and 144 deletions

View File

@ -3,25 +3,20 @@
<p class="fst-italic position-absolute top-50 start-50 translate-middle" i18n>Error loading preview</p> <p class="fst-italic position-absolute top-50 start-50 translate-middle" i18n>Error loading preview</p>
</div> </div>
<ng-template #noError> <ng-template #noError>
<ng-container *ngIf="renderAsPlainText; else renderAsObject"> <object *ngIf="renderAsObject; else pngxViewer" [data]="previewURL | safeUrl" width="100%" class="bg-light" [class.p-2]="!isPdf"></object>
<div class="preview-sticky bg-light p-3 overflow-auto" width="100%">{{previewText}}</div> <ng-template #pngxViewer>
</ng-container> <div *ngIf="requiresPassword" class="w-100 h-100 position-relative">
<ng-template #renderAsObject> <svg width="2em" height="2em" fill="currentColor" class="position-absolute top-50 start-50 translate-middle">
<object *ngIf="useNativePdfViewer; else pngxViewer" [data]="previewURL | safeUrl" width="100%"></object> <use xlink:href="assets/bootstrap-icons.svg#file-earmark-lock"/>
<ng-template #pngxViewer> </svg>
<div *ngIf="requiresPassword" class="w-100 h-100 position-relative"> </div>
<svg width="2em" height="2em" fill="currentColor" class="position-absolute top-50 start-50 translate-middle"> <pngx-pdf-viewer *ngIf="!requiresPassword"
<use xlink:href="assets/bootstrap-icons.svg#file-earmark-lock"/> [src]="previewURL"
</svg> [original-size]="false"
</div> [show-borders]="true"
<pngx-pdf-viewer *ngIf="!requiresPassword" [show-all]="true"
[src]="previewURL" (error)="onError($event)">
[original-size]="false" </pngx-pdf-viewer>
[show-borders]="true"
[show-all]="false"
(error)="onError($event)">
</pngx-pdf-viewer>
</ng-template>
</ng-template> </ng-template>
</ng-template> </ng-template>
</div> </div>

View File

@ -4,52 +4,84 @@ import { PreviewPopupComponent } from './preview-popup.component'
import { PdfViewerComponent } from '../pdf-viewer/pdf-viewer.component' import { PdfViewerComponent } from '../pdf-viewer/pdf-viewer.component'
import { By } from '@angular/platform-browser' import { By } from '@angular/platform-browser'
import { SafeUrlPipe } from 'src/app/pipes/safeurl.pipe' 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', () => { describe('PreviewPopupComponent', () => {
let component: PreviewPopupComponent let component: PreviewPopupComponent
let fixture: ComponentFixture<PreviewPopupComponent> let fixture: ComponentFixture<PreviewPopupComponent>
let settingsService: SettingsService
let documentService: DocumentService
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
declarations: [PreviewPopupComponent, PdfViewerComponent, SafeUrlPipe], 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) fixture = TestBed.createComponent(PreviewPopupComponent)
component = fixture.componentInstance component = fixture.componentInstance
component.document = doc
fixture.detectChanges() fixture.detectChanges()
}) })
it('should render object if use native PDF viewer', () => { it('should guess if file is pdf by file name', () => {
component.useNativePdfViewer = true expect(component.isPdf).toBeTruthy()
component.previewURL = 'sample.pdf' 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() fixture.detectChanges()
expect(fixture.debugElement.query(By.css('object'))).not.toBeNull() expect(fixture.debugElement.query(By.css('object'))).not.toBeNull()
}) })
it('should render pngx viewer if not use native PDF viewer', () => { it('should render pngx viewer if native PDF viewer disabled', () => {
component.useNativePdfViewer = false settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, false)
component.previewURL = 'sample.pdf'
fixture.detectChanges() fixture.detectChanges()
expect(fixture.debugElement.query(By.css('object'))).toBeNull() expect(fixture.debugElement.query(By.css('object'))).toBeNull()
expect(fixture.debugElement.query(By.css('pngx-pdf-viewer'))).not.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', () => { it('should show lock icon on password error', () => {
settingsService.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, false)
component.onError({ name: 'PasswordException' }) component.onError({ name: 'PasswordException' })
fixture.detectChanges() fixture.detectChanges()
expect(component.requiresPassword).toBeTruthy()
expect(fixture.debugElement.query(By.css('svg'))).not.toBeNull() 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', () => { it('should show message on error', () => {
component.onError({}) component.onError({})
fixture.detectChanges() fixture.detectChanges()

View File

@ -1,4 +1,8 @@
import { Component, Input } from '@angular/core' 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({ @Component({
selector: 'pngx-preview-popup', selector: 'pngx-preview-popup',
@ -7,21 +11,38 @@ import { Component, Input } from '@angular/core'
}) })
export class PreviewPopupComponent { export class PreviewPopupComponent {
@Input() @Input()
renderAsPlainText: boolean = false document: PaperlessDocument
@Input()
previewText: string
@Input()
previewURL: string
@Input()
useNativePdfViewer: boolean = false
error = false error = false
requiresPassword: boolean = 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') { if (event.name == 'PasswordException') {
this.requiresPassword = true this.requiresPassword = true
} else { } else {

View File

@ -36,12 +36,7 @@
</svg> </svg>
</a> </a>
<ng-template #previewContent> <ng-template #previewContent>
<pngx-preview-popup <pngx-preview-popup [document]="doc"></pngx-preview-popup>
[previewURL]="getPreviewUrl(doc)"
[useNativePdfViewer]="useNativePdfViewer"
[previewText]="doc.content"
[renderAsPlainText]="!isPdf(doc)">
</pngx-preview-popup>
</ng-template> </ng-template>
<a [href]="getDownloadUrl(doc)" class="btn px-4 btn-dark border-dark-subtle" title="Download" i18n-title (click)="$event.stopPropagation()"> <a [href]="getDownloadUrl(doc)" class="btn px-4 btn-dark border-dark-subtle" title="Download" i18n-title (click)="$event.stopPropagation()">
<svg class="buttonicon-xs" fill="currentColor"> <svg class="buttonicon-xs" fill="currentColor">

View File

@ -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 { ComponentWithPermissions } from 'src/app/components/with-permissions/with-permissions.component'
import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
import { queryParamsFromFilterRules } from 'src/app/utils/query-params' 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({ @Component({
selector: 'pngx-saved-view-widget', selector: 'pngx-saved-view-widget',
@ -42,8 +40,7 @@ export class SavedViewWidgetComponent
private list: DocumentListViewService, private list: DocumentListViewService,
private consumerStatusService: ConsumerStatusService, private consumerStatusService: ConsumerStatusService,
public openDocumentsService: OpenDocumentsService, public openDocumentsService: OpenDocumentsService,
public documentListViewService: DocumentListViewService, public documentListViewService: DocumentListViewService
private settingsService: SettingsService
) { ) {
super() 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 { getPreviewUrl(document: PaperlessDocument): string {
return this.documentService.getPreviewUrl(document.id) return this.documentService.getPreviewUrl(document.id)
} }
@ -125,13 +118,6 @@ export class SavedViewWidgetComponent
return this.documentService.getDownloadUrl(document.id) 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) { mouseEnterPreview(doc: PaperlessDocument) {
this.popover = this.popovers.get(this.documents.indexOf(doc)) this.popover = this.popovers.get(this.documents.indexOf(doc))
this.mouseOnPreview = true this.mouseOnPreview = true
@ -155,6 +141,8 @@ export class SavedViewWidgetComponent
} }
mouseLeaveCard() { mouseLeaveCard() {
console.log('leave card')
this.popover?.close() this.popover?.close()
} }

View File

@ -56,12 +56,7 @@
</svg>&nbsp;<span class="d-none d-md-inline" i18n>View</span> </svg>&nbsp;<span class="d-none d-md-inline" i18n>View</span>
</a> </a>
<ng-template #previewContent> <ng-template #previewContent>
<pngx-preview-popup <pngx-preview-popup [document]="document"></pngx-preview-popup>
[previewURL]="previewUrl"
[useNativePdfViewer]="useNativePdfViewer"
[previewText]="document.content"
[renderAsPlainText]="!isPdf">
</pngx-preview-popup>
</ng-template> </ng-template>
<a class="btn btn-sm btn-outline-secondary" [href]="getDownloadUrl()"> <a class="btn btn-sm btn-outline-secondary" [href]="getDownloadUrl()">
<svg class="sidebaricon" fill="currentColor" class="sidebaricon"> <svg class="sidebaricon" fill="currentColor" class="sidebaricon">

View File

@ -19,9 +19,7 @@ import { DocumentTitlePipe } from 'src/app/pipes/document-title.pipe'
import { SafeUrlPipe } from 'src/app/pipes/safeurl.pipe' import { SafeUrlPipe } from 'src/app/pipes/safeurl.pipe'
import { DocumentCardLargeComponent } from './document-card-large.component' import { DocumentCardLargeComponent } from './document-card-large.component'
import { IsNumberPipe } from 'src/app/pipes/is-number.pipe' 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 { PreviewPopupComponent } from '../../common/preview-popup/preview-popup.component'
import { SETTINGS_KEYS } from 'src/app/data/paperless-uisettings'
const doc = { const doc = {
id: 10, id: 10,
@ -43,7 +41,6 @@ const doc = {
describe('DocumentCardLargeComponent', () => { describe('DocumentCardLargeComponent', () => {
let component: DocumentCardLargeComponent let component: DocumentCardLargeComponent
let fixture: ComponentFixture<DocumentCardLargeComponent> let fixture: ComponentFixture<DocumentCardLargeComponent>
let settingsService: SettingsService
beforeEach(async () => { beforeEach(async () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@ -67,7 +64,6 @@ describe('DocumentCardLargeComponent', () => {
}).compileComponents() }).compileComponents()
fixture = TestBed.createComponent(DocumentCardLargeComponent) fixture = TestBed.createComponent(DocumentCardLargeComponent)
settingsService = TestBed.inject(SettingsService)
component = fixture.componentInstance component = fixture.componentInstance
component.document = doc component.document = doc
fixture.detectChanges() fixture.detectChanges()
@ -93,23 +89,6 @@ describe('DocumentCardLargeComponent', () => {
expect(component.popover.isOpen()).toBeFalsy() 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', () => { it('should trim content', () => {
expect(component.contentTrimmed).toHaveLength(503) // includes ... expect(component.contentTrimmed).toHaveLength(503) // includes ...
}) })

View File

@ -103,17 +103,6 @@ export class DocumentCardLargeComponent extends ComponentWithPermissions {
return this.documentService.getPreviewUrl(this.document.id) 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() { mouseEnterPreview() {
this.mouseOnPreview = true this.mouseOnPreview = true
if (!this.popover.isOpen()) { if (!this.popover.isOpen()) {

View File

@ -94,12 +94,7 @@
</svg> </svg>
</a> </a>
<ng-template #previewContent> <ng-template #previewContent>
<pngx-preview-popup <pngx-preview-popup [document]="document"></pngx-preview-popup>
[previewURL]="previewUrl"
[useNativePdfViewer]="useNativePdfViewer"
[previewText]="document.content"
[renderAsPlainText]="!isPdf">
</pngx-preview-popup>
</ng-template> </ng-template>
<a [href]="getDownloadUrl()" class="btn btn-sm btn-outline-secondary" title="Download" i18n-title (click)="$event.stopPropagation()"> <a [href]="getDownloadUrl()" class="btn btn-sm btn-outline-secondary" title="Download" i18n-title (click)="$event.stopPropagation()">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-download" fill="currentColor" xmlns="http://www.w3.org/2000/svg"> <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-download" fill="currentColor" xmlns="http://www.w3.org/2000/svg">

View File

@ -22,8 +22,6 @@ import { By } from '@angular/platform-browser'
import { TagComponent } from '../../common/tag/tag.component' import { TagComponent } from '../../common/tag/tag.component'
import { PaperlessTag } from 'src/app/data/paperless-tag' import { PaperlessTag } from 'src/app/data/paperless-tag'
import { IsNumberPipe } from 'src/app/pipes/is-number.pipe' 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' import { PreviewPopupComponent } from '../../common/preview-popup/preview-popup.component'
const doc = { const doc = {
@ -56,7 +54,6 @@ const doc = {
describe('DocumentCardSmallComponent', () => { describe('DocumentCardSmallComponent', () => {
let component: DocumentCardSmallComponent let component: DocumentCardSmallComponent
let fixture: ComponentFixture<DocumentCardSmallComponent> let fixture: ComponentFixture<DocumentCardSmallComponent>
let settingsService: SettingsService
beforeEach(async () => { beforeEach(async () => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@ -81,7 +78,6 @@ describe('DocumentCardSmallComponent', () => {
}).compileComponents() }).compileComponents()
fixture = TestBed.createComponent(DocumentCardSmallComponent) fixture = TestBed.createComponent(DocumentCardSmallComponent)
settingsService = TestBed.inject(SettingsService)
component = fixture.componentInstance component = fixture.componentInstance
component.document = Object.assign({}, doc) component.document = Object.assign({}, doc)
fixture.detectChanges() fixture.detectChanges()
@ -111,23 +107,6 @@ describe('DocumentCardSmallComponent', () => {
).toHaveLength(6) ).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(() => { it('should show preview on mouseover after delay to preload content', fakeAsync(() => {
component.mouseEnterPreview() component.mouseEnterPreview()
expect(component.popover.isOpen()).toBeTruthy() expect(component.popover.isOpen()).toBeTruthy()

View File

@ -77,17 +77,6 @@ export class DocumentCardSmallComponent extends ComponentWithPermissions {
return $localize`Private` 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$() { getTagsLimited$() {
const limit = this.document.notes.length > 0 ? 6 : 7 const limit = this.document.notes.length > 0 ? 6 : 7
return this.document.tags$.pipe( return this.document.tags$.pipe(