@replohq/canopy-sdk 1.1.0 → 1.3.0

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 (36) hide show
  1. package/_vendor/schemas/generated/pageAnnotations.d.ts +48 -0
  2. package/_vendor/schemas/generated/pageAnnotations.mjs +1 -0
  3. package/_vendor/schemas/loaderKeys.mjs +2 -0
  4. package/_vendor/schemas/pageAnnotations.d.ts +134 -0
  5. package/_vendor/schemas/pageAnnotations.mjs +61 -0
  6. package/_vendor/schemas/selectMode.mjs +1 -1
  7. package/editor/annotations/AnnotationLayerMount.d.ts +1 -0
  8. package/editor/annotations/AnnotationLayerMount.js +13 -0
  9. package/editor/annotations/AnnotationLayerMount.js.map +7 -0
  10. package/editor/annotations/annotationController.d.ts +1 -0
  11. package/editor/annotations/annotationController.js +384 -0
  12. package/editor/annotations/annotationController.js.map +7 -0
  13. package/editor/annotations/bridgeGate.d.ts +7 -0
  14. package/editor/annotations/bridgeGate.js +22 -0
  15. package/editor/annotations/bridgeGate.js.map +7 -0
  16. package/editor/select-mode/annotationFrames.d.ts +33 -0
  17. package/editor/select-mode/annotationFrames.js +119 -0
  18. package/editor/select-mode/annotationFrames.js.map +7 -0
  19. package/editor/select-mode/selectionFrame.d.ts +9 -0
  20. package/editor/select-mode/selectionFrame.js +14 -4
  21. package/editor/select-mode/selectionFrame.js.map +2 -2
  22. package/loaders/loader-keys.d.ts +2 -0
  23. package/loaders/loader-keys.js +2 -0
  24. package/loaders/loader-keys.js.map +2 -2
  25. package/loaders/loader-schemas.d.ts +30 -0
  26. package/loaders/loader-schemas.js +23 -0
  27. package/loaders/loader-schemas.js.map +2 -2
  28. package/loaders/reviews-io-review-aggregate-loader.d.ts +12 -0
  29. package/loaders/reviews-io-review-aggregate-loader.js +43 -0
  30. package/loaders/reviews-io-review-aggregate-loader.js.map +7 -0
  31. package/loaders/reviews-io-reviews-loader.d.ts +14 -0
  32. package/loaders/reviews-io-reviews-loader.js +65 -0
  33. package/loaders/reviews-io-reviews-loader.js.map +7 -0
  34. package/package.json +1 -1
  35. package/providers/replo-provider.js +2 -0
  36. package/providers/replo-provider.js.map +2 -2
@@ -0,0 +1,48 @@
1
+ export type PageAnnotationColor = "red" | "purple" | "amber";
2
+ export type PageAnnotationPlacement = "element" | "pageTop";
3
+ export type PageAnnotationMarker = {
4
+ id: string;
5
+ number: number;
6
+ color: PageAnnotationColor;
7
+ selector: string;
8
+ placement: PageAnnotationPlacement;
9
+ };
10
+ export type PageAnnotationViewportRect = {
11
+ x: number;
12
+ y: number;
13
+ width: number;
14
+ height: number;
15
+ };
16
+ export type PageAnnotationsSetMessage = {
17
+ type: "replo:annotations:set";
18
+ protocolVersion: 1;
19
+ setId: string;
20
+ markers: PageAnnotationMarker[];
21
+ };
22
+ export type PageAnnotationsClearMessage = {
23
+ type: "replo:annotations:clear";
24
+ };
25
+ export type PageAnnotationsRequestReadyMessage = {
26
+ type: "replo:annotations:requestReady";
27
+ };
28
+ export type PageAnnotationsParentMessage = PageAnnotationsSetMessage | PageAnnotationsClearMessage | PageAnnotationsRequestReadyMessage;
29
+ export type PageAnnotationsReadyMessage = {
30
+ type: "replo:annotations:ready";
31
+ protocolVersion: 1;
32
+ };
33
+ export type PageAnnotationsClickedMessage = {
34
+ type: "replo:annotations:clicked";
35
+ id: string;
36
+ viewportRect: PageAnnotationViewportRect;
37
+ };
38
+ export type PageAnnotationsActiveRectMessage = {
39
+ type: "replo:annotations:activeRect";
40
+ id: string;
41
+ viewportRect: PageAnnotationViewportRect;
42
+ };
43
+ export type PageAnnotationsUnresolvedMessage = {
44
+ type: "replo:annotations:unresolved";
45
+ setId: string;
46
+ ids: string[];
47
+ };
48
+ export type PageAnnotationsIframeMessage = PageAnnotationsReadyMessage | PageAnnotationsClickedMessage | PageAnnotationsActiveRectMessage | PageAnnotationsUnresolvedMessage;
@@ -13,6 +13,8 @@ var DATA_LOADER_KEYS = {
13
13
  SMILE_IO_REWARDS: "smile_io.rewards",
14
14
  YOTPO_PRODUCT_REVIEWS: "yotpo.product_reviews",
15
15
  YOTPO_PRODUCT_REVIEW_AGGREGATE: "yotpo.product_review_aggregate",
16
+ REVIEWS_IO_PRODUCT_REVIEWS: "reviews_io.product_reviews",
17
+ REVIEWS_IO_PRODUCT_REVIEW_AGGREGATE: "reviews_io.product_review_aggregate",
16
18
  STATSIG_EXPERIMENT: "statsig.experiment",
17
19
  CONTENTFUL_ENTRY: "contentful.entry",
18
20
  CONTENTFUL_ENTRIES: "contentful.entries"
@@ -0,0 +1,134 @@
1
+ import { z } from "zod";
2
+ export declare const PAGE_ANNOTATIONS_PROTOCOL_VERSION = 1;
3
+ export declare const pageAnnotationColorSchema: z.ZodEnum<{
4
+ red: "red";
5
+ purple: "purple";
6
+ amber: "amber";
7
+ }>;
8
+ export declare const pageAnnotationPlacementSchema: z.ZodEnum<{
9
+ element: "element";
10
+ pageTop: "pageTop";
11
+ }>;
12
+ export declare const pageAnnotationMarkerSchema: z.ZodObject<{
13
+ id: z.ZodString;
14
+ number: z.ZodNumber;
15
+ color: z.ZodEnum<{
16
+ red: "red";
17
+ purple: "purple";
18
+ amber: "amber";
19
+ }>;
20
+ selector: z.ZodString;
21
+ placement: z.ZodDefault<z.ZodEnum<{
22
+ element: "element";
23
+ pageTop: "pageTop";
24
+ }>>;
25
+ }, z.core.$strip>;
26
+ export declare const pageAnnotationViewportRectSchema: z.ZodObject<{
27
+ x: z.ZodNumber;
28
+ y: z.ZodNumber;
29
+ width: z.ZodNumber;
30
+ height: z.ZodNumber;
31
+ }, z.core.$strip>;
32
+ export declare const pageAnnotationsSetMessageSchema: z.ZodObject<{
33
+ type: z.ZodLiteral<"replo:annotations:set">;
34
+ protocolVersion: z.ZodLiteral<1>;
35
+ setId: z.ZodString;
36
+ markers: z.ZodArray<z.ZodObject<{
37
+ id: z.ZodString;
38
+ number: z.ZodNumber;
39
+ color: z.ZodEnum<{
40
+ red: "red";
41
+ purple: "purple";
42
+ amber: "amber";
43
+ }>;
44
+ selector: z.ZodString;
45
+ placement: z.ZodDefault<z.ZodEnum<{
46
+ element: "element";
47
+ pageTop: "pageTop";
48
+ }>>;
49
+ }, z.core.$strip>>;
50
+ }, z.core.$strip>;
51
+ export declare const pageAnnotationsClearMessageSchema: z.ZodObject<{
52
+ type: z.ZodLiteral<"replo:annotations:clear">;
53
+ }, z.core.$strip>;
54
+ export declare const pageAnnotationsRequestReadyMessageSchema: z.ZodObject<{
55
+ type: z.ZodLiteral<"replo:annotations:requestReady">;
56
+ }, z.core.$strip>;
57
+ export declare const pageAnnotationsParentMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
58
+ type: z.ZodLiteral<"replo:annotations:set">;
59
+ protocolVersion: z.ZodLiteral<1>;
60
+ setId: z.ZodString;
61
+ markers: z.ZodArray<z.ZodObject<{
62
+ id: z.ZodString;
63
+ number: z.ZodNumber;
64
+ color: z.ZodEnum<{
65
+ red: "red";
66
+ purple: "purple";
67
+ amber: "amber";
68
+ }>;
69
+ selector: z.ZodString;
70
+ placement: z.ZodDefault<z.ZodEnum<{
71
+ element: "element";
72
+ pageTop: "pageTop";
73
+ }>>;
74
+ }, z.core.$strip>>;
75
+ }, z.core.$strip>, z.ZodObject<{
76
+ type: z.ZodLiteral<"replo:annotations:clear">;
77
+ }, z.core.$strip>, z.ZodObject<{
78
+ type: z.ZodLiteral<"replo:annotations:requestReady">;
79
+ }, z.core.$strip>], "type">;
80
+ export declare const pageAnnotationsReadyMessageSchema: z.ZodObject<{
81
+ type: z.ZodLiteral<"replo:annotations:ready">;
82
+ protocolVersion: z.ZodLiteral<1>;
83
+ }, z.core.$strip>;
84
+ export declare const pageAnnotationsClickedMessageSchema: z.ZodObject<{
85
+ type: z.ZodLiteral<"replo:annotations:clicked">;
86
+ id: z.ZodString;
87
+ viewportRect: z.ZodObject<{
88
+ x: z.ZodNumber;
89
+ y: z.ZodNumber;
90
+ width: z.ZodNumber;
91
+ height: z.ZodNumber;
92
+ }, z.core.$strip>;
93
+ }, z.core.$strip>;
94
+ export declare const pageAnnotationsActiveRectMessageSchema: z.ZodObject<{
95
+ type: z.ZodLiteral<"replo:annotations:activeRect">;
96
+ id: z.ZodString;
97
+ viewportRect: z.ZodObject<{
98
+ x: z.ZodNumber;
99
+ y: z.ZodNumber;
100
+ width: z.ZodNumber;
101
+ height: z.ZodNumber;
102
+ }, z.core.$strip>;
103
+ }, z.core.$strip>;
104
+ export declare const pageAnnotationsUnresolvedMessageSchema: z.ZodObject<{
105
+ type: z.ZodLiteral<"replo:annotations:unresolved">;
106
+ setId: z.ZodString;
107
+ ids: z.ZodArray<z.ZodString>;
108
+ }, z.core.$strip>;
109
+ export declare const pageAnnotationsIframeMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
110
+ type: z.ZodLiteral<"replo:annotations:ready">;
111
+ protocolVersion: z.ZodLiteral<1>;
112
+ }, z.core.$strip>, z.ZodObject<{
113
+ type: z.ZodLiteral<"replo:annotations:clicked">;
114
+ id: z.ZodString;
115
+ viewportRect: z.ZodObject<{
116
+ x: z.ZodNumber;
117
+ y: z.ZodNumber;
118
+ width: z.ZodNumber;
119
+ height: z.ZodNumber;
120
+ }, z.core.$strip>;
121
+ }, z.core.$strip>, z.ZodObject<{
122
+ type: z.ZodLiteral<"replo:annotations:activeRect">;
123
+ id: z.ZodString;
124
+ viewportRect: z.ZodObject<{
125
+ x: z.ZodNumber;
126
+ y: z.ZodNumber;
127
+ width: z.ZodNumber;
128
+ height: z.ZodNumber;
129
+ }, z.core.$strip>;
130
+ }, z.core.$strip>, z.ZodObject<{
131
+ type: z.ZodLiteral<"replo:annotations:unresolved">;
132
+ setId: z.ZodString;
133
+ ids: z.ZodArray<z.ZodString>;
134
+ }, z.core.$strip>], "type">;
@@ -0,0 +1,61 @@
1
+ import { z } from 'zod';
2
+
3
+ // ../schemas/pageAnnotations.ts
4
+ var PAGE_ANNOTATIONS_PROTOCOL_VERSION = 1;
5
+ var pageAnnotationColorSchema = z.enum(["red", "purple", "amber"]).describe("PageAnnotationColor");
6
+ var pageAnnotationPlacementSchema = z.enum(["element", "pageTop"]).describe("PageAnnotationPlacement");
7
+ var pageAnnotationMarkerSchema = z.object({
8
+ id: z.string().min(1).max(256),
9
+ number: z.number().int().min(1).max(99),
10
+ color: pageAnnotationColorSchema,
11
+ selector: z.string().min(1).max(2e3),
12
+ // "pageTop" markers ignore the selector and stack near the top of the
13
+ // page (page-wide findings); "element" markers pin to their selector.
14
+ placement: pageAnnotationPlacementSchema.default("element")
15
+ }).describe("PageAnnotationMarker");
16
+ var pageAnnotationViewportRectSchema = z.object({
17
+ x: z.number(),
18
+ y: z.number(),
19
+ width: z.number(),
20
+ height: z.number()
21
+ }).describe("PageAnnotationViewportRect");
22
+ var pageAnnotationsSetMessageSchema = z.object({
23
+ type: z.literal("replo:annotations:set"),
24
+ protocolVersion: z.literal(PAGE_ANNOTATIONS_PROTOCOL_VERSION),
25
+ setId: z.string().min(1),
26
+ markers: z.array(pageAnnotationMarkerSchema).max(50)
27
+ }).describe("PageAnnotationsSetMessage");
28
+ var pageAnnotationsClearMessageSchema = z.object({ type: z.literal("replo:annotations:clear") }).describe("PageAnnotationsClearMessage");
29
+ var pageAnnotationsRequestReadyMessageSchema = z.object({ type: z.literal("replo:annotations:requestReady") }).describe("PageAnnotationsRequestReadyMessage");
30
+ var pageAnnotationsParentMessageSchema = z.discriminatedUnion("type", [
31
+ pageAnnotationsSetMessageSchema,
32
+ pageAnnotationsClearMessageSchema,
33
+ pageAnnotationsRequestReadyMessageSchema
34
+ ]).describe("PageAnnotationsParentMessage");
35
+ var pageAnnotationsReadyMessageSchema = z.object({
36
+ type: z.literal("replo:annotations:ready"),
37
+ protocolVersion: z.literal(PAGE_ANNOTATIONS_PROTOCOL_VERSION)
38
+ }).describe("PageAnnotationsReadyMessage");
39
+ var pageAnnotationsClickedMessageSchema = z.object({
40
+ type: z.literal("replo:annotations:clicked"),
41
+ id: z.string().min(1).max(256),
42
+ viewportRect: pageAnnotationViewportRectSchema
43
+ }).describe("PageAnnotationsClickedMessage");
44
+ var pageAnnotationsActiveRectMessageSchema = z.object({
45
+ type: z.literal("replo:annotations:activeRect"),
46
+ id: z.string().min(1).max(256),
47
+ viewportRect: pageAnnotationViewportRectSchema
48
+ }).describe("PageAnnotationsActiveRectMessage");
49
+ var pageAnnotationsUnresolvedMessageSchema = z.object({
50
+ type: z.literal("replo:annotations:unresolved"),
51
+ setId: z.string().min(1),
52
+ ids: z.array(z.string().min(1).max(256)).max(50)
53
+ }).describe("PageAnnotationsUnresolvedMessage");
54
+ var pageAnnotationsIframeMessageSchema = z.discriminatedUnion("type", [
55
+ pageAnnotationsReadyMessageSchema,
56
+ pageAnnotationsClickedMessageSchema,
57
+ pageAnnotationsActiveRectMessageSchema,
58
+ pageAnnotationsUnresolvedMessageSchema
59
+ ]).describe("PageAnnotationsIframeMessage");
60
+
61
+ export { PAGE_ANNOTATIONS_PROTOCOL_VERSION, pageAnnotationColorSchema, pageAnnotationMarkerSchema, pageAnnotationPlacementSchema, pageAnnotationViewportRectSchema, pageAnnotationsActiveRectMessageSchema, pageAnnotationsClearMessageSchema, pageAnnotationsClickedMessageSchema, pageAnnotationsIframeMessageSchema, pageAnnotationsParentMessageSchema, pageAnnotationsReadyMessageSchema, pageAnnotationsRequestReadyMessageSchema, pageAnnotationsSetMessageSchema, pageAnnotationsUnresolvedMessageSchema };
@@ -1,5 +1,5 @@
1
- import { exhaustiveSwitch } from '../../chunk-UWHM3FKU.mjs';
2
1
  import { parseJsonOrNull } from '../../chunk-VKO7P3ED.mjs';
2
+ import { exhaustiveSwitch } from '../../chunk-UWHM3FKU.mjs';
3
3
  import '../../chunk-ILGEM42C.mjs';
4
4
  import '../../chunk-FNTGY7I5.mjs';
5
5
  import { z } from 'zod';
@@ -0,0 +1 @@
1
+ export declare function AnnotationLayerMount(): null;
@@ -0,0 +1,13 @@
1
+ "use client";
2
+ if (typeof window !== "undefined") {
3
+ void import("./annotationController").then((module) => {
4
+ module.initAnnotationLayer();
5
+ });
6
+ }
7
+ function AnnotationLayerMount() {
8
+ return null;
9
+ }
10
+ export {
11
+ AnnotationLayerMount
12
+ };
13
+ //# sourceMappingURL=AnnotationLayerMount.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../editor/annotations/AnnotationLayerMount.tsx"],
4
+ "sourcesContent": ["\"use client\";\n\n/**\n * Boots the vanilla annotation controller inside preview iframes. The chunk\n * loads (and this module-scope boot runs) only when the provider actually\n * renders the element, which it folds away on a NODE_ENV literal \u2014 so\n * annotation code never ships to published storefronts.\n */\nif (typeof window !== \"undefined\") {\n void import(\"./annotationController\").then((module) => {\n module.initAnnotationLayer();\n });\n}\n\nexport function AnnotationLayerMount() {\n return null;\n}\n"],
5
+ "mappings": ";AAQA,IAAI,OAAO,WAAW,aAAa;AACjC,OAAK,OAAO,wBAAwB,EAAE,KAAK,CAAC,WAAW;AACrD,WAAO,oBAAoB;AAAA,EAC7B,CAAC;AACH;AAEO,SAAS,uBAAuB;AACrC,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1 @@
1
+ export declare function initAnnotationLayer(): void;
@@ -0,0 +1,384 @@
1
+ import { exhaustiveSwitch } from "../../_vendor/replo-utils/lib/misc.mjs";
2
+ import {
3
+ PAGE_ANNOTATIONS_PROTOCOL_VERSION,
4
+ pageAnnotationsParentMessageSchema
5
+ } from "../../_vendor/schemas/pageAnnotations.mjs";
6
+ import {
7
+ ANNOTATION_CONTAINER_ID,
8
+ clearAnnotationFrames,
9
+ getAnnotationBadgeViewportRect,
10
+ setActiveAnnotationBadge,
11
+ showAnnotationFrames
12
+ } from "../select-mode/annotationFrames";
13
+ import { getDocumentScrollOffset } from "../select-mode/interactions/selectionGeometry";
14
+ import { isAllowedEditorMessage, isAllowedEditorOrigin } from "./bridgeGate";
15
+ const CLUSTER_DISTANCE_PX = 24;
16
+ const CLUSTER_FAN_RADIUS_PX = 30;
17
+ const REMEASURE_DEBOUNCE_MS = 250;
18
+ const PAGE_TOP_STACK_X_PX = 20;
19
+ const PAGE_TOP_STACK_Y_PX = 20;
20
+ const PAGE_TOP_STACK_GAP_PX = 36;
21
+ const COLOR_HEX = {
22
+ red: "#ef196e",
23
+ purple: "#8b32a8",
24
+ amber: "#274ae2"
25
+ };
26
+ const COLOR_SEVERITY = [
27
+ "red",
28
+ "purple",
29
+ "amber"
30
+ ];
31
+ let isInitialized = false;
32
+ let editorOrigin = null;
33
+ let markers = [];
34
+ let setId = null;
35
+ let expandedClusterId = null;
36
+ let activeMarkerId = null;
37
+ let remeasureFrameId = null;
38
+ let remeasureDebounceId = null;
39
+ let activeRectFrameId = null;
40
+ let observersActive = false;
41
+ let lastRenderSignature = null;
42
+ let resizeObserver = null;
43
+ let mutationObserver = null;
44
+ function postToEditor(message) {
45
+ if (editorOrigin === null) {
46
+ return;
47
+ }
48
+ window.parent.postMessage(message, editorOrigin);
49
+ }
50
+ function hasFixedAncestor(element) {
51
+ let current = element;
52
+ while (current !== null && current !== document.documentElement) {
53
+ const position = getComputedStyle(current).position;
54
+ if (position === "fixed" || position === "sticky") {
55
+ return true;
56
+ }
57
+ current = current.parentElement;
58
+ }
59
+ return false;
60
+ }
61
+ function resolveMarkers() {
62
+ const resolved = [];
63
+ const unresolvedIds = [];
64
+ let pageTopSlot = 0;
65
+ for (const marker of markers) {
66
+ if (marker.placement === "pageTop") {
67
+ resolved.push({
68
+ marker,
69
+ element: null,
70
+ isFixed: false,
71
+ rect: {
72
+ x: PAGE_TOP_STACK_X_PX + pageTopSlot * PAGE_TOP_STACK_GAP_PX,
73
+ y: PAGE_TOP_STACK_Y_PX,
74
+ width: 0,
75
+ height: 0
76
+ },
77
+ // The editor owns numbering; rendering the wire number keeps dots in sync with the panel.
78
+ displayNumber: marker.number
79
+ });
80
+ pageTopSlot += 1;
81
+ continue;
82
+ }
83
+ let element = null;
84
+ try {
85
+ element = document.querySelector(marker.selector);
86
+ } catch {
87
+ }
88
+ const domRect = element?.getBoundingClientRect();
89
+ if (!element || !domRect || domRect.width === 0 && domRect.height === 0) {
90
+ unresolvedIds.push(marker.id);
91
+ continue;
92
+ }
93
+ const isFixed = hasFixedAncestor(element);
94
+ const scrollOffset = getDocumentScrollOffset();
95
+ resolved.push({
96
+ marker,
97
+ element,
98
+ isFixed,
99
+ // Fixed frames use viewport coords; absolute frames use page coords.
100
+ rect: {
101
+ x: domRect.x + (isFixed ? 0 : scrollOffset.left),
102
+ y: domRect.y + (isFixed ? 0 : scrollOffset.top),
103
+ width: domRect.width,
104
+ height: domRect.height
105
+ },
106
+ displayNumber: marker.number
107
+ });
108
+ }
109
+ return { resolved, unresolvedIds };
110
+ }
111
+ function clusterResolvedMarkers(resolved) {
112
+ const clusters = [];
113
+ for (const entry of resolved) {
114
+ const centerX = entry.rect.x + entry.rect.width / 2;
115
+ const centerY = entry.rect.y;
116
+ const host = clusters.find((cluster) => {
117
+ return cluster.isFixed === entry.isFixed && Math.hypot(cluster.centerX - centerX, cluster.centerY - centerY) < CLUSTER_DISTANCE_PX;
118
+ });
119
+ if (host) {
120
+ host.members.push(entry);
121
+ } else {
122
+ clusters.push({
123
+ id: entry.marker.id,
124
+ centerX,
125
+ centerY,
126
+ isFixed: entry.isFixed,
127
+ members: [entry]
128
+ });
129
+ }
130
+ }
131
+ return clusters;
132
+ }
133
+ function getMostSevereColorHex(members) {
134
+ const color = COLOR_SEVERITY.find((candidate) => {
135
+ return members.some((member) => member.marker.color === candidate);
136
+ }) ?? "amber";
137
+ return COLOR_HEX[color];
138
+ }
139
+ function activateMarker(id) {
140
+ activeMarkerId = id;
141
+ const viewportRect = getAnnotationBadgeViewportRect(id);
142
+ if (viewportRect) {
143
+ postToEditor({ type: "replo:annotations:clicked", id, viewportRect });
144
+ }
145
+ setActiveAnnotationBadge(id);
146
+ }
147
+ function buildMemberSpec({
148
+ member,
149
+ badgeCenter
150
+ }) {
151
+ const { marker } = member;
152
+ return {
153
+ id: marker.id,
154
+ pageRect: member.rect,
155
+ badgeCenter,
156
+ positioning: member.isFixed ? "fixed" : "absolute",
157
+ colorHex: COLOR_HEX[marker.color],
158
+ badgeLabel: String(member.displayNumber),
159
+ badgeAriaLabel: `Page issue ${member.displayNumber}`,
160
+ isActive: activeMarkerId === marker.id,
161
+ onBadgeClick: () => activateMarker(marker.id)
162
+ };
163
+ }
164
+ function buildClusterSpecs(cluster) {
165
+ const [only] = cluster.members;
166
+ if (cluster.members.length === 1 && only) {
167
+ return [
168
+ buildMemberSpec({
169
+ member: only,
170
+ badgeCenter: { x: cluster.centerX, y: cluster.centerY }
171
+ })
172
+ ];
173
+ }
174
+ const hub = {
175
+ x: cluster.centerX,
176
+ y: cluster.centerY,
177
+ width: 0,
178
+ height: 0
179
+ };
180
+ if (expandedClusterId !== cluster.id) {
181
+ return [
182
+ {
183
+ id: `cluster:${cluster.id}`,
184
+ pageRect: hub,
185
+ badgeCenter: { x: cluster.centerX, y: cluster.centerY },
186
+ positioning: cluster.isFixed ? "fixed" : "absolute",
187
+ colorHex: getMostSevereColorHex(cluster.members),
188
+ badgeLabel: String(cluster.members.length),
189
+ badgeAriaLabel: `${cluster.members.length} page issues near this element`,
190
+ isActive: false,
191
+ onBadgeClick: () => {
192
+ expandedClusterId = cluster.id;
193
+ render();
194
+ }
195
+ }
196
+ ];
197
+ }
198
+ const memberSpecs = cluster.members.map((member, index) => {
199
+ const angle = index / cluster.members.length * 2 * Math.PI - Math.PI / 2;
200
+ return buildMemberSpec({
201
+ member,
202
+ badgeCenter: {
203
+ x: cluster.centerX + Math.cos(angle) * CLUSTER_FAN_RADIUS_PX,
204
+ y: cluster.centerY + Math.sin(angle) * CLUSTER_FAN_RADIUS_PX
205
+ }
206
+ });
207
+ });
208
+ return [
209
+ {
210
+ id: `cluster:${cluster.id}`,
211
+ pageRect: hub,
212
+ badgeCenter: { x: cluster.centerX, y: cluster.centerY },
213
+ positioning: cluster.isFixed ? "fixed" : "absolute",
214
+ colorHex: "#94a3b8",
215
+ badgeLabel: "\xD7",
216
+ badgeAriaLabel: "Collapse issue cluster",
217
+ isActive: false,
218
+ onBadgeClick: () => {
219
+ expandedClusterId = null;
220
+ render();
221
+ }
222
+ },
223
+ ...memberSpecs
224
+ ];
225
+ }
226
+ function render() {
227
+ if (markers.length === 0) {
228
+ lastRenderSignature = null;
229
+ clearAnnotationFrames();
230
+ syncObservers();
231
+ return;
232
+ }
233
+ const { resolved, unresolvedIds } = resolveMarkers();
234
+ if (activeMarkerId !== null && unresolvedIds.includes(activeMarkerId)) {
235
+ activeMarkerId = null;
236
+ }
237
+ const signature = [
238
+ resolved.map((entry) => {
239
+ return `${entry.marker.id}:${Math.round(entry.rect.x)},${Math.round(entry.rect.y)}`;
240
+ }).join("|"),
241
+ `active:${activeMarkerId ?? ""}`,
242
+ `expanded:${expandedClusterId ?? ""}`,
243
+ `unresolved:${unresolvedIds.join(",")}`
244
+ ].join("#");
245
+ if (signature === lastRenderSignature) {
246
+ return;
247
+ }
248
+ lastRenderSignature = signature;
249
+ const frames = clusterResolvedMarkers(resolved).flatMap((cluster) => {
250
+ return buildClusterSpecs(cluster);
251
+ });
252
+ showAnnotationFrames({ frames });
253
+ if (setId !== null) {
254
+ postToEditor({
255
+ type: "replo:annotations:unresolved",
256
+ setId,
257
+ ids: unresolvedIds
258
+ });
259
+ }
260
+ syncObservers();
261
+ }
262
+ function scheduleRemeasure() {
263
+ remeasureFrameId ??= requestAnimationFrame(() => {
264
+ remeasureFrameId = null;
265
+ render();
266
+ });
267
+ }
268
+ function scheduleDebouncedRemeasure() {
269
+ if (remeasureDebounceId !== null) {
270
+ clearTimeout(remeasureDebounceId);
271
+ }
272
+ remeasureDebounceId = setTimeout(scheduleRemeasure, REMEASURE_DEBOUNCE_MS);
273
+ }
274
+ function handleScroll(event) {
275
+ if (event.target !== document) {
276
+ scheduleRemeasure();
277
+ }
278
+ if (activeMarkerId !== null) {
279
+ const id = activeMarkerId;
280
+ activeRectFrameId ??= requestAnimationFrame(() => {
281
+ activeRectFrameId = null;
282
+ const viewportRect = getAnnotationBadgeViewportRect(id);
283
+ if (viewportRect) {
284
+ postToEditor({
285
+ type: "replo:annotations:activeRect",
286
+ id,
287
+ viewportRect
288
+ });
289
+ }
290
+ });
291
+ }
292
+ }
293
+ function syncObservers() {
294
+ const shouldObserve = markers.length > 0;
295
+ if (shouldObserve === observersActive) {
296
+ return;
297
+ }
298
+ observersActive = shouldObserve;
299
+ if (shouldObserve) {
300
+ window.addEventListener("resize", scheduleRemeasure);
301
+ window.addEventListener("scroll", handleScroll, { capture: true });
302
+ if (typeof ResizeObserver !== "undefined") {
303
+ resizeObserver = new ResizeObserver(scheduleRemeasure);
304
+ resizeObserver.observe(document.body);
305
+ }
306
+ mutationObserver = new MutationObserver((records) => {
307
+ const container = document.getElementById(ANNOTATION_CONTAINER_ID);
308
+ const hasForeignMutation = records.some((record) => {
309
+ return !container || !container.contains(record.target);
310
+ });
311
+ if (hasForeignMutation) {
312
+ scheduleDebouncedRemeasure();
313
+ }
314
+ });
315
+ mutationObserver.observe(document.body, { childList: true, subtree: true });
316
+ } else {
317
+ window.removeEventListener("resize", scheduleRemeasure);
318
+ window.removeEventListener("scroll", handleScroll, { capture: true });
319
+ resizeObserver?.disconnect();
320
+ resizeObserver = null;
321
+ mutationObserver?.disconnect();
322
+ mutationObserver = null;
323
+ }
324
+ }
325
+ function handleMessage(event) {
326
+ if (!isAllowedEditorMessage(event)) {
327
+ return;
328
+ }
329
+ const parsed = pageAnnotationsParentMessageSchema.safeParse(event.data);
330
+ if (!parsed.success) {
331
+ return;
332
+ }
333
+ editorOrigin = event.origin;
334
+ exhaustiveSwitch(parsed.data)({
335
+ "replo:annotations:set": (message) => {
336
+ markers = message.markers;
337
+ setId = message.setId;
338
+ expandedClusterId = null;
339
+ activeMarkerId = null;
340
+ lastRenderSignature = null;
341
+ render();
342
+ },
343
+ "replo:annotations:clear": () => {
344
+ markers = [];
345
+ setId = null;
346
+ expandedClusterId = null;
347
+ activeMarkerId = null;
348
+ render();
349
+ },
350
+ "replo:annotations:requestReady": () => {
351
+ postToEditor({
352
+ type: "replo:annotations:ready",
353
+ protocolVersion: PAGE_ANNOTATIONS_PROTOCOL_VERSION
354
+ });
355
+ }
356
+ });
357
+ }
358
+ function initAnnotationLayer() {
359
+ if (isInitialized || typeof window === "undefined") {
360
+ return;
361
+ }
362
+ if (window.self === window.top) {
363
+ return;
364
+ }
365
+ isInitialized = true;
366
+ window.addEventListener("message", handleMessage);
367
+ try {
368
+ const referrerOrigin = new URL(document.referrer).origin;
369
+ if (isAllowedEditorOrigin(referrerOrigin)) {
370
+ window.parent.postMessage(
371
+ {
372
+ type: "replo:annotations:ready",
373
+ protocolVersion: PAGE_ANNOTATIONS_PROTOCOL_VERSION
374
+ },
375
+ referrerOrigin
376
+ );
377
+ }
378
+ } catch {
379
+ }
380
+ }
381
+ export {
382
+ initAnnotationLayer
383
+ };
384
+ //# sourceMappingURL=annotationController.js.map