This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { flattenToAppURL } from '@plone/volto/helpers'; | ||
|
||
const niceBytes = (bytes) => { | ||
bytes = Number(bytes); | ||
|
||
const divider = 1000; | ||
const magnitude = (Math.log(bytes) / Math.log(divider)) | 0; | ||
const result = bytes / Math.pow(divider, magnitude); | ||
const fixed = result.toFixed(2); | ||
|
||
const suffix = magnitude ? 'kMGTPEZY'[magnitude - 1] + 'B' : 'B'; | ||
|
||
return fixed + suffix; | ||
}; | ||
|
||
export const FileWidget = ({ value, children, className }) => { | ||
if (!value) { | ||
return ''; | ||
} | ||
|
||
const url = flattenToAppURL(value.download || value.filename || value); | ||
const filename = value.filename || url; | ||
const size = value.size || 0; | ||
const ctype = value['content-type'] || ''; | ||
return ( | ||
<a | ||
title={ctype || filename} | ||
href={url} | ||
className={cx(className, 'file', 'widget')} | ||
data-size={size} | ||
data-size-fmt={niceBytes(size)} | ||
data-content-type={ctype} | ||
> | ||
{children ? children(filename) : filename} | ||
</a> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import { FileWidget } from './FileWidget'; | ||
|
||
describe('FileWidget', () => { | ||
it('renders an empty file view widget component', () => { | ||
const component = renderer.create(<FileWidget />); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a simple file view widget component', () => { | ||
const component = renderer.create( | ||
<FileWidget className="metadata" value="/foo-bar.pdf" />, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a file view widget component', () => { | ||
const component = renderer.create( | ||
<FileWidget className="metadata" value={{ download: '/foo-bar.pdf' }} />, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders a file view widget component with children', () => { | ||
const component = renderer.create( | ||
<FileWidget | ||
className="metadata" | ||
value={{ | ||
download: '/foo-bar.pdf', | ||
filename: 'foo-bar.pdf', | ||
'content-type': 'application/x-pdf', | ||
size: 123456, | ||
}} | ||
> | ||
{(child) => <strong>{child}</strong>} | ||
</FileWidget>, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import { flattenToAppURL } from '@plone/volto/helpers'; | ||
|
||
const niceBytes = (bytes) => { | ||
bytes = Number(bytes); | ||
|
||
const divider = 1000; | ||
const magnitude = (Math.log(bytes) / Math.log(divider)) | 0; | ||
const result = bytes / Math.pow(divider, magnitude); | ||
const fixed = result.toFixed(2); | ||
|
||
const suffix = magnitude ? 'kMGTPEZY'[magnitude - 1] + 'B' : 'B'; | ||
|
||
return fixed + suffix; | ||
}; | ||
|
||
export const ImageWidget = ({ value, className }) => | ||
value ? ( | ||
<span className={cx(className, 'image', 'widget')}> | ||
<img | ||
src={flattenToAppURL(value.download)} | ||
alt={value.file_name || ''} | ||
data-size={value.size || 0} | ||
data-size-fmt={niceBytes(value.size || 0)} | ||
data-content-type={value['content-type'] || ''} | ||
/> | ||
</span> | ||
) : ( | ||
'' | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
import renderer from 'react-test-renderer'; | ||
import { ImageWidget } from './ImageWidget'; | ||
|
||
describe('ImageWidget', () => { | ||
it('renders an empty image view widget component', () => { | ||
const component = renderer.create(<ImageWidget />); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders an image view widget component', () => { | ||
const component = renderer.create( | ||
<ImageWidget className="metadata" value={{ download: '/foo-bar.png' }} />, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders an image view widget component with children', () => { | ||
const component = renderer.create( | ||
<ImageWidget | ||
className="metadata" | ||
value={{ | ||
download: '/foo-bar.png', | ||
file_name: 'foo-bar.png', | ||
}} | ||
> | ||
{(child) => <strong>{child}</strong>} | ||
</ImageWidget>, | ||
); | ||
const json = component.toJSON(); | ||
expect(json).toMatchSnapshot(); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
src/components/theme/Widgets/__snapshots__/FileWidget.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`FileWidget renders a file view widget component 1`] = ` | ||
<a | ||
className="metadata file widget" | ||
data-content-type="" | ||
data-size={0} | ||
data-size-fmt="0.00B" | ||
href="/foo-bar.pdf" | ||
title="/foo-bar.pdf" | ||
> | ||
/foo-bar.pdf | ||
</a> | ||
`; | ||
|
||
exports[`FileWidget renders a file view widget component with children 1`] = ` | ||
<a | ||
className="metadata file widget" | ||
data-content-type="application/x-pdf" | ||
data-size={123456} | ||
data-size-fmt="123.46kB" | ||
href="/foo-bar.pdf" | ||
title="application/x-pdf" | ||
> | ||
<strong> | ||
foo-bar.pdf | ||
</strong> | ||
</a> | ||
`; | ||
|
||
exports[`FileWidget renders a simple file view widget component 1`] = ` | ||
<a | ||
className="metadata file widget" | ||
data-content-type="" | ||
data-size={0} | ||
data-size-fmt="0.00B" | ||
href="/foo-bar.pdf" | ||
title="/foo-bar.pdf" | ||
> | ||
/foo-bar.pdf | ||
</a> | ||
`; | ||
|
||
exports[`FileWidget renders an empty file view widget component 1`] = `""`; |
31 changes: 31 additions & 0 deletions
31
src/components/theme/Widgets/__snapshots__/ImageWidget.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`ImageWidget renders an empty image view widget component 1`] = `""`; | ||
|
||
exports[`ImageWidget renders an image view widget component 1`] = ` | ||
<span | ||
className="metadata image widget" | ||
> | ||
<img | ||
alt="" | ||
data-content-type="" | ||
data-size={0} | ||
data-size-fmt="0.00B" | ||
src="/foo-bar.png" | ||
/> | ||
</span> | ||
`; | ||
|
||
exports[`ImageWidget renders an image view widget component with children 1`] = ` | ||
<span | ||
className="metadata image widget" | ||
> | ||
<img | ||
alt="foo-bar.png" | ||
data-content-type="" | ||
data-size={0} | ||
data-size-fmt="0.00B" | ||
src="/foo-bar.png" | ||
/> | ||
</span> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters