1- import React , { useState , useLayoutEffect , useContext } from 'react'
2- import PlayerContainer , { MessageContext , EVENTS } from 'griffith'
1+ import React , {
2+ useRef ,
3+ useState ,
4+ useLayoutEffect ,
5+ useContext ,
6+ } from 'react'
7+ import PlayerContainer , { MessageContext , ACTIONS , EVENTS } from 'griffith'
38import Logo from './Logo'
49import { logEvent } from './utils'
510
@@ -36,10 +41,11 @@ const props = {
3641 autoplay : true ,
3742 shouldObserveResize : true ,
3843 src : 'https://zhstatic.zhihu.com/cfe/griffith/zhihu2018_sd.mp4' ,
39- onEvent : logEvent ,
4044}
4145
46+ const shouldLoop = new URLSearchParams ( location . search ) . has ( 'loop' )
4247const canShowLogo = new URLSearchParams ( location . search ) . has ( 'logo' )
48+
4349/** 常规通讯方式,建议直接使用 `onEvent` 替代 */
4450const LogoListener = ( ) => {
4551 const [ isLogoVisible , setIsLogoVisible ] = useState ( false )
@@ -52,10 +58,24 @@ const LogoListener = () => {
5258 return canShowLogo && isLogoVisible ? < Logo /> : null
5359}
5460
55- const App = ( ) => (
56- < PlayerContainer { ...props } >
57- < LogoListener />
58- </ PlayerContainer >
59- )
61+ const App = ( ) => {
62+ const dispatchRef = useRef ( )
63+ return (
64+ < PlayerContainer
65+ { ...props }
66+ dispatchRef = { dispatchRef }
67+ onEvent = { ( e , data ) => {
68+ if ( shouldLoop && e === EVENTS . DOM . ENDED ) {
69+ dispatchRef . current ?.( ACTIONS . PLAYER . PLAY )
70+ }
71+ logEvent ( e , data )
72+ } }
73+ >
74+ < LogoListener />
75+ < button onClick = { ( ) => dispatchRef . current ?.( ACTIONS . PLAYER . PLAY ) } > Play</ button >
76+ < button onClick = { ( ) => dispatchRef . current ?.( ACTIONS . PLAYER . PAUSE ) } > Pause</ button >
77+ </ PlayerContainer >
78+ )
79+ }
6080
6181export default App
0 commit comments