Spaces:
Running
Running
File size: 7,514 Bytes
a9881b5 fa92696 a9881b5 fa92696 a9881b5 0aa7811 a9881b5 fa92696 a9881b5 fa92696 a9881b5 fa92696 a9881b5 fa92696 a9881b5 fa92696 a9881b5 6972ed9 a9881b5 fa92696 a9881b5 fa92696 a9881b5 fa92696 a9881b5 0aa7811 a9881b5 9e5dc6f a9881b5 f469b12 a9881b5 175664b a9881b5 0aa7811 a9881b5 0aa7811 a9881b5 4a72230 a9881b5 0aa7811 a9881b5 0aa7811 758df19 0aa7811 c9965c1 758df19 a9881b5 408e343 a9881b5 fa92696 758df19 408e343 a9881b5 0aa7811 a9881b5 408e343 a9881b5 0aa7811 408e343 0aa7811 a9881b5 0aa7811 a9881b5 0aa7811 a9881b5 0aa7811 2a83384 0aa7811 2a83384 0aa7811 2a83384 8bc1807 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | // fullscreen.js
(function () {
// βββ 1. Localiser la balise <script> ββββββββββββββββββββββββββββββββββββββββ
const scriptTag = document.currentScript || (function () {
const all = document.getElementsByTagName('script');
for (let i = all.length - 1; i >= 0; i--) {
if (all[i].src && all[i].src.includes('fullscreen.js')) return all[i];
}
return all[all.length - 1];
})();
const playcanvasUrl = scriptTag.getAttribute('data-src');
if (!playcanvasUrl) return;
const isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
const id = Math.random().toString(36).substr(2, 8);
// βββ 4. Calcul des ratios (Desktop & Mobile Portrait) ββββββββββββββββββββββββ
function computeAspectPadding(aspectStr) {
if (!aspectStr) return null;
if (aspectStr.includes(':')) {
const [w, h] = aspectStr.split(':').map(Number);
return (w > 0 && h > 0) ? (h / w) * 100 + '%' : null;
}
const v = parseFloat(aspectStr);
return (v > 0) ? (100 / v) + '%' : null;
}
const desktopPadding = computeAspectPadding(scriptTag.getAttribute('data-aspect')) || '56.25%';
const mobilePadding = computeAspectPadding(scriptTag.getAttribute('data-aspect-mobile')) || desktopPadding;
// βββ 5. Injection du CSS avec Media Queries ββββββββββββββββββββββββββββββββββ
const style = document.createElement('style');
style.textContent = `
.pc-embed-wrapper-${id} {
position: relative;
width: 100%;
height: 0;
overflow: hidden;
background: #000;
box-sizing: border-box;
/* Ratio par dΓ©faut (Paysage / Desktop) */
padding-bottom: ${desktopPadding};
}
/* Ratio Mobile Portrait : appliquΓ© uniquement si l'Γ©cran est plus haut que large */
@media (orientation: portrait) and (max-width: 768px) {
.pc-embed-wrapper-${id} {
padding-bottom: ${mobilePadding};
}
}
.pc-embed-wrapper-${id}.fake-fullscreen {
position: fixed !important;
top: 0 !important;
left: 0 !important;
width: 100vw !important;
height: 100vh !important;
height: 100dvh !important;
max-width: 100vw !important;
max-height: 100dvh !important;
padding-bottom: 0 !important;
margin: 0 !important;
z-index: 99999;
}
.pc-embed-inner-${id} {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
}
.pc-embed-inner-${id} iframe {
width: 100%; height: 100%;
border: none;
display: block;
}
.pc-fs-btn-${id} {
position: absolute;
top: 10px;
right: 10px;
z-index: 10;
width: 36px;
height: 36px;
border-radius: 50%;
border: none;
background: #2E2E2EB3;
color: #fff;
font-size: 18px;
line-height: 36px;
text-align: center;
cursor: pointer;
user-select: none;
}
`;
document.head.appendChild(style);
// βββ 6. Construction du DOM ββββββββββββββββββββββββββββββββββββββββββββββββββ
const wrapper = document.createElement('div');
wrapper.className = `pc-embed-wrapper-${id}`;
const inner = document.createElement('div');
inner.className = `pc-embed-inner-${id}`;
const iframe = document.createElement('iframe');
const urlObj = new URL(playcanvasUrl);
urlObj.searchParams.set('overlay', 'false');
iframe.src = urlObj.href;
iframe.setAttribute('allowfullscreen', '');
iframe.setAttribute('allow', 'autoplay; fullscreen');
iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-pointer-lock allow-popups allow-forms');
const fsBtn = document.createElement('button');
fsBtn.className = `pc-fs-btn-${id}`;
fsBtn.textContent = 'β±';
inner.appendChild(iframe);
wrapper.appendChild(inner);
wrapper.appendChild(fsBtn);
scriptTag.parentNode.insertBefore(wrapper, scriptTag.nextSibling);
// βββ 7. Γtat & Helper ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
let isFullscreen = false;
let savedParent = null;
let savedNextSibling = null;
function getHeightUnit() {
return (CSS && CSS.supports && CSS.supports('height', '100dvh')) ? '100dvh' : '100vh';
}
// βββ 8. Styles Plein Γcran βββββββββββββββββββββββββββββββββββββββββββββββββββ
function applyFullscreenStyles() {
const h = isIOS ? getHeightUnit() : '100vh';
wrapper.style.position = 'fixed';
wrapper.style.top = '0';
wrapper.style.left = '0';
wrapper.style.width = '100vw';
wrapper.style.height = h;
wrapper.style.maxHeight = h;
wrapper.style.paddingBottom = '0';
wrapper.style.margin = '0';
wrapper.style.zIndex = '99999';
wrapper.classList.add('fake-fullscreen');
fsBtn.textContent = 'β²';
isFullscreen = true;
}
function applyFakeFullscreenStyles() {
savedParent = wrapper.parentNode;
savedNextSibling = wrapper.nextSibling;
document.body.appendChild(wrapper);
applyFullscreenStyles();
}
function restoreStyles() {
wrapper.style.cssText = '';
// Le CSS via les Media Queries reprendra le dessus automatiquement ici
wrapper.classList.remove('fake-fullscreen');
fsBtn.textContent = 'β±';
isFullscreen = false;
if (savedParent) {
savedParent.insertBefore(wrapper, savedNextSibling);
savedParent = null;
savedNextSibling = null;
}
}
// βββ 9. Gestionnaires d'Γ©vΓ©nements βββββββββββββββββββββββββββββββββββββββββββ
function enterFullscreen() {
if (isIOS) {
applyFakeFullscreenStyles();
document.body.style.overflow = 'hidden';
} else {
const el = wrapper;
const req = el.requestFullscreen || el.webkitRequestFullscreen || el.mozRequestFullScreen || el.msRequestFullscreen;
if (req) {
req.call(el).catch(() => {
applyFakeFullscreenStyles();
document.body.style.overflow = 'hidden';
});
} else {
applyFakeFullscreenStyles();
document.body.style.overflow = 'hidden';
}
}
}
function exitFullscreen() {
if (document.fullscreenElement || document.webkitFullscreenElement) {
(document.exitFullscreen || document.webkitExitFullscreen || function(){}).call(document);
}
restoreStyles();
document.body.style.overflow = '';
}
fsBtn.addEventListener('click', (e) => {
e.stopPropagation();
isFullscreen ? exitFullscreen() : enterFullscreen();
});
document.addEventListener('fullscreenchange', () => {
const fsEl = document.fullscreenElement || document.webkitFullscreenElement;
if (!fsEl && isFullscreen) exitFullscreen();
else if (fsEl === wrapper && !isFullscreen) applyFullscreenStyles();
});
window.addEventListener('resize', () => {
if (isFullscreen) {
const h = isIOS ? getHeightUnit() : '100vh';
wrapper.style.height = h;
wrapper.style.maxHeight = h;
}
});
})(); |