2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import SourcecodeViewer from "../components/content/SourcecodeViewer";
|
|
|
|
|
import ImageViewer from "../components/content/ImageViewer";
|
|
|
|
|
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";
|
2020-01-16 10:00:44 +01:00
|
|
|
import { ErrorNotification, Loading } from "@scm-manager/ui-components";
|
|
|
|
|
import SwitchableMarkdownViewer from "../components/content/SwitchableMarkdownViewer";
|
2020-01-15 20:55:09 +01:00
|
|
|
|
2020-01-16 10:00:44 +01:00
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
repository: Repository;
|
|
|
|
|
file: File;
|
|
|
|
|
revision: string;
|
|
|
|
|
path: string;
|
2018-11-26 15:20:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type State = {
|
2019-10-19 16:38:07 +02:00
|
|
|
contentType: string;
|
|
|
|
|
language: string;
|
|
|
|
|
loaded: boolean;
|
|
|
|
|
error?: Error;
|
2018-11-26 15:20:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SourcesView extends React.Component<Props, State> {
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2019-10-20 18:02:52 +02:00
|
|
|
contentType: "",
|
|
|
|
|
language: "",
|
2020-01-16 10:00:44 +01:00
|
|
|
loaded: false
|
2018-11-26 15:20:44 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
const { file } = this.props;
|
|
|
|
|
getContentType(file._links.self.href)
|
|
|
|
|
.then(result => {
|
2020-01-15 08:25:59 +01:00
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
contentType: result.type,
|
|
|
|
|
language: result.language,
|
|
|
|
|
loaded: true
|
|
|
|
|
});
|
2018-11-26 15:20:44 +01:00
|
|
|
})
|
2020-01-15 08:25:59 +01:00
|
|
|
.catch(error => {
|
|
|
|
|
this.setState({
|
|
|
|
|
...this.state,
|
|
|
|
|
error,
|
|
|
|
|
loaded: true
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-11-26 15:20:44 +01:00
|
|
|
}
|
|
|
|
|
|
2020-05-19 15:52:11 +02:00
|
|
|
createBasePath() {
|
|
|
|
|
const { repository, revision } = this.props;
|
|
|
|
|
return `/repo/${repository.namespace}/${repository.name}/code/sources/${revision}/`;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-26 15:20:44 +01:00
|
|
|
showSources() {
|
2020-01-16 10:00:44 +01:00
|
|
|
const { file, revision } = this.props;
|
|
|
|
|
const { contentType, language } = this.state;
|
2020-05-19 15:52:11 +02:00
|
|
|
const basePath = this.createBasePath();
|
2019-10-20 18:02:52 +02:00
|
|
|
if (contentType.startsWith("image/")) {
|
2018-11-26 15:20:44 +01:00
|
|
|
return <ImageViewer file={file} />;
|
2020-08-27 08:11:23 +02:00
|
|
|
} else if (contentType.includes("markdown") || (language && language.toLowerCase() === "markdown")) {
|
2020-05-19 15:52:11 +02:00
|
|
|
return <SwitchableMarkdownViewer file={file} basePath={basePath} />;
|
2018-11-26 15:20:44 +01:00
|
|
|
} else if (language) {
|
|
|
|
|
return <SourcecodeViewer file={file} language={language} />;
|
2019-10-20 18:02:52 +02:00
|
|
|
} else if (contentType.startsWith("text/")) {
|
2018-11-26 15:20:44 +01:00
|
|
|
return <SourcecodeViewer file={file} language="none" />;
|
|
|
|
|
} else {
|
|
|
|
|
return (
|
|
|
|
|
<ExtensionPoint
|
|
|
|
|
name="repos.sources.view"
|
2019-10-19 16:38:07 +02:00
|
|
|
props={{
|
|
|
|
|
file,
|
|
|
|
|
contentType,
|
2020-05-19 15:52:11 +02:00
|
|
|
revision,
|
|
|
|
|
basePath
|
2019-10-19 16:38:07 +02:00
|
|
|
}}
|
2018-11-26 15:20:44 +01:00
|
|
|
>
|
|
|
|
|
<DownloadViewer file={file} />
|
|
|
|
|
</ExtensionPoint>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-02-01 10:12:45 +01:00
|
|
|
const { file } = this.props;
|
2018-11-26 15:20:44 +01:00
|
|
|
const { loaded, error } = this.state;
|
|
|
|
|
|
|
|
|
|
if (!file || !loaded) {
|
|
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
if (error) {
|
|
|
|
|
return <ErrorNotification error={error} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const sources = this.showSources();
|
|
|
|
|
|
2019-02-01 10:12:45 +01:00
|
|
|
return <div className="panel-block">{sources}</div>;
|
2018-11-26 15:20:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-16 10:00:44 +01:00
|
|
|
export default SourcesView;
|