From 5842944d1ef817c11a47ed5c19ba8b7886c9fbfe Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 20 Jan 2024 08:26:24 -0800 Subject: [PATCH] Fix: render images not converted to pdf, refactor doc detail rendering (#5475) --- .../document-detail.component.html | 80 ++++++++++--------- .../document-detail.component.scss | 6 ++ .../document-detail.component.spec.ts | 47 +++++++++++ .../document-detail.component.ts | 30 +++++-- 4 files changed, 116 insertions(+), 47 deletions(-) diff --git a/src-ui/src/app/components/document-detail/document-detail.component.html b/src-ui/src/app/components/document-detail/document-detail.component.html index c44a202d0..038a44dbe 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.html +++ b/src-ui/src/app/components/document-detail/document-detail.component.html @@ -1,5 +1,5 @@ - @if (getContentType() === 'application/pdf' && !useNativePdfViewer) { + @if (contentRenderType === ContentRenderType.PDF && !useNativePdfViewer) {
Page
@@ -106,7 +106,7 @@ @for (fieldInstance of document?.custom_fields; track fieldInstance; let i = $index) {
@switch (getCustomFieldFromInstance(fieldInstance)?.data_type) { - @case (PaperlessCustomFieldDataType.String) { + @case (CustomFieldDataType.String) { } - @case (PaperlessCustomFieldDataType.Date) { + @case (CustomFieldDataType.Date) { } - @case (PaperlessCustomFieldDataType.Integer) { + @case (CustomFieldDataType.Integer) { } - @case (PaperlessCustomFieldDataType.Float) { + @case (CustomFieldDataType.Float) { } - @case (PaperlessCustomFieldDataType.Monetary) { + @case (CustomFieldDataType.Monetary) { } - @case (PaperlessCustomFieldDataType.Boolean) { + @case (CustomFieldDataType.Boolean) { } - @case (PaperlessCustomFieldDataType.Url) { + @case (CustomFieldDataType.Url) { } - @case (PaperlessCustomFieldDataType.DocumentLink) { + @case (CustomFieldDataType.DocumentLink) { Preview @if (!pdfPreview.offsetParent) { - +
+ +
} @@ -295,16 +297,6 @@
- @if (renderAsPlainText) { -
- } - @if (requiresPassword) { -
-
- -
-
- }
@@ -333,29 +325,39 @@
} - @if (getContentType() === 'application/pdf') { - @if (!useNativePdfViewer ) { -
- - + @switch (contentRenderType) { + @case (ContentRenderType.PDF) { + @if (!useNativePdfViewer) { +
+ + +
+ } @else { + + } + } + @case (ContentRenderType.Text) { +
{{previewText}}
+ } + @case (ContentRenderType.Image) { +
+ {{title}}
- } @else { + } + @case (ContentRenderType.Other) { } } - @if (renderAsPlainText) { -
- } - @if (showPasswordField) { + @if (requiresPassword) {
diff --git a/src-ui/src/app/components/document-detail/document-detail.component.scss b/src-ui/src/app/components/document-detail/document-detail.component.scss index 026969339..a7871cc9e 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.scss +++ b/src-ui/src/app/components/document-detail/document-detail.component.scss @@ -58,3 +58,9 @@ textarea.rtl { border-top-right-radius: var(--bs-border-radius-sm); border-bottom-right-radius: var(--bs-border-radius-sm); } + +.preview-sticky img { + width: 100%; + height: 100%; + object-fit: contain; +} diff --git a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts index 2399c88c6..cccf2677b 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.spec.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.spec.ts @@ -996,6 +996,53 @@ describe('DocumentDetailComponent', () => { expect(closeSpy).toHaveBeenCalled() }) + it('should change preview element by render type', () => { + component.metadata = { has_archive_version: true } + initNormally() + fixture.detectChanges() + expect(component.contentRenderType).toEqual(component.ContentRenderType.PDF) + expect( + fixture.debugElement.query(By.css('pdf-viewer-container')) + ).not.toBeUndefined() + + component.metadata = { + has_archive_version: false, + original_mime_type: 'text/plain', + } + fixture.detectChanges() + expect(component.contentRenderType).toEqual( + component.ContentRenderType.Text + ) + expect( + fixture.debugElement.query(By.css('div.preview-sticky')) + ).not.toBeUndefined() + + component.metadata = { + has_archive_version: false, + original_mime_type: 'image/jpg', + } + fixture.detectChanges() + expect(component.contentRenderType).toEqual( + component.ContentRenderType.Image + ) + expect( + fixture.debugElement.query(By.css('.preview-sticky img')) + ).not.toBeUndefined() + + component.metadata = { + has_archive_version: false, + original_mime_type: + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + } + fixture.detectChanges() + expect(component.contentRenderType).toEqual( + component.ContentRenderType.Other + ) + expect( + fixture.debugElement.query(By.css('object.preview-sticky')) + ).not.toBeUndefined() + }) + function initNormally() { jest .spyOn(activatedRoute, 'paramMap', 'get') diff --git a/src-ui/src/app/components/document-detail/document-detail.component.ts b/src-ui/src/app/components/document-detail/document-detail.component.ts index 798e867bf..18e6b1211 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.ts +++ b/src-ui/src/app/components/document-detail/document-detail.component.ts @@ -77,6 +77,13 @@ enum DocumentDetailNavIDs { Permissions = 6, } +enum ContentRenderType { + PDF = 'pdf', + Image = 'image', + Text = 'text', + Other = 'other', +} + enum ZoomSetting { PageFit = 'page-fit', PageWidth = 'page-width', @@ -154,7 +161,9 @@ export class DocumentDetailComponent ogDate: Date customFields: CustomField[] - public readonly PaperlessCustomFieldDataType = CustomFieldDataType + public readonly CustomFieldDataType = CustomFieldDataType + + public readonly ContentRenderType = ContentRenderType @ViewChild('nav') nav: NgbNav @ViewChild('pdfPreview') set pdfPreview(element) { @@ -201,16 +210,21 @@ export class DocumentDetailComponent return this.settings.get(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER) } - getContentType() { - return this.metadata?.has_archive_version + get contentRenderType(): ContentRenderType { + const contentType = this.metadata?.has_archive_version ? 'application/pdf' : this.metadata?.original_mime_type - } - get renderAsPlainText(): boolean { - return ['text/plain', 'application/csv', 'text/csv'].includes( - this.getContentType() - ) + if (contentType === 'application/pdf') { + return ContentRenderType.PDF + } else if ( + ['text/plain', 'application/csv', 'text/csv'].includes(contentType) + ) { + return ContentRenderType.Text + } else if (contentType?.indexOf('image/') === 0) { + return ContentRenderType.Image + } + return ContentRenderType.Other } get isRTL() {