From 825197b4144161d5bba6cba08336a407a04ef708 Mon Sep 17 00:00:00 2001 From: fantasy525 <1056726491@qq.com> Date: Thu, 23 Sep 2021 18:33:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E5=A4=8D=20UI=20=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E4=BB=A5=E5=8F=8A=E5=A2=9E=E5=8A=A0=20shouldShowPageF?= =?UTF-8?q?ullScreenButton=20prop=20=20(#219)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: zuoxiaofei --- example/src/MP4Page.tsx | 22 +++++++++--- packages/griffith/package.json | 2 +- .../src/components/Controller.styles.ts | 1 - .../griffith/src/components/Controller.tsx | 36 +++++++++++++++---- packages/griffith/src/components/Player.tsx | 21 +++++++++-- .../src/components/Timeline.styles.ts | 4 +-- .../src/components/TranslatedText.tsx | 13 +++++-- packages/griffith/src/components/Video.tsx | 2 ++ .../MinimalTimeline.spec.tsx.snap | 4 +-- .../items/PageFullScreenButtonItem.tsx | 2 +- .../src/components/items/QualityMenuItem.tsx | 7 +++- .../__snapshots__/TimelineItem.spec.tsx.snap | 2 +- packages/griffith/src/constants/KeyCode.ts | 1 + packages/griffith/src/constants/locales.ts | 5 +-- 14 files changed, 94 insertions(+), 28 deletions(-) diff --git a/example/src/MP4Page.tsx b/example/src/MP4Page.tsx index 7592331c..8c015332 100644 --- a/example/src/MP4Page.tsx +++ b/example/src/MP4Page.tsx @@ -73,13 +73,25 @@ const App = () => { {...props} localeConfig={{ 'zh-Hans': { - 'quality-ld': '流畅 360P', - 'quality-sd': '清晰 480P', - 'quality-hd': '高清 720P', - 'quality-fhd': '超清 1080P', + 'quality-ld': { + text: '流畅 360P', + narrow: '流畅', + }, + 'quality-sd': { + text: '清晰 480P', + narrow: '清晰', + }, + 'quality-hd': { + text: '高清 720P', + narrow: '高清', + }, + 'quality-fhd': { + text: '超清 1080P', + narrow: '超清', + }, }, }} - locale={'ja'} + locale={'zh-Hans'} messageContextRef={messageContextRef} onEvent={logEvent} > diff --git a/packages/griffith/package.json b/packages/griffith/package.json index dd1cfbec..ed6f27d6 100644 --- a/packages/griffith/package.json +++ b/packages/griffith/package.json @@ -1,6 +1,6 @@ { "name": "griffith", - "version": "1.18.4", + "version": "1.18.5", "description": "Zhihu Video Player", "homepage": "https://github.com/zhihu/griffith/tree/master/packages/griffith", "license": "MIT", diff --git a/packages/griffith/src/components/Controller.styles.ts b/packages/griffith/src/components/Controller.styles.ts index 2ef1de96..4b87fd93 100644 --- a/packages/griffith/src/components/Controller.styles.ts +++ b/packages/griffith/src/components/Controller.styles.ts @@ -2,7 +2,6 @@ import {StyleSheet} from 'aphrodite/no-important' export default StyleSheet.create({ root: { - height: '5em', padding: '0 0.5em', }, rootTop: { diff --git a/packages/griffith/src/components/Controller.tsx b/packages/griffith/src/components/Controller.tsx index fda8ebe4..ffe4f674 100644 --- a/packages/griffith/src/components/Controller.tsx +++ b/packages/griffith/src/components/Controller.tsx @@ -12,6 +12,8 @@ import VolumeItem from './items/VolumeItem' import FullScreenButtonItem from './items/FullScreenButtonItem' import PipButtonItem from './items/PipButtonItem' import styles from './Controller.styles' +import PlaybackRateMenuItem from './items/PlaybackRateMenuItem' +import PageFullScreenButtonItem from './items/PageFullScreenButtonItem' export type ToggleType = 'button' | 'keyCode' | 'video' | null @@ -33,7 +35,7 @@ type ControllerProps = { onQualityChange?: (...args: any[]) => any onVolumeChange?: (volume: number) => void onToggleFullScreen?: () => void - onTogglePageFullScreen: (...args: void[]) => void + onTogglePageFullScreen?: () => void onTogglePip?: (...args: any[]) => void onProgressDotHover?: (...args: any[]) => any onProgressDotLeave?: (...args: any[]) => any @@ -47,6 +49,7 @@ type ControllerProps = { hiddenVolumeItem?: boolean hiddenFullScreenButton?: boolean hiddenPlaybackRateItem?: boolean + shouldShowPageFullScreenButton?: boolean } type State = { @@ -66,6 +69,7 @@ class Controller extends Component { volume: 0.5, buffered: 0, isFullScreen: false, + isPageFullScreen: false, showPip: false, hiddenPlayButton: false, hiddenTimeline: false, @@ -178,7 +182,15 @@ class Controller extends Component { } handleKeyDown = (event: KeyboardEvent) => { - const {duration, currentTime, volume, show, onToggleFullScreen} = this.props + const { + duration, + currentTime, + volume, + show, + onToggleFullScreen, + onTogglePageFullScreen, + isPageFullScreen, + } = this.props let handled = true switch (event.keyCode) { case KeyCode.SPACE: @@ -194,7 +206,11 @@ class Controller extends Component { onToggleFullScreen?.() } break - + case KeyCode.ESC: + if (this.firstKey && isPageFullScreen) { + onTogglePageFullScreen?.() + } + break case KeyCode.LEFT: this.handleSeek(currentTime - 5) break @@ -287,10 +303,12 @@ class Controller extends Component { currentTime, volume, isFullScreen, + isPageFullScreen, isPip, onDragStart, onDragEnd, onToggleFullScreen, + onTogglePageFullScreen, onTogglePip, showPip, progressDots, @@ -299,7 +317,9 @@ class Controller extends Component { hiddenTime, hiddenQualityMenu, hiddenVolumeItem, + hiddenPlaybackRateItem, hiddenFullScreenButton, + shouldShowPageFullScreenButton, onProgressDotHover, onProgressDotLeave, } = this.props @@ -347,11 +367,15 @@ class Controller extends Component { )}
- {/* TODO: 倍速按钮暂时删除 */} - {/* {!hiddenPlaybackRateItem && } */} + {!hiddenPlaybackRateItem && } {!hiddenQualityMenu && } {showPip && } - {/* TODO: 网页全屏暂时删除 */} + {shouldShowPageFullScreenButton && ( + + )} {!hiddenFullScreenButton && ( { const onExit = () => { return onEvent(EVENTS.EXIT_FULLSCREEN) } - BigScreen.toggle(this.playerRef.current!, onEnter, onExit) + BigScreen?.toggle(this.playerRef.current!, onEnter, onExit) } } handleTogglePageFullScreen = () => { const {onEvent} = this.props + // 如果当前正在全屏就先关闭全屏 + if (Boolean(BigScreen.element) && !Pip.pictureInPictureElement) { + this.handleToggleFullScreen() + } if (this.state.isEnterPageFullScreen) { this.setState({isEnterPageFullScreen: false}) onEvent(EVENTS.EXIT_PAGE_FULLSCREEN) @@ -448,6 +452,11 @@ class InnerPlayer extends Component { } handleTogglePip = () => { + const {onEvent} = this.props + if (this.state.isEnterPageFullScreen) { + this.setState({isEnterPageFullScreen: false}) + onEvent(EVENTS.EXIT_PAGE_FULLSCREEN) + } Pip.toggle() } @@ -542,6 +551,7 @@ class InnerPlayer extends Component { hiddenQualityMenu, hiddenVolume, hiddenFullScreenButton, + shouldShowPageFullScreenButton, children, hiddenPlaybackRateItem, } = this.props @@ -777,6 +787,9 @@ class InnerPlayer extends Component { hiddenVolumeItem={hiddenVolume} hiddenPlaybackRateItem={hiddenPlaybackRateItem} hiddenFullScreenButton={hiddenFullScreenButton} + shouldShowPageFullScreenButton={ + shouldShowPageFullScreenButton + } />
)} @@ -817,6 +830,7 @@ const Player: React.FC = ({ initialObjectFit, locale = defaultLocale, localeConfig, + shouldShowPageFullScreenButton = false, // 默认不展示网页全屏,防止页面被嵌入到iframe时候无法达到效果 defaultQuality, defaultPlaybackRate = DEFAULT_PLAYBACK_RATE, playbackRates = DEFAULT_PLAYBACK_RATES, @@ -847,6 +861,9 @@ const Player: React.FC = ({ > { +const TranslatedText: React.FC<{ + name: LocaleConfigKey + style?: 'narrow' | 'text' // narrow 简短的文本展示,text 正常文本展示,两种展示格式满足长短文案需求 +}> = ({name, style = 'text'}): ReactElement => { return ( {(locale) => { + if (typeof locale[name] === 'object') + return ( + locale[name] as Exclude + )[style] return locale[name] }} diff --git a/packages/griffith/src/components/Video.tsx b/packages/griffith/src/components/Video.tsx index 70cdfc11..4b1837c7 100644 --- a/packages/griffith/src/components/Video.tsx +++ b/packages/griffith/src/components/Video.tsx @@ -232,6 +232,7 @@ class Video extends Component { this.applyPendingAction() if (this.isSwitchDefinition) { this.isSwitchDefinition = false + this.setRate(this.props.currentPlaybackRate) this.props.onEvent( EVENTS.CHANGE_QUALITY_SUCCESS, (this.props as any).currentQuality @@ -320,6 +321,7 @@ class Video extends Component { if (this.isSwitchDefinition) { this.isSwitchDefinition = false + this.setRate(this.props.currentPlaybackRate) this.props.onEvent(EVENTS.CHANGE_QUALITY_FAIL, this.props.currentQuality) } this.props.onError(error) diff --git a/packages/griffith/src/components/__tests__/__snapshots__/MinimalTimeline.spec.tsx.snap b/packages/griffith/src/components/__tests__/__snapshots__/MinimalTimeline.spec.tsx.snap index c5ef1aad..779e9e98 100644 --- a/packages/griffith/src/components/__tests__/__snapshots__/MinimalTimeline.spec.tsx.snap +++ b/packages/griffith/src/components/__tests__/__snapshots__/MinimalTimeline.spec.tsx.snap @@ -3,7 +3,7 @@ exports[`MinimalTimeline get MinimalTimeline component 1`] = `
@@ -26,7 +26,7 @@ exports[`MinimalTimeline get MinimalTimeline component 1`] = ` exports[`MinimalTimeline get MinimalTimeline component 2`] = `
diff --git a/packages/griffith/src/components/items/PageFullScreenButtonItem.tsx b/packages/griffith/src/components/items/PageFullScreenButtonItem.tsx index 71e6bc17..797b3068 100644 --- a/packages/griffith/src/components/items/PageFullScreenButtonItem.tsx +++ b/packages/griffith/src/components/items/PageFullScreenButtonItem.tsx @@ -11,7 +11,7 @@ const {isMobile} = ua const PageFullScreenButtonItem: React.FC<{ isFullScreen: boolean - onClick: React.HTMLAttributes['onClick'] + onClick: React.HTMLAttributes['onClick'] | (() => void) }> = ({isFullScreen, onClick}) => ( {(isFullScreenHovered) => ( diff --git a/packages/griffith/src/components/items/QualityMenuItem.tsx b/packages/griffith/src/components/items/QualityMenuItem.tsx index dfc463b7..87eca8fb 100644 --- a/packages/griffith/src/components/items/QualityMenuItem.tsx +++ b/packages/griffith/src/components/items/QualityMenuItem.tsx @@ -44,7 +44,12 @@ class QualityMenuItem extends React.PureComponent { onMouseLeave={this.handleQualityPointerLeave} > } + label={ + + } />
diff --git a/packages/griffith/src/constants/KeyCode.ts b/packages/griffith/src/constants/KeyCode.ts index 61a30335..815b1518 100644 --- a/packages/griffith/src/constants/KeyCode.ts +++ b/packages/griffith/src/constants/KeyCode.ts @@ -1,6 +1,7 @@ export default { ENTER: 13, SPACE: 32, + ESC: 27, LEFT: 37, UP: 38, RIGHT: 39, diff --git a/packages/griffith/src/constants/locales.ts b/packages/griffith/src/constants/locales.ts index 3ccff2e2..9b956761 100644 --- a/packages/griffith/src/constants/locales.ts +++ b/packages/griffith/src/constants/locales.ts @@ -14,9 +14,10 @@ const en = { 'playback-rate': 'Speed', replay: 'Replay', } - export type LocaleCode = 'en' | 'ja' | 'zh-Hans' | 'zh-Hant' -export type LocaleConfig = typeof en +export type LocaleConfig = { + [K in keyof typeof en]: {narrow: string; text: string} | string +} export type LocaleConfigKey = keyof LocaleConfig export type LocaleConfigMap = Record export type PartialLocaleConfigMap = Partial<