2018-07-24 13:57:57 -04:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-03-15 10:19:57 -04:00
|
|
|
import styled from '@emotion/styled';
|
2018-08-30 16:24:28 -04:00
|
|
|
import { List } from 'immutable';
|
2019-12-18 18:16:02 +02:00
|
|
|
import { WidgetPreviewContainer, Asset } from 'netlify-cms-ui-default';
|
2018-07-24 13:57:57 -04:00
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
const FileLink = styled(({ value: href, path }) => (
|
|
|
|
<a href={href} rel="noopener noreferrer" target="_blank">
|
|
|
|
{path}
|
2018-08-30 16:24:28 -04:00
|
|
|
</a>
|
|
|
|
))`
|
|
|
|
display: block;
|
|
|
|
`;
|
|
|
|
|
2019-12-18 18:16:02 +02:00
|
|
|
const FileLinkAsset = ({ value, getAsset }) => {
|
|
|
|
return <Asset path={value} getAsset={getAsset} component={FileLink} />;
|
|
|
|
};
|
|
|
|
|
2018-08-30 16:24:28 -04:00
|
|
|
function FileLinkList({ values, getAsset }) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{values.map(value => (
|
2019-12-18 18:16:02 +02:00
|
|
|
<FileLinkAsset key={value} value={value} getAsset={getAsset} />
|
2018-08-30 16:24:28 -04:00
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function FileContent({ value, getAsset }) {
|
|
|
|
if (Array.isArray(value) || List.isList(value)) {
|
|
|
|
return <FileLinkList values={value} getAsset={getAsset} />;
|
|
|
|
}
|
2019-12-18 18:16:02 +02:00
|
|
|
return <FileLinkAsset value={value} getAsset={getAsset} />;
|
2018-08-30 16:24:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const FilePreview = props => (
|
|
|
|
<WidgetPreviewContainer>{props.value ? <FileContent {...props} /> : null}</WidgetPreviewContainer>
|
2018-07-24 13:57:57 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
FilePreview.propTypes = {
|
|
|
|
getAsset: PropTypes.func.isRequired,
|
|
|
|
value: PropTypes.node,
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FilePreview;
|