clarity-js 0.7.66 → 0.7.67

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 (102) hide show
  1. package/README.md +26 -26
  2. package/build/clarity.extended.js +1 -1
  3. package/build/clarity.insight.js +1 -1
  4. package/build/clarity.js +5410 -5372
  5. package/build/clarity.min.js +1 -1
  6. package/build/clarity.module.js +5410 -5372
  7. package/build/clarity.performance.js +1 -1
  8. package/package.json +70 -70
  9. package/rollup.config.ts +98 -98
  10. package/src/clarity.ts +60 -60
  11. package/src/core/api.ts +8 -8
  12. package/src/core/config.ts +32 -32
  13. package/src/core/copy.ts +3 -3
  14. package/src/core/event.ts +26 -26
  15. package/src/core/hash.ts +19 -19
  16. package/src/core/history.ts +73 -73
  17. package/src/core/index.ts +83 -83
  18. package/src/core/measure.ts +19 -19
  19. package/src/core/report.ts +28 -28
  20. package/src/core/scrub.ts +196 -196
  21. package/src/core/task.ts +180 -180
  22. package/src/core/time.ts +19 -19
  23. package/src/core/timeout.ts +10 -10
  24. package/src/core/version.ts +2 -2
  25. package/src/data/baseline.ts +123 -92
  26. package/src/data/compress.ts +31 -31
  27. package/src/data/consent.ts +20 -20
  28. package/src/data/custom.ts +23 -23
  29. package/src/data/dimension.ts +53 -53
  30. package/src/data/encode.ts +134 -127
  31. package/src/data/envelope.ts +53 -53
  32. package/src/data/extract.ts +204 -204
  33. package/src/data/index.ts +46 -46
  34. package/src/data/limit.ts +44 -44
  35. package/src/data/metadata.ts +356 -356
  36. package/src/data/metric.ts +51 -51
  37. package/src/data/ping.ts +36 -36
  38. package/src/data/signal.ts +30 -30
  39. package/src/data/summary.ts +34 -34
  40. package/src/data/token.ts +39 -39
  41. package/src/data/upgrade.ts +37 -37
  42. package/src/data/upload.ts +283 -283
  43. package/src/data/variable.ts +83 -83
  44. package/src/diagnostic/encode.ts +40 -40
  45. package/src/diagnostic/fraud.ts +36 -36
  46. package/src/diagnostic/index.ts +15 -15
  47. package/src/diagnostic/internal.ts +28 -28
  48. package/src/diagnostic/script.ts +37 -37
  49. package/src/global.ts +6 -6
  50. package/src/index.ts +9 -9
  51. package/src/insight/blank.ts +14 -14
  52. package/src/insight/encode.ts +60 -60
  53. package/src/insight/snapshot.ts +114 -114
  54. package/src/interaction/change.ts +40 -40
  55. package/src/interaction/click.ts +156 -156
  56. package/src/interaction/clipboard.ts +34 -34
  57. package/src/interaction/encode.ts +191 -191
  58. package/src/interaction/index.ts +59 -59
  59. package/src/interaction/input.ts +59 -59
  60. package/src/interaction/pointer.ts +139 -139
  61. package/src/interaction/resize.ts +47 -47
  62. package/src/interaction/scroll.ts +124 -124
  63. package/src/interaction/selection.ts +68 -68
  64. package/src/interaction/submit.ts +32 -32
  65. package/src/interaction/timeline.ts +65 -65
  66. package/src/interaction/unload.ts +28 -28
  67. package/src/interaction/visibility.ts +26 -26
  68. package/src/layout/animation.ts +133 -133
  69. package/src/layout/discover.ts +31 -31
  70. package/src/layout/document.ts +48 -48
  71. package/src/layout/dom.ts +414 -414
  72. package/src/layout/encode.ts +145 -145
  73. package/src/layout/index.ts +41 -41
  74. package/src/layout/mutation.ts +340 -340
  75. package/src/layout/node.ts +264 -264
  76. package/src/layout/offset.ts +19 -19
  77. package/src/layout/region.ts +153 -153
  78. package/src/layout/schema.ts +63 -63
  79. package/src/layout/selector.ts +82 -82
  80. package/src/layout/style.ts +149 -149
  81. package/src/layout/target.ts +32 -32
  82. package/src/layout/traverse.ts +27 -27
  83. package/src/performance/blank.ts +7 -7
  84. package/src/performance/encode.ts +31 -31
  85. package/src/performance/index.ts +14 -14
  86. package/src/performance/interaction.ts +125 -125
  87. package/src/performance/navigation.ts +31 -31
  88. package/src/performance/observer.ts +108 -108
  89. package/src/queue.ts +33 -33
  90. package/test/core.test.ts +139 -139
  91. package/test/helper.ts +162 -162
  92. package/test/html/core.html +27 -27
  93. package/test/tsconfig.test.json +5 -5
  94. package/tsconfig.json +21 -21
  95. package/tslint.json +32 -32
  96. package/types/core.d.ts +151 -151
  97. package/types/data.d.ts +504 -497
  98. package/types/diagnostic.d.ts +24 -24
  99. package/types/index.d.ts +39 -39
  100. package/types/interaction.d.ts +157 -157
  101. package/types/layout.d.ts +273 -273
  102. package/types/performance.d.ts +64 -64
@@ -1,139 +1,139 @@
1
- import { Event } from "@clarity-types/data";
2
- import { PointerState, Setting } from "@clarity-types/interaction";
3
- import { FunctionNames } from "@clarity-types/performance";
4
- import { bind } from "@src/core/event";
5
- import { schedule } from "@src/core/task";
6
- import { time } from "@src/core/time";
7
- import { clearTimeout, setTimeout } from "@src/core/timeout";
8
- import { iframe } from "@src/layout/dom";
9
- import { offset } from "@src/layout/offset";
10
- import { target } from "@src/layout/target";
11
- import encode from "./encode";
12
-
13
- export let state: PointerState[] = [];
14
- let timeout: number = null;
15
- let hasPrimaryTouch = false;
16
- let primaryTouchId = 0;
17
- const activeTouchPointIds = new Set<number>();
18
-
19
- export function start(): void {
20
- reset();
21
- }
22
-
23
- export function observe(root: Node): void {
24
- bind(root, "mousedown", mouse.bind(this, Event.MouseDown, root), true);
25
- bind(root, "mouseup", mouse.bind(this, Event.MouseUp, root), true);
26
- bind(root, "mousemove", mouse.bind(this, Event.MouseMove, root), true);
27
- bind(root, "wheel", mouse.bind(this, Event.MouseWheel, root), true);
28
- bind(root, "dblclick", mouse.bind(this, Event.DoubleClick, root), true);
29
- bind(root, "touchstart", touch.bind(this, Event.TouchStart, root), true);
30
- bind(root, "touchend", touch.bind(this, Event.TouchEnd, root), true);
31
- bind(root, "touchmove", touch.bind(this, Event.TouchMove, root), true);
32
- bind(root, "touchcancel", touch.bind(this, Event.TouchCancel, root), true);
33
- }
34
-
35
- function mouse(event: Event, root: Node, evt: MouseEvent): void {
36
- let frame = iframe(root);
37
- let d = frame ? frame.contentDocument.documentElement : document.documentElement;
38
- let x = "pageX" in evt ? Math.round(evt.pageX) : ("clientX" in evt ? Math.round(evt["clientX"] + d.scrollLeft) : null);
39
- let y = "pageY" in evt ? Math.round(evt.pageY) : ("clientY" in evt ? Math.round(evt["clientY"] + d.scrollTop) : null);
40
- // In case of iframe, we adjust (x,y) to be relative to top parent's origin
41
- if (frame) {
42
- let distance = offset(frame);
43
- x = x ? x + Math.round(distance.x) : x;
44
- y = y ? y + Math.round(distance.y) : y;
45
- }
46
-
47
- // Check for null values before processing this event
48
- if (x !== null && y !== null) { handler({ time: time(evt), event, data: { target: target(evt), x, y } }); }
49
- }
50
- mouse.dn = FunctionNames.PointerMouse;
51
-
52
- function touch(event: Event, root: Node, evt: TouchEvent): void {
53
- let frame = iframe(root);
54
- let d = frame ? frame.contentDocument.documentElement : document.documentElement;
55
- let touches = evt.changedTouches;
56
-
57
- let t = time(evt);
58
- if (touches) {
59
- for (let i = 0; i < touches.length; i++) {
60
- let entry = touches[i];
61
- let x = "clientX" in entry ? Math.round(entry["clientX"] + d.scrollLeft) : null;
62
- let y = "clientY" in entry ? Math.round(entry["clientY"] + d.scrollTop) : null;
63
- x = x && frame ? x + Math.round(frame.offsetLeft) : x;
64
- y = y && frame ? y + Math.round(frame.offsetTop) : y;
65
-
66
- // We cannot rely on identifier to determine primary touch as its value doesn't always start with 0.
67
- // Safari/Webkit uses the address of the UITouch object as the identifier value for each touch point.
68
- const id = "identifier" in entry ? entry["identifier"] : undefined;
69
-
70
- switch(event) {
71
- case Event.TouchStart:
72
- if (activeTouchPointIds.size === 0) {
73
- // Track presence of primary touch separately to handle scenarios when same id is repeated
74
- hasPrimaryTouch = true;
75
- primaryTouchId = id;
76
- }
77
- activeTouchPointIds.add(id);
78
- break;
79
- case Event.TouchEnd:
80
- case Event.TouchCancel:
81
- activeTouchPointIds.delete(id);
82
- break;
83
- }
84
- const isPrimary = hasPrimaryTouch && primaryTouchId === id;
85
-
86
- // Check for null values before processing this event
87
- if (x !== null && y !== null) { handler({ time: t, event, data: { target: target(evt), x, y, id, isPrimary } }); }
88
-
89
- // Reset primary touch point id once touch event ends
90
- if (event === Event.TouchCancel || event === Event.TouchEnd) {
91
- if (primaryTouchId === id) { hasPrimaryTouch = false; }
92
- }
93
- }
94
- }
95
- }
96
- touch.dn = FunctionNames.PointerTouch;
97
-
98
- function handler(current: PointerState): void {
99
- switch (current.event) {
100
- case Event.MouseMove:
101
- case Event.MouseWheel:
102
- case Event.TouchMove:
103
- let length = state.length;
104
- let last = length > 1 ? state[length - 2] : null;
105
- if (last && similar(last, current)) { state.pop(); }
106
- state.push(current);
107
-
108
- clearTimeout(timeout);
109
- timeout = setTimeout(process, Setting.LookAhead, current.event);
110
- break;
111
- default:
112
- state.push(current);
113
- process(current.event);
114
- break;
115
- }
116
- }
117
-
118
- function process(event: Event): void {
119
- schedule(encode.bind(this, event));
120
- }
121
-
122
- export function reset(): void {
123
- state = [];
124
- }
125
-
126
- function similar(last: PointerState, current: PointerState): boolean {
127
- let dx = last.data.x - current.data.x;
128
- let dy = last.data.y - current.data.y;
129
- let distance = Math.sqrt(dx * dx + dy * dy);
130
- let gap = current.time - last.time;
131
- let match = current.data.target === last.data.target;
132
- return current.event === last.event && match && distance < Setting.Distance && gap < Setting.Interval;
133
- }
134
-
135
- export function stop(): void {
136
- clearTimeout(timeout);
137
- // Send out any pending pointer events in the pipeline
138
- if (state.length > 0) { process(state[state.length - 1].event); }
139
- }
1
+ import { Event } from "@clarity-types/data";
2
+ import { PointerState, Setting } from "@clarity-types/interaction";
3
+ import { FunctionNames } from "@clarity-types/performance";
4
+ import { bind } from "@src/core/event";
5
+ import { schedule } from "@src/core/task";
6
+ import { time } from "@src/core/time";
7
+ import { clearTimeout, setTimeout } from "@src/core/timeout";
8
+ import { iframe } from "@src/layout/dom";
9
+ import { offset } from "@src/layout/offset";
10
+ import { target } from "@src/layout/target";
11
+ import encode from "./encode";
12
+
13
+ export let state: PointerState[] = [];
14
+ let timeout: number = null;
15
+ let hasPrimaryTouch = false;
16
+ let primaryTouchId = 0;
17
+ const activeTouchPointIds = new Set<number>();
18
+
19
+ export function start(): void {
20
+ reset();
21
+ }
22
+
23
+ export function observe(root: Node): void {
24
+ bind(root, "mousedown", mouse.bind(this, Event.MouseDown, root), true);
25
+ bind(root, "mouseup", mouse.bind(this, Event.MouseUp, root), true);
26
+ bind(root, "mousemove", mouse.bind(this, Event.MouseMove, root), true);
27
+ bind(root, "wheel", mouse.bind(this, Event.MouseWheel, root), true);
28
+ bind(root, "dblclick", mouse.bind(this, Event.DoubleClick, root), true);
29
+ bind(root, "touchstart", touch.bind(this, Event.TouchStart, root), true);
30
+ bind(root, "touchend", touch.bind(this, Event.TouchEnd, root), true);
31
+ bind(root, "touchmove", touch.bind(this, Event.TouchMove, root), true);
32
+ bind(root, "touchcancel", touch.bind(this, Event.TouchCancel, root), true);
33
+ }
34
+
35
+ function mouse(event: Event, root: Node, evt: MouseEvent): void {
36
+ mouse.dn = FunctionNames.PointerMouse;
37
+ let frame = iframe(root);
38
+ let d = frame ? frame.contentDocument.documentElement : document.documentElement;
39
+ let x = "pageX" in evt ? Math.round(evt.pageX) : ("clientX" in evt ? Math.round(evt["clientX"] + d.scrollLeft) : null);
40
+ let y = "pageY" in evt ? Math.round(evt.pageY) : ("clientY" in evt ? Math.round(evt["clientY"] + d.scrollTop) : null);
41
+ // In case of iframe, we adjust (x,y) to be relative to top parent's origin
42
+ if (frame) {
43
+ let distance = offset(frame);
44
+ x = x ? x + Math.round(distance.x) : x;
45
+ y = y ? y + Math.round(distance.y) : y;
46
+ }
47
+
48
+ // Check for null values before processing this event
49
+ if (x !== null && y !== null) { handler({ time: time(evt), event, data: { target: target(evt), x, y } }); }
50
+ }
51
+
52
+ function touch(event: Event, root: Node, evt: TouchEvent): void {
53
+ touch.dn = FunctionNames.PointerTouch;
54
+ let frame = iframe(root);
55
+ let d = frame ? frame.contentDocument.documentElement : document.documentElement;
56
+ let touches = evt.changedTouches;
57
+
58
+ let t = time(evt);
59
+ if (touches) {
60
+ for (let i = 0; i < touches.length; i++) {
61
+ let entry = touches[i];
62
+ let x = "clientX" in entry ? Math.round(entry["clientX"] + d.scrollLeft) : null;
63
+ let y = "clientY" in entry ? Math.round(entry["clientY"] + d.scrollTop) : null;
64
+ x = x && frame ? x + Math.round(frame.offsetLeft) : x;
65
+ y = y && frame ? y + Math.round(frame.offsetTop) : y;
66
+
67
+ // We cannot rely on identifier to determine primary touch as its value doesn't always start with 0.
68
+ // Safari/Webkit uses the address of the UITouch object as the identifier value for each touch point.
69
+ const id = "identifier" in entry ? entry["identifier"] : undefined;
70
+
71
+ switch(event) {
72
+ case Event.TouchStart:
73
+ if (activeTouchPointIds.size === 0) {
74
+ // Track presence of primary touch separately to handle scenarios when same id is repeated
75
+ hasPrimaryTouch = true;
76
+ primaryTouchId = id;
77
+ }
78
+ activeTouchPointIds.add(id);
79
+ break;
80
+ case Event.TouchEnd:
81
+ case Event.TouchCancel:
82
+ activeTouchPointIds.delete(id);
83
+ break;
84
+ }
85
+ const isPrimary = hasPrimaryTouch && primaryTouchId === id;
86
+
87
+ // Check for null values before processing this event
88
+ if (x !== null && y !== null) { handler({ time: t, event, data: { target: target(evt), x, y, id, isPrimary } }); }
89
+
90
+ // Reset primary touch point id once touch event ends
91
+ if (event === Event.TouchCancel || event === Event.TouchEnd) {
92
+ if (primaryTouchId === id) { hasPrimaryTouch = false; }
93
+ }
94
+ }
95
+ }
96
+ }
97
+
98
+ function handler(current: PointerState): void {
99
+ switch (current.event) {
100
+ case Event.MouseMove:
101
+ case Event.MouseWheel:
102
+ case Event.TouchMove:
103
+ let length = state.length;
104
+ let last = length > 1 ? state[length - 2] : null;
105
+ if (last && similar(last, current)) { state.pop(); }
106
+ state.push(current);
107
+
108
+ clearTimeout(timeout);
109
+ timeout = setTimeout(process, Setting.LookAhead, current.event);
110
+ break;
111
+ default:
112
+ state.push(current);
113
+ process(current.event);
114
+ break;
115
+ }
116
+ }
117
+
118
+ function process(event: Event): void {
119
+ schedule(encode.bind(this, event));
120
+ }
121
+
122
+ export function reset(): void {
123
+ state = [];
124
+ }
125
+
126
+ function similar(last: PointerState, current: PointerState): boolean {
127
+ let dx = last.data.x - current.data.x;
128
+ let dy = last.data.y - current.data.y;
129
+ let distance = Math.sqrt(dx * dx + dy * dy);
130
+ let gap = current.time - last.time;
131
+ let match = current.data.target === last.data.target;
132
+ return current.event === last.event && match && distance < Setting.Distance && gap < Setting.Interval;
133
+ }
134
+
135
+ export function stop(): void {
136
+ clearTimeout(timeout);
137
+ // Send out any pending pointer events in the pipeline
138
+ if (state.length > 0) { process(state[state.length - 1].event); }
139
+ }
@@ -1,48 +1,48 @@
1
- import { Event } from "@clarity-types/data";
2
- import { ResizeData, Setting } from "@clarity-types/interaction";
3
- import { FunctionNames } from "@clarity-types/performance";
4
- import { clearTimeout, setTimeout } from "@src/core/timeout";
5
- import { bind } from "@src/core/event";
6
- import encode from "./encode";
7
- import { schedule } from "@src/core/task";
8
-
9
- export let data: ResizeData;
10
- let timeout: number = null;
11
- let initialStateLogged = false;
12
-
13
- export function start(): void {
14
- initialStateLogged = false;
15
- bind(window, "resize", recompute);
16
- recompute();
17
- }
18
-
19
- function recompute(): void {
20
- let de = document.documentElement;
21
- // window.innerWidth includes width of the scrollbar and is not a true representation of the viewport width.
22
- // Therefore, when possible, use documentElement's clientWidth property.
23
- data = {
24
- width: de && "clientWidth" in de ? Math.min(de.clientWidth, window.innerWidth) : window.innerWidth,
25
- height: de && "clientHeight" in de ? Math.min(de.clientHeight, window.innerHeight) : window.innerHeight,
26
- };
27
- if (initialStateLogged) {
28
- clearTimeout(timeout);
29
- timeout = setTimeout(process, Setting.LookAhead, Event.Resize);
30
- } else {
31
- encode(Event.Resize);
32
- initialStateLogged = true;
33
- }
34
- }
35
- recompute.dn = FunctionNames.ResizeRecompute;
36
-
37
- function process(event: Event): void {
38
- schedule(encode.bind(this, event));
39
- }
40
-
41
- export function reset(): void {
42
- data = null;
43
- clearTimeout(timeout);
44
- }
45
-
46
- export function stop(): void {
47
- reset();
1
+ import { Event } from "@clarity-types/data";
2
+ import { ResizeData, Setting } from "@clarity-types/interaction";
3
+ import { FunctionNames } from "@clarity-types/performance";
4
+ import { clearTimeout, setTimeout } from "@src/core/timeout";
5
+ import { bind } from "@src/core/event";
6
+ import encode from "./encode";
7
+ import { schedule } from "@src/core/task";
8
+
9
+ export let data: ResizeData;
10
+ let timeout: number = null;
11
+ let initialStateLogged = false;
12
+
13
+ export function start(): void {
14
+ initialStateLogged = false;
15
+ bind(window, "resize", recompute);
16
+ recompute();
17
+ }
18
+
19
+ function recompute(): void {
20
+ recompute.dn = FunctionNames.ResizeRecompute;
21
+ let de = document.documentElement;
22
+ // window.innerWidth includes width of the scrollbar and is not a true representation of the viewport width.
23
+ // Therefore, when possible, use documentElement's clientWidth property.
24
+ data = {
25
+ width: de && "clientWidth" in de ? Math.min(de.clientWidth, window.innerWidth) : window.innerWidth,
26
+ height: de && "clientHeight" in de ? Math.min(de.clientHeight, window.innerHeight) : window.innerHeight,
27
+ };
28
+ if (initialStateLogged) {
29
+ clearTimeout(timeout);
30
+ timeout = setTimeout(process, Setting.LookAhead, Event.Resize);
31
+ } else {
32
+ encode(Event.Resize);
33
+ initialStateLogged = true;
34
+ }
35
+ }
36
+
37
+ function process(event: Event): void {
38
+ schedule(encode.bind(this, event));
39
+ }
40
+
41
+ export function reset(): void {
42
+ data = null;
43
+ clearTimeout(timeout);
44
+ }
45
+
46
+ export function stop(): void {
47
+ reset();
48
48
  }
@@ -1,124 +1,124 @@
1
- import { Constant, Dimension, Event } from "@clarity-types/data";
2
- import { ScrollState, Setting } from "@clarity-types/interaction";
3
- import { FunctionNames } from "@clarity-types/performance";
4
- import { bind } from "@src/core/event";
5
- import { schedule } from "@src/core/task";
6
- import { time } from "@src/core/time";
7
- import { clearTimeout, setTimeout } from "@src/core/timeout";
8
- import { iframe } from "@src/layout/dom";
9
- import { target, metadata } from "@src/layout/target";
10
- import encode from "./encode";
11
- import * as dimension from "@src/data/dimension";
12
-
13
- export let state: ScrollState[] = [];
14
- let initialTop: Node = null;
15
- let initialBottom: Node = null;
16
- let timeout: number = null;
17
-
18
- export function start(): void {
19
- state = [];
20
- recompute();
21
- }
22
-
23
- export function observe(root: Node): void {
24
- let frame = iframe(root);
25
- let node = frame ? frame.contentWindow : (root === document ? window : root);
26
- bind(node, "scroll", recompute, true);
27
- }
28
-
29
- function recompute(event: UIEvent = null): void {
30
- let w = window as Window;
31
- let de = document.documentElement;
32
- let element = event ? target(event) : de;
33
-
34
- // If the target is a Document node, then identify corresponding documentElement and window for this document
35
- if (element && element.nodeType === Node.DOCUMENT_NODE) {
36
- let frame = iframe(element);
37
- w = frame ? frame.contentWindow : w;
38
- element = de = (element as Document).documentElement;
39
- }
40
-
41
- // Edge doesn't support scrollTop position on document.documentElement.
42
- // For cross browser compatibility, looking up pageYOffset on window if the scroll is on document.
43
- // And, if for some reason that is not available, fall back to looking up scrollTop on document.documentElement.
44
- let x = element === de && "pageXOffset" in w ? Math.round(w.pageXOffset) : Math.round((element as HTMLElement).scrollLeft);
45
- let y = element === de && "pageYOffset" in w ? Math.round(w.pageYOffset) : Math.round((element as HTMLElement).scrollTop);
46
- const width = window.innerWidth;
47
- const height = window.innerHeight;
48
- const xPosition = width / 3;
49
- const yOffset = width > height ? height * 0.15 : height * 0.2;
50
- const startYPosition = yOffset;
51
- const endYPosition = height - yOffset;
52
- const top = getPositionNode(xPosition, startYPosition);
53
- const bottom = getPositionNode(xPosition, endYPosition);
54
-
55
- let current: ScrollState = { time: time(event), event: Event.Scroll, data: {target: element, x, y, top, bottom} };
56
-
57
- // We don't send any scroll events if this is the first event and the current position is top (0,0)
58
- if ((event === null && x === 0 && y === 0) || (x === null || y === null)) {
59
- initialTop = top;
60
- initialBottom = bottom;
61
- return;
62
- }
63
-
64
- let length = state.length;
65
- let last = length > 1 ? state[length - 2] : null;
66
- if (last && similar(last, current)) { state.pop(); }
67
- state.push(current);
68
-
69
- clearTimeout(timeout);
70
- timeout = setTimeout(process, Setting.LookAhead, Event.Scroll);
71
- }
72
- recompute.dn = FunctionNames.ScrollRecompute;
73
-
74
- function getPositionNode(x: number, y: number): Node {
75
- let node: Node;
76
- if ("caretPositionFromPoint" in document) {
77
- node = (document as any).caretPositionFromPoint(x, y)?.offsetNode;
78
- } else if ("caretRangeFromPoint" in document) {
79
- node = document.caretRangeFromPoint(x, y)?.startContainer;
80
- }
81
- if (!node) {
82
- node = document.elementFromPoint(x, y) as Node;
83
- }
84
- if (node && node.nodeType === Node.TEXT_NODE) {
85
- node = node.parentNode;
86
- }
87
-
88
- return node;
89
- }
90
-
91
- export function reset(): void {
92
- state = [];
93
- initialTop = null;
94
- initialBottom = null;
95
- }
96
-
97
- function process(event: Event): void {
98
- schedule(encode.bind(this, event));
99
- }
100
-
101
- function similar(last: ScrollState, current: ScrollState): boolean {
102
- let dx = last.data.x - current.data.x;
103
- let dy = last.data.y - current.data.y;
104
- return (dx * dx + dy * dy < Setting.Distance * Setting.Distance) && (current.time - last.time < Setting.Interval);
105
- }
106
-
107
- export function compute(): void {
108
- if (initialTop) {
109
- const top = metadata(initialTop, null);
110
- dimension.log(Dimension.InitialScrollTop, top?.hash?.join(Constant.Dot));
111
- }
112
- if (initialBottom) {
113
- const bottom = metadata(initialBottom, null);
114
- dimension.log(Dimension.InitialScrollBottom, bottom?.hash?.join(Constant.Dot));
115
- }
116
- }
117
- compute.dn = FunctionNames.ScrollCompute;
118
-
119
- export function stop(): void {
120
- clearTimeout(timeout);
121
- state = [];
122
- initialTop = null;
123
- initialBottom = null;
124
- }
1
+ import { Constant, Dimension, Event } from "@clarity-types/data";
2
+ import { ScrollState, Setting } from "@clarity-types/interaction";
3
+ import { FunctionNames } from "@clarity-types/performance";
4
+ import { bind } from "@src/core/event";
5
+ import { schedule } from "@src/core/task";
6
+ import { time } from "@src/core/time";
7
+ import { clearTimeout, setTimeout } from "@src/core/timeout";
8
+ import { iframe } from "@src/layout/dom";
9
+ import { target, metadata } from "@src/layout/target";
10
+ import encode from "./encode";
11
+ import * as dimension from "@src/data/dimension";
12
+
13
+ export let state: ScrollState[] = [];
14
+ let initialTop: Node = null;
15
+ let initialBottom: Node = null;
16
+ let timeout: number = null;
17
+
18
+ export function start(): void {
19
+ state = [];
20
+ recompute();
21
+ }
22
+
23
+ export function observe(root: Node): void {
24
+ let frame = iframe(root);
25
+ let node = frame ? frame.contentWindow : (root === document ? window : root);
26
+ bind(node, "scroll", recompute, true);
27
+ }
28
+
29
+ function recompute(event: UIEvent = null): void {
30
+ recompute.dn = FunctionNames.ScrollRecompute;
31
+ let w = window as Window;
32
+ let de = document.documentElement;
33
+ let element = event ? target(event) : de;
34
+
35
+ // If the target is a Document node, then identify corresponding documentElement and window for this document
36
+ if (element && element.nodeType === Node.DOCUMENT_NODE) {
37
+ let frame = iframe(element);
38
+ w = frame ? frame.contentWindow : w;
39
+ element = de = (element as Document).documentElement;
40
+ }
41
+
42
+ // Edge doesn't support scrollTop position on document.documentElement.
43
+ // For cross browser compatibility, looking up pageYOffset on window if the scroll is on document.
44
+ // And, if for some reason that is not available, fall back to looking up scrollTop on document.documentElement.
45
+ let x = element === de && "pageXOffset" in w ? Math.round(w.pageXOffset) : Math.round((element as HTMLElement).scrollLeft);
46
+ let y = element === de && "pageYOffset" in w ? Math.round(w.pageYOffset) : Math.round((element as HTMLElement).scrollTop);
47
+ const width = window.innerWidth;
48
+ const height = window.innerHeight;
49
+ const xPosition = width / 3;
50
+ const yOffset = width > height ? height * 0.15 : height * 0.2;
51
+ const startYPosition = yOffset;
52
+ const endYPosition = height - yOffset;
53
+ const top = getPositionNode(xPosition, startYPosition);
54
+ const bottom = getPositionNode(xPosition, endYPosition);
55
+
56
+ let current: ScrollState = { time: time(event), event: Event.Scroll, data: {target: element, x, y, top, bottom} };
57
+
58
+ // We don't send any scroll events if this is the first event and the current position is top (0,0)
59
+ if ((event === null && x === 0 && y === 0) || (x === null || y === null)) {
60
+ initialTop = top;
61
+ initialBottom = bottom;
62
+ return;
63
+ }
64
+
65
+ let length = state.length;
66
+ let last = length > 1 ? state[length - 2] : null;
67
+ if (last && similar(last, current)) { state.pop(); }
68
+ state.push(current);
69
+
70
+ clearTimeout(timeout);
71
+ timeout = setTimeout(process, Setting.LookAhead, Event.Scroll);
72
+ }
73
+
74
+ function getPositionNode(x: number, y: number): Node {
75
+ let node: Node;
76
+ if ("caretPositionFromPoint" in document) {
77
+ node = (document as any).caretPositionFromPoint(x, y)?.offsetNode;
78
+ } else if ("caretRangeFromPoint" in document) {
79
+ node = (document as any).caretRangeFromPoint(x, y)?.startContainer;
80
+ }
81
+ if (!node) {
82
+ node = document.elementFromPoint(x, y) as Node;
83
+ }
84
+ if (node && node.nodeType === Node.TEXT_NODE) {
85
+ node = node.parentNode;
86
+ }
87
+
88
+ return node;
89
+ }
90
+
91
+ export function reset(): void {
92
+ state = [];
93
+ initialTop = null;
94
+ initialBottom = null;
95
+ }
96
+
97
+ function process(event: Event): void {
98
+ schedule(encode.bind(this, event));
99
+ }
100
+
101
+ function similar(last: ScrollState, current: ScrollState): boolean {
102
+ let dx = last.data.x - current.data.x;
103
+ let dy = last.data.y - current.data.y;
104
+ return (dx * dx + dy * dy < Setting.Distance * Setting.Distance) && (current.time - last.time < Setting.Interval);
105
+ }
106
+
107
+ export function compute(): void {
108
+ compute.dn = FunctionNames.ScrollCompute;
109
+ if (initialTop) {
110
+ const top = metadata(initialTop, null);
111
+ dimension.log(Dimension.InitialScrollTop, top?.hash?.join(Constant.Dot));
112
+ }
113
+ if (initialBottom) {
114
+ const bottom = metadata(initialBottom, null);
115
+ dimension.log(Dimension.InitialScrollBottom, bottom?.hash?.join(Constant.Dot));
116
+ }
117
+ }
118
+
119
+ export function stop(): void {
120
+ clearTimeout(timeout);
121
+ state = [];
122
+ initialTop = null;
123
+ initialBottom = null;
124
+ }