@gemx-dev/clarity-js 0.8.39

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 (118) hide show
  1. package/README.md +1 -0
  2. package/build/clarity.extended.js +1 -0
  3. package/build/clarity.insight.js +1 -0
  4. package/build/clarity.js +6433 -0
  5. package/build/clarity.livechat.js +1 -0
  6. package/build/clarity.min.js +1 -0
  7. package/build/clarity.module.js +6429 -0
  8. package/build/clarity.performance.js +1 -0
  9. package/build/dynamic/clarity.crisp.js +1 -0
  10. package/build/dynamic/clarity.tidio.js +1 -0
  11. package/package.json +55 -0
  12. package/rollup.config.ts +161 -0
  13. package/src/clarity.ts +71 -0
  14. package/src/core/api.ts +8 -0
  15. package/src/core/config.ts +29 -0
  16. package/src/core/copy.ts +3 -0
  17. package/src/core/dynamic.ts +57 -0
  18. package/src/core/event.ts +53 -0
  19. package/src/core/hash.ts +19 -0
  20. package/src/core/history.ts +71 -0
  21. package/src/core/index.ts +81 -0
  22. package/src/core/measure.ts +19 -0
  23. package/src/core/report.ts +35 -0
  24. package/src/core/scrub.ts +202 -0
  25. package/src/core/task.ts +181 -0
  26. package/src/core/throttle.ts +46 -0
  27. package/src/core/time.ts +26 -0
  28. package/src/core/timeout.ts +10 -0
  29. package/src/core/version.ts +2 -0
  30. package/src/data/baseline.ts +162 -0
  31. package/src/data/compress.ts +31 -0
  32. package/src/data/consent.ts +75 -0
  33. package/src/data/custom.ts +23 -0
  34. package/src/data/dimension.ts +53 -0
  35. package/src/data/encode.ts +155 -0
  36. package/src/data/envelope.ts +53 -0
  37. package/src/data/extract.ts +211 -0
  38. package/src/data/index.ts +50 -0
  39. package/src/data/limit.ts +44 -0
  40. package/src/data/metadata.ts +408 -0
  41. package/src/data/metric.ts +51 -0
  42. package/src/data/ping.ts +36 -0
  43. package/src/data/signal.ts +30 -0
  44. package/src/data/summary.ts +34 -0
  45. package/src/data/token.ts +39 -0
  46. package/src/data/upgrade.ts +44 -0
  47. package/src/data/upload.ts +333 -0
  48. package/src/data/variable.ts +84 -0
  49. package/src/diagnostic/encode.ts +40 -0
  50. package/src/diagnostic/fraud.ts +37 -0
  51. package/src/diagnostic/index.ts +13 -0
  52. package/src/diagnostic/internal.ts +28 -0
  53. package/src/diagnostic/script.ts +35 -0
  54. package/src/dynamic/agent/blank.ts +2 -0
  55. package/src/dynamic/agent/crisp.ts +40 -0
  56. package/src/dynamic/agent/encode.ts +25 -0
  57. package/src/dynamic/agent/index.ts +8 -0
  58. package/src/dynamic/agent/livechat.ts +58 -0
  59. package/src/dynamic/agent/tidio.ts +44 -0
  60. package/src/global.ts +6 -0
  61. package/src/index.ts +9 -0
  62. package/src/insight/blank.ts +14 -0
  63. package/src/insight/encode.ts +61 -0
  64. package/src/insight/snapshot.ts +115 -0
  65. package/src/interaction/change.ts +38 -0
  66. package/src/interaction/click.ts +163 -0
  67. package/src/interaction/clipboard.ts +32 -0
  68. package/src/interaction/encode.ts +207 -0
  69. package/src/interaction/focus.ts +25 -0
  70. package/src/interaction/index.ts +60 -0
  71. package/src/interaction/input.ts +58 -0
  72. package/src/interaction/pointer.ts +137 -0
  73. package/src/interaction/resize.ts +50 -0
  74. package/src/interaction/scroll.ts +129 -0
  75. package/src/interaction/selection.ts +66 -0
  76. package/src/interaction/submit.ts +30 -0
  77. package/src/interaction/timeline.ts +69 -0
  78. package/src/interaction/unload.ts +26 -0
  79. package/src/interaction/visibility.ts +28 -0
  80. package/src/layout/animation.ts +133 -0
  81. package/src/layout/custom.ts +43 -0
  82. package/src/layout/discover.ts +31 -0
  83. package/src/layout/document.ts +46 -0
  84. package/src/layout/dom.ts +439 -0
  85. package/src/layout/encode.ts +154 -0
  86. package/src/layout/index.ts +42 -0
  87. package/src/layout/mutation.ts +412 -0
  88. package/src/layout/node.ts +294 -0
  89. package/src/layout/offset.ts +19 -0
  90. package/src/layout/region.ts +151 -0
  91. package/src/layout/schema.ts +63 -0
  92. package/src/layout/selector.ts +82 -0
  93. package/src/layout/style.ts +160 -0
  94. package/src/layout/target.ts +32 -0
  95. package/src/layout/traverse.ts +28 -0
  96. package/src/performance/blank.ts +10 -0
  97. package/src/performance/encode.ts +31 -0
  98. package/src/performance/index.ts +12 -0
  99. package/src/performance/interaction.ts +125 -0
  100. package/src/performance/navigation.ts +31 -0
  101. package/src/performance/observer.ts +112 -0
  102. package/src/queue.ts +33 -0
  103. package/test/core.test.ts +139 -0
  104. package/test/helper.ts +167 -0
  105. package/test/html/core.html +34 -0
  106. package/test/stub.test.ts +7 -0
  107. package/test/tsconfig.test.json +6 -0
  108. package/tsconfig.json +21 -0
  109. package/tslint.json +33 -0
  110. package/types/agent.d.ts +39 -0
  111. package/types/core.d.ts +150 -0
  112. package/types/data.d.ts +568 -0
  113. package/types/diagnostic.d.ts +24 -0
  114. package/types/global.d.ts +30 -0
  115. package/types/index.d.ts +40 -0
  116. package/types/interaction.d.ts +174 -0
  117. package/types/layout.d.ts +277 -0
  118. package/types/performance.d.ts +31 -0
@@ -0,0 +1,207 @@
1
+ import { Constant, Event, Token } from "@clarity-types/data";
2
+ import * as scrub from "@src/core/scrub";
3
+ import { time } from "@src/core/time";
4
+ import * as baseline from "@src/data/baseline";
5
+ import { queue } from "@src/data/upload";
6
+ import { metadata } from "@src/layout/target";
7
+ import * as change from "@src/interaction/change";
8
+ import * as click from "@src/interaction/click";
9
+ import * as clipboard from "@src/interaction/clipboard";
10
+ import * as input from "@src/interaction/input";
11
+ import * as pointer from "@src/interaction/pointer";
12
+ import * as resize from "@src/interaction/resize";
13
+ import * as scroll from "@src/interaction/scroll";
14
+ import * as selection from "@src/interaction/selection";
15
+ import * as submit from "@src/interaction/submit";
16
+ import * as timeline from "@src/interaction/timeline";
17
+ import * as unload from "@src/interaction/unload";
18
+ import * as visibility from "@src/interaction/visibility";
19
+ import * as focus from "@src/interaction/focus";
20
+
21
+ export default async function (type: Event, ts: number = null): Promise<void> {
22
+ let t = ts || time();
23
+ let tokens: Token[] = [t, type];
24
+ switch (type) {
25
+ case Event.MouseDown:
26
+ case Event.MouseUp:
27
+ case Event.MouseMove:
28
+ case Event.MouseWheel:
29
+ case Event.DoubleClick:
30
+ case Event.TouchStart:
31
+ case Event.TouchEnd:
32
+ case Event.TouchMove:
33
+ case Event.TouchCancel:
34
+ for (let entry of pointer.state) {
35
+ let pTarget = metadata(entry.data.target as Node, entry.event);
36
+ if (pTarget.id > 0) {
37
+ tokens = [entry.time, entry.event];
38
+ tokens.push(pTarget.id);
39
+ tokens.push(entry.data.x);
40
+ tokens.push(entry.data.y);
41
+ if (entry.data.id !== undefined) {
42
+ tokens.push(entry.data.id);
43
+
44
+ if (entry.data.isPrimary !== undefined) {
45
+ tokens.push(entry.data.isPrimary.toString());
46
+ }
47
+ }
48
+ queue(tokens);
49
+ if (entry.data.isPrimary === undefined || entry.data.isPrimary) {
50
+ baseline.track(entry.event, entry.data.x, entry.data.y, entry.time);
51
+ }
52
+ }
53
+ }
54
+ pointer.reset();
55
+ break;
56
+ case Event.Click:
57
+ case Event.ContextMenu:
58
+ for (let entry of click.state) {
59
+ let cTarget = metadata(entry.data.target as Node, entry.event, entry.data.text);
60
+ tokens = [entry.time, entry.event];
61
+ let cHash = cTarget.hash ? cTarget.hash.join(Constant.Dot) : Constant.Empty;
62
+ tokens.push(cTarget.id);
63
+ tokens.push(entry.data.x);
64
+ tokens.push(entry.data.y);
65
+ tokens.push(entry.data.eX);
66
+ tokens.push(entry.data.eY);
67
+ tokens.push(entry.data.button);
68
+ tokens.push(entry.data.reaction);
69
+ tokens.push(entry.data.context);
70
+ tokens.push(scrub.text(entry.data.text, "click", cTarget.privacy));
71
+ tokens.push(scrub.url(entry.data.link));
72
+ tokens.push(cHash);
73
+ tokens.push(entry.data.trust);
74
+ tokens.push(entry.data.isFullText);
75
+ tokens.push(entry.data.w);
76
+ tokens.push(entry.data.h);
77
+ queue(tokens);
78
+ timeline.track(entry.time, entry.event, cHash, entry.data.x, entry.data.y, entry.data.reaction, entry.data.context);
79
+ }
80
+ click.reset();
81
+ break;
82
+ case Event.Clipboard:
83
+ for (let entry of clipboard.state) {
84
+ tokens = [entry.time, entry.event];
85
+ let target = metadata(entry.data.target as Node, entry.event);
86
+ if (target.id > 0) {
87
+ tokens.push(target.id);
88
+ tokens.push(entry.data.action);
89
+ queue(tokens);
90
+ }
91
+ }
92
+ clipboard.reset();
93
+ break;
94
+ case Event.Resize:
95
+ let r = resize.data;
96
+ tokens.push(r.width);
97
+ tokens.push(r.height);
98
+ baseline.track(type, r.width, r.height);
99
+ resize.reset();
100
+ queue(tokens);
101
+ break;
102
+ case Event.Unload:
103
+ let u = unload.data;
104
+ tokens.push(u.name);
105
+ tokens.push(u.persisted);
106
+ unload.reset();
107
+ queue(tokens);
108
+ break;
109
+ case Event.Input:
110
+ for (let entry of input.state) {
111
+ let iTarget = metadata(entry.data.target as Node, entry.event, entry.data.value);
112
+ tokens = [entry.time, entry.event];
113
+ tokens.push(iTarget.id);
114
+ tokens.push(scrub.text(entry.data.value, "input", iTarget.privacy, false, entry.data.type));
115
+ tokens.push(entry.data.trust);
116
+ queue(tokens);
117
+ }
118
+ input.reset();
119
+ break;
120
+ case Event.Selection:
121
+ let s = selection.data;
122
+ if (s) {
123
+ let startTarget = metadata(s.start as Node, type);
124
+ let endTarget = metadata(s.end as Node, type);
125
+ tokens.push(startTarget.id);
126
+ tokens.push(s.startOffset);
127
+ tokens.push(endTarget.id);
128
+ tokens.push(s.endOffset);
129
+ selection.reset();
130
+ queue(tokens);
131
+ }
132
+ break;
133
+ case Event.Scroll:
134
+ for (let entry of scroll.state) {
135
+ let sTarget = metadata(entry.data.target as Node, entry.event);
136
+ const top = metadata(entry.data.top as Node, entry.event);
137
+ const bottom = metadata(entry.data.bottom as Node, entry.event);
138
+ const sTopHash = top?.hash ? top.hash.join(Constant.Dot) : Constant.Empty;
139
+ const sBottomHash = bottom?.hash ? bottom.hash.join(Constant.Dot) : Constant.Empty;
140
+ if (sTarget.id > 0) {
141
+ tokens = [entry.time, entry.event];
142
+ tokens.push(sTarget.id);
143
+ tokens.push(entry.data.x);
144
+ tokens.push(entry.data.y);
145
+ tokens.push(sTopHash);
146
+ tokens.push(sBottomHash);
147
+ queue(tokens);
148
+ baseline.track(entry.event, entry.data.x, entry.data.y, entry.time);
149
+ }
150
+ }
151
+ scroll.reset();
152
+ break;
153
+ case Event.Change:
154
+ for (let entry of change.state) {
155
+ tokens = [entry.time, entry.event];
156
+ let target = metadata(entry.data.target as Node, entry.event);
157
+ if (target.id > 0) {
158
+ tokens = [entry.time, entry.event];
159
+ tokens.push(target.id);
160
+ tokens.push(entry.data.type);
161
+ tokens.push(scrub.text(entry.data.value, "change", target.privacy));
162
+ tokens.push(scrub.text(entry.data.checksum, "checksum", target.privacy));
163
+ queue(tokens);
164
+ }
165
+ }
166
+ change.reset();
167
+ break;
168
+ case Event.Submit:
169
+ for (let entry of submit.state) {
170
+ tokens = [entry.time, entry.event];
171
+ let target = metadata(entry.data.target as Node, entry.event);
172
+ if (target.id > 0) {
173
+ tokens.push(target.id);
174
+ queue(tokens);
175
+ }
176
+ }
177
+ submit.reset();
178
+ break;
179
+ case Event.Timeline:
180
+ for (let entry of timeline.updates) {
181
+ tokens = [entry.time, entry.event];
182
+ tokens.push(entry.data.type);
183
+ tokens.push(entry.data.hash);
184
+ tokens.push(entry.data.x);
185
+ tokens.push(entry.data.y);
186
+ tokens.push(entry.data.reaction);
187
+ tokens.push(entry.data.context);
188
+ queue(tokens, false);
189
+ }
190
+ timeline.reset();
191
+ break;
192
+ case Event.Visibility:
193
+ let v = visibility.data;
194
+ tokens.push(v.visible);
195
+ queue(tokens);
196
+ baseline.visibility(t, v.visible);
197
+ visibility.reset();
198
+ break;
199
+ case Event.Focus: {
200
+ let f = focus.data;
201
+ tokens.push(f.focused);
202
+ queue(tokens, false);
203
+ focus.reset();
204
+ break;
205
+ }
206
+ }
207
+ }
@@ -0,0 +1,25 @@
1
+ import { BooleanFlag, Event } from "@clarity-types/data";
2
+ import { FocusData } from "@clarity-types/interaction";
3
+ import { bind } from "@src/core/event";
4
+ import encode from "./encode";
5
+
6
+ export let data: FocusData;
7
+
8
+ export function start(): void {
9
+ bind(window, "focus", () => compute(BooleanFlag.True));
10
+ bind(window, "blur", () => compute(BooleanFlag.False));
11
+ }
12
+
13
+ export function stop(): void {
14
+ reset();
15
+ }
16
+
17
+ export function reset(): void {
18
+ data = null;
19
+ }
20
+
21
+ function compute(focus: BooleanFlag): void {
22
+ data = { focused: focus };
23
+
24
+ encode(Event.Focus);
25
+ }
@@ -0,0 +1,60 @@
1
+ import * as change from "@src/interaction/change";
2
+ import * as click from "@src/interaction/click";
3
+ import * as clipboard from "@src/interaction/clipboard";
4
+ import * as input from "@src/interaction/input";
5
+ import * as pointer from "@src/interaction/pointer";
6
+ import * as resize from "@src/interaction/resize";
7
+ import * as scroll from "@src/interaction/scroll";
8
+ import * as selection from "@src/interaction/selection";
9
+ import * as submit from "@src/interaction/submit";
10
+ import * as timeline from "@src/interaction/timeline";
11
+ import * as unload from "@src/interaction/unload";
12
+ import * as visibility from "@src/interaction/visibility";
13
+ import * as focus from "@src/interaction/focus";
14
+
15
+ export function start(): void {
16
+ timeline.start();
17
+ click.start();
18
+ clipboard.start();
19
+ pointer.start();
20
+ input.start();
21
+ resize.start();
22
+ visibility.start();
23
+ focus.start();
24
+ scroll.start();
25
+ selection.start();
26
+ change.start();
27
+ submit.start();
28
+ unload.start();
29
+ }
30
+
31
+ export function stop(): void {
32
+ timeline.stop();
33
+ click.stop();
34
+ clipboard.stop();
35
+ pointer.stop();
36
+ input.stop();
37
+ resize.stop();
38
+ visibility.stop();
39
+ focus.stop();
40
+ scroll.stop();
41
+ selection.stop();
42
+ change.stop();
43
+ submit.stop();
44
+ unload.stop()
45
+ }
46
+
47
+ export function observe(root: Node): void {
48
+ scroll.observe(root);
49
+ // Only monitor following interactions if the root node is a document
50
+ // In case of shadow DOM, following events automatically bubble up to the parent document.
51
+ if (root.nodeType === Node.DOCUMENT_NODE) {
52
+ click.observe(root);
53
+ clipboard.observe(root);
54
+ pointer.observe(root);
55
+ input.observe(root);
56
+ selection.observe(root);
57
+ change.observe(root);
58
+ submit.observe(root);
59
+ }
60
+ }
@@ -0,0 +1,58 @@
1
+ import { BooleanFlag, Event } from "@clarity-types/data";
2
+ import { InputData, InputState, 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 { get } from "@src/layout/dom";
8
+ import encode from "./encode";
9
+ import { target } from "@src/layout/target";
10
+
11
+ let timeout: number = null;
12
+ export let state: InputState[] = [];
13
+
14
+ export function start(): void {
15
+ reset();
16
+ }
17
+
18
+ export function observe(root: Node): void {
19
+ bind(root, "input", recompute, true);
20
+ }
21
+
22
+ function recompute(evt: UIEvent): void {
23
+ let input = target(evt) as HTMLInputElement;
24
+ let value = get(input);
25
+ if (input && input.type && value) {
26
+ let v = input.value;
27
+ let t = input.type;
28
+ switch (input.type) {
29
+ case "radio":
30
+ case "checkbox":
31
+ v = input.checked ? "true" : "false";
32
+ break;
33
+ }
34
+
35
+ let data: InputData = { target: input, value: v, type: t, trust: evt.isTrusted ? BooleanFlag.True : BooleanFlag.False };
36
+
37
+ // If last entry in the queue is for the same target node as the current one, remove it so we can later swap it with current data.
38
+ if (state.length > 0 && (state[state.length - 1].data.target === data.target)) { state.pop(); }
39
+
40
+ state.push({ time: time(evt), event: Event.Input, data });
41
+
42
+ clearTimeout(timeout);
43
+ timeout = setTimeout(process, Setting.InputLookAhead, Event.Input);
44
+ }
45
+ }
46
+
47
+ function process(event: Event): void {
48
+ schedule(encode.bind(this, event));
49
+ }
50
+
51
+ export function reset(): void {
52
+ state = [];
53
+ }
54
+
55
+ export function stop(): void {
56
+ clearTimeout(timeout);
57
+ reset();
58
+ }
@@ -0,0 +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
+ }
@@ -0,0 +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
+ }
@@ -0,0 +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
+ }