Add viewer for pdf files (#1843)

Adds an viewer for pdf files to the source code browser.
This commit is contained in:
Sebastian Sdorra
2021-11-03 14:47:14 +01:00
committed by GitHub
parent 0cc12ba061
commit 241dcb6f0c
8 changed files with 106 additions and 1 deletions

View File

@@ -0,0 +1,2 @@
- type: added
description: Viewer for pdf files ([#1843](https://github.com/scm-manager/scm-manager/pull/1843))

View File

@@ -0,0 +1,34 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React from "react";
import PdfViewer from "./PdfViewer";
// @ts-ignore no need to declare module for a single import
import pdf from "./__resources__/doc.pdf";
import { storiesOf } from "@storybook/react";
storiesOf("PdfViewer", module)
.add("Simple", () => <PdfViewer src={pdf} />)
.add("Error", () => <PdfViewer src="/does/not/exists" />)
.add("Error with download URL", () => <PdfViewer src="/does/not/exists" download={pdf} />);

View File

@@ -0,0 +1,60 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React, { FC } from "react";
import { File, Link } from "@scm-manager/ui-types";
import { Notification } from "@scm-manager/ui-components";
import { Trans, useTranslation } from "react-i18next";
type Props = {
src: string | File;
download?: string | File;
height?: string;
};
const createHref = (src: string | File): string => {
if (typeof src === "string") {
return src;
}
return (src._links.self as Link).href;
};
const PdfViewer: FC<Props> = ({ src, download, height = "50rem" }) => {
const [t] = useTranslation("commons");
const href = createHref(src);
const downloadHref = download ? createHref(download) : href;
return (
<div style={{ height }}>
<object height="100%" width="100%" type="application/pdf" data={href + "#toolbar=0&navpanes=0&scrollbar=0"}>
<Notification type="warning">
<Trans t={t} i18nKey="pdfViewer.error">
Failed to display the document. Please download it from <a href={downloadHref}>here</a>.
</Trans>
</Notification>
</object>
</div>
);
};
export default PdfViewer;

Binary file not shown.

View File

@@ -71,6 +71,7 @@ export { default as UserAutocomplete } from "./UserAutocomplete";
export { default as BranchSelector } from "./BranchSelector"; export { default as BranchSelector } from "./BranchSelector";
export { default as Breadcrumb } from "./Breadcrumb"; export { default as Breadcrumb } from "./Breadcrumb";
export { default as MarkdownView } from "./markdown/MarkdownView"; export { default as MarkdownView } from "./markdown/MarkdownView";
export { default as PdfViewer } from "./PdfViewer";
export { default as SyntaxHighlighter } from "./SyntaxHighlighter"; export { default as SyntaxHighlighter } from "./SyntaxHighlighter";
export { default as ErrorBoundary } from "./ErrorBoundary"; export { default as ErrorBoundary } from "./ErrorBoundary";
export { default as OverviewPageActions } from "./OverviewPageActions"; export { default as OverviewPageActions } from "./OverviewPageActions";

View File

@@ -215,5 +215,8 @@
"copyTimestampTooltip": "Zeitstempel in Zwischenablage kopieren" "copyTimestampTooltip": "Zeitstempel in Zwischenablage kopieren"
} }
} }
},
"pdfViewer": {
"error": "Das Dokument konnte nicht angezeigt werden. Es kann <1>hier</1> heruntergeladen werden."
} }
} }

View File

@@ -216,5 +216,8 @@
"copyTimestampTooltip": "Copy Timestamp to Clipboard" "copyTimestampTooltip": "Copy Timestamp to Clipboard"
} }
} }
},
"pdfViewer": {
"error": "Failed to display the document. Please download it from <1>here</1>."
} }
} }

View File

@@ -28,7 +28,7 @@ import ImageViewer from "../components/content/ImageViewer";
import DownloadViewer from "../components/content/DownloadViewer"; import DownloadViewer from "../components/content/DownloadViewer";
import { ExtensionPoint } from "@scm-manager/ui-extensions"; import { ExtensionPoint } from "@scm-manager/ui-extensions";
import { File, Link, Repository } from "@scm-manager/ui-types"; import { File, Link, Repository } from "@scm-manager/ui-types";
import { ErrorNotification, Loading } from "@scm-manager/ui-components"; import { ErrorNotification, Loading, PdfViewer } from "@scm-manager/ui-components";
import SwitchableMarkdownViewer from "../components/content/SwitchableMarkdownViewer"; import SwitchableMarkdownViewer from "../components/content/SwitchableMarkdownViewer";
import styled from "styled-components"; import styled from "styled-components";
import { useContentType } from "@scm-manager/ui-api"; import { useContentType } from "@scm-manager/ui-api";
@@ -69,6 +69,8 @@ const SourcesView: FC<Props> = ({ file, repository, revision }) => {
sources = <SourcecodeViewer file={file} language={language} />; sources = <SourcecodeViewer file={file} language={language} />;
} else if (contentType.startsWith("text/")) { } else if (contentType.startsWith("text/")) {
sources = <SourcecodeViewer file={file} language="none" />; sources = <SourcecodeViewer file={file} language="none" />;
} else if (contentType.startsWith("application/pdf")) {
sources = <PdfViewer src={file} />;
} else { } else {
sources = ( sources = (
<ExtensionPoint <ExtensionPoint