From 584f65c3492a447c8e30c2056c2a161cb17703db Mon Sep 17 00:00:00 2001 From: Eduard Heimbuch Date: Wed, 15 Jan 2020 20:29:18 +0100 Subject: [PATCH] render markdown files in sourcesView --- .../components/content/MarkdownViewer.tsx | 38 +++++++++++++++++++ .../repos/sources/containers/SourcesView.tsx | 37 +++++++++++++++--- 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 scm-ui/ui-webapp/src/repos/sources/components/content/MarkdownViewer.tsx diff --git a/scm-ui/ui-webapp/src/repos/sources/components/content/MarkdownViewer.tsx b/scm-ui/ui-webapp/src/repos/sources/components/content/MarkdownViewer.tsx new file mode 100644 index 0000000000..86731ace1b --- /dev/null +++ b/scm-ui/ui-webapp/src/repos/sources/components/content/MarkdownViewer.tsx @@ -0,0 +1,38 @@ +import React, { FC, useEffect, useState } from "react"; +import { getContent } from "./SourcecodeViewer"; +import { Link, File } from "@scm-manager/ui-types"; +import { Loading, ErrorNotification, MarkdownView, Button, Level } from "@scm-manager/ui-components"; + +type Props = { + file: File; +}; + +const MarkdownViewer: FC = ({ file }) => { + const [loading, setLoading] = useState(true); + const [error, setError] = useState(undefined); + const [content, setContent] = useState(""); + + useEffect(() => { + getContent((file._links.self as Link).href) + .then(content => { + setLoading(false); + setContent(content); + }) + .catch(error => { + setLoading(false); + setError(error); + }); + }, [file]); + + if (loading) { + return ; + } + + if (error) { + return ; + } + + return ; +}; + +export default MarkdownViewer; diff --git a/scm-ui/ui-webapp/src/repos/sources/containers/SourcesView.tsx b/scm-ui/ui-webapp/src/repos/sources/containers/SourcesView.tsx index a22c2c8e92..c0c35c8bb9 100644 --- a/scm-ui/ui-webapp/src/repos/sources/containers/SourcesView.tsx +++ b/scm-ui/ui-webapp/src/repos/sources/containers/SourcesView.tsx @@ -1,14 +1,16 @@ import React from "react"; - +import { WithTranslation, withTranslation } from "react-i18next"; import SourcecodeViewer from "../components/content/SourcecodeViewer"; import ImageViewer from "../components/content/ImageViewer"; +import MarkdownViewer from "../components/content/MarkdownViewer"; import DownloadViewer from "../components/content/DownloadViewer"; import { ExtensionPoint } from "@scm-manager/ui-extensions"; import { getContentType } from "./contentType"; import { File, Repository } from "@scm-manager/ui-types"; -import { ErrorNotification, Loading } from "@scm-manager/ui-components"; +import { ErrorNotification, Loading, Button, Level } from "@scm-manager/ui-components"; +import { Icon } from "@scm-manager/ui-components/src"; -type Props = { +type Props = WithTranslation & { repository: Repository; file: File; revision: string; @@ -18,6 +20,7 @@ type Props = { type State = { contentType: string; language: string; + renderMarkdown: boolean; loaded: boolean; error?: Error; }; @@ -29,7 +32,8 @@ class SourcesView extends React.Component { this.state = { contentType: "", language: "", - loaded: false + loaded: false, + renderMarkdown: true }; } @@ -53,11 +57,32 @@ class SourcesView extends React.Component { }); } + toggleMarkdown = () => { + this.setState({ renderMarkdown: !this.state.renderMarkdown }); + }; + showSources() { const { file, revision } = this.props; - const { contentType, language } = this.state; + const { contentType, language, renderMarkdown } = this.state; if (contentType.startsWith("image/")) { return ; + } else if (contentType.includes("markdown")) { + return ( + <> + + + } + /> + {renderMarkdown ? : } + + ); } else if (language) { return ; } else if (contentType.startsWith("text/")) { @@ -95,4 +120,4 @@ class SourcesView extends React.Component { } } -export default SourcesView; +export default withTranslation("repos")(SourcesView);