|
1 | 1 | import lock from './lock.ts'; |
| 2 | +import type { Filter } from './lock.ts'; |
| 3 | + |
| 4 | +export type Scope = 'inside' | 'outside' | 'self'; |
| 5 | + |
| 6 | +export interface Options { |
| 7 | + scope?: Scope; |
| 8 | + timeout?: number; |
| 9 | +} |
| 10 | + |
| 11 | +const blokrs = new WeakMap<Element | typeof globalThis, Blokr>(); |
2 | 12 |
|
3 | 13 | class Blokr { |
4 | | - private _timeout: number; |
| 14 | + private _target: Element | undefined; |
5 | 15 |
|
6 | | - private _timerId: number; |
| 16 | + private _timerId: number | undefined; |
7 | 17 |
|
8 | | - private _counter: number; |
| 18 | + private _filter: Filter | undefined; |
9 | 19 |
|
10 | 20 | /** |
11 | 21 | * Creates the Blokr singleton instance. |
12 | 22 | */ |
13 | | - constructor () { |
14 | | - this._timeout = 10000; // Default timeout of 10 seconds |
15 | | - this._timerId = 0; |
16 | | - this._counter = 0; |
| 23 | + constructor (target?: Element) { |
| 24 | + this._target = target; |
| 25 | + this._timerId = undefined; |
17 | 26 | } |
18 | 27 |
|
19 | 28 | /** |
20 | | - * Prevents user interactions. |
| 29 | + * Locks user interactions with optional timeout and scope configuration. |
| 30 | + * Returns false if already locked without making any changes. |
| 31 | + * @param [options] - Lock configuration options. |
| 32 | + * @returns true if lock was applied, false if already locked. |
21 | 33 | */ |
22 | | - lock () { |
23 | | - lock.on(); |
24 | | - this._counter++; |
25 | | - |
26 | | - if (this._timerId) { |
27 | | - self.clearTimeout(this._timerId); |
28 | | - this._timerId = 0; |
| 34 | + lock (options?: Options) { |
| 35 | + if (this.isLocked()) { |
| 36 | + return false; |
29 | 37 | } |
30 | | - if (this._timeout) { |
31 | | - this._timerId = self.setTimeout(() => { |
32 | | - this._timerId = 0; |
33 | | - this._counter = 0; |
34 | | - lock.off(); |
35 | | - }, this._timeout); |
| 38 | + |
| 39 | + const scope = options?.scope ?? 'inside'; |
| 40 | + const timeout = options?.timeout ?? 0; |
| 41 | + |
| 42 | + this._filter = (eventTarget: Element) => { |
| 43 | + if (this._target) { |
| 44 | + if (scope === 'self') { |
| 45 | + return this._target === eventTarget; |
| 46 | + } |
| 47 | + const contains = this._target.contains(eventTarget); |
| 48 | + // For 'outside' scope, block events outside target; otherwise block events inside target |
| 49 | + return scope === 'outside' ? !contains : contains; |
| 50 | + } |
| 51 | + // No target specified: block all events |
| 52 | + return true; |
| 53 | + }; |
| 54 | + lock.register(this._filter); |
| 55 | + |
| 56 | + if (timeout > 0) { |
| 57 | + this._timerId = globalThis.setTimeout(() => this.unlock(), timeout); |
36 | 58 | } |
| 59 | + |
| 60 | + return true; |
37 | 61 | } |
38 | 62 |
|
39 | 63 | /** |
40 | | - * Checks if user interactions are currently prevented. |
41 | | - * @returns {boolean} Returns true if interactions are blocked, false otherwise. |
| 64 | + * Checks if user interactions are currently locked. |
| 65 | + * @returns true if locked, false otherwise. |
42 | 66 | */ |
43 | 67 | isLocked () { |
44 | | - return this._counter > 0; |
| 68 | + return !!this._filter; |
45 | 69 | } |
46 | 70 |
|
47 | 71 | /** |
48 | | - * Sets the timeout duration for automatic unlock. |
49 | | - * @param timeout - The timeout in milliseconds. Set to 0 to disable automatic unlock. Negative values are treated as 0. |
50 | | - * @returns {boolean} Returns true if the timeout was set successfully, false if currently locked. |
| 72 | + * Unlocks user interactions and clears any pending timeout. |
| 73 | + * Safe to call even when not locked. |
51 | 74 | */ |
52 | | - setTimeout(timeout: number) { |
53 | | - if (!this.isLocked()) { |
54 | | - this._timeout = timeout < 0 ? 0 : timeout; |
55 | | - return true; |
| 75 | + unlock () { |
| 76 | + if (this._timerId) { |
| 77 | + globalThis.clearTimeout(this._timerId); |
| 78 | + this._timerId = undefined; |
56 | 79 | } |
57 | | - return false; |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Decrements the internal counter and releases the lock when the counter reaches zero. |
62 | | - * If abort is true, the counter is reset to zero immediately, effectively releasing the lock. |
63 | | - * Clears any pending timeout and triggers the unlock event. |
64 | | - * @param abort - If true, immediately resets the counter to zero and releases the lock |
65 | | - */ |
66 | | - unlock (abort = false) { |
67 | | - if (this._counter > 0) { |
68 | | - this._counter--; |
69 | | - |
70 | | - if (abort) { |
71 | | - this._counter = 0; |
72 | | - } |
73 | | - if (this._counter === 0) { |
74 | | - if (this._timerId) { |
75 | | - self.clearTimeout(this._timerId); |
76 | | - this._timerId = 0; |
77 | | - } |
78 | | - lock.off(); |
79 | | - } |
| 80 | + if (this._filter) { |
| 81 | + lock.unregister(this._filter); |
80 | 82 | } |
| 83 | + this._filter = undefined; |
81 | 84 | } |
82 | 85 | } |
83 | 86 |
|
84 | | -export default new Blokr(); |
| 87 | +const blokr = (target?: Element) => { |
| 88 | + return blokrs.get(target ?? globalThis) ?? (() => { |
| 89 | + const instance = new Blokr(target); |
| 90 | + blokrs.set(target ?? globalThis, instance); |
| 91 | + return instance; |
| 92 | + })(); |
| 93 | +}; |
| 94 | + |
| 95 | +export default blokr; |
0 commit comments