UNPKG

ideaz-element

Version:

<p align="center"> <a href="" target="_blank" rel="noopener noreferrer"> <img src="./docs/public/logo.svg" alt="Ideaz Element" width="180" style="width: 180px;" /> </a> </p>

1 lines 7.8 kB
{"version":3,"file":"fullscreen.cjs","sources":["../../../packages/utils/fullscreen.ts"],"sourcesContent":["export interface EnhancedHTMLElement extends HTMLElement {\n webkitRequestFullScreen?: () => Promise<void>\n mozRequestFullScreen?: () => Promise<void>\n msRequestFullscreen?: () => Promise<void>\n}\n\ninterface EnhancedDocument extends Document {\n webkitExitFullscreen?: () => Promise<void>\n /**\n * @deprecated https://developer.apple.com/documentation/webkit/domdocument/1494852-webkitcancelfullscreen\n */\n webkitCancelFullScreen?: () => Promise<void>\n mozCancelFullScreen?: () => Promise<void>\n msExitFullscreen?: () => Promise<void>\n webkitFullScreenEnabled?: boolean\n mozFullScreenEnabled?: boolean\n msFullScreenEnabled?: boolean\n webkitFullscreenElement?: Element\n mozFullScreenElement?: Element\n msFullscreenElement?: Element\n}\n\nexport function isUndefined(val: unknown): val is undefined {\n return val === undefined\n}\n\n/**\n * @description Determine whether the current state of the browser allows full screen access\n */\nexport function isFullscreenEnabled(): boolean {\n return !!(\n (document as EnhancedDocument).fullscreenEnabled\n || (document as EnhancedDocument).webkitFullScreenEnabled\n || (document as EnhancedDocument).mozFullScreenEnabled\n || (document as EnhancedDocument).msFullScreenEnabled\n )\n}\n\n/**\n * @description Get Full Screen Elements\n */\nexport function getFullscreenElement(): Element | null {\n const doc: EnhancedDocument = document\n return doc.fullscreenElement || doc.webkitFullscreenElement || doc.mozFullScreenElement || doc.msFullscreenElement || null\n}\n\n/**\n * @description Determine whether the current state is full screen\n */\nexport function isFullscreen(): boolean {\n return !!getFullscreenElement() || window.innerHeight === window.screen.height\n}\n\n/**\n * 退出全屏\n * https://developer.mozilla.org/en-US/docs/Web/API/Document/exitFullscreen\n */\nexport async function exitFullscreen() {\n const doc: EnhancedDocument = document\n try {\n if (doc.exitFullscreen)\n await doc.exitFullscreen()\n\n else if (doc.webkitExitFullscreen)\n await doc.webkitExitFullscreen()\n\n else if (doc.webkitCancelFullScreen)\n await doc.webkitCancelFullScreen()\n\n else if (doc.mozCancelFullScreen)\n await doc.mozCancelFullScreen()\n\n else if (doc.msExitFullscreen)\n await doc.msExitFullscreen()\n\n else\n throw new Error('该浏览器不支持全屏API')\n }\n catch (err) {\n console.error(err)\n }\n}\n\n/**\n * Enter full screen\n * https://developer.mozilla.org/zh-CN/docs/Web/API/Element/requestFullScreen\n * There is a problem with top layer stacking. If you want to avoid the problem caused by stacking order,\n * you need to manually determine the full screen status. If the current state is already full screen, you can exit full screen first and then let the target element enter full screen\n * @param {EnhancedHTMLElement} [element=document.body] - Full screen target element, default to body\n * @param {FullscreenOptions} options - Full screen options\n */\nexport async function enterFullscreen(element: EnhancedHTMLElement = document.body, options?: FullscreenOptions) {\n try {\n if (element.requestFullscreen)\n await element.requestFullscreen(options)\n\n else if (element.webkitRequestFullScreen)\n await element.webkitRequestFullScreen()\n\n else if (element.mozRequestFullScreen)\n await element.mozRequestFullScreen()\n\n else if (element.msRequestFullscreen)\n await element.msRequestFullscreen()\n\n else\n throw new Error('该浏览器不支持全屏API')\n }\n catch (err) {\n console.error(err)\n }\n}\n\n/**\n * @description Monitor full screen change events\n * @param {Function} callback\n * @param {boolean|AddEventListenerOptions} options Listening Event Options\n */\nexport function listenFullscreen(callback: EventListener, options?: boolean | AddEventListenerOptions): void {\n const doc: EnhancedDocument = document\n if (!isUndefined(doc.exitFullscreen))\n doc.addEventListener('fullscreenchange', callback, options)\n\n else if (doc.webkitExitFullscreen)\n doc.addEventListener('webkitfullscreenchange', callback, options)\n\n else if (doc.mozCancelFullScreen)\n doc.addEventListener('mozfullscreenchange', callback, options)\n\n else if (doc.msExitFullscreen)\n doc.addEventListener('MSFullscreenChange', callback, options)\n\n else\n throw new Error('该浏览器不支持全屏API')\n}\n\n/**\n * @description Remove monitoring for full screen change events\n * @param {Function} callback\n * @param {boolean|EventListenerOptions} options Listening Event Options\n */\nexport function unlistenFullscreen(callback: EventListener, options?: boolean | EventListenerOptions): void {\n const doc: EnhancedDocument = document\n if (!isUndefined(doc.exitFullscreen))\n doc.removeEventListener('fullscreenchange', callback, options)\n\n else if (doc.webkitExitFullscreen)\n doc.removeEventListener('webkitfullscreenchange', callback, options)\n\n else if (doc.mozCancelFullScreen)\n doc.removeEventListener('mozfullscreenchange', callback, options)\n\n else if (doc.msExitFullscreen)\n doc.removeEventListener('MSFullscreenChange', callback, options)\n\n else\n throw new Error('该浏览器不支持全屏API')\n}\n\n/**\n * Block the default behavior of the F11 button and call enter/exit full screen based on the current full screen state,\n * Resolve the issue of inconsistent status when entering full screen through both F11 button and API methods.\n */\nexport function patchF11DefaultAction(): void {\n window.addEventListener('keydown', (e) => {\n // https://w3c.github.io/uievents-code/\n if (e.code === 'F11') {\n e.preventDefault()\n if (isFullscreen())\n exitFullscreen()\n\n else\n enterFullscreen()\n }\n })\n}\n"],"names":["isUndefined","val","isFullscreenEnabled","getFullscreenElement","doc","isFullscreen","exitFullscreen","err","enterFullscreen","element","options","listenFullscreen","callback","unlistenFullscreen","patchF11DefaultAction"],"mappings":"gFAsBO,SAAAA,EAAAC,EAAA,CACL,OAAAA,IAAA,MACF,CAKO,SAAAC,GAAA,CACL,MAAA,CAAA,EAAA,SAAA,mBAAA,SAAA,yBAAA,SAAA,sBAAA,SAAA,oBAMF,CAKO,SAAAC,GAAA,CACL,MAAAC,EAAA,SACA,OAAAA,EAAA,mBAAAA,EAAA,yBAAAA,EAAA,sBAAAA,EAAA,qBAAA,IACF,CAKO,SAAAC,GAAA,CACL,MAAA,CAAA,CAAAF,EAAA,GAAA,OAAA,cAAA,OAAA,OAAA,MACF,CAMA,eAAAG,GAAA,CACE,MAAAF,EAAA,SACA,GAAA,CACE,GAAAA,EAAA,eACE,MAAAA,EAAA,eAAA,UAAyBA,EAAA,qBAGzB,MAAAA,EAAA,qBAAA,UAA+BA,EAAA,uBAG/B,MAAAA,EAAA,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