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';
|
2020-02-13 02:12:36 +02:00
|
|
|
import { WidgetPreviewContainer } from 'netlify-cms-ui-default';
|
2018-07-24 13:57:57 -04:00
|
|
|
|
2020-02-13 02:12:36 +02:00
|
|
|
const FileLink = styled(({ href, path }) => (
|
2019-12-18 18:16:02 +02:00
|
|
|
<a href={href} rel="noopener noreferrer" target="_blank">
|
|
|
|
{path}
|
2018-08-30 16:24:28 -04:00
|
|
|
</a>
|
|
|
|
))`
|
|
|
|
display: block;
|
|
|
|
`;
|
|
|
|
|
2020-02-14 22:31:33 +02:00
|
|
|
function FileLinkList({ values, getAsset, field }) {
|
2018-08-30 16:24:28 -04:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{values.map(value => (
|
2020-02-14 22:31:33 +02:00
|
|
|
<FileLink key={value} path={value} href={getAsset(value, field)} />
|
2018-08-30 16:24:28 -04:00
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-13 02:12:36 +02:00
|
|
|
function FileContent(props) {
|
|
|
|
const { value, getAsset, field } = props;
|
2018-08-30 16:24:28 -04:00
|
|
|
if (Array.isArray(value) || List.isList(value)) {
|
2020-02-14 22:31:33 +02:00
|
|
|
return <FileLinkList values={value} getAsset={getAsset} field={field} />;
|
2018-08-30 16:24:28 -04:00
|
|
|
}
|
2020-02-14 22:31:33 +02:00
|
|
|
return <FileLink key={value} path={value} href={getAsset(value, field)} />;
|
2018-08-30 16:24:28 -04:00
|
|
|
}
|
|
|
|
|
2021-02-08 20:01:21 +02:00
|
|
|
function FilePreview(props) {
|
|
|
|
return (
|
|
|
|
<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;
|