@ax-design/reveal-highlight 0.4.1 → 0.4.2

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.
@@ -1,78 +1,86 @@
1
- import { RevealBoundaryStore } from './RevealBoundryStore.js';
2
-
3
- import { config } from './config.js';
4
-
5
- export class RevealStateManager {
6
- private _currentHashId = 0;
7
- private _storage: RevealBoundaryStore[] = [];
8
-
9
- private requestedTraverseBoundaries = false;
10
- cachedMouseX = -1;
11
- cachedMouseY = -1;
12
-
13
- constructor() {
14
- if (config.borderDetectionMode === 'experimentalAutoFit') {
15
- if (typeof Window === 'undefined') return;
16
-
17
- window.addEventListener('pointermove', this.handlePointerMove);
18
- // This is for touch screen
19
- // Window.pointerleave won't be triggered if the finger
20
- // leave the screen, so we use pointer up here.
21
- window.addEventListener('pointerup', (ev) => {
22
- if (ev.pointerType !== 'mouse') this.handlePointerLeave();
23
- });
24
- // This is for desktop
25
- // If the mouse leave the window, we need to terminate
26
- // all animations and clean up the canvas.
27
- window.addEventListener('pointerleave', (ev) => {
28
- if (ev.pointerType === 'mouse') this.handlePointerLeave();
29
- });
30
- }
31
- }
32
-
33
- pointerLeaveScheduled = false;
34
-
35
- handlePointerLeave = () => {
36
- if (this.pointerLeaveScheduled) return;
37
-
38
- this.pointerLeaveScheduled = true;
39
-
40
- window.requestAnimationFrame(() => {
41
- for (let i = 0; i < this._storage.length; i++) {
42
- this._storage[i].onPointerLeaveBoundary();
43
- }
44
-
45
- this.pointerLeaveScheduled = false;
46
- });
47
- }
48
-
49
- handlePointerMove = (event: PointerEvent) => {
50
- this.cachedMouseX = event.clientX;
51
- this.cachedMouseY = event.clientY;
52
-
53
- if (!this.requestedTraverseBoundaries) {
54
- window.requestAnimationFrame(this.traverseBoundaries);
55
- this.requestedTraverseBoundaries = true;
56
- }
57
- };
58
-
59
- traverseBoundaries = () => {
60
- this.requestedTraverseBoundaries = false;
61
- for (let i = 0; i < this._storage.length; i++) {
62
- this._storage[i].onPointerMoveOnScreen(this.cachedMouseX, this.cachedMouseY);
63
- }
64
- };
65
-
66
- newBoundary = ($el: HTMLElement) => {
67
- const hashId = this._currentHashId++;
68
-
69
- const store = new RevealBoundaryStore(hashId, $el);
70
- this._storage.push(store);
71
-
72
- return store;
73
- };
74
-
75
- removeBoundary = (store: RevealBoundaryStore) => {
76
- this._storage = this._storage.filter((x) => x !== store);
77
- };
78
- }
1
+ import { RevealBoundaryStore } from './RevealBoundryStore.js';
2
+
3
+ import { config } from './config.js';
4
+
5
+ export class RevealStateManager {
6
+ private _currentHashId = 0;
7
+ private _storage: RevealBoundaryStore[] = [];
8
+
9
+ private requestedTraverseBoundaries = false;
10
+ cachedMouseX = -1;
11
+ cachedMouseY = -1;
12
+
13
+ constructor() {
14
+ if (config.borderDetectionMode === 'experimentalAutoFit') {
15
+ if (typeof Window === 'undefined') return;
16
+
17
+ window.addEventListener('pointermove', this.handlePointerMove);
18
+ // This is for touch screen
19
+ // Window.pointerleave won't be triggered if the finger
20
+ // leave the screen, so we use pointer up here.
21
+ window.addEventListener('pointerup', (ev) => {
22
+ if (ev.pointerType !== 'mouse') this.handlePointerLeave();
23
+ });
24
+ // This is for desktop
25
+ // If the mouse leave the window, we need to terminate
26
+ // all animations and clean up the canvas.
27
+ window.addEventListener('pointerleave', (ev) => {
28
+ if (ev.pointerType === 'mouse') this.handlePointerLeave();
29
+ });
30
+ // Handles boundary conditions such as the user scrolling
31
+ // the page, dragging the image, which can cause mouse events
32
+ // to fail, when Reveal should disappear to prevent the
33
+ // image from getting stuck.
34
+ window.addEventListener('pointercancel', (ev) => {
35
+ console.log('pointer cancel');
36
+ this.handlePointerLeave();
37
+ });
38
+ }
39
+ }
40
+
41
+ pointerLeaveScheduled = false;
42
+
43
+ handlePointerLeave = () => {
44
+ if (this.pointerLeaveScheduled) return;
45
+
46
+ this.pointerLeaveScheduled = true;
47
+
48
+ window.requestAnimationFrame(() => {
49
+ for (let i = 0; i < this._storage.length; i++) {
50
+ this._storage[i].onPointerLeaveBoundary();
51
+ }
52
+
53
+ this.pointerLeaveScheduled = false;
54
+ });
55
+ }
56
+
57
+ handlePointerMove = (event: PointerEvent) => {
58
+ this.cachedMouseX = event.clientX;
59
+ this.cachedMouseY = event.clientY;
60
+
61
+ if (!this.requestedTraverseBoundaries) {
62
+ window.requestAnimationFrame(this.traverseBoundaries);
63
+ this.requestedTraverseBoundaries = true;
64
+ }
65
+ };
66
+
67
+ traverseBoundaries = () => {
68
+ this.requestedTraverseBoundaries = false;
69
+ for (let i = 0; i < this._storage.length; i++) {
70
+ this._storage[i].onPointerMoveOnScreen(this.cachedMouseX, this.cachedMouseY);
71
+ }
72
+ };
73
+
74
+ newBoundary = ($el: HTMLElement) => {
75
+ const hashId = this._currentHashId++;
76
+
77
+ const store = new RevealBoundaryStore(hashId, $el);
78
+ this._storage.push(store);
79
+
80
+ return store;
81
+ };
82
+
83
+ removeBoundary = (store: RevealBoundaryStore) => {
84
+ this._storage = this._storage.filter((x) => x !== store);
85
+ };
86
+ }