@gem-sdk/clarity-js 0.8.94

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 (127) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/README.md +1 -0
  3. package/build/clarity.extended.js +1 -0
  4. package/build/clarity.insight.js +1 -0
  5. package/build/clarity.js +7148 -0
  6. package/build/clarity.livechat.js +1 -0
  7. package/build/clarity.min.js +1 -0
  8. package/build/clarity.module.js +7144 -0
  9. package/build/clarity.performance.js +1 -0
  10. package/build/dynamic/clarity.crisp.js +1 -0
  11. package/build/dynamic/clarity.tidio.js +1 -0
  12. package/package.json +49 -0
  13. package/rollup.config.ts +161 -0
  14. package/src/clarity.ts +72 -0
  15. package/src/core/api.ts +8 -0
  16. package/src/core/config.ts +35 -0
  17. package/src/core/copy.ts +3 -0
  18. package/src/core/dynamic.ts +68 -0
  19. package/src/core/event.ts +53 -0
  20. package/src/core/hash.ts +19 -0
  21. package/src/core/history.ts +82 -0
  22. package/src/core/index.ts +81 -0
  23. package/src/core/measure.ts +19 -0
  24. package/src/core/report.ts +28 -0
  25. package/src/core/scrub.ts +201 -0
  26. package/src/core/task.ts +181 -0
  27. package/src/core/throttle.ts +46 -0
  28. package/src/core/time.ts +26 -0
  29. package/src/core/timeout.ts +10 -0
  30. package/src/core/version.ts +2 -0
  31. package/src/custom/README.md +44 -0
  32. package/src/custom/dialog.ts +69 -0
  33. package/src/custom/index.ts +6 -0
  34. package/src/data/baseline.ts +162 -0
  35. package/src/data/compress.ts +31 -0
  36. package/src/data/consent.ts +84 -0
  37. package/src/data/cookie.ts +90 -0
  38. package/src/data/custom.ts +23 -0
  39. package/src/data/dimension.ts +53 -0
  40. package/src/data/encode.ts +155 -0
  41. package/src/data/envelope.ts +53 -0
  42. package/src/data/extract.ts +217 -0
  43. package/src/data/gemx-upload.ts +51 -0
  44. package/src/data/gemx.ts +35 -0
  45. package/src/data/index.ts +57 -0
  46. package/src/data/limit.ts +44 -0
  47. package/src/data/metadata.ts +340 -0
  48. package/src/data/metric.ts +51 -0
  49. package/src/data/ping.ts +36 -0
  50. package/src/data/signal.ts +30 -0
  51. package/src/data/summary.ts +34 -0
  52. package/src/data/token.ts +39 -0
  53. package/src/data/upgrade.ts +44 -0
  54. package/src/data/upload.ts +375 -0
  55. package/src/data/util.ts +18 -0
  56. package/src/data/variable.ts +84 -0
  57. package/src/diagnostic/encode.ts +40 -0
  58. package/src/diagnostic/fraud.ts +37 -0
  59. package/src/diagnostic/index.ts +13 -0
  60. package/src/diagnostic/internal.ts +28 -0
  61. package/src/diagnostic/script.ts +35 -0
  62. package/src/dynamic/agent/blank.ts +2 -0
  63. package/src/dynamic/agent/crisp.ts +40 -0
  64. package/src/dynamic/agent/encode.ts +25 -0
  65. package/src/dynamic/agent/index.ts +8 -0
  66. package/src/dynamic/agent/livechat.ts +58 -0
  67. package/src/dynamic/agent/tidio.ts +44 -0
  68. package/src/global.ts +6 -0
  69. package/src/index.ts +10 -0
  70. package/src/insight/blank.ts +14 -0
  71. package/src/insight/encode.ts +64 -0
  72. package/src/insight/snapshot.ts +115 -0
  73. package/src/interaction/change.ts +38 -0
  74. package/src/interaction/click.ts +256 -0
  75. package/src/interaction/clipboard.ts +32 -0
  76. package/src/interaction/encode.ts +217 -0
  77. package/src/interaction/focus.ts +25 -0
  78. package/src/interaction/index.ts +66 -0
  79. package/src/interaction/input.ts +66 -0
  80. package/src/interaction/pageshow.ts +35 -0
  81. package/src/interaction/pointer.ts +137 -0
  82. package/src/interaction/resize.ts +50 -0
  83. package/src/interaction/scroll.ts +134 -0
  84. package/src/interaction/selection.ts +66 -0
  85. package/src/interaction/submit.ts +30 -0
  86. package/src/interaction/timeline.ts +69 -0
  87. package/src/interaction/unload.ts +32 -0
  88. package/src/interaction/visibility.ts +28 -0
  89. package/src/layout/animation.ts +133 -0
  90. package/src/layout/constants.ts +13 -0
  91. package/src/layout/custom.ts +48 -0
  92. package/src/layout/discover.ts +31 -0
  93. package/src/layout/document.ts +46 -0
  94. package/src/layout/dom.ts +444 -0
  95. package/src/layout/encode.ts +157 -0
  96. package/src/layout/gemx-snapshot.ts +84 -0
  97. package/src/layout/index.ts +47 -0
  98. package/src/layout/mutation.ts +424 -0
  99. package/src/layout/node.ts +302 -0
  100. package/src/layout/offset.ts +19 -0
  101. package/src/layout/region.ts +151 -0
  102. package/src/layout/schema.ts +63 -0
  103. package/src/layout/selector.ts +113 -0
  104. package/src/layout/style.ts +160 -0
  105. package/src/layout/target.ts +34 -0
  106. package/src/layout/traverse.ts +28 -0
  107. package/src/performance/blank.ts +10 -0
  108. package/src/performance/encode.ts +31 -0
  109. package/src/performance/index.ts +12 -0
  110. package/src/performance/interaction.ts +125 -0
  111. package/src/performance/navigation.ts +31 -0
  112. package/src/performance/observer.ts +112 -0
  113. package/src/queue.ts +33 -0
  114. package/test/consentv2.test.ts +1309 -0
  115. package/test/hash.test.ts +68 -0
  116. package/test/time.test.ts +42 -0
  117. package/tsconfig.json +12 -0
  118. package/tslint.json +33 -0
  119. package/types/agent.d.ts +39 -0
  120. package/types/core.d.ts +156 -0
  121. package/types/data.d.ts +611 -0
  122. package/types/diagnostic.d.ts +24 -0
  123. package/types/global.d.ts +43 -0
  124. package/types/index.d.ts +46 -0
  125. package/types/interaction.d.ts +187 -0
  126. package/types/layout.d.ts +271 -0
  127. package/types/performance.d.ts +34 -0
@@ -0,0 +1,217 @@
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
+ import * as dom from "@src/layout/dom";
21
+
22
+ export default async function (type: Event, ts: number = null): Promise<void> {
23
+ let t = ts || time();
24
+ let tokens: Token[] = [t, type];
25
+ switch (type) {
26
+ case Event.MouseDown:
27
+ case Event.MouseUp:
28
+ case Event.MouseMove:
29
+ case Event.MouseWheel:
30
+ case Event.DoubleClick:
31
+ case Event.TouchStart:
32
+ case Event.TouchEnd:
33
+ case Event.TouchMove:
34
+ case Event.TouchCancel:
35
+ for (let entry of pointer.state) {
36
+ let pTarget = metadata(entry.data.target as Node, entry.event);
37
+ if (pTarget.id > 0) {
38
+ tokens = [entry.time, entry.event];
39
+ tokens.push(pTarget.id);
40
+ tokens.push(entry.data.x);
41
+ tokens.push(entry.data.y);
42
+ if (entry.data.id !== undefined) {
43
+ tokens.push(entry.data.id);
44
+
45
+ if (entry.data.isPrimary !== undefined) {
46
+ tokens.push(entry.data.isPrimary.toString());
47
+ }
48
+ }
49
+ queue(tokens);
50
+ if (entry.data.isPrimary === undefined || entry.data.isPrimary) {
51
+ baseline.track(entry.event, entry.data.x, entry.data.y, entry.time);
52
+ }
53
+ }
54
+ }
55
+ pointer.reset();
56
+ break;
57
+ case Event.Click:
58
+ case Event.ContextMenu:
59
+ for (let entry of click.state) {
60
+ let cTarget = metadata(entry.data.target as Node, entry.event, entry.data.text);
61
+ tokens = [entry.time, entry.event];
62
+ let cHash = cTarget.hash ? cTarget.hash.join(Constant.Dot) : Constant.Empty;
63
+
64
+ const info = dom.get(entry.data.target as Node);
65
+ console.log(`[GemX - Interaction - Encode]`, info);
66
+
67
+ tokens.push(cTarget.id);
68
+ tokens.push(entry.data.x);
69
+ tokens.push(entry.data.y);
70
+ tokens.push(entry.data.eX);
71
+ tokens.push(entry.data.eY);
72
+ tokens.push(entry.data.button);
73
+ tokens.push(entry.data.reaction);
74
+ tokens.push(entry.data.context);
75
+ tokens.push(scrub.text(entry.data.text, "click", cTarget.privacy));
76
+ tokens.push(scrub.url(entry.data.link));
77
+ tokens.push(cHash);
78
+ tokens.push(entry.data.trust);
79
+ tokens.push(entry.data.isFullText);
80
+ tokens.push(entry.data.w);
81
+ tokens.push(entry.data.h);
82
+ tokens.push(entry.data.tag);
83
+ tokens.push(entry.data.class);
84
+ tokens.push(entry.data.id);
85
+ tokens.push(entry.data.source);
86
+ queue(tokens);
87
+ timeline.track(entry.time, entry.event, cHash, entry.data.x, entry.data.y, entry.data.reaction, entry.data.context);
88
+ }
89
+ click.reset();
90
+ break;
91
+ case Event.Clipboard:
92
+ for (let entry of clipboard.state) {
93
+ tokens = [entry.time, entry.event];
94
+ let target = metadata(entry.data.target as Node, entry.event);
95
+ if (target.id > 0) {
96
+ tokens.push(target.id);
97
+ tokens.push(entry.data.action);
98
+ queue(tokens);
99
+ }
100
+ }
101
+ clipboard.reset();
102
+ break;
103
+ case Event.Resize:
104
+ let r = resize.data;
105
+ tokens.push(r.width);
106
+ tokens.push(r.height);
107
+ baseline.track(type, r.width, r.height);
108
+ resize.reset();
109
+ queue(tokens);
110
+ break;
111
+ case Event.Unload:
112
+ let u = unload.data;
113
+ tokens.push(u.name);
114
+ tokens.push(u.persisted);
115
+ unload.reset();
116
+ queue(tokens);
117
+ break;
118
+ case Event.Input:
119
+ for (let entry of input.state) {
120
+ let iTarget = metadata(entry.data.target as Node, entry.event, entry.data.value);
121
+ tokens = [entry.time, entry.event];
122
+ tokens.push(iTarget.id);
123
+ tokens.push(scrub.text(entry.data.value, "input", iTarget.privacy, false, entry.data.type));
124
+ tokens.push(entry.data.trust);
125
+ queue(tokens);
126
+ }
127
+ input.reset();
128
+ break;
129
+ case Event.Selection:
130
+ let s = selection.data;
131
+ if (s) {
132
+ let startTarget = metadata(s.start as Node, type);
133
+ let endTarget = metadata(s.end as Node, type);
134
+ tokens.push(startTarget.id);
135
+ tokens.push(s.startOffset);
136
+ tokens.push(endTarget.id);
137
+ tokens.push(s.endOffset);
138
+ selection.reset();
139
+ queue(tokens);
140
+ }
141
+ break;
142
+ case Event.Scroll:
143
+ for (let entry of scroll.state) {
144
+ let sTarget = metadata(entry.data.target as Node, entry.event);
145
+ const top = metadata(entry.data.top as Node, entry.event);
146
+ const bottom = metadata(entry.data.bottom as Node, entry.event);
147
+ const sTopHash = top?.hash ? top.hash.join(Constant.Dot) : Constant.Empty;
148
+ const sBottomHash = bottom?.hash ? bottom.hash.join(Constant.Dot) : Constant.Empty;
149
+ if (sTarget.id > 0) {
150
+ tokens = [entry.time, entry.event];
151
+ tokens.push(sTarget.id);
152
+ tokens.push(entry.data.x);
153
+ tokens.push(entry.data.y);
154
+ tokens.push(sTopHash);
155
+ tokens.push(sBottomHash);
156
+ tokens.push(entry.data.trust);
157
+ queue(tokens);
158
+ baseline.track(entry.event, entry.data.x, entry.data.y, entry.time);
159
+ }
160
+ }
161
+ scroll.reset();
162
+ break;
163
+ case Event.Change:
164
+ for (let entry of change.state) {
165
+ tokens = [entry.time, entry.event];
166
+ let target = metadata(entry.data.target as Node, entry.event);
167
+ if (target.id > 0) {
168
+ tokens = [entry.time, entry.event];
169
+ tokens.push(target.id);
170
+ tokens.push(entry.data.type);
171
+ tokens.push(scrub.text(entry.data.value, "change", target.privacy));
172
+ tokens.push(scrub.text(entry.data.checksum, "checksum", target.privacy));
173
+ queue(tokens);
174
+ }
175
+ }
176
+ change.reset();
177
+ break;
178
+ case Event.Submit:
179
+ for (let entry of submit.state) {
180
+ tokens = [entry.time, entry.event];
181
+ let target = metadata(entry.data.target as Node, entry.event);
182
+ if (target.id > 0) {
183
+ tokens.push(target.id);
184
+ queue(tokens);
185
+ }
186
+ }
187
+ submit.reset();
188
+ break;
189
+ case Event.Timeline:
190
+ for (let entry of timeline.updates) {
191
+ tokens = [entry.time, entry.event];
192
+ tokens.push(entry.data.type);
193
+ tokens.push(entry.data.hash);
194
+ tokens.push(entry.data.x);
195
+ tokens.push(entry.data.y);
196
+ tokens.push(entry.data.reaction);
197
+ tokens.push(entry.data.context);
198
+ queue(tokens, false);
199
+ }
200
+ timeline.reset();
201
+ break;
202
+ case Event.Visibility:
203
+ let v = visibility.data;
204
+ tokens.push(v.visible);
205
+ queue(tokens);
206
+ baseline.visibility(t, v.visible);
207
+ visibility.reset();
208
+ break;
209
+ case Event.Focus: {
210
+ let f = focus.data;
211
+ tokens.push(f.focused);
212
+ queue(tokens, false);
213
+ focus.reset();
214
+ break;
215
+ }
216
+ }
217
+ }
@@ -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,66 @@
1
+ import type { Module } from "@clarity-types/core";
2
+ import config from "@src/core/config";
3
+
4
+ import * as change from "@src/interaction/change";
5
+ import * as click from "@src/interaction/click";
6
+ import * as clipboard from "@src/interaction/clipboard";
7
+ import * as input from "@src/interaction/input";
8
+ import * as pointer from "@src/interaction/pointer";
9
+ import * as resize from "@src/interaction/resize";
10
+ import * as scroll from "@src/interaction/scroll";
11
+ import * as selection from "@src/interaction/selection";
12
+ import * as submit from "@src/interaction/submit";
13
+ import * as timeline from "@src/interaction/timeline";
14
+ import * as unload from "@src/interaction/unload";
15
+ import * as visibility from "@src/interaction/visibility";
16
+ import * as focus from "@src/interaction/focus";
17
+ import * as pageshow from "@src/interaction/pageshow";
18
+
19
+ const GEMX: Module[] = [
20
+ timeline,
21
+ click,
22
+ resize,
23
+ visibility,
24
+ scroll,
25
+ pageshow,
26
+ unload,
27
+ ];
28
+
29
+ const ALL: Module[] = [
30
+ timeline,
31
+ click,
32
+ clipboard,
33
+ pointer,
34
+ input,
35
+ resize,
36
+ visibility,
37
+ focus,
38
+ pageshow,
39
+ scroll,
40
+ selection,
41
+ change,
42
+ submit,
43
+ unload
44
+ ];
45
+
46
+ const GEMX_OBSERVE = { allRoots: [scroll], documentOnly: [click] };
47
+ const ALL_OBSERVE = { allRoots: [scroll], documentOnly: [click, clipboard, pointer, input, selection, change, submit] };
48
+
49
+ let modules: Module[] = ALL;
50
+
51
+ export function start(): void {
52
+ modules = config?.mode === "gemx" ? GEMX : ALL;
53
+ modules.forEach(m => m.start());
54
+ }
55
+
56
+ export function stop(): void {
57
+ modules.forEach(m => m.stop());
58
+ }
59
+
60
+ export function observe(root: Node): void {
61
+ const observeModules = config?.mode === "gemx" ? GEMX_OBSERVE : ALL_OBSERVE;
62
+ observeModules.allRoots.forEach(m => m.observe(root));
63
+ if (root.nodeType === Node.DOCUMENT_NODE) {
64
+ observeModules.documentOnly.forEach(m => m.observe(root));
65
+ }
66
+ }
@@ -0,0 +1,66 @@
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
+ bind(root, "keydown", enter, true);
21
+ }
22
+
23
+ function recompute(evt: UIEvent): void {
24
+ let input = target(evt) as HTMLInputElement;
25
+ let value = get(input);
26
+ if (input && input.type && value) {
27
+ let v = input.value;
28
+ let t = input.type;
29
+ switch (input.type) {
30
+ case "radio":
31
+ case "checkbox":
32
+ v = input.checked ? "true" : "false";
33
+ break;
34
+ }
35
+
36
+ let data: InputData = { target: input, value: v, type: t, trust: evt.isTrusted ? BooleanFlag.True : BooleanFlag.False };
37
+
38
+ // 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.
39
+ if (state.length > 0 && (state[state.length - 1].data.target === data.target)) { state.pop(); }
40
+
41
+ state.push({ time: time(evt), event: Event.Input, data });
42
+
43
+ clearTimeout(timeout);
44
+ timeout = setTimeout(process, Setting.InputLookAhead, Event.Input);
45
+ }
46
+ }
47
+
48
+ function enter(evt: KeyboardEvent): void {
49
+ if (evt.key === "Enter" && !evt.repeat && state.length > 0) {
50
+ clearTimeout(timeout);
51
+ process(Event.Input);
52
+ }
53
+ }
54
+
55
+ function process(event: Event): void {
56
+ schedule(encode.bind(this, event));
57
+ }
58
+
59
+ export function reset(): void {
60
+ state = [];
61
+ }
62
+
63
+ export function stop(): void {
64
+ clearTimeout(timeout);
65
+ reset();
66
+ }
@@ -0,0 +1,35 @@
1
+ import { Constant } from "@clarity-types/core";
2
+ import { Code, Severity } from "@clarity-types/data";
3
+ import * as clarity from "@src/clarity";
4
+ import api from "@src/core/api";
5
+ import measure from "@src/core/measure";
6
+ import * as internal from "@src/diagnostic/internal";
7
+
8
+ let bound = false;
9
+
10
+ export function start(): void {
11
+ // Only bind once - this listener must persist even when Clarity stops
12
+ // to detect when the page is restored from bfcache
13
+ if (!bound) {
14
+ try {
15
+ window[api(Constant.AddEventListener)]("pageshow", measure(handler) as EventListener, { capture: false, passive: true });
16
+ bound = true;
17
+ } catch {
18
+ /* do nothing */
19
+ }
20
+ }
21
+ }
22
+
23
+ function handler(evt: PageTransitionEvent): void {
24
+ // The persisted property indicates if the page was loaded from bfcache
25
+ if (evt && evt.persisted) {
26
+ // Restart Clarity since it was stopped when the page entered bfcache
27
+ clarity.start();
28
+ internal.log(Code.BFCache, Severity.Info);
29
+ }
30
+ }
31
+
32
+ export function stop(): void {
33
+ // Intentionally don't remove the listener or reset 'bound' flag
34
+ // We need the listener to persist to detect bfcache restoration
35
+ }
@@ -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
+ }