fullscreen-engine 1.0.0 → 1.2.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.
Files changed (40) hide show
  1. package/README.md +109 -49
  2. package/bin/fullscreen-engine.js +162 -0
  3. package/dist/adapters/electron.cjs +2 -1
  4. package/dist/adapters/electron.cjs.map +1 -1
  5. package/dist/adapters/electron.esm.js +2 -1
  6. package/dist/adapters/electron.esm.js.map +1 -1
  7. package/dist/adapters/puppeteer.cjs +1 -1
  8. package/dist/adapters/puppeteer.esm.js +1 -1
  9. package/dist/adapters/ssr.cjs +2 -1
  10. package/dist/adapters/ssr.cjs.map +1 -1
  11. package/dist/adapters/ssr.esm.js +2 -1
  12. package/dist/adapters/ssr.esm.js.map +1 -1
  13. package/dist/config.cjs +2282 -0
  14. package/dist/config.cjs.map +1 -0
  15. package/dist/config.esm.js +2276 -0
  16. package/dist/config.esm.js.map +1 -0
  17. package/dist/fullscreen.esm.js +1013 -18
  18. package/dist/fullscreen.esm.js.map +1 -1
  19. package/dist/fullscreen.esm.min.js +2 -2
  20. package/dist/fullscreen.esm.min.js.map +1 -1
  21. package/dist/fullscreen.iife.js +1017 -18
  22. package/dist/fullscreen.iife.js.map +1 -1
  23. package/dist/fullscreen.umd.cjs +1017 -18
  24. package/dist/fullscreen.umd.cjs.map +1 -1
  25. package/dist/fullscreen.umd.min.cjs +2 -2
  26. package/dist/fullscreen.umd.min.cjs.map +1 -1
  27. package/package.json +15 -3
  28. package/src/adapters/electron.js +1 -0
  29. package/src/adapters/ssr.js +1 -0
  30. package/src/config-loader.js +114 -0
  31. package/src/config.js +187 -0
  32. package/src/core.js +201 -5
  33. package/src/fallback.js +22 -8
  34. package/src/guards.js +277 -0
  35. package/src/index.js +2 -1
  36. package/src/vendor.js +1 -0
  37. package/types/config.d.ts +40 -0
  38. package/types/electron.d.ts +4 -0
  39. package/types/index.d.ts +94 -1
  40. package/types/ssr.d.ts +1 -0
package/src/guards.js ADDED
@@ -0,0 +1,277 @@
1
+ /**
2
+ * Cross-browser kiosk & exit-key guards (browser-safe, zero Node built-ins).
3
+ *
4
+ * Supports:
5
+ * - keyboardLock: navigator.keyboard?.lock(keys) on native fullscreen
6
+ * - blockShortcuts: keydown guard for F11, Escape (when exitLocked), or custom keys
7
+ * - confirmOnLeave: beforeunload prompt while exitLocked && active
8
+ * - blockContextMenu: contextmenu prevention while active
9
+ */
10
+
11
+ export function normalizeKeyboardLock(val) {
12
+ if (val === undefined || val === null || val === false) {
13
+ return { enabled: false, keys: ['Escape', 'F11'] };
14
+ }
15
+ if (val === true) {
16
+ return { enabled: true, keys: ['Escape', 'F11'] };
17
+ }
18
+ if (Array.isArray(val)) {
19
+ const keys = val.map((k) => String(k)).filter(Boolean);
20
+ return { enabled: keys.length > 0, keys };
21
+ }
22
+ throw new TypeError(
23
+ '[fullscreen-engine] invalid keyboardLock option: expected boolean or string[]'
24
+ );
25
+ }
26
+
27
+ const DEFAULT_SHORTCUTS = [
28
+ 'F11',
29
+ 'Escape',
30
+ 'F5',
31
+ 'F12',
32
+ 'Ctrl+R',
33
+ 'Meta+R',
34
+ 'Ctrl+Shift+I',
35
+ 'Meta+Alt+I'
36
+ ];
37
+
38
+ function parseShortcut(raw) {
39
+ const parts = String(raw)
40
+ .split('+')
41
+ .map((p) => p.trim())
42
+ .filter(Boolean);
43
+ if (parts.length === 0) return null;
44
+ const key = parts[parts.length - 1].toLowerCase();
45
+ const mods = parts.slice(0, -1).map((m) => m.toLowerCase());
46
+ const hasModifiers = mods.length > 0;
47
+ return {
48
+ key,
49
+ hasModifiers,
50
+ ctrl: mods.includes('ctrl') || mods.includes('control'),
51
+ meta: mods.includes('meta') || mods.includes('cmd') || mods.includes('command'),
52
+ alt: mods.includes('alt') || mods.includes('option'),
53
+ shift: mods.includes('shift')
54
+ };
55
+ }
56
+
57
+ function matchesShortcut(e, rule) {
58
+ if (!rule || !e || !e.key) return false;
59
+ if (String(e.key).toLowerCase() !== rule.key) return false;
60
+ if (!rule.hasModifiers) return true;
61
+ return (
62
+ !!e.ctrlKey === rule.ctrl &&
63
+ !!e.metaKey === rule.meta &&
64
+ !!e.altKey === rule.alt &&
65
+ !!e.shiftKey === rule.shift
66
+ );
67
+ }
68
+
69
+ export function normalizeBlockShortcuts(val) {
70
+ if (val === undefined || val === null || val === false) {
71
+ return { enabled: false, keys: ['F11'], rules: [parseShortcut('F11')] };
72
+ }
73
+ if (val === true) {
74
+ return {
75
+ enabled: true,
76
+ keys: DEFAULT_SHORTCUTS.slice(),
77
+ rules: DEFAULT_SHORTCUTS.map(parseShortcut).filter(Boolean)
78
+ };
79
+ }
80
+ if (Array.isArray(val)) {
81
+ const keys = val.map((k) => String(k).trim()).filter(Boolean);
82
+ const rules = keys.map(parseShortcut).filter(Boolean);
83
+ return { enabled: rules.length > 0, keys, rules };
84
+ }
85
+ throw new TypeError(
86
+ '[fullscreen-engine] invalid blockShortcuts option: expected boolean or string[]'
87
+ );
88
+ }
89
+
90
+ export function createGuards({
91
+ keyboardLock,
92
+ blockShortcuts,
93
+ confirmOnLeave,
94
+ blockContextMenu,
95
+ watchFocus = false,
96
+ isExitLocked,
97
+ onExitAttempt,
98
+ onEscapeExit,
99
+ onFocusLost
100
+ }) {
101
+ let keyboardLocked = false;
102
+ let keydownBound = false;
103
+ let beforeUnloadBound = false;
104
+ let contextMenuBound = false;
105
+ let focusBound = false;
106
+
107
+ function releaseKeyboardLock() {
108
+ if (!keyboardLocked) return;
109
+ keyboardLocked = false;
110
+ if (
111
+ typeof navigator !== 'undefined' &&
112
+ navigator.keyboard &&
113
+ typeof navigator.keyboard.unlock === 'function'
114
+ ) {
115
+ try {
116
+ navigator.keyboard.unlock();
117
+ } catch {
118
+ // ignore unsupported unlock errors
119
+ }
120
+ }
121
+ }
122
+
123
+ function onKeyDown(e) {
124
+ if (!e || !e.key) return;
125
+ const rules = blockShortcuts.rules || blockShortcuts.keys.map(parseShortcut).filter(Boolean);
126
+ const matched = rules.some((rule) => matchesShortcut(e, rule));
127
+ if (!matched) return;
128
+ if (e.key === 'Escape' && !isExitLocked()) {
129
+ if (typeof onEscapeExit === 'function') onEscapeExit();
130
+ return;
131
+ }
132
+ if (typeof e.preventDefault === 'function') e.preventDefault();
133
+ if (typeof e.stopPropagation === 'function') e.stopPropagation();
134
+ if (typeof onExitAttempt === 'function') {
135
+ onExitAttempt(e.key === 'Escape' ? 'escape' : 'shortcut');
136
+ }
137
+ }
138
+
139
+ function onVisibilityChange() {
140
+ if (typeof document !== 'undefined' && document.hidden && typeof onFocusLost === 'function') {
141
+ onFocusLost('visibilitychange');
142
+ }
143
+ }
144
+
145
+ function onWindowBlur() {
146
+ if (typeof onFocusLost === 'function') {
147
+ onFocusLost('blur');
148
+ }
149
+ }
150
+
151
+ function onBeforeUnload(e) {
152
+ if (!isExitLocked()) return;
153
+ if (typeof e.preventDefault === 'function') e.preventDefault();
154
+ e.returnValue = '';
155
+ }
156
+
157
+ function onContextMenu(e) {
158
+ if (typeof e.preventDefault === 'function') e.preventDefault();
159
+ }
160
+
161
+ function bindKeydown() {
162
+ if (typeof document === 'undefined' || keydownBound) return;
163
+ document.addEventListener('keydown', onKeyDown, true);
164
+ keydownBound = true;
165
+ }
166
+
167
+ function unbindKeydown() {
168
+ if (typeof document === 'undefined' || !keydownBound) return;
169
+ document.removeEventListener('keydown', onKeyDown, true);
170
+ keydownBound = false;
171
+ }
172
+
173
+ function bindBeforeUnload() {
174
+ if (typeof window === 'undefined' || beforeUnloadBound) return;
175
+ window.addEventListener('beforeunload', onBeforeUnload);
176
+ beforeUnloadBound = true;
177
+ }
178
+
179
+ function unbindBeforeUnload() {
180
+ if (typeof window === 'undefined' || !beforeUnloadBound) return;
181
+ window.removeEventListener('beforeunload', onBeforeUnload);
182
+ beforeUnloadBound = false;
183
+ }
184
+
185
+ function bindContextMenu() {
186
+ if (typeof document === 'undefined' || contextMenuBound) return;
187
+ document.addEventListener('contextmenu', onContextMenu, true);
188
+ contextMenuBound = true;
189
+ }
190
+
191
+ function unbindContextMenu() {
192
+ if (typeof document === 'undefined' || !contextMenuBound) return;
193
+ document.removeEventListener('contextmenu', onContextMenu, true);
194
+ contextMenuBound = false;
195
+ }
196
+
197
+ function bindFocus() {
198
+ if (focusBound) return;
199
+ if (typeof document !== 'undefined') {
200
+ document.addEventListener('visibilitychange', onVisibilityChange);
201
+ }
202
+ if (typeof window !== 'undefined') {
203
+ window.addEventListener('blur', onWindowBlur);
204
+ }
205
+ focusBound = true;
206
+ }
207
+
208
+ function unbindFocus() {
209
+ if (!focusBound) return;
210
+ if (typeof document !== 'undefined') {
211
+ document.removeEventListener('visibilitychange', onVisibilityChange);
212
+ }
213
+ if (typeof window !== 'undefined') {
214
+ window.removeEventListener('blur', onWindowBlur);
215
+ }
216
+ focusBound = false;
217
+ }
218
+
219
+ function sync(active, source) {
220
+ if (!active) {
221
+ releaseKeyboardLock();
222
+ unbindKeydown();
223
+ unbindBeforeUnload();
224
+ unbindContextMenu();
225
+ unbindFocus();
226
+ return;
227
+ }
228
+
229
+ if (
230
+ keyboardLock.enabled &&
231
+ source === 'native' &&
232
+ !keyboardLocked &&
233
+ typeof navigator !== 'undefined' &&
234
+ navigator.keyboard &&
235
+ typeof navigator.keyboard.lock === 'function'
236
+ ) {
237
+ keyboardLocked = true;
238
+ try {
239
+ Promise.resolve(navigator.keyboard.lock(keyboardLock.keys)).catch(() => {
240
+ keyboardLocked = false;
241
+ });
242
+ } catch {
243
+ keyboardLocked = false;
244
+ }
245
+ } else if (source !== 'native') {
246
+ releaseKeyboardLock();
247
+ }
248
+
249
+ if (blockShortcuts.enabled) bindKeydown();
250
+ else unbindKeydown();
251
+
252
+ if (confirmOnLeave && isExitLocked()) bindBeforeUnload();
253
+ else unbindBeforeUnload();
254
+
255
+ if (blockContextMenu) bindContextMenu();
256
+ else unbindContextMenu();
257
+
258
+ if (watchFocus) bindFocus();
259
+ else unbindFocus();
260
+ }
261
+
262
+ function unlock() {
263
+ releaseKeyboardLock();
264
+ unbindBeforeUnload();
265
+ unbindFocus();
266
+ }
267
+
268
+ function destroy() {
269
+ sync(false, 'none');
270
+ }
271
+
272
+ return {
273
+ sync,
274
+ unlock,
275
+ destroy
276
+ };
277
+ }
package/src/index.js CHANGED
@@ -19,4 +19,5 @@
19
19
  export { createFullscreen } from './core.js';
20
20
  export { Emitter } from './events.js';
21
21
  export { detectEnvironment } from './vendor.js';
22
- export { isBrowser, isNode, isElectron } from './utils.js';
22
+ export { isBrowser, isNode, isElectron } from './utils.js';
23
+ export { PRESETS, defineConfig, resolveConfig, initFullscreen } from './config.js';
package/src/vendor.js CHANGED
@@ -111,6 +111,7 @@ export function detectEnvironment(root = null) {
111
111
  exit,
112
112
  elementProp,
113
113
  enabledProp,
114
+ prefix: vendorPrefix,
114
115
  changeEvent,
115
116
  errorEvent,
116
117
  supported: !!(request && exit && elementProp && policyAllowed)
@@ -0,0 +1,40 @@
1
+ import type {
2
+ FullscreenGlobalConfig,
3
+ InitFullscreenContext,
4
+ ResolvedFullscreenConfig
5
+ } from './index.d.ts';
6
+
7
+ export {
8
+ PRESETS,
9
+ defineConfig,
10
+ resolveConfig,
11
+ initFullscreen
12
+ } from './index.d.ts';
13
+
14
+ export interface LoadConfigOptions {
15
+ required?: boolean;
16
+ overrides?: FullscreenGlobalConfig;
17
+ }
18
+
19
+ export interface LoadAndInitContext extends InitFullscreenContext {
20
+ required?: boolean;
21
+ }
22
+
23
+ export declare const CONFIG_FILES: readonly [
24
+ 'fullscreen.config.js',
25
+ 'fullscreen.config.mjs',
26
+ 'fullscreen.config.cjs',
27
+ 'fullscreen.config.json'
28
+ ];
29
+
30
+ export function findConfigFile(cwd?: string): string | null;
31
+
32
+ export function loadConfig(
33
+ cwdOrPath?: string | LoadConfigOptions,
34
+ options?: LoadConfigOptions
35
+ ): Promise<ResolvedFullscreenConfig>;
36
+
37
+ export function loadAndInit(
38
+ cwdOrPath?: string | LoadAndInitContext,
39
+ context?: LoadAndInitContext
40
+ ): Promise<any>;
@@ -2,6 +2,7 @@ import type {
2
2
  AutoRelockOptions,
3
3
  FullscreenErrorListener,
4
4
  FullscreenEvent,
5
+ FullscreenExitAttemptListener,
5
6
  FullscreenListener,
6
7
  FullscreenState
7
8
  } from './index.d.ts';
@@ -28,12 +29,15 @@ export interface ElectronFullscreenController {
28
29
  isSupported(): boolean;
29
30
  getState(): FullscreenState;
30
31
  on(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): () => void;
32
+ on(event: 'exitAttempt', cb: FullscreenExitAttemptListener): () => void;
31
33
  on(event: 'error', cb: FullscreenErrorListener): () => void;
32
34
  on(event: FullscreenEvent, cb: (...args: any[]) => void): () => void;
33
35
  once(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): () => void;
36
+ once(event: 'exitAttempt', cb: FullscreenExitAttemptListener): () => void;
34
37
  once(event: 'error', cb: FullscreenErrorListener): () => void;
35
38
  once(event: FullscreenEvent, cb: (...args: any[]) => void): () => void;
36
39
  off(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): void;
40
+ off(event: 'exitAttempt', cb: FullscreenExitAttemptListener): void;
37
41
  off(event: 'error', cb: FullscreenErrorListener): void;
38
42
  off(event: FullscreenEvent, cb: (...args: any[]) => void): void;
39
43
  destroy(): void;
package/types/index.d.ts CHANGED
@@ -2,6 +2,8 @@ export type FullscreenSource = 'native' | 'fallback' | 'electron' | 'none';
2
2
 
3
3
  export type FullscreenTriggerMode = 'manual' | 'hover' | 'scroll';
4
4
 
5
+ export type FullscreenPresetMode = 'default' | 'kiosk' | 'presentation' | 'strict';
6
+
5
7
  export interface AutoRelockOptions {
6
8
  enabled?: boolean;
7
9
  delay?: number;
@@ -13,6 +15,7 @@ export interface FullscreenEnvironment {
13
15
  exit: string | null;
14
16
  elementProp: string | null;
15
17
  enabledProp: string | null;
18
+ prefix?: '' | 'webkit' | 'moz' | 'ms';
16
19
  changeEvent: string;
17
20
  errorEvent: string;
18
21
  supported: boolean;
@@ -58,11 +61,80 @@ export interface FullscreenOptions {
58
61
  autoRelock?: boolean | AutoRelockOptions;
59
62
  /** Block CSS Escape key and manual exit() calls until unlock() is called. Default: false. */
60
63
  preventExit?: boolean;
64
+ /** Standard FullscreenOptions.navigationUI passed to requestFullscreen(). Default: 'hide'. */
65
+ navigationUI?: 'hide' | 'show' | 'auto';
66
+ /** Optional DOM element(s) wired to exit() on click. Default: null. */
67
+ exitButton?: Element | Element[] | null;
68
+ /** Inject overscroll-behavior, touch-action, and user-select freeze CSS in fallback mode. Default: false. */
69
+ freezePage?: boolean;
70
+ /** Upgrade CSS fallback back to native fullscreen on next pointerdown gesture. Default: false. */
71
+ gestureUpgrade?: boolean;
72
+ /** Lock Escape/F11 or custom keys via navigator.keyboard.lock() in native fullscreen. Default: false. */
73
+ keyboardLock?: boolean | string[];
74
+ /** Block F11, Escape (when preventExit is active), or custom keys/modifiers via keydown. Default: false. */
75
+ blockShortcuts?: boolean | string[];
76
+ /** Prompt beforeunload while preventExit is active and fullscreen is active. Default: false. */
77
+ confirmOnLeave?: boolean;
78
+ /** Prevent contextmenu while fullscreen is active. Default: false. */
79
+ blockContextMenu?: boolean;
80
+ /** Emit 'focusLost' on visibilitychange (hidden) or window.blur while fullscreen is active. Default: false. */
81
+ watchFocus?: boolean;
82
+ }
83
+
84
+ export interface FullscreenAdaptersConfig {
85
+ electron?: {
86
+ autoRelock?: boolean | AutoRelockOptions;
87
+ preventExit?: boolean;
88
+ onError?: ((err: Error) => void) | null;
89
+ };
90
+ puppeteer?: {
91
+ restoreState?: 'normal' | 'maximized' | 'minimized';
92
+ preventExit?: boolean;
93
+ };
94
+ ssr?: {
95
+ enabled?: boolean;
96
+ };
97
+ }
98
+
99
+ export interface FullscreenGlobalConfig {
100
+ mode?: FullscreenPresetMode;
101
+ core?: FullscreenOptions;
102
+ adapters?: FullscreenAdaptersConfig;
103
+ }
104
+
105
+ export interface ResolvedFullscreenConfig {
106
+ mode: FullscreenPresetMode;
107
+ core: FullscreenOptions;
108
+ adapters: {
109
+ electron: NonNullable<FullscreenAdaptersConfig['electron']>;
110
+ puppeteer: NonNullable<FullscreenAdaptersConfig['puppeteer']>;
111
+ ssr: NonNullable<FullscreenAdaptersConfig['ssr']>;
112
+ };
113
+ }
114
+
115
+ export interface InitFullscreenContext {
116
+ adapter?: 'electron' | 'puppeteer' | 'ssr' | 'core';
117
+ target?: Element | null;
118
+ bridge?: any;
119
+ page?: any;
120
+ overrides?: FullscreenGlobalConfig;
61
121
  }
62
122
 
63
- export type FullscreenEvent = 'change' | 'enter' | 'exit' | 'error';
123
+ export type FullscreenEvent = 'change' | 'enter' | 'exit' | 'exitAttempt' | 'focusLost' | 'error';
124
+
125
+ export interface ExitAttemptPayload {
126
+ reason: 'api' | 'escape' | 'shortcut' | 'button' | 'external';
127
+ state: FullscreenState;
128
+ }
129
+
130
+ export interface FocusLostPayload {
131
+ reason: 'visibilitychange' | 'blur';
132
+ state: FullscreenState;
133
+ }
64
134
 
65
135
  export type FullscreenListener = (state: FullscreenState) => void;
136
+ export type FullscreenExitAttemptListener = (payload: ExitAttemptPayload) => void;
137
+ export type FullscreenFocusLostListener = (payload: FocusLostPayload) => void;
66
138
  export type FullscreenErrorListener = (err: Error) => void;
67
139
 
68
140
  export interface FullscreenInstance {
@@ -70,17 +142,24 @@ export interface FullscreenInstance {
70
142
  exit(): Promise<void>;
71
143
  toggle(): Promise<void>;
72
144
  unlock(): void;
145
+ relock(): Promise<void>;
73
146
  isActive(): boolean;
74
147
  isSupported(): boolean;
75
148
  isFallback(): boolean;
76
149
  getState(): FullscreenState;
77
150
  on(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): () => void;
151
+ on(event: 'exitAttempt', cb: FullscreenExitAttemptListener): () => void;
152
+ on(event: 'focusLost', cb: FullscreenFocusLostListener): () => void;
78
153
  on(event: 'error', cb: FullscreenErrorListener): () => void;
79
154
  on(event: FullscreenEvent, cb: (...args: any[]) => void): () => void;
80
155
  once(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): () => void;
156
+ once(event: 'exitAttempt', cb: FullscreenExitAttemptListener): () => void;
157
+ once(event: 'focusLost', cb: FullscreenFocusLostListener): () => void;
81
158
  once(event: 'error', cb: FullscreenErrorListener): () => void;
82
159
  once(event: FullscreenEvent, cb: (...args: any[]) => void): () => void;
83
160
  off(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): void;
161
+ off(event: 'exitAttempt', cb: FullscreenExitAttemptListener): void;
162
+ off(event: 'focusLost', cb: FullscreenFocusLostListener): void;
84
163
  off(event: 'error', cb: FullscreenErrorListener): void;
85
164
  off(event: FullscreenEvent, cb: (...args: any[]) => void): void;
86
165
  destroy(): void;
@@ -88,6 +167,20 @@ export interface FullscreenInstance {
88
167
  readonly _options: FullscreenOptions;
89
168
  }
90
169
 
170
+ export declare const PRESETS: Record<FullscreenPresetMode, ResolvedFullscreenConfig>;
171
+
172
+ export function defineConfig(config?: FullscreenGlobalConfig): FullscreenGlobalConfig;
173
+
174
+ export function resolveConfig(
175
+ config?: FullscreenGlobalConfig,
176
+ overrides?: FullscreenGlobalConfig
177
+ ): ResolvedFullscreenConfig;
178
+
179
+ export function initFullscreen(
180
+ config?: FullscreenGlobalConfig,
181
+ context?: InitFullscreenContext
182
+ ): any;
183
+
91
184
  export function createFullscreen(options?: FullscreenOptions): FullscreenInstance;
92
185
 
93
186
  export function detectEnvironment(root?: Element | null): FullscreenEnvironment | null;
package/types/ssr.d.ts CHANGED
@@ -9,6 +9,7 @@ export interface SSRFullscreenInstance {
9
9
  request(): Promise<never>;
10
10
  exit(): Promise<void>;
11
11
  toggle(): Promise<never>;
12
+ unlock(): void;
12
13
  isActive(): false;
13
14
  isSupported(): false;
14
15
  isFallback(): false;