UNPKG

@cmchu/screen-full

Version:

This module provides a straightforward set of APIs to control the browser's fullscreen functionality and listen for changes and errors in the fullscreen state. By encapsulating the native fullscreen APIs, it enables developers to uniformly handle fullscre

2 lines (1 loc) 2.02 kB
(function(t,n){typeof exports=="object"&&typeof module<"u"?module.exports=n():typeof define=="function"&&define.amd?define(n):(t=typeof globalThis<"u"?globalThis:t||self,t.index=n())})(this,function(){"use strict";const t=[["requestFullscreen","exitFullscreen","fullscreenElement","fullscreenEnabled","fullscreenchange","fullscreenerror"],["webkitRequestFullscreen","webkitExitFullscreen","webkitFullscreenElement","webkitFullscreenEnabled","webkitfullscreenchange","webkitfullscreenerror"],["webkitRequestFullScreen","webkitCancelFullScreen","webkitCurrentFullScreenElement","webkitCancelFullScreen","webkitfullscreenchange","webkitfullscreenerror"],["mozRequestFullScreen","mozCancelFullScreen","mozFullScreenElement","mozFullScreenEnabled","mozfullscreenchange","mozfullscreenerror"],["msRequestFullscreen","msExitFullscreen","msFullscreenElement","msFullscreenEnabled","MSFullscreenChange","MSFullscreenError"]],n=(()=>{const e=t[0],r={};for(const l of t)if((l==null?void 0:l[1])in document){for(const[g,w]of l.entries())r[e[g]]=w;return r}})(),a="fullscreenchange",b="fullscreenerror",h=27,k=122;function c(e,r,l=!1){document.addEventListener(e,r,l)}function i(){return!!document[n.fullscreenEnabled]}function s(){return!!document[n.fullscreenElement]}function f(){return document[n.fullscreenElement]}function o(e,r){const l=e||document.documentElement;try{return l[n.requestFullscreen](r)}catch(m){console.error("请求全屏失败",m)}}function u(){try{return document[n.exitFullscreen]()}catch(e){console.error("退出全屏失败",e)}}function E(e){return c(a,e)}function d(e){return c(b,e)}const F={request:o,requestFullscreen:o,exit:u,exitFullscreen:u,onChange:E,fullscreenchange:E,fullscreenEnabled:i,isFullscreen:s,fullscreenElement:f,onError:d,fullscreenerror:d};Object.defineProperties(F,{isEnabled:{enumerable:!0,get:i},isFull:{get:s},fullElement:{enumerable:!0,get:f}});function S(e){e.keyCode===k&&(e.stopPropagation(),e.preventDefault(),s()?u():o()),e.keyCode===h&&u()}return c("keydown",S),F});