fullscreen-engine 1.1.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.
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/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)
@@ -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
@@ -15,6 +15,7 @@ export interface FullscreenEnvironment {
15
15
  exit: string | null;
16
16
  elementProp: string | null;
17
17
  enabledProp: string | null;
18
+ prefix?: '' | 'webkit' | 'moz' | 'ms';
18
19
  changeEvent: string;
19
20
  errorEvent: string;
20
21
  supported: boolean;
@@ -60,6 +61,24 @@ export interface FullscreenOptions {
60
61
  autoRelock?: boolean | AutoRelockOptions;
61
62
  /** Block CSS Escape key and manual exit() calls until unlock() is called. Default: false. */
62
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;
63
82
  }
64
83
 
65
84
  export interface FullscreenAdaptersConfig {
@@ -101,9 +120,21 @@ export interface InitFullscreenContext {
101
120
  overrides?: FullscreenGlobalConfig;
102
121
  }
103
122
 
104
- 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
+ }
105
134
 
106
135
  export type FullscreenListener = (state: FullscreenState) => void;
136
+ export type FullscreenExitAttemptListener = (payload: ExitAttemptPayload) => void;
137
+ export type FullscreenFocusLostListener = (payload: FocusLostPayload) => void;
107
138
  export type FullscreenErrorListener = (err: Error) => void;
108
139
 
109
140
  export interface FullscreenInstance {
@@ -111,17 +142,24 @@ export interface FullscreenInstance {
111
142
  exit(): Promise<void>;
112
143
  toggle(): Promise<void>;
113
144
  unlock(): void;
145
+ relock(): Promise<void>;
114
146
  isActive(): boolean;
115
147
  isSupported(): boolean;
116
148
  isFallback(): boolean;
117
149
  getState(): FullscreenState;
118
150
  on(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): () => void;
151
+ on(event: 'exitAttempt', cb: FullscreenExitAttemptListener): () => void;
152
+ on(event: 'focusLost', cb: FullscreenFocusLostListener): () => void;
119
153
  on(event: 'error', cb: FullscreenErrorListener): () => void;
120
154
  on(event: FullscreenEvent, cb: (...args: any[]) => void): () => void;
121
155
  once(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): () => void;
156
+ once(event: 'exitAttempt', cb: FullscreenExitAttemptListener): () => void;
157
+ once(event: 'focusLost', cb: FullscreenFocusLostListener): () => void;
122
158
  once(event: 'error', cb: FullscreenErrorListener): () => void;
123
159
  once(event: FullscreenEvent, cb: (...args: any[]) => void): () => void;
124
160
  off(event: 'change' | 'enter' | 'exit', cb: FullscreenListener): void;
161
+ off(event: 'exitAttempt', cb: FullscreenExitAttemptListener): void;
162
+ off(event: 'focusLost', cb: FullscreenFocusLostListener): void;
125
163
  off(event: 'error', cb: FullscreenErrorListener): void;
126
164
  off(event: FullscreenEvent, cb: (...args: any[]) => void): void;
127
165
  destroy(): void;
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;