fullscreen-engine 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +232 -2
- package/bin/fullscreen-engine.js +162 -0
- package/dist/adapters/electron.cjs +249 -0
- package/dist/adapters/electron.cjs.map +1 -0
- package/dist/adapters/electron.esm.js +247 -0
- package/dist/adapters/electron.esm.js.map +1 -0
- package/dist/adapters/puppeteer.cjs +125 -0
- package/dist/adapters/puppeteer.cjs.map +1 -0
- package/dist/adapters/puppeteer.esm.js +123 -0
- package/dist/adapters/puppeteer.esm.js.map +1 -0
- package/dist/adapters/ssr.cjs +31 -0
- package/dist/adapters/ssr.cjs.map +1 -0
- package/dist/adapters/ssr.esm.js +29 -0
- package/dist/adapters/ssr.esm.js.map +1 -0
- package/dist/config.cjs +1789 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.esm.js +1783 -0
- package/dist/config.esm.js.map +1 -0
- package/dist/fullscreen.esm.js +1673 -0
- package/dist/fullscreen.esm.js.map +1 -0
- package/dist/fullscreen.esm.min.js +3 -0
- package/dist/fullscreen.esm.min.js.map +1 -0
- package/dist/fullscreen.iife.js +1689 -0
- package/dist/fullscreen.iife.js.map +1 -0
- package/dist/fullscreen.umd.cjs +1690 -0
- package/dist/fullscreen.umd.cjs.map +1 -0
- package/dist/fullscreen.umd.min.cjs +3 -0
- package/dist/fullscreen.umd.min.cjs.map +1 -0
- package/package.json +82 -4
- package/src/adapters/electron.js +180 -0
- package/src/adapters/puppeteer.js +119 -0
- package/src/adapters/ssr.js +25 -0
- package/src/config-loader.js +114 -0
- package/src/config.js +180 -0
- package/src/core.js +524 -0
- package/src/events.js +63 -0
- package/src/fallback.js +136 -0
- package/src/index.js +23 -0
- package/src/triggers.js +282 -0
- package/src/utils.js +67 -0
- package/src/vendor.js +118 -0
- package/types/config.d.ts +40 -0
- package/types/electron.d.ts +45 -0
- package/types/index.d.ts +161 -0
- package/types/puppeteer.d.ts +23 -0
- package/types/ssr.d.ts +22 -0
|
@@ -0,0 +1,1783 @@
|
|
|
1
|
+
/*! fullscreen-engine v1.1.0 | MIT */
|
|
2
|
+
import { existsSync, statSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { resolve, extname } from 'node:path';
|
|
5
|
+
import { pathToFileURL } from 'node:url';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Tiny event emitter. No dependencies.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
class Emitter {
|
|
12
|
+
#listeners = new Map();
|
|
13
|
+
|
|
14
|
+
on(event, cb) {
|
|
15
|
+
if (typeof cb !== 'function') throw new TypeError('listener must be a function');
|
|
16
|
+
if (!this.#listeners.has(event)) this.#listeners.set(event, new Set());
|
|
17
|
+
this.#listeners.get(event).add(cb);
|
|
18
|
+
return () => this.off(event, cb);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
once(event, cb) {
|
|
22
|
+
if (typeof cb !== 'function') throw new TypeError('listener must be a function');
|
|
23
|
+
const wrapper = (...args) => {
|
|
24
|
+
this.off(event, wrapper);
|
|
25
|
+
cb(...args);
|
|
26
|
+
};
|
|
27
|
+
wrapper._original = cb;
|
|
28
|
+
return this.on(event, wrapper);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
off(event, cb) {
|
|
32
|
+
const set = this.#listeners.get(event);
|
|
33
|
+
if (!set) return;
|
|
34
|
+
if (set.has(cb)) {
|
|
35
|
+
set.delete(cb);
|
|
36
|
+
} else {
|
|
37
|
+
for (const fn of set) {
|
|
38
|
+
if (fn._original === cb) {
|
|
39
|
+
set.delete(fn);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (set.size === 0) this.#listeners.delete(event);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
emit(event, ...args) {
|
|
48
|
+
const set = this.#listeners.get(event);
|
|
49
|
+
if (!set) return;
|
|
50
|
+
// copy so listeners can safely remove themselves
|
|
51
|
+
for (const cb of Array.from(set)) {
|
|
52
|
+
try {
|
|
53
|
+
cb(...args);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
// don't break other listeners
|
|
56
|
+
// eslint-disable-next-line no-console
|
|
57
|
+
console.error(`[fullscreen-engine] listener error on "${event}":`, err);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
clear() {
|
|
63
|
+
this.#listeners.clear();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
listenerCount(event) {
|
|
67
|
+
return this.#listeners.get(event)?.size ?? 0;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Small, dependency-free helpers.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
function isBrowser() {
|
|
76
|
+
return (
|
|
77
|
+
typeof window !== 'undefined' &&
|
|
78
|
+
typeof document !== 'undefined' &&
|
|
79
|
+
typeof document.createElement === 'function'
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isNode() {
|
|
84
|
+
return (
|
|
85
|
+
typeof process !== 'undefined' &&
|
|
86
|
+
!!(process.versions && process.versions.node) &&
|
|
87
|
+
!isBrowser()
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function isElectron() {
|
|
92
|
+
if (!isBrowser()) return false;
|
|
93
|
+
return !!(
|
|
94
|
+
(window.process && window.process.versions && window.process.versions.electron) ||
|
|
95
|
+
typeof window.fsNative !== 'undefined'
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Wrap a function that may or may not return a Promise
|
|
101
|
+
* (older prefixed fullscreen methods don't return one).
|
|
102
|
+
*/
|
|
103
|
+
function maybePromise(fn) {
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
try {
|
|
106
|
+
const result = fn();
|
|
107
|
+
if (result && typeof result.then === 'function') {
|
|
108
|
+
result.then(resolve, reject);
|
|
109
|
+
} else {
|
|
110
|
+
resolve(result);
|
|
111
|
+
}
|
|
112
|
+
} catch (err) {
|
|
113
|
+
reject(err);
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Vendor / prefix detection for fullscreen APIs.
|
|
120
|
+
* Works in browsers; returns null in Node/SSR.
|
|
121
|
+
*/
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
const PREFIXES = ['webkit', 'moz', 'ms'];
|
|
125
|
+
|
|
126
|
+
function deriveVendorPrefix(reqMethod) {
|
|
127
|
+
if (!reqMethod) return '';
|
|
128
|
+
if (reqMethod.startsWith('webkit')) return 'webkit';
|
|
129
|
+
if (reqMethod.startsWith('moz')) return 'moz';
|
|
130
|
+
if (reqMethod.startsWith('ms')) return 'ms';
|
|
131
|
+
return '';
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function detectEnvironment(root = null) {
|
|
135
|
+
if (!isBrowser()) return null;
|
|
136
|
+
|
|
137
|
+
const el = root || (typeof document !== 'undefined' ? document.documentElement : null);
|
|
138
|
+
if (!el) return null;
|
|
139
|
+
|
|
140
|
+
// request method
|
|
141
|
+
let request = null;
|
|
142
|
+
if (typeof el.requestFullscreen === 'function') request = 'requestFullscreen';
|
|
143
|
+
else {
|
|
144
|
+
for (const p of PREFIXES) {
|
|
145
|
+
const m = `${p}RequestFullscreen`;
|
|
146
|
+
const m2 = `${p}RequestFullScreen`; // Firefox older
|
|
147
|
+
if (typeof el[m] === 'function') { request = m; break; }
|
|
148
|
+
if (typeof el[m2] === 'function') { request = m2; break; }
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// exit method
|
|
153
|
+
let exit = null;
|
|
154
|
+
if (typeof document.exitFullscreen === 'function') exit = 'exitFullscreen';
|
|
155
|
+
else {
|
|
156
|
+
for (const p of PREFIXES) {
|
|
157
|
+
const m = `${p}ExitFullscreen`;
|
|
158
|
+
const m2 = `${p}CancelFullScreen`;
|
|
159
|
+
if (typeof document[m] === 'function') { exit = m; break; }
|
|
160
|
+
if (typeof document[m2] === 'function') { exit = m2; break; }
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// element property (who is fullscreen right now)
|
|
165
|
+
let elementProp = null;
|
|
166
|
+
for (const prop of [
|
|
167
|
+
'fullscreenElement',
|
|
168
|
+
'webkitFullscreenElement',
|
|
169
|
+
'mozFullScreenElement',
|
|
170
|
+
'msFullscreenElement'
|
|
171
|
+
]) {
|
|
172
|
+
if (prop in document) { elementProp = prop; break; }
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// enabled property (policy / iframe allowfullscreen check)
|
|
176
|
+
let enabledProp = null;
|
|
177
|
+
for (const prop of [
|
|
178
|
+
'fullscreenEnabled',
|
|
179
|
+
'webkitFullscreenEnabled',
|
|
180
|
+
'mozFullScreenEnabled',
|
|
181
|
+
'msFullscreenEnabled'
|
|
182
|
+
]) {
|
|
183
|
+
if (prop in document) { enabledProp = prop; break; }
|
|
184
|
+
}
|
|
185
|
+
const policyAllowed = enabledProp ? Boolean(document[enabledProp]) : true;
|
|
186
|
+
|
|
187
|
+
const vendorPrefix = deriveVendorPrefix(request);
|
|
188
|
+
|
|
189
|
+
// change event name
|
|
190
|
+
let changeEvent = null;
|
|
191
|
+
for (const evt of [
|
|
192
|
+
'fullscreenchange',
|
|
193
|
+
'webkitfullscreenchange',
|
|
194
|
+
'mozfullscreenchange',
|
|
195
|
+
'MSFullscreenChange',
|
|
196
|
+
'msfullscreenchange'
|
|
197
|
+
]) {
|
|
198
|
+
if (`on${evt}` in document) { changeEvent = evt; break; }
|
|
199
|
+
}
|
|
200
|
+
if (!changeEvent) {
|
|
201
|
+
if (vendorPrefix === 'webkit') changeEvent = 'webkitfullscreenchange';
|
|
202
|
+
else if (vendorPrefix === 'moz') changeEvent = 'mozfullscreenchange';
|
|
203
|
+
else if (vendorPrefix === 'ms') changeEvent = 'MSFullscreenChange';
|
|
204
|
+
else changeEvent = 'fullscreenchange';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// error event name
|
|
208
|
+
let errorEvent = null;
|
|
209
|
+
for (const evt of [
|
|
210
|
+
'fullscreenerror',
|
|
211
|
+
'webkitfullscreenerror',
|
|
212
|
+
'mozfullscreenerror',
|
|
213
|
+
'MSFullscreenError',
|
|
214
|
+
'msfullscreenerror'
|
|
215
|
+
]) {
|
|
216
|
+
if (`on${evt}` in document) { errorEvent = evt; break; }
|
|
217
|
+
}
|
|
218
|
+
if (!errorEvent) {
|
|
219
|
+
if (vendorPrefix === 'webkit') errorEvent = 'webkitfullscreenerror';
|
|
220
|
+
else if (vendorPrefix === 'moz') errorEvent = 'mozfullscreenerror';
|
|
221
|
+
else if (vendorPrefix === 'ms') errorEvent = 'MSFullscreenError';
|
|
222
|
+
else errorEvent = 'fullscreenerror';
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
request,
|
|
227
|
+
exit,
|
|
228
|
+
elementProp,
|
|
229
|
+
enabledProp,
|
|
230
|
+
changeEvent,
|
|
231
|
+
errorEvent,
|
|
232
|
+
supported: !!(request && exit && elementProp && policyAllowed)
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* CSS-based fake fullscreen fallback.
|
|
238
|
+
* Used when native API is unavailable (e.g. iPhone Safari)
|
|
239
|
+
* or when the request is rejected.
|
|
240
|
+
*
|
|
241
|
+
* This does NOT cover browser chrome; it only makes the target
|
|
242
|
+
* element fill the viewport. That's the best a page can do.
|
|
243
|
+
*/
|
|
244
|
+
|
|
245
|
+
const styleRefCounts = new Map();
|
|
246
|
+
const lockRefCounts = new Map();
|
|
247
|
+
|
|
248
|
+
function sanitizeClassName(name, fallback) {
|
|
249
|
+
const cleaned = String(name || '').replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
250
|
+
return cleaned || fallback;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function sanitizeZIndex(val) {
|
|
254
|
+
if (val === null || val === undefined || val === '' || typeof val === 'boolean') {
|
|
255
|
+
return 2147483647;
|
|
256
|
+
}
|
|
257
|
+
const num = Number(val);
|
|
258
|
+
return Number.isFinite(num) ? Math.trunc(num) : 2147483647;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function getStyleId(baseClass, lockClass, zIndex) {
|
|
262
|
+
return `__fullscreen_engine_style_${baseClass}_${lockClass}_${zIndex}__`;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function buildCSS(baseClass, lockClass, zIndex) {
|
|
266
|
+
return `
|
|
267
|
+
.${baseClass} {
|
|
268
|
+
position: fixed !important;
|
|
269
|
+
top: 0 !important;
|
|
270
|
+
left: 0 !important;
|
|
271
|
+
right: 0 !important;
|
|
272
|
+
bottom: 0 !important;
|
|
273
|
+
width: 100vw !important;
|
|
274
|
+
height: 100vh !important;
|
|
275
|
+
margin: 0 !important;
|
|
276
|
+
padding: 0 !important;
|
|
277
|
+
border: 0 !important;
|
|
278
|
+
z-index: ${zIndex} !important;
|
|
279
|
+
background: #000 !important;
|
|
280
|
+
overflow: auto !important;
|
|
281
|
+
}
|
|
282
|
+
html.${lockClass}, body.${lockClass} {
|
|
283
|
+
overflow: hidden !important;
|
|
284
|
+
}
|
|
285
|
+
`;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function ensureStyle(styleId, baseClass, lockClass, zIndex) {
|
|
289
|
+
if (typeof document === 'undefined') return;
|
|
290
|
+
if (document.getElementById(styleId)) return;
|
|
291
|
+
const style = document.createElement('style');
|
|
292
|
+
style.id = styleId;
|
|
293
|
+
style.textContent = buildCSS(baseClass, lockClass, zIndex);
|
|
294
|
+
(document.head || document.documentElement).appendChild(style);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function removeStyle(styleId) {
|
|
298
|
+
if (typeof document === 'undefined') return;
|
|
299
|
+
const style = document.getElementById(styleId);
|
|
300
|
+
if (style) style.remove();
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function createFallback(opts) {
|
|
304
|
+
const baseClass = sanitizeClassName(opts.fallbackClass, 'fs-engine-active');
|
|
305
|
+
const lockClass = opts.lockBody === false ? '__no_lock__' : 'fs-engine-scroll-lock';
|
|
306
|
+
const zIndex = sanitizeZIndex(opts.zIndex);
|
|
307
|
+
const styleId = getStyleId(baseClass, lockClass, zIndex);
|
|
308
|
+
let active = false;
|
|
309
|
+
let activeTarget = null;
|
|
310
|
+
let styleRegistered = false;
|
|
311
|
+
let lockApplied = false;
|
|
312
|
+
|
|
313
|
+
function enter(target) {
|
|
314
|
+
if (typeof document === 'undefined' || !target || !target.classList) return;
|
|
315
|
+
if (!styleRegistered) {
|
|
316
|
+
styleRefCounts.set(styleId, (styleRefCounts.get(styleId) ?? 0) + 1);
|
|
317
|
+
styleRegistered = true;
|
|
318
|
+
}
|
|
319
|
+
ensureStyle(styleId, baseClass, lockClass, zIndex);
|
|
320
|
+
activeTarget = target;
|
|
321
|
+
target.classList.add(baseClass);
|
|
322
|
+
if (opts.lockBody !== false && !lockApplied) {
|
|
323
|
+
lockRefCounts.set(lockClass, (lockRefCounts.get(lockClass) ?? 0) + 1);
|
|
324
|
+
lockApplied = true;
|
|
325
|
+
document.documentElement.classList.add(lockClass);
|
|
326
|
+
document.body && document.body.classList.add(lockClass);
|
|
327
|
+
}
|
|
328
|
+
active = true;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function exit() {
|
|
332
|
+
if (typeof document === 'undefined') return;
|
|
333
|
+
if (activeTarget && activeTarget.classList) {
|
|
334
|
+
activeTarget.classList.remove(baseClass);
|
|
335
|
+
}
|
|
336
|
+
if (opts.lockBody !== false && lockApplied) {
|
|
337
|
+
lockApplied = false;
|
|
338
|
+
const nextLockCount = (lockRefCounts.get(lockClass) ?? 1) - 1;
|
|
339
|
+
if (nextLockCount <= 0) {
|
|
340
|
+
lockRefCounts.delete(lockClass);
|
|
341
|
+
document.documentElement.classList.remove(lockClass);
|
|
342
|
+
document.body && document.body.classList.remove(lockClass);
|
|
343
|
+
} else {
|
|
344
|
+
lockRefCounts.set(lockClass, nextLockCount);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
activeTarget = null;
|
|
348
|
+
active = false;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function destroy() {
|
|
352
|
+
exit();
|
|
353
|
+
if (styleRegistered) {
|
|
354
|
+
styleRegistered = false;
|
|
355
|
+
const nextCount = (styleRefCounts.get(styleId) ?? 1) - 1;
|
|
356
|
+
if (nextCount <= 0) {
|
|
357
|
+
styleRefCounts.delete(styleId);
|
|
358
|
+
removeStyle(styleId);
|
|
359
|
+
} else {
|
|
360
|
+
styleRefCounts.set(styleId, nextCount);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return {
|
|
366
|
+
enter,
|
|
367
|
+
exit,
|
|
368
|
+
destroy,
|
|
369
|
+
get active() { return active; }
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Auto-trigger controller for mouse hover and page scroll fullscreen activation.
|
|
375
|
+
*
|
|
376
|
+
* Pairs with CSS fallback (or Electron IPC bridge) so hover and scroll events
|
|
377
|
+
* can enter fullscreen without triggering browser user-gesture SecurityErrors.
|
|
378
|
+
*/
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
const VALID_TRIGGERS = new Set(['manual', 'hover', 'scroll']);
|
|
382
|
+
|
|
383
|
+
function normalizeThreshold(val) {
|
|
384
|
+
if (val === null || val === '' || typeof val === 'boolean') return 100;
|
|
385
|
+
const num = Number(val);
|
|
386
|
+
return Number.isFinite(num) ? Math.max(0, num) : 100;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function normalizeTriggers(triggerOption) {
|
|
390
|
+
const raw = Array.isArray(triggerOption)
|
|
391
|
+
? triggerOption
|
|
392
|
+
: triggerOption !== undefined && triggerOption !== null
|
|
393
|
+
? [triggerOption]
|
|
394
|
+
: ['manual'];
|
|
395
|
+
|
|
396
|
+
for (const item of raw) {
|
|
397
|
+
if (!VALID_TRIGGERS.has(item)) {
|
|
398
|
+
throw new TypeError(`[fullscreen-engine] invalid trigger "${String(item)}": expected 'manual', 'hover', or 'scroll'`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const hasHover = raw.includes('hover');
|
|
403
|
+
const hasScroll = raw.includes('scroll');
|
|
404
|
+
|
|
405
|
+
return {
|
|
406
|
+
list: raw,
|
|
407
|
+
hasHover,
|
|
408
|
+
hasScroll,
|
|
409
|
+
isAuto: hasHover || hasScroll
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function createAutoTriggers({
|
|
414
|
+
triggers,
|
|
415
|
+
resolveHoverTarget,
|
|
416
|
+
resolveScrollTarget,
|
|
417
|
+
hoverDelay = 0,
|
|
418
|
+
scrollThreshold = 100,
|
|
419
|
+
autoExit = true,
|
|
420
|
+
suppressOnManualExit = true,
|
|
421
|
+
isActive,
|
|
422
|
+
onEnter,
|
|
423
|
+
onExit
|
|
424
|
+
}) {
|
|
425
|
+
if (!triggers.isAuto || !isBrowser()) {
|
|
426
|
+
return {
|
|
427
|
+
notifyManualExit() {},
|
|
428
|
+
destroy() {}
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const threshold = normalizeThreshold(scrollThreshold);
|
|
433
|
+
|
|
434
|
+
let destroyed = false;
|
|
435
|
+
let activeSource = null; // 'hover' | 'scroll' | null
|
|
436
|
+
let isHovered = false;
|
|
437
|
+
let hoverTimer = null;
|
|
438
|
+
let domReadyBound = false;
|
|
439
|
+
let enterSeq = 0;
|
|
440
|
+
let pendingExitSeq = null;
|
|
441
|
+
const suppressed = {
|
|
442
|
+
hover: false,
|
|
443
|
+
scroll: false
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
function clearHoverTimer() {
|
|
447
|
+
if (hoverTimer !== null) {
|
|
448
|
+
clearTimeout(hoverTimer);
|
|
449
|
+
hoverTimer = null;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// ---------- Hover & Scroll target state ----------
|
|
454
|
+
let boundHoverEl = null;
|
|
455
|
+
let boundScrollEl = null;
|
|
456
|
+
|
|
457
|
+
function getScrollY() {
|
|
458
|
+
if (boundScrollEl && typeof window !== 'undefined' && boundScrollEl !== window) {
|
|
459
|
+
return boundScrollEl.scrollTop ?? 0;
|
|
460
|
+
}
|
|
461
|
+
if (typeof window === 'undefined') return 0;
|
|
462
|
+
return (
|
|
463
|
+
window.scrollY ??
|
|
464
|
+
window.pageYOffset ??
|
|
465
|
+
(typeof document !== 'undefined' && document.documentElement
|
|
466
|
+
? document.documentElement.scrollTop
|
|
467
|
+
: 0) ??
|
|
468
|
+
0
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function isScrollPastThreshold() {
|
|
473
|
+
return getScrollY() >= threshold;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
function triggerEnter(source) {
|
|
477
|
+
if (destroyed) return;
|
|
478
|
+
if (suppressed[source]) return;
|
|
479
|
+
if (isActive()) return;
|
|
480
|
+
activeSource = source;
|
|
481
|
+
const seq = ++enterSeq;
|
|
482
|
+
Promise.resolve(onEnter(source))
|
|
483
|
+
.then(() => {
|
|
484
|
+
if (destroyed || seq !== enterSeq) {
|
|
485
|
+
if (pendingExitSeq === seq && isActive()) {
|
|
486
|
+
pendingExitSeq = null;
|
|
487
|
+
Promise.resolve(onExit(source)).catch(() => {});
|
|
488
|
+
}
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (!isActive() && activeSource === source) {
|
|
492
|
+
activeSource = null;
|
|
493
|
+
}
|
|
494
|
+
})
|
|
495
|
+
.catch(() => {
|
|
496
|
+
if (seq === enterSeq && activeSource === source) {
|
|
497
|
+
activeSource = null;
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function triggerExit(source) {
|
|
503
|
+
if (destroyed || !autoExit) return;
|
|
504
|
+
if (activeSource !== source) return;
|
|
505
|
+
|
|
506
|
+
// Transfer ownership if the other trigger condition is still active
|
|
507
|
+
if (source === 'hover' && triggers.hasScroll && isScrollPastThreshold() && !suppressed.scroll) {
|
|
508
|
+
activeSource = 'scroll';
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
if (source === 'scroll' && triggers.hasHover && isHovered && !suppressed.hover) {
|
|
512
|
+
activeSource = 'hover';
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
if (!isActive()) {
|
|
517
|
+
pendingExitSeq = enterSeq;
|
|
518
|
+
activeSource = null;
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
activeSource = null;
|
|
523
|
+
onExit(source);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// ---------- Hover handlers ----------
|
|
527
|
+
function onMouseEnter() {
|
|
528
|
+
if (destroyed) return;
|
|
529
|
+
isHovered = true;
|
|
530
|
+
if (suppressed.hover) return;
|
|
531
|
+
|
|
532
|
+
clearHoverTimer();
|
|
533
|
+
const delay = Math.max(0, Number(hoverDelay) || 0);
|
|
534
|
+
if (delay > 0) {
|
|
535
|
+
hoverTimer = setTimeout(() => {
|
|
536
|
+
hoverTimer = null;
|
|
537
|
+
if (isHovered && !destroyed) {
|
|
538
|
+
triggerEnter('hover');
|
|
539
|
+
}
|
|
540
|
+
}, delay);
|
|
541
|
+
} else {
|
|
542
|
+
triggerEnter('hover');
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function onMouseLeave() {
|
|
547
|
+
if (destroyed) return;
|
|
548
|
+
isHovered = false;
|
|
549
|
+
clearHoverTimer();
|
|
550
|
+
suppressed.hover = false;
|
|
551
|
+
triggerExit('hover');
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function bindHoverTarget() {
|
|
555
|
+
if (destroyed || !triggers.hasHover || boundHoverEl) return;
|
|
556
|
+
const el = typeof resolveHoverTarget === 'function' ? resolveHoverTarget() : null;
|
|
557
|
+
if (el && typeof el.addEventListener === 'function') {
|
|
558
|
+
boundHoverEl = el;
|
|
559
|
+
boundHoverEl.addEventListener('mouseenter', onMouseEnter);
|
|
560
|
+
boundHoverEl.addEventListener('mouseleave', onMouseLeave);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// ---------- Scroll handler ----------
|
|
565
|
+
function onScroll() {
|
|
566
|
+
if (destroyed) return;
|
|
567
|
+
if (isScrollPastThreshold()) {
|
|
568
|
+
triggerEnter('scroll');
|
|
569
|
+
} else {
|
|
570
|
+
suppressed.scroll = false;
|
|
571
|
+
triggerExit('scroll');
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
function bindScrollTarget() {
|
|
576
|
+
if (destroyed || !triggers.hasScroll || boundScrollEl) return;
|
|
577
|
+
const target =
|
|
578
|
+
typeof resolveScrollTarget === 'function'
|
|
579
|
+
? resolveScrollTarget()
|
|
580
|
+
: typeof window !== 'undefined'
|
|
581
|
+
? window
|
|
582
|
+
: null;
|
|
583
|
+
if (target && typeof target.addEventListener === 'function') {
|
|
584
|
+
boundScrollEl = target;
|
|
585
|
+
boundScrollEl.addEventListener('scroll', onScroll, { passive: true });
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
bindHoverTarget();
|
|
590
|
+
bindScrollTarget();
|
|
591
|
+
|
|
592
|
+
function onDomReady() {
|
|
593
|
+
domReadyBound = false;
|
|
594
|
+
bindHoverTarget();
|
|
595
|
+
bindScrollTarget();
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
const needsDeferredBind =
|
|
599
|
+
(triggers.hasHover && !boundHoverEl) || (triggers.hasScroll && !boundScrollEl);
|
|
600
|
+
|
|
601
|
+
if (
|
|
602
|
+
needsDeferredBind &&
|
|
603
|
+
typeof document !== 'undefined' &&
|
|
604
|
+
document.readyState === 'loading' &&
|
|
605
|
+
typeof document.addEventListener === 'function'
|
|
606
|
+
) {
|
|
607
|
+
domReadyBound = true;
|
|
608
|
+
document.addEventListener('DOMContentLoaded', onDomReady, { once: true });
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function notifyManualExit() {
|
|
612
|
+
if (destroyed) return;
|
|
613
|
+
clearHoverTimer();
|
|
614
|
+
enterSeq += 1;
|
|
615
|
+
pendingExitSeq = null;
|
|
616
|
+
if (suppressOnManualExit) {
|
|
617
|
+
if (isHovered) suppressed.hover = true;
|
|
618
|
+
if (isScrollPastThreshold()) suppressed.scroll = true;
|
|
619
|
+
}
|
|
620
|
+
activeSource = null;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function destroy() {
|
|
624
|
+
if (destroyed) return;
|
|
625
|
+
destroyed = true;
|
|
626
|
+
clearHoverTimer();
|
|
627
|
+
enterSeq += 1;
|
|
628
|
+
pendingExitSeq = null;
|
|
629
|
+
if (
|
|
630
|
+
domReadyBound &&
|
|
631
|
+
typeof document !== 'undefined' &&
|
|
632
|
+
typeof document.removeEventListener === 'function'
|
|
633
|
+
) {
|
|
634
|
+
document.removeEventListener('DOMContentLoaded', onDomReady);
|
|
635
|
+
domReadyBound = false;
|
|
636
|
+
}
|
|
637
|
+
if (boundHoverEl && typeof boundHoverEl.removeEventListener === 'function') {
|
|
638
|
+
boundHoverEl.removeEventListener('mouseenter', onMouseEnter);
|
|
639
|
+
boundHoverEl.removeEventListener('mouseleave', onMouseLeave);
|
|
640
|
+
}
|
|
641
|
+
boundHoverEl = null;
|
|
642
|
+
if (boundScrollEl && typeof boundScrollEl.removeEventListener === 'function') {
|
|
643
|
+
boundScrollEl.removeEventListener('scroll', onScroll);
|
|
644
|
+
}
|
|
645
|
+
boundScrollEl = null;
|
|
646
|
+
activeSource = null;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return {
|
|
650
|
+
notifyManualExit,
|
|
651
|
+
destroy
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Core engine: createFullscreen()
|
|
657
|
+
*
|
|
658
|
+
* Environment-aware. Works in:
|
|
659
|
+
* - Browser (native, with prefixes)
|
|
660
|
+
* - Electron renderer (via window.fsNative bridge)
|
|
661
|
+
* - SSR/Node (import safe; request() rejects gracefully)
|
|
662
|
+
*
|
|
663
|
+
* Fallback: CSS-based fake fullscreen when native is unavailable.
|
|
664
|
+
* Auto-triggers: Opt-in mouse hover and page scroll fullscreen activation.
|
|
665
|
+
*/
|
|
666
|
+
|
|
667
|
+
|
|
668
|
+
function isValidElement(el) {
|
|
669
|
+
return (
|
|
670
|
+
typeof el === 'object' &&
|
|
671
|
+
el !== null &&
|
|
672
|
+
el.nodeType === 1 &&
|
|
673
|
+
typeof el.classList === 'object'
|
|
674
|
+
);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function normalizeAutoRelock$1(val) {
|
|
678
|
+
if (val === undefined || val === null || val === false) {
|
|
679
|
+
return { enabled: false, delay: 50, maxRetries: 3 };
|
|
680
|
+
}
|
|
681
|
+
if (val === true) {
|
|
682
|
+
return { enabled: true, delay: 50, maxRetries: 3 };
|
|
683
|
+
}
|
|
684
|
+
if (typeof val === 'object') {
|
|
685
|
+
const delayNum = Number(val.delay);
|
|
686
|
+
const retriesNum = Number(val.maxRetries);
|
|
687
|
+
return {
|
|
688
|
+
enabled: val.enabled ?? true,
|
|
689
|
+
delay: Number.isFinite(delayNum) ? Math.max(0, delayNum) : 50,
|
|
690
|
+
maxRetries: Number.isFinite(retriesNum) ? Math.max(1, Math.trunc(retriesNum)) : 3
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
throw new TypeError('[fullscreen-engine] invalid autoRelock option: expected boolean or object');
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function createFullscreen$1(userOptions = {}) {
|
|
697
|
+
const triggers = normalizeTriggers(userOptions.trigger);
|
|
698
|
+
const relockConfig = normalizeAutoRelock$1(userOptions.autoRelock);
|
|
699
|
+
|
|
700
|
+
const fallbackOption = userOptions.fallback ?? 'none';
|
|
701
|
+
if (fallbackOption !== 'css' && fallbackOption !== 'none') {
|
|
702
|
+
throw new TypeError(`[fullscreen-engine] invalid fallback "${String(fallbackOption)}": expected 'css' or 'none'`);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if (userOptions.target !== undefined && userOptions.target !== null && !isValidElement(userOptions.target)) {
|
|
706
|
+
throw new TypeError('[fullscreen-engine] target must be a valid DOM Element');
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
const options = {
|
|
710
|
+
target: userOptions.target ?? null,
|
|
711
|
+
fallback: fallbackOption, // 'css' | 'none'
|
|
712
|
+
fallbackClass: userOptions.fallbackClass ?? 'fs-engine-active',
|
|
713
|
+
lockBody: userOptions.lockBody ?? true,
|
|
714
|
+
zIndex: userOptions.zIndex ?? 2147483647,
|
|
715
|
+
onError: userOptions.onError ?? null,
|
|
716
|
+
autoDetectElectron: userOptions.autoDetectElectron ?? true,
|
|
717
|
+
trigger: triggers.list,
|
|
718
|
+
hoverTarget: userOptions.hoverTarget ?? null,
|
|
719
|
+
hoverDelay: userOptions.hoverDelay ?? 0,
|
|
720
|
+
scrollTarget: userOptions.scrollTarget ?? null,
|
|
721
|
+
scrollThreshold: userOptions.scrollThreshold ?? 100,
|
|
722
|
+
autoExit: userOptions.autoExit ?? true,
|
|
723
|
+
suppressOnManualExit: userOptions.suppressOnManualExit ?? true,
|
|
724
|
+
autoRelock: relockConfig,
|
|
725
|
+
preventExit: userOptions.preventExit ?? false
|
|
726
|
+
};
|
|
727
|
+
|
|
728
|
+
const emitter = new Emitter();
|
|
729
|
+
const env = detectEnvironment();
|
|
730
|
+
|
|
731
|
+
function emitError(err) {
|
|
732
|
+
emitter.emit('error', err);
|
|
733
|
+
if (typeof options.onError === 'function') options.onError(err);
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// Resolve target lazily (document may not exist in SSR)
|
|
737
|
+
function resolveTarget() {
|
|
738
|
+
if (options.target) return options.target;
|
|
739
|
+
if (typeof document !== 'undefined') return document.documentElement;
|
|
740
|
+
return null;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
function resolveHoverTarget() {
|
|
744
|
+
if (options.hoverTarget) return options.hoverTarget;
|
|
745
|
+
return resolveTarget();
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
function resolveScrollTarget() {
|
|
749
|
+
if (options.scrollTarget) return options.scrollTarget;
|
|
750
|
+
if (typeof window !== 'undefined') return window;
|
|
751
|
+
return null;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// Electron bridge
|
|
755
|
+
const electronBridge =
|
|
756
|
+
options.autoDetectElectron && isElectron() && typeof window !== 'undefined'
|
|
757
|
+
? window.fsNative || null
|
|
758
|
+
: null;
|
|
759
|
+
|
|
760
|
+
const hasNative = !!(env && env.supported);
|
|
761
|
+
const hasElectron = !!(electronBridge && typeof electronBridge.set === 'function');
|
|
762
|
+
const hasFallback =
|
|
763
|
+
userOptions.fallback !== undefined
|
|
764
|
+
? options.fallback === 'css'
|
|
765
|
+
: triggers.isAuto || relockConfig.enabled;
|
|
766
|
+
const effectiveLockBody = triggers.hasScroll && options.autoExit ? false : options.lockBody;
|
|
767
|
+
|
|
768
|
+
const fallback = hasFallback
|
|
769
|
+
? createFallback({
|
|
770
|
+
fallbackClass: options.fallbackClass,
|
|
771
|
+
lockBody: effectiveLockBody,
|
|
772
|
+
zIndex: options.zIndex
|
|
773
|
+
})
|
|
774
|
+
: null;
|
|
775
|
+
|
|
776
|
+
let destroyed = false;
|
|
777
|
+
let electronActive = false;
|
|
778
|
+
let lastActive = false;
|
|
779
|
+
let lastSource = 'none';
|
|
780
|
+
let electronUnsub = null;
|
|
781
|
+
let escapeBound = false;
|
|
782
|
+
let exitLocked = !!options.preventExit;
|
|
783
|
+
let manualUnlocking = false;
|
|
784
|
+
let relockTimer = null;
|
|
785
|
+
let relockRetries = 0;
|
|
786
|
+
|
|
787
|
+
function clearRelockTimer() {
|
|
788
|
+
if (relockTimer !== null) {
|
|
789
|
+
clearTimeout(relockTimer);
|
|
790
|
+
relockTimer = null;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
function scheduleRelock() {
|
|
795
|
+
if (destroyed || !relockConfig.enabled || manualUnlocking) return;
|
|
796
|
+
if (relockRetries >= relockConfig.maxRetries) {
|
|
797
|
+
emitError(new Error('[fullscreen-engine] autoRelock failed after maxRetries'));
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
clearRelockTimer();
|
|
801
|
+
relockTimer = setTimeout(() => {
|
|
802
|
+
relockTimer = null;
|
|
803
|
+
if (destroyed || !relockConfig.enabled || manualUnlocking || getState().active) return;
|
|
804
|
+
relockRetries += 1;
|
|
805
|
+
requestAuto()
|
|
806
|
+
.then(() => {
|
|
807
|
+
if (!destroyed && !getState().active && relockConfig.enabled && !manualUnlocking) {
|
|
808
|
+
scheduleRelock();
|
|
809
|
+
}
|
|
810
|
+
})
|
|
811
|
+
.catch((err) => {
|
|
812
|
+
emitError(err);
|
|
813
|
+
if (!destroyed && relockConfig.enabled && !manualUnlocking) {
|
|
814
|
+
scheduleRelock();
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
}, relockConfig.delay);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
function onFallbackEscape(e) {
|
|
821
|
+
if (destroyed || !fallback || !fallback.active) return;
|
|
822
|
+
if (e && e.key === 'Escape') {
|
|
823
|
+
if (exitLocked) {
|
|
824
|
+
if (typeof e.preventDefault === 'function') e.preventDefault();
|
|
825
|
+
if (typeof e.stopPropagation === 'function') e.stopPropagation();
|
|
826
|
+
emitError(new Error('[fullscreen-engine] Exit blocked: preventExit is active'));
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
exit().catch((err) => emitError(err));
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
function syncEscapeListener(isFallbackActive) {
|
|
834
|
+
if (typeof document === 'undefined') return;
|
|
835
|
+
if (isFallbackActive && !escapeBound) {
|
|
836
|
+
document.addEventListener('keydown', onFallbackEscape);
|
|
837
|
+
escapeBound = true;
|
|
838
|
+
} else if (!isFallbackActive && escapeBound) {
|
|
839
|
+
document.removeEventListener('keydown', onFallbackEscape);
|
|
840
|
+
escapeBound = false;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
function getState() {
|
|
845
|
+
let active = false;
|
|
846
|
+
let source = 'none';
|
|
847
|
+
let element = null;
|
|
848
|
+
|
|
849
|
+
if (hasElectron && electronActive) {
|
|
850
|
+
active = true;
|
|
851
|
+
source = 'electron';
|
|
852
|
+
element = resolveTarget();
|
|
853
|
+
} else if (hasNative && typeof document !== 'undefined' && env.elementProp) {
|
|
854
|
+
const fsEl = document[env.elementProp];
|
|
855
|
+
if (fsEl) {
|
|
856
|
+
const target = options.target;
|
|
857
|
+
const matchesTarget =
|
|
858
|
+
!target ||
|
|
859
|
+
fsEl === target ||
|
|
860
|
+
(typeof target.contains === 'function' && target.contains(fsEl));
|
|
861
|
+
if (matchesTarget) {
|
|
862
|
+
active = true;
|
|
863
|
+
source = 'native';
|
|
864
|
+
element = fsEl;
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
if (!active && fallback && fallback.active) {
|
|
870
|
+
active = true;
|
|
871
|
+
source = 'fallback';
|
|
872
|
+
element = resolveTarget();
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
return {
|
|
876
|
+
active,
|
|
877
|
+
source,
|
|
878
|
+
element,
|
|
879
|
+
supported: hasNative || hasElectron || hasFallback
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
function notifyChange(force = false) {
|
|
884
|
+
const state = getState();
|
|
885
|
+
if (state.active && state.source !== 'fallback' && fallback && fallback.active) {
|
|
886
|
+
fallback.exit();
|
|
887
|
+
}
|
|
888
|
+
syncEscapeListener(state.active && state.source === 'fallback');
|
|
889
|
+
if (state.active) {
|
|
890
|
+
relockRetries = 0;
|
|
891
|
+
clearRelockTimer();
|
|
892
|
+
}
|
|
893
|
+
if (!force && state.active === lastActive && state.source === lastSource) return;
|
|
894
|
+
const wasActive = lastActive;
|
|
895
|
+
lastActive = state.active;
|
|
896
|
+
lastSource = state.source;
|
|
897
|
+
if (wasActive && !state.active) {
|
|
898
|
+
autoTriggers.notifyManualExit();
|
|
899
|
+
}
|
|
900
|
+
emitter.emit('change', state);
|
|
901
|
+
if (state.active) emitter.emit('enter', state);
|
|
902
|
+
else {
|
|
903
|
+
emitter.emit('exit', state);
|
|
904
|
+
if (wasActive && relockConfig.enabled && !manualUnlocking) {
|
|
905
|
+
scheduleRelock();
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// Sync external Electron window state transitions (e.g. F11, Esc, macOS window button)
|
|
911
|
+
if (hasElectron) {
|
|
912
|
+
if (typeof electronBridge.onChange === 'function') {
|
|
913
|
+
electronUnsub = electronBridge.onChange((on) => {
|
|
914
|
+
if (destroyed) return;
|
|
915
|
+
electronActive = !!on;
|
|
916
|
+
notifyChange(false);
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
if (typeof electronBridge.get === 'function') {
|
|
920
|
+
Promise.resolve(electronBridge.get())
|
|
921
|
+
.then((on) => {
|
|
922
|
+
if (destroyed) return;
|
|
923
|
+
const next = !!on;
|
|
924
|
+
if (next !== electronActive) {
|
|
925
|
+
electronActive = next;
|
|
926
|
+
notifyChange(false);
|
|
927
|
+
}
|
|
928
|
+
})
|
|
929
|
+
.catch(() => {});
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
// ---------- native listener wiring ----------
|
|
934
|
+
function onNativeChange() {
|
|
935
|
+
if (destroyed) return;
|
|
936
|
+
notifyChange();
|
|
937
|
+
}
|
|
938
|
+
function onNativeError(e) {
|
|
939
|
+
if (destroyed) return;
|
|
940
|
+
const err = (e && (e.error || e)) || new Error('Fullscreen error');
|
|
941
|
+
emitError(err);
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
let nativeChangeBound = false;
|
|
945
|
+
function bindNativeListeners() {
|
|
946
|
+
if (!isBrowser() || !hasNative || nativeChangeBound) return;
|
|
947
|
+
document.addEventListener(env.changeEvent, onNativeChange);
|
|
948
|
+
document.addEventListener(env.errorEvent, onNativeError);
|
|
949
|
+
nativeChangeBound = true;
|
|
950
|
+
}
|
|
951
|
+
function unbindNativeListeners() {
|
|
952
|
+
if (!isBrowser() || !hasNative || !nativeChangeBound) return;
|
|
953
|
+
document.removeEventListener(env.changeEvent, onNativeChange);
|
|
954
|
+
document.removeEventListener(env.errorEvent, onNativeError);
|
|
955
|
+
nativeChangeBound = false;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
bindNativeListeners();
|
|
959
|
+
|
|
960
|
+
// ---------- auto-trigger activation (gesture-safe) ----------
|
|
961
|
+
async function requestAuto() {
|
|
962
|
+
if (destroyed) return;
|
|
963
|
+
if (getState().active) return;
|
|
964
|
+
|
|
965
|
+
// 1. Electron native (IPC has no browser user-gesture restriction)
|
|
966
|
+
if (hasElectron) {
|
|
967
|
+
try {
|
|
968
|
+
const res = await Promise.resolve(electronBridge.set(true));
|
|
969
|
+
if (res === false) {
|
|
970
|
+
throw new Error('[fullscreen-engine] Electron bridge rejected fullscreen request');
|
|
971
|
+
}
|
|
972
|
+
electronActive = true;
|
|
973
|
+
notifyChange(false);
|
|
974
|
+
return;
|
|
975
|
+
} catch (err) {
|
|
976
|
+
emitError(err);
|
|
977
|
+
// fall through to CSS fallback
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
// 2. CSS fallback (avoids browser SecurityError on hover/scroll events)
|
|
982
|
+
if (hasFallback && fallback) {
|
|
983
|
+
const target = resolveTarget();
|
|
984
|
+
if (!target) return;
|
|
985
|
+
fallback.enter(target);
|
|
986
|
+
notifyChange(false);
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
async function exitInternal(fromAutoTrigger = false) {
|
|
991
|
+
if (destroyed) {
|
|
992
|
+
if (!fromAutoTrigger) throw new Error('[fullscreen-engine] instance destroyed');
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
if (exitLocked && !fromAutoTrigger) {
|
|
996
|
+
const err = new Error('[fullscreen-engine] Exit blocked: preventExit is active');
|
|
997
|
+
emitError(err);
|
|
998
|
+
throw err;
|
|
999
|
+
}
|
|
1000
|
+
if (!getState().active) return;
|
|
1001
|
+
if (!fromAutoTrigger) {
|
|
1002
|
+
autoTriggers.notifyManualExit();
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
if (hasElectron && electronActive) {
|
|
1006
|
+
try {
|
|
1007
|
+
const res = await Promise.resolve(electronBridge.set(false));
|
|
1008
|
+
if (res === true) {
|
|
1009
|
+
throw new Error('[fullscreen-engine] Electron bridge failed to exit fullscreen');
|
|
1010
|
+
}
|
|
1011
|
+
electronActive = false;
|
|
1012
|
+
} catch (err) {
|
|
1013
|
+
emitError(err);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
if (
|
|
1018
|
+
hasNative &&
|
|
1019
|
+
isBrowser() &&
|
|
1020
|
+
env.elementProp &&
|
|
1021
|
+
document[env.elementProp] &&
|
|
1022
|
+
typeof document[env.exit] === 'function'
|
|
1023
|
+
) {
|
|
1024
|
+
try {
|
|
1025
|
+
await maybePromise(() => document[env.exit]());
|
|
1026
|
+
} catch (err) {
|
|
1027
|
+
emitError(err);
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
if (fallback && fallback.active) {
|
|
1032
|
+
fallback.exit();
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
notifyChange(false);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
const autoTriggers = createAutoTriggers({
|
|
1039
|
+
triggers,
|
|
1040
|
+
resolveHoverTarget,
|
|
1041
|
+
resolveScrollTarget,
|
|
1042
|
+
hoverDelay: options.hoverDelay,
|
|
1043
|
+
scrollThreshold: options.scrollThreshold,
|
|
1044
|
+
autoExit: options.autoExit,
|
|
1045
|
+
suppressOnManualExit: options.suppressOnManualExit,
|
|
1046
|
+
isActive: () => getState().active,
|
|
1047
|
+
onEnter: () => requestAuto(),
|
|
1048
|
+
onExit: () => exitInternal(true)
|
|
1049
|
+
});
|
|
1050
|
+
|
|
1051
|
+
// ---------- public API ----------
|
|
1052
|
+
async function request() {
|
|
1053
|
+
if (destroyed) throw new Error('[fullscreen-engine] instance destroyed');
|
|
1054
|
+
manualUnlocking = false;
|
|
1055
|
+
if (getState().active) return;
|
|
1056
|
+
|
|
1057
|
+
// 1. Electron native (do not fall through to Chromium HTML5 native)
|
|
1058
|
+
if (hasElectron) {
|
|
1059
|
+
try {
|
|
1060
|
+
const res = await Promise.resolve(electronBridge.set(true));
|
|
1061
|
+
if (res === false) {
|
|
1062
|
+
throw new Error('[fullscreen-engine] Electron bridge rejected fullscreen request');
|
|
1063
|
+
}
|
|
1064
|
+
electronActive = true;
|
|
1065
|
+
notifyChange(false);
|
|
1066
|
+
return;
|
|
1067
|
+
} catch (err) {
|
|
1068
|
+
emitError(err);
|
|
1069
|
+
// fall through to CSS fallback
|
|
1070
|
+
}
|
|
1071
|
+
} else if (hasNative) {
|
|
1072
|
+
// 2. Browser native
|
|
1073
|
+
const target = resolveTarget();
|
|
1074
|
+
if (!target) throw new Error('[fullscreen-engine] no target element');
|
|
1075
|
+
try {
|
|
1076
|
+
await maybePromise(() => target[env.request]());
|
|
1077
|
+
// Avoid duplicate emissions if native changeEvent already fired
|
|
1078
|
+
notifyChange(false);
|
|
1079
|
+
return;
|
|
1080
|
+
} catch (err) {
|
|
1081
|
+
emitError(err);
|
|
1082
|
+
// fall through to fallback
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
// 3. CSS fallback
|
|
1087
|
+
if (hasFallback && fallback) {
|
|
1088
|
+
const target = resolveTarget();
|
|
1089
|
+
if (!target) throw new Error('[fullscreen-engine] no target element for fallback');
|
|
1090
|
+
fallback.enter(target);
|
|
1091
|
+
notifyChange(false);
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
throw new Error(
|
|
1096
|
+
'[fullscreen-engine] fullscreen unavailable: no native API, no Electron bridge, fallback disabled'
|
|
1097
|
+
);
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
async function exit() {
|
|
1101
|
+
return exitInternal(false);
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
async function toggle() {
|
|
1105
|
+
if (getState().active) return exit();
|
|
1106
|
+
return request();
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
function unlock() {
|
|
1110
|
+
exitLocked = false;
|
|
1111
|
+
manualUnlocking = true;
|
|
1112
|
+
clearRelockTimer();
|
|
1113
|
+
relockRetries = 0;
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
function isActive() {
|
|
1117
|
+
return getState().active;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
function isSupported() {
|
|
1121
|
+
return hasNative || hasElectron || hasFallback;
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
function isFallback() {
|
|
1125
|
+
return getState().source === 'fallback';
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
function on(event, cb) {
|
|
1129
|
+
return emitter.on(event, cb);
|
|
1130
|
+
}
|
|
1131
|
+
function once(event, cb) {
|
|
1132
|
+
return emitter.once(event, cb);
|
|
1133
|
+
}
|
|
1134
|
+
function off(event, cb) {
|
|
1135
|
+
return emitter.off(event, cb);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
function destroy() {
|
|
1139
|
+
if (destroyed) return;
|
|
1140
|
+
destroyed = true;
|
|
1141
|
+
clearRelockTimer();
|
|
1142
|
+
syncEscapeListener(false);
|
|
1143
|
+
unbindNativeListeners();
|
|
1144
|
+
if (typeof electronUnsub === 'function') {
|
|
1145
|
+
electronUnsub();
|
|
1146
|
+
electronUnsub = null;
|
|
1147
|
+
}
|
|
1148
|
+
autoTriggers.destroy();
|
|
1149
|
+
if (fallback) fallback.destroy();
|
|
1150
|
+
emitter.clear();
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
// Expose current state snapshots
|
|
1154
|
+
const state = () => getState();
|
|
1155
|
+
|
|
1156
|
+
return {
|
|
1157
|
+
request,
|
|
1158
|
+
exit,
|
|
1159
|
+
toggle,
|
|
1160
|
+
unlock,
|
|
1161
|
+
isActive,
|
|
1162
|
+
isSupported,
|
|
1163
|
+
isFallback,
|
|
1164
|
+
getState: state,
|
|
1165
|
+
on,
|
|
1166
|
+
once,
|
|
1167
|
+
off,
|
|
1168
|
+
destroy,
|
|
1169
|
+
// convenience for debugging
|
|
1170
|
+
_env: env,
|
|
1171
|
+
_options: options
|
|
1172
|
+
};
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
/**
|
|
1176
|
+
* Electron renderer-side adapter.
|
|
1177
|
+
*
|
|
1178
|
+
* Provide this on the renderer via contextBridge (window.fsNative).
|
|
1179
|
+
*/
|
|
1180
|
+
|
|
1181
|
+
|
|
1182
|
+
function normalizeAutoRelock(val) {
|
|
1183
|
+
if (val === undefined || val === null || val === false) {
|
|
1184
|
+
return { enabled: false, delay: 50, maxRetries: 3 };
|
|
1185
|
+
}
|
|
1186
|
+
if (val === true) {
|
|
1187
|
+
return { enabled: true, delay: 50, maxRetries: 3 };
|
|
1188
|
+
}
|
|
1189
|
+
if (typeof val === 'object') {
|
|
1190
|
+
const delayNum = Number(val.delay);
|
|
1191
|
+
const retriesNum = Number(val.maxRetries);
|
|
1192
|
+
return {
|
|
1193
|
+
enabled: val.enabled ?? true,
|
|
1194
|
+
delay: Number.isFinite(delayNum) ? Math.max(0, delayNum) : 50,
|
|
1195
|
+
maxRetries: Number.isFinite(retriesNum) ? Math.max(1, Math.trunc(retriesNum)) : 3
|
|
1196
|
+
};
|
|
1197
|
+
}
|
|
1198
|
+
throw new TypeError('[fullscreen-engine/electron] invalid autoRelock option: expected boolean or object');
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
function createElectronFullscreen(bridge, opts = {}) {
|
|
1202
|
+
if (!bridge || typeof bridge.set !== 'function') {
|
|
1203
|
+
throw new Error('[fullscreen-engine/electron] bridge with .set() required');
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
const relockConfig = normalizeAutoRelock(opts.autoRelock);
|
|
1207
|
+
const onError = typeof opts.onError === 'function' ? opts.onError : null;
|
|
1208
|
+
const emitter = new Emitter();
|
|
1209
|
+
let active = false;
|
|
1210
|
+
let destroyed = false;
|
|
1211
|
+
let unsubscribe = null;
|
|
1212
|
+
let exitLocked = !!opts.preventExit;
|
|
1213
|
+
let manualUnlocking = false;
|
|
1214
|
+
let relockTimer = null;
|
|
1215
|
+
let relockRetries = 0;
|
|
1216
|
+
|
|
1217
|
+
function emitError(err) {
|
|
1218
|
+
emitter.emit('error', err);
|
|
1219
|
+
if (onError) onError(err);
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
function clearRelockTimer() {
|
|
1223
|
+
if (relockTimer !== null) {
|
|
1224
|
+
clearTimeout(relockTimer);
|
|
1225
|
+
relockTimer = null;
|
|
1226
|
+
}
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
function getState() {
|
|
1230
|
+
return {
|
|
1231
|
+
active,
|
|
1232
|
+
source: active ? 'electron' : 'none',
|
|
1233
|
+
element: null,
|
|
1234
|
+
supported: true
|
|
1235
|
+
};
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
function scheduleRelock() {
|
|
1239
|
+
if (destroyed || !relockConfig.enabled || manualUnlocking) return;
|
|
1240
|
+
if (relockRetries >= relockConfig.maxRetries) {
|
|
1241
|
+
emitError(new Error('[fullscreen-engine/electron] autoRelock failed after maxRetries'));
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
clearRelockTimer();
|
|
1245
|
+
relockTimer = setTimeout(() => {
|
|
1246
|
+
relockTimer = null;
|
|
1247
|
+
if (destroyed || !relockConfig.enabled || manualUnlocking || active) return;
|
|
1248
|
+
relockRetries += 1;
|
|
1249
|
+
request()
|
|
1250
|
+
.catch((err) => {
|
|
1251
|
+
emitError(err);
|
|
1252
|
+
if (!destroyed && relockConfig.enabled && !manualUnlocking && !active) {
|
|
1253
|
+
scheduleRelock();
|
|
1254
|
+
}
|
|
1255
|
+
});
|
|
1256
|
+
}, relockConfig.delay);
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
function updateActive(next) {
|
|
1260
|
+
const boolNext = !!next;
|
|
1261
|
+
if (boolNext) {
|
|
1262
|
+
relockRetries = 0;
|
|
1263
|
+
clearRelockTimer();
|
|
1264
|
+
}
|
|
1265
|
+
if (boolNext === active) return;
|
|
1266
|
+
const wasActive = active;
|
|
1267
|
+
active = boolNext;
|
|
1268
|
+
const state = getState();
|
|
1269
|
+
emitter.emit('change', state);
|
|
1270
|
+
if (active) emitter.emit('enter', state);
|
|
1271
|
+
else {
|
|
1272
|
+
emitter.emit('exit', state);
|
|
1273
|
+
if (wasActive && relockConfig.enabled && !manualUnlocking) {
|
|
1274
|
+
scheduleRelock();
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
if (typeof bridge.onChange === 'function') {
|
|
1280
|
+
unsubscribe = bridge.onChange((on) => {
|
|
1281
|
+
if (!destroyed) updateActive(on);
|
|
1282
|
+
});
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
if (typeof bridge.get === 'function') {
|
|
1286
|
+
Promise.resolve(bridge.get())
|
|
1287
|
+
.then((on) => {
|
|
1288
|
+
if (!destroyed) updateActive(on);
|
|
1289
|
+
})
|
|
1290
|
+
.catch(() => {});
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
async function request() {
|
|
1294
|
+
if (destroyed) throw new Error('[fullscreen-engine/electron] destroyed');
|
|
1295
|
+
manualUnlocking = false;
|
|
1296
|
+
const res = await bridge.set(true);
|
|
1297
|
+
if (res === false) {
|
|
1298
|
+
throw new Error('[fullscreen-engine/electron] bridge.set(true) returned false');
|
|
1299
|
+
}
|
|
1300
|
+
updateActive(true);
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
async function exit() {
|
|
1304
|
+
if (destroyed) throw new Error('[fullscreen-engine/electron] destroyed');
|
|
1305
|
+
if (exitLocked) {
|
|
1306
|
+
const err = new Error('[fullscreen-engine/electron] Exit blocked: preventExit is active');
|
|
1307
|
+
emitError(err);
|
|
1308
|
+
throw err;
|
|
1309
|
+
}
|
|
1310
|
+
const res = await bridge.set(false);
|
|
1311
|
+
if (res === true) {
|
|
1312
|
+
throw new Error('[fullscreen-engine/electron] bridge.set(false) failed to exit');
|
|
1313
|
+
}
|
|
1314
|
+
updateActive(false);
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
function unlock() {
|
|
1318
|
+
exitLocked = false;
|
|
1319
|
+
manualUnlocking = true;
|
|
1320
|
+
clearRelockTimer();
|
|
1321
|
+
relockRetries = 0;
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
async function relock() {
|
|
1325
|
+
manualUnlocking = false;
|
|
1326
|
+
return request();
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
return {
|
|
1330
|
+
request,
|
|
1331
|
+
exit,
|
|
1332
|
+
async toggle() {
|
|
1333
|
+
return active ? exit() : request();
|
|
1334
|
+
},
|
|
1335
|
+
unlock,
|
|
1336
|
+
relock,
|
|
1337
|
+
isActive() { return active; },
|
|
1338
|
+
isSupported() { return true; },
|
|
1339
|
+
getState,
|
|
1340
|
+
on(event, cb) { return emitter.on(event, cb); },
|
|
1341
|
+
once(event, cb) { return emitter.once(event, cb); },
|
|
1342
|
+
off(event, cb) { return emitter.off(event, cb); },
|
|
1343
|
+
destroy() {
|
|
1344
|
+
if (destroyed) return;
|
|
1345
|
+
destroyed = true;
|
|
1346
|
+
clearRelockTimer();
|
|
1347
|
+
if (typeof unsubscribe === 'function') unsubscribe();
|
|
1348
|
+
unsubscribe = null;
|
|
1349
|
+
active = false;
|
|
1350
|
+
emitter.clear();
|
|
1351
|
+
}
|
|
1352
|
+
};
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
/**
|
|
1356
|
+
* True backend fullscreen control from Node.js.
|
|
1357
|
+
*
|
|
1358
|
+
* Uses Chrome DevTools Protocol (CDP) via Puppeteer to control the
|
|
1359
|
+
* browser window's fullscreen state. This works from a Node.js process.
|
|
1360
|
+
*/
|
|
1361
|
+
|
|
1362
|
+
function createPuppeteerFullscreen(page, opts = {}) {
|
|
1363
|
+
if (!page || typeof page.createCDPSession !== 'function') {
|
|
1364
|
+
throw new Error('[fullscreen-engine/puppeteer] page with .createCDPSession() required');
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
let client = null;
|
|
1368
|
+
let windowId = null;
|
|
1369
|
+
let initPromise = null;
|
|
1370
|
+
let active = false;
|
|
1371
|
+
let destroyed = false;
|
|
1372
|
+
let exitLocked = !!opts.preventExit;
|
|
1373
|
+
|
|
1374
|
+
async function ensure() {
|
|
1375
|
+
if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
|
|
1376
|
+
if (client && windowId !== null) return;
|
|
1377
|
+
if (!initPromise) {
|
|
1378
|
+
initPromise = (async () => {
|
|
1379
|
+
let session = null;
|
|
1380
|
+
try {
|
|
1381
|
+
session = await page.createCDPSession();
|
|
1382
|
+
const res = await session.send('Browser.getWindowForTarget');
|
|
1383
|
+
if (destroyed) {
|
|
1384
|
+
try { await session.detach(); } catch {}
|
|
1385
|
+
throw new Error('[fullscreen-engine/puppeteer] destroyed');
|
|
1386
|
+
}
|
|
1387
|
+
client = session;
|
|
1388
|
+
windowId = res.windowId;
|
|
1389
|
+
} catch (err) {
|
|
1390
|
+
if (session) {
|
|
1391
|
+
try { await session.detach(); } catch {}
|
|
1392
|
+
}
|
|
1393
|
+
client = null;
|
|
1394
|
+
windowId = null;
|
|
1395
|
+
throw err;
|
|
1396
|
+
} finally {
|
|
1397
|
+
initPromise = null;
|
|
1398
|
+
}
|
|
1399
|
+
})();
|
|
1400
|
+
}
|
|
1401
|
+
return initPromise;
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
async function setState(state) {
|
|
1405
|
+
if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
|
|
1406
|
+
await ensure();
|
|
1407
|
+
await client.send('Browser.setWindowBounds', {
|
|
1408
|
+
windowId,
|
|
1409
|
+
bounds: { windowState: state } // 'fullscreen' | 'normal' | 'minimized' | 'maximized'
|
|
1410
|
+
});
|
|
1411
|
+
active = state === 'fullscreen';
|
|
1412
|
+
return active;
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
return {
|
|
1416
|
+
async request() {
|
|
1417
|
+
if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
|
|
1418
|
+
if (active) return true;
|
|
1419
|
+
return setState('fullscreen');
|
|
1420
|
+
},
|
|
1421
|
+
async exit() {
|
|
1422
|
+
if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
|
|
1423
|
+
if (exitLocked) {
|
|
1424
|
+
throw new Error('[fullscreen-engine/puppeteer] Exit blocked: preventExit is active');
|
|
1425
|
+
}
|
|
1426
|
+
if (!active) {
|
|
1427
|
+
const current = await this.getWindowState();
|
|
1428
|
+
if (current !== 'fullscreen') return false;
|
|
1429
|
+
}
|
|
1430
|
+
return setState(opts.restoreState || 'normal');
|
|
1431
|
+
},
|
|
1432
|
+
async toggle() {
|
|
1433
|
+
if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
|
|
1434
|
+
const current = await this.getWindowState();
|
|
1435
|
+
return current === 'fullscreen' ? this.exit() : this.request();
|
|
1436
|
+
},
|
|
1437
|
+
unlock() {
|
|
1438
|
+
exitLocked = false;
|
|
1439
|
+
},
|
|
1440
|
+
async relock() {
|
|
1441
|
+
if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
|
|
1442
|
+
const current = await this.getWindowState();
|
|
1443
|
+
if (current === 'fullscreen') return true;
|
|
1444
|
+
return setState('fullscreen');
|
|
1445
|
+
},
|
|
1446
|
+
isActive() {
|
|
1447
|
+
return active;
|
|
1448
|
+
},
|
|
1449
|
+
isSupported() {
|
|
1450
|
+
return true;
|
|
1451
|
+
},
|
|
1452
|
+
async getWindowState() {
|
|
1453
|
+
if (destroyed) throw new Error('[fullscreen-engine/puppeteer] destroyed');
|
|
1454
|
+
await ensure();
|
|
1455
|
+
const res = await client.send('Browser.getWindowBounds', { windowId });
|
|
1456
|
+
const state = res.bounds.windowState;
|
|
1457
|
+
active = state === 'fullscreen';
|
|
1458
|
+
return state;
|
|
1459
|
+
},
|
|
1460
|
+
async destroy() {
|
|
1461
|
+
if (destroyed) return;
|
|
1462
|
+
destroyed = true;
|
|
1463
|
+
if (initPromise) {
|
|
1464
|
+
try { await initPromise; } catch {}
|
|
1465
|
+
}
|
|
1466
|
+
try { if (client) await client.detach(); } catch {}
|
|
1467
|
+
client = null;
|
|
1468
|
+
windowId = null;
|
|
1469
|
+
initPromise = null;
|
|
1470
|
+
active = false;
|
|
1471
|
+
}
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
/**
|
|
1476
|
+
* SSR / Node.js safe stub.
|
|
1477
|
+
*
|
|
1478
|
+
* Importing the main entry already works in Node (all guards are in place).
|
|
1479
|
+
* But if you explicitly need a no-op controller (e.g. for template
|
|
1480
|
+
* environments that can't tree-shake), use this:
|
|
1481
|
+
*
|
|
1482
|
+
* import { createFullscreen } from 'fullscreen-engine/adapters/ssr';
|
|
1483
|
+
*/
|
|
1484
|
+
|
|
1485
|
+
function createFullscreen() {
|
|
1486
|
+
return {
|
|
1487
|
+
async request() { throw new Error('[fullscreen-engine/ssr] fullscreen unavailable in SSR'); },
|
|
1488
|
+
async exit() {},
|
|
1489
|
+
async toggle() { return this.request(); },
|
|
1490
|
+
isActive() { return false; },
|
|
1491
|
+
isSupported() { return false; },
|
|
1492
|
+
isFallback() { return false; },
|
|
1493
|
+
getState() { return { active: false, source: 'none', element: null, supported: false }; },
|
|
1494
|
+
on() { return () => {}; },
|
|
1495
|
+
once() { return () => {}; },
|
|
1496
|
+
off() {},
|
|
1497
|
+
destroy() {}
|
|
1498
|
+
};
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
/**
|
|
1502
|
+
* Global configuration engine for fullscreen-engine.
|
|
1503
|
+
*
|
|
1504
|
+
* Pure isomorphic module (no Node.js built-in imports) safe for Browser,
|
|
1505
|
+
* Electron renderer, Puppeteer/Node, and SSR environments.
|
|
1506
|
+
*/
|
|
1507
|
+
|
|
1508
|
+
|
|
1509
|
+
function isPlainObject$1(val) {
|
|
1510
|
+
return typeof val === 'object' && val !== null && !Array.isArray(val);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
function mergeAutoRelock(baseVal, nextVal) {
|
|
1514
|
+
if (nextVal === undefined) return baseVal;
|
|
1515
|
+
if (isPlainObject$1(baseVal) && isPlainObject$1(nextVal)) {
|
|
1516
|
+
return { ...baseVal, ...nextVal };
|
|
1517
|
+
}
|
|
1518
|
+
return nextVal;
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
function mergeSection(base, ext) {
|
|
1522
|
+
if (!ext) return { ...base };
|
|
1523
|
+
const merged = { ...base, ...ext };
|
|
1524
|
+
if ('autoRelock' in base || 'autoRelock' in ext) {
|
|
1525
|
+
merged.autoRelock = mergeAutoRelock(base.autoRelock, ext.autoRelock);
|
|
1526
|
+
}
|
|
1527
|
+
return merged;
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
function validateConfigShape(cfg, label = 'config') {
|
|
1531
|
+
if (!isPlainObject$1(cfg)) {
|
|
1532
|
+
throw new TypeError(`[fullscreen-engine/config] ${label} must be a plain object`);
|
|
1533
|
+
}
|
|
1534
|
+
if (cfg.mode !== undefined && (typeof cfg.mode !== 'string' || !(cfg.mode in PRESETS))) {
|
|
1535
|
+
throw new TypeError(`[fullscreen-engine/config] unknown mode/preset: ${String(cfg.mode)}`);
|
|
1536
|
+
}
|
|
1537
|
+
if (cfg.core !== undefined && !isPlainObject$1(cfg.core)) {
|
|
1538
|
+
throw new TypeError(`[fullscreen-engine/config] ${label}.core must be a plain object`);
|
|
1539
|
+
}
|
|
1540
|
+
if (cfg.adapters !== undefined) {
|
|
1541
|
+
if (!isPlainObject$1(cfg.adapters)) {
|
|
1542
|
+
throw new TypeError(`[fullscreen-engine/config] ${label}.adapters must be a plain object`);
|
|
1543
|
+
}
|
|
1544
|
+
for (const key of ['electron', 'puppeteer', 'ssr']) {
|
|
1545
|
+
if (cfg.adapters[key] !== undefined && !isPlainObject$1(cfg.adapters[key])) {
|
|
1546
|
+
throw new TypeError(
|
|
1547
|
+
`[fullscreen-engine/config] adapter "${key}" options must be a plain object`
|
|
1548
|
+
);
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
|
|
1554
|
+
const PRESETS = Object.freeze({
|
|
1555
|
+
default: Object.freeze({
|
|
1556
|
+
core: Object.freeze({}),
|
|
1557
|
+
adapters: Object.freeze({
|
|
1558
|
+
electron: Object.freeze({}),
|
|
1559
|
+
puppeteer: Object.freeze({}),
|
|
1560
|
+
ssr: Object.freeze({ enabled: true })
|
|
1561
|
+
})
|
|
1562
|
+
}),
|
|
1563
|
+
kiosk: Object.freeze({
|
|
1564
|
+
core: Object.freeze({
|
|
1565
|
+
fallback: 'css',
|
|
1566
|
+
lockBody: true,
|
|
1567
|
+
autoRelock: true,
|
|
1568
|
+
preventExit: true,
|
|
1569
|
+
suppressOnManualExit: true
|
|
1570
|
+
}),
|
|
1571
|
+
adapters: Object.freeze({
|
|
1572
|
+
electron: Object.freeze({ autoRelock: true, preventExit: true }),
|
|
1573
|
+
puppeteer: Object.freeze({ preventExit: true }),
|
|
1574
|
+
ssr: Object.freeze({ enabled: true })
|
|
1575
|
+
})
|
|
1576
|
+
}),
|
|
1577
|
+
presentation: Object.freeze({
|
|
1578
|
+
core: Object.freeze({
|
|
1579
|
+
fallback: 'css',
|
|
1580
|
+
lockBody: true,
|
|
1581
|
+
trigger: 'manual',
|
|
1582
|
+
autoExit: false,
|
|
1583
|
+
suppressOnManualExit: true,
|
|
1584
|
+
autoRelock: false,
|
|
1585
|
+
preventExit: false
|
|
1586
|
+
}),
|
|
1587
|
+
adapters: Object.freeze({
|
|
1588
|
+
electron: Object.freeze({ autoRelock: false, preventExit: false }),
|
|
1589
|
+
puppeteer: Object.freeze({ restoreState: 'normal', preventExit: false }),
|
|
1590
|
+
ssr: Object.freeze({ enabled: true })
|
|
1591
|
+
})
|
|
1592
|
+
}),
|
|
1593
|
+
strict: Object.freeze({
|
|
1594
|
+
core: Object.freeze({
|
|
1595
|
+
fallback: 'css',
|
|
1596
|
+
lockBody: true,
|
|
1597
|
+
autoRelock: Object.freeze({ enabled: true, delay: 50, maxRetries: 5 }),
|
|
1598
|
+
preventExit: true,
|
|
1599
|
+
suppressOnManualExit: true
|
|
1600
|
+
}),
|
|
1601
|
+
adapters: Object.freeze({
|
|
1602
|
+
electron: Object.freeze({
|
|
1603
|
+
autoRelock: Object.freeze({ enabled: true, delay: 50, maxRetries: 5 }),
|
|
1604
|
+
preventExit: true
|
|
1605
|
+
}),
|
|
1606
|
+
puppeteer: Object.freeze({ preventExit: true }),
|
|
1607
|
+
ssr: Object.freeze({ enabled: false })
|
|
1608
|
+
})
|
|
1609
|
+
})
|
|
1610
|
+
});
|
|
1611
|
+
|
|
1612
|
+
function resolveConfig(config = {}, overrides = {}) {
|
|
1613
|
+
validateConfigShape(config, 'config');
|
|
1614
|
+
validateConfigShape(overrides, 'overrides');
|
|
1615
|
+
|
|
1616
|
+
const mode = overrides.mode ?? config.mode ?? 'default';
|
|
1617
|
+
const preset = PRESETS[mode];
|
|
1618
|
+
|
|
1619
|
+
const cfgAdapters = config.adapters || {};
|
|
1620
|
+
const ovAdapters = overrides.adapters || {};
|
|
1621
|
+
|
|
1622
|
+
return {
|
|
1623
|
+
mode,
|
|
1624
|
+
core: mergeSection(mergeSection(preset.core, config.core), overrides.core),
|
|
1625
|
+
adapters: {
|
|
1626
|
+
electron: mergeSection(
|
|
1627
|
+
mergeSection(preset.adapters.electron, cfgAdapters.electron),
|
|
1628
|
+
ovAdapters.electron
|
|
1629
|
+
),
|
|
1630
|
+
puppeteer: mergeSection(
|
|
1631
|
+
mergeSection(preset.adapters.puppeteer, cfgAdapters.puppeteer),
|
|
1632
|
+
ovAdapters.puppeteer
|
|
1633
|
+
),
|
|
1634
|
+
ssr: mergeSection(
|
|
1635
|
+
mergeSection(preset.adapters.ssr, cfgAdapters.ssr),
|
|
1636
|
+
ovAdapters.ssr
|
|
1637
|
+
)
|
|
1638
|
+
}
|
|
1639
|
+
};
|
|
1640
|
+
}
|
|
1641
|
+
|
|
1642
|
+
function initFullscreen(config = {}, context = {}) {
|
|
1643
|
+
if (!isPlainObject$1(context)) {
|
|
1644
|
+
throw new TypeError('[fullscreen-engine/config] context must be a plain object');
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
const resolved = resolveConfig(config, context.overrides || {});
|
|
1648
|
+
if (context.target !== undefined) {
|
|
1649
|
+
resolved.core.target = context.target;
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
if (context.bridge !== undefined || context.adapter === 'electron') {
|
|
1653
|
+
const bridge =
|
|
1654
|
+
context.bridge ?? (typeof window !== 'undefined' ? window.fsNative : null);
|
|
1655
|
+
return createElectronFullscreen(bridge, resolved.adapters.electron);
|
|
1656
|
+
}
|
|
1657
|
+
|
|
1658
|
+
if (context.page !== undefined || context.adapter === 'puppeteer') {
|
|
1659
|
+
return createPuppeteerFullscreen(context.page, resolved.adapters.puppeteer);
|
|
1660
|
+
}
|
|
1661
|
+
|
|
1662
|
+
if (context.adapter === 'ssr' || (isNode() && !isBrowser() && !isElectron())) {
|
|
1663
|
+
if (resolved.adapters.ssr.enabled === false) {
|
|
1664
|
+
throw new Error('[fullscreen-engine/config] SSR adapter is disabled by config');
|
|
1665
|
+
}
|
|
1666
|
+
return createFullscreen();
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
return createFullscreen$1(resolved.core);
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
/**
|
|
1673
|
+
* Node.js / Electron main-process global config auto-loader.
|
|
1674
|
+
*
|
|
1675
|
+
* Discovers and loads fullscreen.config.{js,mjs,cjs,json} from a project
|
|
1676
|
+
* directory without requiring engine file edits.
|
|
1677
|
+
*/
|
|
1678
|
+
|
|
1679
|
+
|
|
1680
|
+
const CONFIG_FILES = Object.freeze([
|
|
1681
|
+
'fullscreen.config.js',
|
|
1682
|
+
'fullscreen.config.mjs',
|
|
1683
|
+
'fullscreen.config.cjs',
|
|
1684
|
+
'fullscreen.config.json'
|
|
1685
|
+
]);
|
|
1686
|
+
|
|
1687
|
+
const CONFIG_EXTENSIONS = new Set(['.js', '.mjs', '.cjs', '.json']);
|
|
1688
|
+
|
|
1689
|
+
function isPlainObject(val) {
|
|
1690
|
+
return typeof val === 'object' && val !== null && !Array.isArray(val);
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
function findConfigFile(cwd = process.cwd()) {
|
|
1694
|
+
const baseDir = resolve(cwd);
|
|
1695
|
+
for (const filename of CONFIG_FILES) {
|
|
1696
|
+
const candidate = resolve(baseDir, filename);
|
|
1697
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) {
|
|
1698
|
+
return candidate;
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
return null;
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
async function readRawConfigFile(filePath) {
|
|
1705
|
+
const ext = extname(filePath).toLowerCase();
|
|
1706
|
+
try {
|
|
1707
|
+
if (ext === '.json') {
|
|
1708
|
+
const text = readFileSync(filePath, 'utf8');
|
|
1709
|
+
return JSON.parse(text);
|
|
1710
|
+
}
|
|
1711
|
+
if (ext === '.cjs') {
|
|
1712
|
+
const require = createRequire(import.meta.url);
|
|
1713
|
+
const loaded = require(filePath);
|
|
1714
|
+
return loaded && loaded.__esModule ? loaded.default : loaded;
|
|
1715
|
+
}
|
|
1716
|
+
const mod = await import(pathToFileURL(filePath).href);
|
|
1717
|
+
return mod?.default ?? mod;
|
|
1718
|
+
} catch (err) {
|
|
1719
|
+
throw new Error(
|
|
1720
|
+
`[fullscreen-engine/config] failed to load ${filePath}: ${err?.message || String(err)}`,
|
|
1721
|
+
{ cause: err }
|
|
1722
|
+
);
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1725
|
+
|
|
1726
|
+
async function loadConfig(cwdOrPath = process.cwd(), options = {}) {
|
|
1727
|
+
let target = cwdOrPath;
|
|
1728
|
+
let opts = options;
|
|
1729
|
+
|
|
1730
|
+
if (isPlainObject(cwdOrPath)) {
|
|
1731
|
+
opts = cwdOrPath;
|
|
1732
|
+
target = process.cwd();
|
|
1733
|
+
}
|
|
1734
|
+
if (!isPlainObject(opts)) {
|
|
1735
|
+
throw new TypeError('[fullscreen-engine/config] loadConfig options must be a plain object');
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
const resolvedTarget = resolve(target);
|
|
1739
|
+
const ext = extname(resolvedTarget).toLowerCase();
|
|
1740
|
+
let filePath = null;
|
|
1741
|
+
|
|
1742
|
+
if (CONFIG_EXTENSIONS.has(ext)) {
|
|
1743
|
+
if (!existsSync(resolvedTarget) || !statSync(resolvedTarget).isFile()) {
|
|
1744
|
+
throw new Error(`[fullscreen-engine/config] config file not found: ${resolvedTarget}`);
|
|
1745
|
+
}
|
|
1746
|
+
filePath = resolvedTarget;
|
|
1747
|
+
} else {
|
|
1748
|
+
filePath = findConfigFile(resolvedTarget);
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
if (!filePath) {
|
|
1752
|
+
if (opts.required) {
|
|
1753
|
+
throw new Error(
|
|
1754
|
+
`[fullscreen-engine/config] no fullscreen.config.{js,mjs,cjs,json} found in ${resolvedTarget}`
|
|
1755
|
+
);
|
|
1756
|
+
}
|
|
1757
|
+
return resolveConfig({}, opts.overrides || {});
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
const rawConfig = await readRawConfigFile(filePath);
|
|
1761
|
+
return resolveConfig(rawConfig, opts.overrides || {});
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
async function loadAndInit(cwdOrPath = process.cwd(), context = {}) {
|
|
1765
|
+
let target = cwdOrPath;
|
|
1766
|
+
let ctx = context;
|
|
1767
|
+
|
|
1768
|
+
if (isPlainObject(cwdOrPath)) {
|
|
1769
|
+
ctx = cwdOrPath;
|
|
1770
|
+
target = process.cwd();
|
|
1771
|
+
}
|
|
1772
|
+
if (!isPlainObject(ctx)) {
|
|
1773
|
+
throw new TypeError('[fullscreen-engine/config] context must be a plain object');
|
|
1774
|
+
}
|
|
1775
|
+
const config = await loadConfig(target, {
|
|
1776
|
+
required: ctx.required ?? false,
|
|
1777
|
+
overrides: ctx.overrides || {}
|
|
1778
|
+
});
|
|
1779
|
+
return initFullscreen(config, ctx);
|
|
1780
|
+
}
|
|
1781
|
+
|
|
1782
|
+
export { CONFIG_FILES, findConfigFile, loadAndInit, loadConfig };
|
|
1783
|
+
//# sourceMappingURL=config.esm.js.map
|