-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dash): Add react version of filmstrip controls
- Loading branch information
Showing
8 changed files
with
330 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
@import './styles'; | ||
|
||
.bp-Filmstrip { | ||
position: absolute; | ||
bottom: 100%; | ||
overflow: hidden; | ||
background: transparent; | ||
box-shadow: 0 0 1px $sunset-grey; | ||
visibility: hidden; // Use visibility instead of display to prevent layout thrash | ||
|
||
&.bp-is-shown { | ||
visibility: visible; | ||
} | ||
} | ||
|
||
.bp-Filmstrip-frame { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.bp-Filmstrip-time { | ||
height: 20px; | ||
color: $white; | ||
font-size: 13px; | ||
line-height: 20px; | ||
text-align: center; | ||
background-color: $twos; | ||
} |
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,72 @@ | ||
import React from 'react'; | ||
import classNames from 'classnames'; | ||
import { formatTime } from './DurationLabels'; | ||
import './Filmstrip.scss'; | ||
|
||
const FILMSTRIP_FRAMES_PER_ROW = 100; | ||
const FILMSTRIP_FRAME_HEIGHT = 90; | ||
const FILMSTRIP_FRAME_WIDTH = 160; // Default frame width for loading crawler | ||
|
||
export type Props = { | ||
aspectRatio?: number; | ||
imageUrl?: string; | ||
interval?: number; | ||
isShown?: boolean; | ||
position?: number; | ||
positionMax?: number; | ||
time?: number; | ||
}; | ||
|
||
export default function Filmstrip({ | ||
aspectRatio = 0, | ||
imageUrl = '', | ||
interval = 1, | ||
isShown, | ||
position = 0, | ||
positionMax = 0, | ||
time = 0, | ||
}: Props): JSX.Element | null { | ||
const [isLoading, setIsLoading] = React.useState(true); | ||
const frameNumber = Math.floor(time / interval); // Current frame based on current time | ||
const frameRow = Math.floor(frameNumber / FILMSTRIP_FRAMES_PER_ROW); // Row number if there is more than one row | ||
const frameWidth = Math.floor(aspectRatio * FILMSTRIP_FRAME_HEIGHT) || FILMSTRIP_FRAME_WIDTH; | ||
const frameBackgroundLeft = -(frameNumber % FILMSTRIP_FRAMES_PER_ROW) * frameWidth; // Frame position in its row | ||
const frameBackgroundTop = -(frameRow * FILMSTRIP_FRAME_HEIGHT); // Row position in its filmstrip | ||
const filmstripLeft = Math.min(Math.max(0, position - frameWidth / 2), positionMax - frameWidth); | ||
|
||
React.useEffect((): void => { | ||
if (!imageUrl) return; | ||
|
||
const filmstripImage = document.createElement('img'); | ||
filmstripImage.onload = (): void => setIsLoading(false); | ||
filmstripImage.src = imageUrl; | ||
}, [imageUrl]); | ||
|
||
return ( | ||
<div className={classNames('bp-Filmstrip', { 'bp-is-shown': isShown })} style={{ left: `${filmstripLeft}px` }}> | ||
<div | ||
className="bp-Filmstrip-frame" | ||
data-testid="bp-Filmstrip-frame" | ||
style={{ | ||
backgroundImage: imageUrl ? `url('${imageUrl}')` : '', | ||
backgroundPositionX: frameBackgroundLeft, | ||
backgroundPositionY: frameBackgroundTop, | ||
height: FILMSTRIP_FRAME_HEIGHT, | ||
width: frameWidth, | ||
}} | ||
> | ||
{isLoading && ( | ||
<div className="bp-crawler" data-testid="bp-Filmstrip-crawler"> | ||
<div /> | ||
<div /> | ||
<div /> | ||
</div> | ||
)} | ||
</div> | ||
|
||
<div className="bp-Filmstrip-time" data-testid="bp-Filmstrip-time"> | ||
{formatTime(time)} | ||
</div> | ||
</div> | ||
); | ||
} |
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
77 changes: 77 additions & 0 deletions
77
src/lib/viewers/controls/media/__tests__/Filmstrip-test.tsx
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,77 @@ | ||
import React from 'react'; | ||
import { shallow, ShallowWrapper } from 'enzyme'; | ||
import Filmstrip from '../Filmstrip'; | ||
|
||
describe('Filmstrip', () => { | ||
const getWrapper = (props = {}): ShallowWrapper => | ||
shallow(<Filmstrip aspectRatio={2} imageUrl="https://app.box.com" {...props} />); | ||
|
||
describe('render', () => { | ||
test('should return a valid wrapper', () => { | ||
const wrapper = getWrapper(); | ||
expect(wrapper.hasClass('bp-Filmstrip')).toBe(true); | ||
}); | ||
|
||
test.each` | ||
time | left | top | ||
${0} | ${-0} | ${-0} | ||
${1} | ${-180} | ${-0} | ||
${10} | ${-1800} | ${-0} | ||
${30} | ${-5400} | ${-0} | ||
${60} | ${-10800} | ${-0} | ||
${100} | ${-0} | ${-90} | ||
${110} | ${-1800} | ${-90} | ||
${500} | ${-0} | ${-450} | ||
${510} | ${-1800} | ${-450} | ||
`('should display the frame position for time $time as $left/$top', ({ left, time, top }) => { | ||
const wrapper = getWrapper({ time }); | ||
expect(wrapper.find('[data-testid="bp-Filmstrip-frame"]').prop('style')).toMatchObject({ | ||
backgroundImage: "url('https://app.box.com')", | ||
backgroundPositionX: left, | ||
backgroundPositionY: top, | ||
}); | ||
}); | ||
|
||
test.each` | ||
aspectRatio | width | ||
${undefined} | ${160} | ||
${0} | ${160} | ||
${1} | ${90} | ||
${2} | ${180} | ||
`('should display the frame size for aspect ratio $aspectRatio as $width', ({ aspectRatio, width }) => { | ||
const wrapper = getWrapper({ aspectRatio }); | ||
expect(wrapper.find('[data-testid="bp-Filmstrip-frame"]').prop('style')).toMatchObject({ | ||
height: 90, | ||
width, | ||
}); | ||
}); | ||
|
||
test('should display the correct filmstrip time', () => { | ||
const wrapper = getWrapper({ time: 120 }); | ||
expect(wrapper.find('[data-testid="bp-Filmstrip-time"]').text()).toBe('2:00'); | ||
}); | ||
|
||
test('should display the crawler while the filmstrip image loads', done => { | ||
const mockImage = document.createElement('img'); | ||
|
||
Object.defineProperty(mockImage, 'src', { | ||
set() { | ||
setTimeout(() => { | ||
this.onload(); | ||
done(); | ||
}); | ||
}, | ||
}); | ||
|
||
jest.useFakeTimers(); | ||
jest.spyOn(document, 'createElement').mockImplementation(() => mockImage); | ||
jest.spyOn(React, 'useEffect').mockImplementationOnce(func => func()); | ||
|
||
const wrapper = getWrapper(); | ||
expect(wrapper.exists('[data-testid="bp-Filmstrip-crawler"]')).toBe(true); | ||
|
||
jest.advanceTimersByTime(0); // Simulate loading complete | ||
expect(wrapper.exists('[data-testid="bp-Filmstrip-crawler"]')).toBe(false); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.