-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change fullscreen hook implementation to use native fullscreen api in…
…stead of screenfull package full screenD remove data-id add null check for fullscreen fix overflow for logs make requested changes support older browser using prefixes make requesteddd changes changes small fix
- Loading branch information
Showing
8 changed files
with
787 additions
and
118 deletions.
There are no files selected for viewing
115 changes: 90 additions & 25 deletions
115
frontend/packages/console-shared/src/hooks/fullscreen.ts
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 |
---|---|---|
@@ -1,35 +1,100 @@ | ||
import * as React from 'react'; | ||
import * as screenfull from 'screenfull'; | ||
|
||
export const useFullscreen = (id: string): [boolean, (node) => void] => { | ||
const [isFullScreen, setIsFullScreen] = React.useState<boolean>(false); | ||
type FullScreenAPI = { | ||
requestFullscreen: string; | ||
exitFullscreen: string; | ||
fullscreenElement: string; | ||
fullscreenEnabled: string; | ||
fullscreenchange: string; | ||
fullscreenerror: string; | ||
}; | ||
|
||
const fullScreenToggleCallback = React.useCallback((node) => { | ||
if (node && screenfull.enabled) { | ||
screenfull.toggle(node); | ||
} | ||
const spec: FullScreenAPI = { | ||
requestFullscreen: 'requestFullscreen', | ||
exitFullscreen: 'exitFullscreen', | ||
fullscreenElement: 'fullscreenElement', | ||
fullscreenEnabled: 'fullscreenEnabled', | ||
fullscreenchange: 'fullscreenchange', | ||
fullscreenerror: 'fullscreenerror', | ||
}; | ||
|
||
const moz: FullScreenAPI = { | ||
requestFullscreen: 'mozRequestFullscreen', | ||
exitFullscreen: 'mozExitFullscreen', | ||
fullscreenElement: 'mozFullscreenElement', | ||
fullscreenEnabled: 'mozFullscreenEnabled', | ||
fullscreenchange: 'mozfullscreenchange', | ||
fullscreenerror: 'mozfullscreenerror', | ||
}; | ||
|
||
const webkit: FullScreenAPI = { | ||
requestFullscreen: 'webkitRequestFullscreen', | ||
exitFullscreen: 'webkitExitFullscreen', | ||
fullscreenElement: 'webkitFullscreenElement', | ||
fullscreenEnabled: 'webkitFullscreenEnabled', | ||
fullscreenchange: 'webkitfullscreenchange', | ||
fullscreenerror: 'webkitfullscreenerror', | ||
}; | ||
|
||
const ms: FullScreenAPI = { | ||
requestFullscreen: 'msRequestFullscreen', | ||
exitFullscreen: 'msExitFullscreen', | ||
fullscreenElement: 'msFullscreenElement', | ||
fullscreenEnabled: 'msFullscreenEnabled', | ||
fullscreenchange: 'msfullscreenchange', | ||
fullscreenerror: 'msfullscreenerror', | ||
}; | ||
|
||
const allPrefixes: FullScreenAPI[] = [spec, moz, webkit, ms]; | ||
|
||
const nativeAPI: FullScreenAPI = (function(doc) { | ||
return allPrefixes.find((x: FullScreenAPI) => !!doc[x.fullscreenEnabled]); | ||
})(document); | ||
|
||
export const useFullscreen = <T extends HTMLElement>(): [ | ||
boolean, | ||
(node: T) => void, | ||
() => void, | ||
boolean, | ||
] => { | ||
const [isFullscreen, setIsFullscreen] = React.useState<boolean>(false); | ||
const fullscreenRef = React.useRef<boolean>(isFullscreen); | ||
fullscreenRef.current = isFullscreen; | ||
const elementRef = React.useRef<any>(); | ||
|
||
const listener = React.useCallback((event) => { | ||
setIsFullscreen(document[nativeAPI.fullscreenElement] === event.target); | ||
}, []); | ||
|
||
React.useEffect(() => { | ||
if (screenfull.enabled) { | ||
screenfull.on('change', (event) => { | ||
if (event.target.getAttribute('data-fullscreen-id') === id) { | ||
setIsFullScreen(screenfull.isFullscreen); | ||
const targetCallbackRef = React.useCallback( | ||
(node: T) => { | ||
if (document[nativeAPI.fullscreenEnabled]) { | ||
if (elementRef.current && elementRef.current !== node) { | ||
elementRef.current.removeEventListener(nativeAPI.fullscreenchange, listener); | ||
elementRef.current.removeEventListener(nativeAPI.fullscreenerror, listener); | ||
} | ||
}); | ||
screenfull.on('error', (event) => { | ||
if (event.target.getAttribute('data-fullscreen-id') === id) { | ||
setIsFullScreen(false); | ||
if (node != null) { | ||
elementRef.current = node; | ||
node.addEventListener(nativeAPI.fullscreenchange, listener); | ||
node.addEventListener(nativeAPI.fullscreenerror, listener); | ||
} | ||
}); | ||
} | ||
return () => { | ||
if (screenfull.enabled) { | ||
screenfull.off('change'); | ||
screenfull.off('error'); | ||
} | ||
}; | ||
}, [id]); | ||
}, | ||
[listener], | ||
); | ||
|
||
const fullscreenToggleCallback = React.useCallback(() => { | ||
if (elementRef.current && document[nativeAPI.fullscreenEnabled]) { | ||
fullscreenRef.current | ||
? document[nativeAPI.exitFullscreen]() | ||
: elementRef.current[nativeAPI.requestFullscreen](); | ||
} | ||
}, []); | ||
|
||
return [isFullScreen, fullScreenToggleCallback]; | ||
return [ | ||
isFullscreen, | ||
targetCallbackRef, | ||
fullscreenToggleCallback, | ||
document[nativeAPI.fullscreenEnabled], | ||
]; | ||
}; |
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
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
20 changes: 20 additions & 0 deletions
20
frontend/packages/dev-console/src/components/pipelineruns/logs/LogsWrapperComponent.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,20 @@ | ||
import * as React from 'react'; | ||
import * as _ from 'lodash'; | ||
import { MultiStreamLogs } from './MultiStreamLogs'; | ||
import { FirehoseResult } from '@console/internal/components/utils'; | ||
import { PodKind } from '@console/internal/module/k8s'; | ||
|
||
type LogsWrapperComponentProps = { | ||
obj?: FirehoseResult<PodKind>; | ||
taskName: string; | ||
}; | ||
|
||
const LogsWrapperComponent: React.FC<LogsWrapperComponentProps> = ({ obj, taskName }) => { | ||
const ref = React.useRef(obj?.data); | ||
if (!_.isEmpty(obj?.data)) { | ||
ref.current = obj.data; | ||
} | ||
return obj ? <MultiStreamLogs taskName={taskName} resource={ref.current} /> : null; | ||
}; | ||
|
||
export default LogsWrapperComponent; |
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.