-
Notifications
You must be signed in to change notification settings - Fork 316
feat: implementing asset player with media-viewer #5257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c50ee2e
feat: implementing asset player with media-viewer
albert-0229 5e1e52f
fix: init iframe-resizer with sendMessage
albert-0229 1134391
chore: cspell
albert-0229 b9d63f3
chore: eslinter
albert-0229 fab86e7
Merge branch 'develop' into feat/asset-player
albert-0229 4e1d09a
fix: autoplay and allow full screen when source be video
albert-0229 c0bfcb0
chore: clean userless default option
albert-0229 097da15
chore: add muted option
albert-0229 7356b96
Merge branch 'develop' into feat/asset-player
albert-0229 551f296
Merge branch 'develop' into feat/asset-player
albert-0229 b6d9bcf
feat: add fallback and load placeholder
albert-0229 0b77a38
Merge branch 'develop' into feat/asset-player
albert-0229 d6c3f76
fix: set url be optional
albert-0229 c356638
Merge branch 'develop' into feat/asset-player
albert-0229 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,16 @@ | ||
| import { createIcon } from '../utils' | ||
|
|
||
| export const AssetLoadingIcon = createIcon( | ||
| 'AssetLoadingIcon', | ||
| <g fill="none"> | ||
| <path d="M18 36c9.941 0 18-8.059 18-18S27.941 0 18 0 0 8.059 0 18s8.059 18 18 18Z" fill="#1C68F3" /> | ||
| <path | ||
| fillRule="evenodd" | ||
| clipRule="evenodd" | ||
| d="M28.864 13.575v5.978H9.86c.83 3.627 4.123 6.336 8.058 6.336a8.273 8.273 0 0 0 7.415-4.543h3.531v5.021c0 1.189-.979 2.152-2.186 2.152H9.186C7.98 28.52 7 27.556 7 26.367V13.575h21.864Zm-5.628 7.771a6.46 6.46 0 0 1-5.318 2.764 6.46 6.46 0 0 1-5.317-2.764h10.635Zm-9.98-6.336c-1.714 0-3.131 1.247-3.367 2.87h1.864c.205-.625.8-1.077 1.503-1.077.702 0 1.298.452 1.503 1.076h1.863c-.235-1.622-1.653-2.869-3.366-2.869Zm9.353 0c-1.714 0-3.131 1.247-3.367 2.87h1.864c.205-.625.8-1.077 1.503-1.077.702 0 1.298.452 1.503 1.076h1.863c-.235-1.622-1.653-2.869-3.366-2.869ZM26.678 7c1.207 0 2.186.963 2.186 2.152v2.63H7v-2.63C7 7.963 7.979 7 9.186 7h17.492Z" | ||
| fill="#fff" | ||
| /> | ||
| <ellipse cx="18" cy="48" rx="11.5" ry="4" fill="#003EAE" /> | ||
| </g>, | ||
| '0 0 36 52', | ||
| ) |
This file contains hidden or 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 hidden or 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 hidden or 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
136 changes: 136 additions & 0 deletions
136
packages/shared/src/UI/components/AssetPlayer/index.tsx
This file contains hidden or 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,136 @@ | ||
| import { memo, useRef, useCallback, useState } from 'react' | ||
| import IframeResizer, { IFrameComponent } from 'iframe-resizer-react' | ||
| import { mediaViewerUrl } from '../../../constants' | ||
| import { useTimeoutFn, useUpdateEffect } from 'react-use' | ||
| import { makeStyles, useStylesExtends } from '@masknet/theme' | ||
| import { Box, SvgIconProps } from '@mui/material' | ||
| import { AssetLoadingIcon, MaskGreyIcon } from '@masknet/icons' | ||
|
|
||
| interface AssetPlayerProps | ||
| extends withClasses<'errorPlaceholder' | 'errorIcon' | 'loadingPlaceholder' | 'loadingIcon'> { | ||
| url?: string | ||
| type?: string | ||
| options?: { | ||
| autoPlay?: boolean | ||
| controls?: boolean | ||
| playsInline?: boolean | ||
| loop?: boolean | ||
| muted?: boolean | ||
| } | ||
| iconProps?: SvgIconProps | ||
| } | ||
| const useStyles = makeStyles()((theme) => ({ | ||
| errorPlaceholder: { | ||
| backgroundColor: theme.palette.background.default, | ||
| display: 'flex', | ||
| justifyContent: 'center', | ||
| alignItems: 'center', | ||
| borderRadius: 12, | ||
| width: '100%', | ||
| }, | ||
| errorIcon: { | ||
| width: 36, | ||
| height: 36, | ||
| }, | ||
| loadingPlaceholder: { | ||
| display: 'flex', | ||
| justifyContent: 'center', | ||
| alignItems: 'center', | ||
| width: '100%', | ||
| }, | ||
| loadingIcon: { | ||
| width: 36, | ||
| height: 52, | ||
| }, | ||
| })) | ||
|
|
||
| enum AssetPlayerState { | ||
| LOADING = 0, | ||
| INIT = 1, | ||
| NORMAL = 2, | ||
| ERROR = 3, | ||
| } | ||
|
|
||
| const TIMEOUT = 10000 | ||
|
|
||
| export const AssetPlayer = memo<AssetPlayerProps>(({ url, type, options, iconProps, ...props }) => { | ||
| const ref = useRef<IFrameComponent | null>(null) | ||
| const classes = useStylesExtends(useStyles(), props) | ||
| const [playerState, setPlayerState] = useState(url ? AssetPlayerState.LOADING : AssetPlayerState.ERROR) | ||
|
|
||
| //#region If onResized is not triggered within the specified time, set player state to error | ||
| const [, cancel, reset] = useTimeoutFn(() => { | ||
| if (playerState !== AssetPlayerState.NORMAL) { | ||
| setPlayerState(AssetPlayerState.ERROR) | ||
| } | ||
| }, TIMEOUT) | ||
| //#endregion | ||
|
|
||
| //#region setup iframe when url and options be changed | ||
| const setIframe = useCallback(() => { | ||
| // if iframe isn't be init or the load error has been existed | ||
| if (!ref.current || playerState === AssetPlayerState.ERROR) return | ||
| if (!url) { | ||
| setPlayerState(AssetPlayerState.ERROR) | ||
| return | ||
| } | ||
| if (playerState === AssetPlayerState.INIT || playerState === AssetPlayerState.NORMAL) { | ||
| reset() | ||
| ref.current.iFrameResizer.sendMessage({ | ||
| url, | ||
| type, | ||
| ...options, | ||
| }) | ||
| return | ||
| } | ||
| }, [url, type, options, playerState]) | ||
| //endregion | ||
|
|
||
| //#region resource loaded error | ||
| const onMessage = useCallback(({ message }: { message: { name: string } }) => { | ||
| if (message?.name === 'Error') { | ||
| setPlayerState(AssetPlayerState.ERROR) | ||
| } | ||
| }, []) | ||
| //#endregion | ||
|
|
||
| useUpdateEffect(() => { | ||
| setIframe() | ||
| }, [setIframe]) | ||
|
|
||
| return ( | ||
| <> | ||
| <Box | ||
| className={ | ||
| playerState === AssetPlayerState.ERROR ? classes.errorPlaceholder : classes.loadingPlaceholder | ||
| } | ||
| style={{ display: playerState === AssetPlayerState.NORMAL ? 'none' : undefined }}> | ||
| {playerState === AssetPlayerState.ERROR ? ( | ||
| <MaskGreyIcon className={classes.errorIcon} viewBox="0 0 36 36" {...iconProps} /> | ||
| ) : ( | ||
| <AssetLoadingIcon className={classes.loadingIcon} /> | ||
| )} | ||
| </Box> | ||
| <IframeResizer | ||
| src={mediaViewerUrl} | ||
| onInit={(iframe: IFrameComponent) => { | ||
| ref.current = iframe | ||
| setPlayerState(AssetPlayerState.INIT) | ||
| setIframe() | ||
| }} | ||
| onResized={({ type }) => { | ||
| if (type !== 'init') { | ||
| cancel() | ||
| setPlayerState(AssetPlayerState.NORMAL) | ||
| } | ||
| }} | ||
| style={{ width: playerState !== AssetPlayerState.NORMAL ? 0 : undefined }} | ||
| checkOrigin={false} | ||
| onMessage={onMessage} | ||
| frameBorder="0" | ||
| allow="autoplay" | ||
| allowFullScreen | ||
| /> | ||
| </> | ||
| ) | ||
| }) |
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.