mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 07:25:44 +01:00
refactor markdownviewer // fix padding
This commit is contained in:
@@ -1,16 +1,21 @@
|
|||||||
import React, { FC, useEffect, useState } from "react";
|
import React, { FC, useEffect, useState } from "react";
|
||||||
import { getContent } from "./SourcecodeViewer";
|
import { getContent } from "./SourcecodeViewer";
|
||||||
import { Link, File } from "@scm-manager/ui-types";
|
import { Link, File } from "@scm-manager/ui-types";
|
||||||
import { Loading, ErrorNotification, MarkdownView, Button, Level } from "@scm-manager/ui-components";
|
import { Loading, ErrorNotification, MarkdownView } from "@scm-manager/ui-components";
|
||||||
|
import styled from "styled-components";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
file: File;
|
file: File;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const MarkdownContent = styled.div`
|
||||||
|
padding: 0.5rem;
|
||||||
|
`;
|
||||||
|
|
||||||
const MarkdownViewer: FC<Props> = ({ file }) => {
|
const MarkdownViewer: FC<Props> = ({ file }) => {
|
||||||
const [loading, setLoading] = useState<boolean>(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<Error | undefined>(undefined);
|
const [error, setError] = useState<Error | undefined>(undefined);
|
||||||
const [content, setContent] = useState<string>("");
|
const [content, setContent] = useState("");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getContent((file._links.self as Link).href)
|
getContent((file._links.self as Link).href)
|
||||||
@@ -32,7 +37,11 @@ const MarkdownViewer: FC<Props> = ({ file }) => {
|
|||||||
return <ErrorNotification error={error} />;
|
return <ErrorNotification error={error} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return <MarkdownView content={content} />;
|
return (
|
||||||
|
<MarkdownContent>
|
||||||
|
<MarkdownView content={content} />
|
||||||
|
</MarkdownContent>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default MarkdownViewer;
|
export default MarkdownViewer;
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import React, { FC, useState } from "react";
|
||||||
|
import styled from "styled-components";
|
||||||
|
import MarkdownViewer from "./MarkdownViewer";
|
||||||
|
import SourcecodeViewer from "./SourcecodeViewer";
|
||||||
|
import { File } from "@scm-manager/ui-types";
|
||||||
|
import { Button } from "@scm-manager/ui-components";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
const ToggleButton = styled(Button)`
|
||||||
|
max-width: 1rem;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
right: 0.25rem;
|
||||||
|
z-index: 999;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const Container = styled.div`
|
||||||
|
position: relative;
|
||||||
|
`;
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
file: File;
|
||||||
|
};
|
||||||
|
|
||||||
|
const SwitchableMarkdownViewer: FC<Props> = ({ file }) => {
|
||||||
|
const { t } = useTranslation("repos");
|
||||||
|
const [renderMarkdown, setRenderMarkdown] = useState(true);
|
||||||
|
|
||||||
|
const toggleMarkdown = () => {
|
||||||
|
setRenderMarkdown(!renderMarkdown);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Container>
|
||||||
|
<ToggleButton
|
||||||
|
color={renderMarkdown ? "link" : ""}
|
||||||
|
action={toggleMarkdown}
|
||||||
|
title={
|
||||||
|
renderMarkdown
|
||||||
|
? t("sources.content.toggleButton.showSources")
|
||||||
|
: t("sources.content.toggleButton.showMarkdown")
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<i className="fab fa-markdown" />
|
||||||
|
</ToggleButton>
|
||||||
|
{renderMarkdown ? <MarkdownViewer file={file} /> : <SourcecodeViewer file={file} language={"MARKDOWN"} />}
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SwitchableMarkdownViewer;
|
||||||
@@ -1,21 +1,14 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import styled from "styled-components";
|
|
||||||
import { WithTranslation, withTranslation } from "react-i18next";
|
|
||||||
import SourcecodeViewer from "../components/content/SourcecodeViewer";
|
import SourcecodeViewer from "../components/content/SourcecodeViewer";
|
||||||
import ImageViewer from "../components/content/ImageViewer";
|
import ImageViewer from "../components/content/ImageViewer";
|
||||||
import MarkdownViewer from "../components/content/MarkdownViewer";
|
|
||||||
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 { getContentType } from "./contentType";
|
import { getContentType } from "./contentType";
|
||||||
import { File, Repository } from "@scm-manager/ui-types";
|
import { File, Repository } from "@scm-manager/ui-types";
|
||||||
import { Button, ErrorNotification, Level, Loading } from "@scm-manager/ui-components";
|
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
|
||||||
|
import SwitchableMarkdownViewer from "../components/content/SwitchableMarkdownViewer";
|
||||||
|
|
||||||
const ToggleButton = styled(Button)`
|
type Props = {
|
||||||
max-width: 2em;
|
|
||||||
margin-right: 0.25em;
|
|
||||||
`;
|
|
||||||
|
|
||||||
type Props = WithTranslation & {
|
|
||||||
repository: Repository;
|
repository: Repository;
|
||||||
file: File;
|
file: File;
|
||||||
revision: string;
|
revision: string;
|
||||||
@@ -25,7 +18,6 @@ type Props = WithTranslation & {
|
|||||||
type State = {
|
type State = {
|
||||||
contentType: string;
|
contentType: string;
|
||||||
language: string;
|
language: string;
|
||||||
renderMarkdown: boolean;
|
|
||||||
loaded: boolean;
|
loaded: boolean;
|
||||||
error?: Error;
|
error?: Error;
|
||||||
};
|
};
|
||||||
@@ -37,8 +29,7 @@ class SourcesView extends React.Component<Props, State> {
|
|||||||
this.state = {
|
this.state = {
|
||||||
contentType: "",
|
contentType: "",
|
||||||
language: "",
|
language: "",
|
||||||
loaded: false,
|
loaded: false
|
||||||
renderMarkdown: true
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,32 +53,13 @@ class SourcesView extends React.Component<Props, State> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleMarkdown = () => {
|
|
||||||
this.setState({ renderMarkdown: !this.state.renderMarkdown });
|
|
||||||
};
|
|
||||||
|
|
||||||
showSources() {
|
showSources() {
|
||||||
const { file, revision, t } = this.props;
|
const { file, revision } = this.props;
|
||||||
const { contentType, language, renderMarkdown } = this.state;
|
const { contentType, language } = this.state;
|
||||||
if (contentType.startsWith("image/")) {
|
if (contentType.startsWith("image/")) {
|
||||||
return <ImageViewer file={file} />;
|
return <ImageViewer file={file} />;
|
||||||
} else if (contentType.includes("markdown")) {
|
} else if (contentType.includes("markdown")) {
|
||||||
return (
|
return <SwitchableMarkdownViewer file={file} />;
|
||||||
<>
|
|
||||||
<Level
|
|
||||||
right={
|
|
||||||
<ToggleButton
|
|
||||||
color={renderMarkdown ? "" : "link"}
|
|
||||||
action={this.toggleMarkdown}
|
|
||||||
title={renderMarkdown ? t("sources.content.toggleButton.showSources") : t("sources.content.toggleButton.showMarkdown")}
|
|
||||||
>
|
|
||||||
<i className="fab fa-markdown"></i>
|
|
||||||
</ToggleButton>
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
{renderMarkdown ? <MarkdownViewer file={file} /> : <SourcecodeViewer file={file} language={language} />}
|
|
||||||
</>
|
|
||||||
);
|
|
||||||
} else if (language) {
|
} else if (language) {
|
||||||
return <SourcecodeViewer file={file} language={language} />;
|
return <SourcecodeViewer file={file} language={language} />;
|
||||||
} else if (contentType.startsWith("text/")) {
|
} else if (contentType.startsWith("text/")) {
|
||||||
@@ -125,4 +97,4 @@ class SourcesView extends React.Component<Props, State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withTranslation("repos")(SourcesView);
|
export default SourcesView;
|
||||||
|
|||||||
Reference in New Issue
Block a user