Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions x-pack/plugins/code/public/components/main/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ interface Props extends RouteComponentProps<MainRouteParams> {
repoScope: string[];
fetchMoreCommits(repoUri: string): void;
}
const LANG_MD = 'markdown';

enum ButtonOption {
Code = 'Code',
Expand All @@ -96,28 +97,18 @@ enum ButtonOption {
Folder = 'Directory',
}

enum ButtonLabel {
Code = 'Code',
Content = 'Content',
Download = 'Download',
Raw = 'Raw',
}

const Title = styled(EuiTitle)`
margin: ${theme.euiSizeXS} 0 ${theme.euiSize};
`;

class CodeContent extends React.PureComponent<Props> {
public buttonOptions = [
{
id: ButtonOption.Code,
label: ButtonOption.Code,
},
{
id: ButtonOption.Blame,
label: ButtonOption.Blame,
},
{
id: ButtonOption.History,
label: ButtonOption.History,
},
];

public rawButtonOptions = [{ id: 'Raw', label: 'Raw' }];

public findNode = (pathSegments: string[], node: FileTree): FileTree | undefined => {
if (!node) {
return undefined;
Expand Down Expand Up @@ -184,20 +175,43 @@ class CodeContent extends React.PureComponent<Props> {
currentTree &&
(currentTree.type === FileTreeItemType.File || currentTree.type === FileTreeItemType.Link)
) {
const { isUnsupported, isOversize, isImage, lang } = this.props.file!;
const isMarkdown = lang === LANG_MD;
const isText = !isUnsupported && !isOversize && !isImage;

const buttonOptions = [
{
id: ButtonOption.Code,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use “content” instead of “code”, in the case of markdown, binary, image

keep "code" when it is actually code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

label: isText && !isMarkdown ? ButtonLabel.Code : ButtonLabel.Content,
},
{
id: ButtonOption.Blame,
label: ButtonOption.Blame,
isDisabled: isUnsupported || isImage || isOversize,
},
{
id: ButtonOption.History,
label: ButtonOption.History,
},
];
const rawButtonOptions = [
{ id: 'Raw', label: isText ? ButtonLabel.Raw : ButtonLabel.Download },
];

return (
<ButtonsContainer>
<EuiButtonGroup
buttonSize="s"
color="primary"
options={this.buttonOptions}
options={buttonOptions}
type="single"
idSelected={buttonId}
onChange={this.switchButton}
/>
<EuiButtonGroup
buttonSize="s"
color="primary"
options={this.rawButtonOptions}
options={rawButtonOptions}
type="single"
idSelected={''}
onChange={this.openRawFile}
Expand Down Expand Up @@ -312,7 +326,13 @@ class CodeContent extends React.PureComponent<Props> {
if (!file) {
return null;
}
const { lang: fileLanguage, content: fileContent, url, isUnsupported, isOversize } = file;
const {
lang: fileLanguage,
content: fileContent,
isUnsupported,
isOversize,
isImage,
} = file;
if (isUnsupported) {
return (
<ErrorPanel
Expand All @@ -329,16 +349,17 @@ class CodeContent extends React.PureComponent<Props> {
/>
);
}
if (fileLanguage === 'markdown') {
if (fileLanguage === LANG_MD) {
return (
<div className="markdown-body code-markdown-container">
<Markdown source={fileContent} escapeHtml={true} skipHtml={true} />
</div>
);
} else if (this.props.file!.isImage) {
} else if (isImage) {
const rawUrl = chrome.addBasePath(`/app/code/repo/${repoUri}/raw/${revision}/${path}`);
return (
<div className="code-auto-margin">
<img src={url} alt={url} />
<img src={rawUrl} alt={rawUrl} />
</div>
);
}
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/code/public/sagas/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,15 @@ export async function requestFile(
return {
payload,
isImage: true,
content: await response.text(),
content: '',
url,
isUnsupported: false,
};
} else {
return {
payload,
isImage: false,
content: await response.text(),
content: '',
url,
isUnsupported: true,
};
Expand Down