clarity-js 0.8.42 → 0.8.43

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