@hagicode/hagilight-core 0.1.0-dev.148.1.e0c630e

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 (205) hide show
  1. package/Analytics51LA.astro +17 -0
  2. package/Copyright.astro +9 -0
  3. package/Footer.astro +208 -0
  4. package/FooterAvatarPicker.astro +9 -0
  5. package/FooterBrand.astro +339 -0
  6. package/GoogleAnalytics.astro +30 -0
  7. package/PostHog.astro +11 -0
  8. package/PromotoBanner.astro +582 -0
  9. package/README.md +485 -0
  10. package/RootTransit.astro +599 -0
  11. package/assets/footer/avatar.avif +0 -0
  12. package/assets/footer/avatar.webp +0 -0
  13. package/assets/footer/manifest.json +237 -0
  14. package/data/comparison-articles/de-DE/claude-vs-hagicode.json +294 -0
  15. package/data/comparison-articles/de-DE/codebuddy-vs-hagicode.json +266 -0
  16. package/data/comparison-articles/de-DE/codex-vs-hagicode.json +266 -0
  17. package/data/comparison-articles/de-DE/copilot-vs-hagicode.json +266 -0
  18. package/data/comparison-articles/de-DE/deepagents-vs-hagicode.json +266 -0
  19. package/data/comparison-articles/de-DE/gemini-vs-hagicode.json +266 -0
  20. package/data/comparison-articles/de-DE/hermes-vs-hagicode.json +266 -0
  21. package/data/comparison-articles/de-DE/kimi-vs-hagicode.json +266 -0
  22. package/data/comparison-articles/de-DE/kiro-vs-hagicode.json +266 -0
  23. package/data/comparison-articles/de-DE/opencode-vs-hagicode.json +266 -0
  24. package/data/comparison-articles/de-DE/pi-vs-hagicode.json +282 -0
  25. package/data/comparison-articles/de-DE/qoder-vs-hagicode.json +266 -0
  26. package/data/comparison-articles/de-DE/reasonix-vs-hagicode.json +281 -0
  27. package/data/comparison-articles/en-US/claude-vs-hagicode.json +346 -0
  28. package/data/comparison-articles/en-US/codebuddy-vs-hagicode.json +322 -0
  29. package/data/comparison-articles/en-US/codex-vs-hagicode.json +321 -0
  30. package/data/comparison-articles/en-US/copilot-vs-hagicode.json +320 -0
  31. package/data/comparison-articles/en-US/deepagents-vs-hagicode.json +323 -0
  32. package/data/comparison-articles/en-US/dsh-vs-hagicode.json +323 -0
  33. package/data/comparison-articles/en-US/gemini-vs-hagicode.json +320 -0
  34. package/data/comparison-articles/en-US/hermes-vs-hagicode.json +323 -0
  35. package/data/comparison-articles/en-US/junie-vs-hagicode.json +323 -0
  36. package/data/comparison-articles/en-US/kimi-vs-hagicode.json +319 -0
  37. package/data/comparison-articles/en-US/kiro-vs-hagicode.json +319 -0
  38. package/data/comparison-articles/en-US/omp-vs-hagicode.json +345 -0
  39. package/data/comparison-articles/en-US/opencode-vs-hagicode.json +333 -0
  40. package/data/comparison-articles/en-US/pi-vs-hagicode.json +345 -0
  41. package/data/comparison-articles/en-US/qoder-vs-hagicode.json +323 -0
  42. package/data/comparison-articles/en-US/reasonix-vs-hagicode.json +344 -0
  43. package/data/comparison-articles/es-ES/claude-vs-hagicode.json +294 -0
  44. package/data/comparison-articles/es-ES/codebuddy-vs-hagicode.json +266 -0
  45. package/data/comparison-articles/es-ES/codex-vs-hagicode.json +266 -0
  46. package/data/comparison-articles/es-ES/copilot-vs-hagicode.json +266 -0
  47. package/data/comparison-articles/es-ES/deepagents-vs-hagicode.json +266 -0
  48. package/data/comparison-articles/es-ES/gemini-vs-hagicode.json +266 -0
  49. package/data/comparison-articles/es-ES/hermes-vs-hagicode.json +266 -0
  50. package/data/comparison-articles/es-ES/kimi-vs-hagicode.json +266 -0
  51. package/data/comparison-articles/es-ES/kiro-vs-hagicode.json +266 -0
  52. package/data/comparison-articles/es-ES/opencode-vs-hagicode.json +266 -0
  53. package/data/comparison-articles/es-ES/pi-vs-hagicode.json +281 -0
  54. package/data/comparison-articles/es-ES/qoder-vs-hagicode.json +266 -0
  55. package/data/comparison-articles/es-ES/reasonix-vs-hagicode.json +281 -0
  56. package/data/comparison-articles/fr-FR/claude-vs-hagicode.json +294 -0
  57. package/data/comparison-articles/fr-FR/codebuddy-vs-hagicode.json +266 -0
  58. package/data/comparison-articles/fr-FR/codex-vs-hagicode.json +266 -0
  59. package/data/comparison-articles/fr-FR/copilot-vs-hagicode.json +266 -0
  60. package/data/comparison-articles/fr-FR/deepagents-vs-hagicode.json +266 -0
  61. package/data/comparison-articles/fr-FR/gemini-vs-hagicode.json +266 -0
  62. package/data/comparison-articles/fr-FR/hermes-vs-hagicode.json +266 -0
  63. package/data/comparison-articles/fr-FR/kimi-vs-hagicode.json +266 -0
  64. package/data/comparison-articles/fr-FR/kiro-vs-hagicode.json +266 -0
  65. package/data/comparison-articles/fr-FR/opencode-vs-hagicode.json +266 -0
  66. package/data/comparison-articles/fr-FR/pi-vs-hagicode.json +281 -0
  67. package/data/comparison-articles/fr-FR/qoder-vs-hagicode.json +266 -0
  68. package/data/comparison-articles/fr-FR/reasonix-vs-hagicode.json +281 -0
  69. package/data/comparison-articles/ja-JP/claude-vs-hagicode.json +294 -0
  70. package/data/comparison-articles/ja-JP/codebuddy-vs-hagicode.json +266 -0
  71. package/data/comparison-articles/ja-JP/codex-vs-hagicode.json +266 -0
  72. package/data/comparison-articles/ja-JP/copilot-vs-hagicode.json +266 -0
  73. package/data/comparison-articles/ja-JP/deepagents-vs-hagicode.json +266 -0
  74. package/data/comparison-articles/ja-JP/gemini-vs-hagicode.json +266 -0
  75. package/data/comparison-articles/ja-JP/hermes-vs-hagicode.json +266 -0
  76. package/data/comparison-articles/ja-JP/kimi-vs-hagicode.json +266 -0
  77. package/data/comparison-articles/ja-JP/kiro-vs-hagicode.json +266 -0
  78. package/data/comparison-articles/ja-JP/opencode-vs-hagicode.json +266 -0
  79. package/data/comparison-articles/ja-JP/pi-vs-hagicode.json +281 -0
  80. package/data/comparison-articles/ja-JP/qoder-vs-hagicode.json +266 -0
  81. package/data/comparison-articles/ja-JP/reasonix-vs-hagicode.json +281 -0
  82. package/data/comparison-articles/ko-KR/claude-vs-hagicode.json +294 -0
  83. package/data/comparison-articles/ko-KR/codebuddy-vs-hagicode.json +266 -0
  84. package/data/comparison-articles/ko-KR/codex-vs-hagicode.json +266 -0
  85. package/data/comparison-articles/ko-KR/copilot-vs-hagicode.json +266 -0
  86. package/data/comparison-articles/ko-KR/deepagents-vs-hagicode.json +266 -0
  87. package/data/comparison-articles/ko-KR/gemini-vs-hagicode.json +266 -0
  88. package/data/comparison-articles/ko-KR/hermes-vs-hagicode.json +266 -0
  89. package/data/comparison-articles/ko-KR/kimi-vs-hagicode.json +266 -0
  90. package/data/comparison-articles/ko-KR/kiro-vs-hagicode.json +266 -0
  91. package/data/comparison-articles/ko-KR/opencode-vs-hagicode.json +266 -0
  92. package/data/comparison-articles/ko-KR/pi-vs-hagicode.json +281 -0
  93. package/data/comparison-articles/ko-KR/qoder-vs-hagicode.json +266 -0
  94. package/data/comparison-articles/ko-KR/reasonix-vs-hagicode.json +281 -0
  95. package/data/comparison-articles/pt-BR/claude-vs-hagicode.json +294 -0
  96. package/data/comparison-articles/pt-BR/codebuddy-vs-hagicode.json +266 -0
  97. package/data/comparison-articles/pt-BR/codex-vs-hagicode.json +266 -0
  98. package/data/comparison-articles/pt-BR/copilot-vs-hagicode.json +266 -0
  99. package/data/comparison-articles/pt-BR/deepagents-vs-hagicode.json +266 -0
  100. package/data/comparison-articles/pt-BR/gemini-vs-hagicode.json +266 -0
  101. package/data/comparison-articles/pt-BR/hermes-vs-hagicode.json +266 -0
  102. package/data/comparison-articles/pt-BR/kimi-vs-hagicode.json +266 -0
  103. package/data/comparison-articles/pt-BR/kiro-vs-hagicode.json +266 -0
  104. package/data/comparison-articles/pt-BR/opencode-vs-hagicode.json +266 -0
  105. package/data/comparison-articles/pt-BR/pi-vs-hagicode.json +281 -0
  106. package/data/comparison-articles/pt-BR/qoder-vs-hagicode.json +266 -0
  107. package/data/comparison-articles/pt-BR/reasonix-vs-hagicode.json +281 -0
  108. package/data/comparison-articles/ru-RU/claude-vs-hagicode.json +294 -0
  109. package/data/comparison-articles/ru-RU/codebuddy-vs-hagicode.json +266 -0
  110. package/data/comparison-articles/ru-RU/codex-vs-hagicode.json +266 -0
  111. package/data/comparison-articles/ru-RU/copilot-vs-hagicode.json +266 -0
  112. package/data/comparison-articles/ru-RU/deepagents-vs-hagicode.json +266 -0
  113. package/data/comparison-articles/ru-RU/gemini-vs-hagicode.json +266 -0
  114. package/data/comparison-articles/ru-RU/hermes-vs-hagicode.json +266 -0
  115. package/data/comparison-articles/ru-RU/kimi-vs-hagicode.json +266 -0
  116. package/data/comparison-articles/ru-RU/kiro-vs-hagicode.json +266 -0
  117. package/data/comparison-articles/ru-RU/opencode-vs-hagicode.json +266 -0
  118. package/data/comparison-articles/ru-RU/pi-vs-hagicode.json +281 -0
  119. package/data/comparison-articles/ru-RU/qoder-vs-hagicode.json +323 -0
  120. package/data/comparison-articles/ru-RU/reasonix-vs-hagicode.json +281 -0
  121. package/data/comparison-articles/zh-CN/claude-vs-hagicode.json +346 -0
  122. package/data/comparison-articles/zh-CN/codebuddy-vs-hagicode.json +322 -0
  123. package/data/comparison-articles/zh-CN/codex-vs-hagicode.json +321 -0
  124. package/data/comparison-articles/zh-CN/copilot-vs-hagicode.json +320 -0
  125. package/data/comparison-articles/zh-CN/deepagents-vs-hagicode.json +323 -0
  126. package/data/comparison-articles/zh-CN/dsh-vs-hagicode.json +323 -0
  127. package/data/comparison-articles/zh-CN/gemini-vs-hagicode.json +320 -0
  128. package/data/comparison-articles/zh-CN/hermes-vs-hagicode.json +323 -0
  129. package/data/comparison-articles/zh-CN/junie-vs-hagicode.json +323 -0
  130. package/data/comparison-articles/zh-CN/kimi-vs-hagicode.json +319 -0
  131. package/data/comparison-articles/zh-CN/kiro-vs-hagicode.json +319 -0
  132. package/data/comparison-articles/zh-CN/omp-vs-hagicode.json +345 -0
  133. package/data/comparison-articles/zh-CN/opencode-vs-hagicode.json +333 -0
  134. package/data/comparison-articles/zh-CN/pi-vs-hagicode.json +345 -0
  135. package/data/comparison-articles/zh-CN/qoder-vs-hagicode.json +323 -0
  136. package/data/comparison-articles/zh-CN/reasonix-vs-hagicode.json +344 -0
  137. package/data/comparison-articles/zh-Hant/claude-vs-hagicode.json +294 -0
  138. package/data/comparison-articles/zh-Hant/codebuddy-vs-hagicode.json +266 -0
  139. package/data/comparison-articles/zh-Hant/codex-vs-hagicode.json +266 -0
  140. package/data/comparison-articles/zh-Hant/copilot-vs-hagicode.json +266 -0
  141. package/data/comparison-articles/zh-Hant/deepagents-vs-hagicode.json +266 -0
  142. package/data/comparison-articles/zh-Hant/gemini-vs-hagicode.json +266 -0
  143. package/data/comparison-articles/zh-Hant/hermes-vs-hagicode.json +266 -0
  144. package/data/comparison-articles/zh-Hant/kimi-vs-hagicode.json +266 -0
  145. package/data/comparison-articles/zh-Hant/kiro-vs-hagicode.json +266 -0
  146. package/data/comparison-articles/zh-Hant/opencode-vs-hagicode.json +266 -0
  147. package/data/comparison-articles/zh-Hant/pi-vs-hagicode.json +281 -0
  148. package/data/comparison-articles/zh-Hant/qoder-vs-hagicode.json +266 -0
  149. package/data/comparison-articles/zh-Hant/reasonix-vs-hagicode.json +281 -0
  150. package/dist/analytics-events.d.ts +49 -0
  151. package/dist/analytics-events.js +113 -0
  152. package/dist/build-info-resolver.d.ts +8 -0
  153. package/dist/build-info-resolver.js +29 -0
  154. package/dist/build-info.d.ts +19 -0
  155. package/dist/build-info.js +60 -0
  156. package/dist/comparison-articles-source.d.ts +13 -0
  157. package/dist/comparison-articles-source.js +58 -0
  158. package/dist/comparison-articles.d.ts +127 -0
  159. package/dist/comparison-articles.js +260 -0
  160. package/dist/content-images.d.ts +126 -0
  161. package/dist/content-images.js +490 -0
  162. package/dist/favicon.d.ts +21 -0
  163. package/dist/favicon.js +27 -0
  164. package/dist/footer-avatar.d.ts +39 -0
  165. package/dist/footer-avatar.js +42 -0
  166. package/dist/footer-brand.d.ts +29 -0
  167. package/dist/footer-brand.js +86 -0
  168. package/dist/head.d.ts +7 -0
  169. package/dist/head.js +1 -0
  170. package/dist/link-policy.d.ts +33 -0
  171. package/dist/link-policy.js +93 -0
  172. package/dist/links.d.ts +70 -0
  173. package/dist/links.js +425 -0
  174. package/dist/posthog.d.ts +6 -0
  175. package/dist/posthog.js +17 -0
  176. package/dist/promotions.d.ts +50 -0
  177. package/dist/promotions.js +243 -0
  178. package/dist/promoto-banner.d.ts +14 -0
  179. package/dist/promoto-banner.js +499 -0
  180. package/dist/random-image.d.ts +69 -0
  181. package/dist/random-image.js +133 -0
  182. package/dist/related-sites.json +378 -0
  183. package/dist/root-transit.d.ts +12 -0
  184. package/dist/root-transit.js +221 -0
  185. package/dist/rss-footer-context.d.ts +6 -0
  186. package/dist/rss-footer-context.js +54 -0
  187. package/dist/rss-ownership.d.ts +26 -0
  188. package/dist/rss-ownership.js +63 -0
  189. package/dist/rss.d.ts +34 -0
  190. package/dist/rss.js +145 -0
  191. package/dist/seo-schema.d.ts +12 -0
  192. package/dist/seo-schema.js +20 -0
  193. package/dist/seo.d.ts +78 -0
  194. package/dist/seo.js +270 -0
  195. package/dist/site-metadata-ownership.d.ts +40 -0
  196. package/dist/site-metadata-ownership.js +72 -0
  197. package/dist/site-metadata-writer.d.ts +25 -0
  198. package/dist/site-metadata-writer.js +55 -0
  199. package/dist/site-metadata.d.ts +113 -0
  200. package/dist/site-metadata.js +256 -0
  201. package/dist/splash-images.d.ts +75 -0
  202. package/dist/splash-images.js +186 -0
  203. package/favicon.ico +0 -0
  204. package/logo.png +0 -0
  205. package/package.json +172 -0
@@ -0,0 +1,499 @@
1
+ import { gaEventAttributes } from './analytics-events.js';
2
+ import { classifyLink, getLinkRel, withCampaignUtm } from './link-policy.js';
3
+ import { loadActivePromotions, } from './promotions.js';
4
+ const ELEMENT_NAME = 'hagilight-promoto-banner';
5
+ const DISMISSED_SIGNATURE_KEY = 'hagilight:promoto-banner:dismissed-signature';
6
+ const VISIT_COUNT_KEY_PREFIX = 'hagilight:promoto-banner:visit-count:';
7
+ const VISIT_EVENT = 'hagilight:promoto-visit';
8
+ const PAGE_TOP_VISIT_LIMIT = 30;
9
+ const ROTATION_INTERVAL_MS = 8000;
10
+ const DISMISS_DURATION_MS = 220;
11
+ const REFRESH_INTERVAL_MS = 5 * 60 * 1000;
12
+ let lastCountedPage = null;
13
+ let siteVisitCount = 0;
14
+ function isRecord(value) {
15
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
16
+ }
17
+ function isSafeUrl(value) {
18
+ try {
19
+ return ['http:', 'https:'].includes(new URL(value, 'https://hagilight.invalid/').protocol);
20
+ }
21
+ catch {
22
+ return false;
23
+ }
24
+ }
25
+ function isUsableCard(value) {
26
+ if (!isRecord(value))
27
+ return false;
28
+ const image = value.image;
29
+ const imageIsValid = image === undefined || (isRecord(image) && typeof image.src === 'string' && isSafeUrl(image.src)
30
+ && typeof image.alt === 'string'
31
+ && (image.variant === undefined || typeof image.variant === 'string')
32
+ && (image.width === undefined || (typeof image.width === 'number' && Number.isFinite(image.width) && image.width > 0))
33
+ && (image.height === undefined || (typeof image.height === 'number' && Number.isFinite(image.height) && image.height > 0)));
34
+ return typeof value.id === 'string' && value.id.trim().length > 0
35
+ && typeof value.title === 'string' && value.title.trim().length > 0
36
+ && typeof value.description === 'string' && value.description.trim().length > 0
37
+ && typeof value.ctaLabel === 'string' && value.ctaLabel.trim().length > 0
38
+ && typeof value.link === 'string' && value.link.trim().length > 0 && isSafeUrl(value.link)
39
+ && imageIsValid;
40
+ }
41
+ export function selectPromotionCards(remote, fallback) {
42
+ const eligibleRemote = remote.filter(isUsableCard);
43
+ if (eligibleRemote.length)
44
+ return eligibleRemote;
45
+ return fallback && isUsableCard(fallback) ? [fallback] : [];
46
+ }
47
+ export function getPromotionImageSources(image) {
48
+ if (!image || !Array.isArray(image.sources))
49
+ return [];
50
+ return image.sources.filter((source) => source.type.toLowerCase().startsWith('image/')
51
+ && isSafeUrl(source.src)
52
+ && source.src !== image.src);
53
+ }
54
+ function sourceUrl(source) {
55
+ return new URL(source.srcset, document.baseURI).href;
56
+ }
57
+ function advancePromotionImageSource(image) {
58
+ const picture = image.parentElement;
59
+ if (!(picture instanceof HTMLPictureElement))
60
+ return;
61
+ const failed = Array.from(picture.querySelectorAll('source'))
62
+ .find((source) => sourceUrl(source) === image.currentSrc);
63
+ if (!failed)
64
+ return;
65
+ failed.remove();
66
+ // Re-assigning src makes the browser re-run source selection over the remaining tiers.
67
+ image.src = image.src;
68
+ }
69
+ export function getPromotionSetSignature(cards) {
70
+ if (!cards.length)
71
+ return null;
72
+ return JSON.stringify(cards.map(({ id, title, description, ctaLabel, link, image }) => [
73
+ id, title, description, ctaLabel, link, image?.src ?? '', image?.alt ?? '',
74
+ image?.variant ?? '', image?.width ?? 0, image?.height ?? 0,
75
+ ]));
76
+ }
77
+ export function isScrolledToTop(scrollY) {
78
+ return scrollY <= 1;
79
+ }
80
+ export function isPageTopRuleActive(visitCount) {
81
+ return visitCount <= PAGE_TOP_VISIT_LIMIT;
82
+ }
83
+ export function getVisitCountKey(hostname) {
84
+ return `${VISIT_COUNT_KEY_PREFIX}${hostname}`;
85
+ }
86
+ export function parseVisitCount(raw) {
87
+ if (raw === null || !/^\d+$/u.test(raw))
88
+ return 0;
89
+ const count = Number(raw);
90
+ return Number.isSafeInteger(count) ? count : 0;
91
+ }
92
+ export function getPromotionVisibility(cards, signature, dismissedSignature, footerVisible, atPageTop = false) {
93
+ if (!cards.length)
94
+ return 'hidden';
95
+ if (signature && signature === dismissedSignature)
96
+ return 'dismissed';
97
+ if (footerVisible)
98
+ return 'footer-hidden';
99
+ return atPageTop ? 'top-hidden' : 'ready';
100
+ }
101
+ export function getNextPromotionIndex(index, count, direction) {
102
+ return count > 0 ? (index + direction + count) % count : 0;
103
+ }
104
+ export function shouldAutoRotate(count, paused, reducedMotion, documentVisible, visibility) {
105
+ return count > 1 && !paused && !reducedMotion && documentVisible && visibility === 'ready';
106
+ }
107
+ function readDismissedSignature() {
108
+ try {
109
+ return window.localStorage.getItem(DISMISSED_SIGNATURE_KEY);
110
+ }
111
+ catch {
112
+ return null;
113
+ }
114
+ }
115
+ function saveDismissedSignature(signature) {
116
+ try {
117
+ window.localStorage.setItem(DISMISSED_SIGNATURE_KEY, signature);
118
+ }
119
+ catch (error) {
120
+ console.warn('Unable to save the dismissed promotion preference.', error);
121
+ }
122
+ }
123
+ function getLocalStorage() {
124
+ try {
125
+ return window.localStorage || null;
126
+ }
127
+ catch {
128
+ return null;
129
+ }
130
+ }
131
+ function countVisit(storage, key) {
132
+ let count;
133
+ try {
134
+ count = parseVisitCount(storage.getItem(key));
135
+ }
136
+ catch {
137
+ return 0;
138
+ }
139
+ if (!isPageTopRuleActive(count))
140
+ return count;
141
+ try {
142
+ storage.setItem(key, String(count + 1));
143
+ return count + 1;
144
+ }
145
+ catch {
146
+ return count;
147
+ }
148
+ }
149
+ export function recordSiteVisit() {
150
+ if (typeof window === 'undefined' || !window.location)
151
+ return siteVisitCount;
152
+ const page = `${window.location.pathname}${window.location.search}`;
153
+ if (page === lastCountedPage)
154
+ return siteVisitCount;
155
+ const storage = getLocalStorage();
156
+ if (!storage)
157
+ return siteVisitCount;
158
+ lastCountedPage = page;
159
+ siteVisitCount = countVisit(storage, getVisitCountKey(window.location.hostname));
160
+ if (typeof document !== 'undefined' && typeof document.dispatchEvent === 'function') {
161
+ document.dispatchEvent(new Event(VISIT_EVENT));
162
+ }
163
+ return siteVisitCount;
164
+ }
165
+ export function definePromotoBannerElement() {
166
+ if (typeof customElements === 'undefined' || typeof HTMLElement === 'undefined'
167
+ || customElements.get(ELEMENT_NAME))
168
+ return;
169
+ class PromotoBannerElement extends HTMLElement {
170
+ cards = [];
171
+ currentIndex = 0;
172
+ dismissedSignature = readDismissedSignature();
173
+ paused = false;
174
+ reducedMotion = false;
175
+ tick = 0;
176
+ dismissing = false;
177
+ refreshTimer;
178
+ motionQuery;
179
+ resizeObserver;
180
+ footer = null;
181
+ loadedLocale = null;
182
+ onPrevious = () => this.move(-1);
183
+ onNext = () => this.move(1);
184
+ onPause = () => {
185
+ if (this.reducedMotion)
186
+ return;
187
+ this.paused = !this.paused;
188
+ this.updateControls();
189
+ // Pausing freezes the progress line where it is; resuming carries on from there.
190
+ this.syncRotation(false);
191
+ };
192
+ onDismiss = () => {
193
+ const signature = getPromotionSetSignature(this.cards);
194
+ if (!signature || this.dismissing)
195
+ return;
196
+ this.dismissedSignature = signature;
197
+ saveDismissedSignature(signature);
198
+ this.stopRotation();
199
+ const shell = this.querySelector('[data-promoto-shell]');
200
+ const finish = () => {
201
+ this.dismissing = false;
202
+ this.updateVisibility();
203
+ };
204
+ if (!shell || this.reducedMotion || typeof shell.animate !== 'function') {
205
+ finish();
206
+ return;
207
+ }
208
+ // Sink away before the card is removed; the filled animation is cancelled once it is hidden.
209
+ this.dismissing = true;
210
+ shell.inert = true;
211
+ const exit = shell.animate([
212
+ { opacity: 1, transform: 'none' },
213
+ { opacity: 0, transform: 'translateY(1rem) scale(0.97)' },
214
+ ], { duration: DISMISS_DURATION_MS, easing: 'ease-in', fill: 'forwards' });
215
+ const settle = () => {
216
+ finish();
217
+ exit.cancel();
218
+ };
219
+ exit.finished.then(settle, settle);
220
+ };
221
+ // The progress line's fill is the rotation clock, so hover, focus and pause hold the card with it.
222
+ onProgressEnd = (event) => {
223
+ if (event.target === event.currentTarget)
224
+ this.move(1);
225
+ };
226
+ onLayoutChange = () => {
227
+ const previousState = this.dataset.state;
228
+ this.updateVisibility();
229
+ if (previousState !== this.dataset.state)
230
+ this.syncRotation(true);
231
+ };
232
+ onVisibilityChange = () => {
233
+ if (document.visibilityState === 'hidden') {
234
+ this.stopRotation();
235
+ this.stopRefresh();
236
+ }
237
+ else {
238
+ this.syncRotation(true);
239
+ this.startRefresh();
240
+ }
241
+ };
242
+ onMotionChange = () => {
243
+ this.reducedMotion = this.motionQuery?.matches ?? false;
244
+ this.updateControls();
245
+ this.syncRotation(true);
246
+ };
247
+ onPageLoad = () => {
248
+ recordSiteVisit();
249
+ // Re-fetch in the current page locale after a Starlight view-transition
250
+ // navigation. The element may persist across a soft language switch, so
251
+ // its previously loaded locale can differ from the new page's data-locale.
252
+ if (this.dataset.locale !== this.loadedLocale)
253
+ void this.reload();
254
+ };
255
+ connectedCallback() {
256
+ recordSiteVisit();
257
+ this.footer = document.querySelector('footer');
258
+ this.motionQuery = window.matchMedia?.('(prefers-reduced-motion: reduce)');
259
+ this.reducedMotion = this.motionQuery?.matches ?? false;
260
+ this.querySelector('[data-promoto-previous]')?.addEventListener('click', this.onPrevious);
261
+ this.querySelector('[data-promoto-next]')?.addEventListener('click', this.onNext);
262
+ this.querySelector('[data-promoto-pause]')?.addEventListener('click', this.onPause);
263
+ this.querySelector('[data-promoto-dismiss]')?.addEventListener('click', this.onDismiss);
264
+ this.querySelector('[data-promoto-progress]')?.addEventListener('animationend', this.onProgressEnd);
265
+ this.style.setProperty('--promoto-rotation', `${ROTATION_INTERVAL_MS}ms`);
266
+ window.addEventListener('scroll', this.onLayoutChange, { passive: true });
267
+ window.addEventListener('resize', this.onLayoutChange);
268
+ document.addEventListener('visibilitychange', this.onVisibilityChange);
269
+ this.motionQuery?.addEventListener('change', this.onMotionChange);
270
+ document.addEventListener('astro:page-load', this.onPageLoad);
271
+ document.addEventListener(VISIT_EVENT, this.onLayoutChange);
272
+ if (typeof ResizeObserver !== 'undefined') {
273
+ this.resizeObserver = new ResizeObserver(this.onLayoutChange);
274
+ if (this.footer)
275
+ this.resizeObserver.observe(this.footer);
276
+ const shell = this.querySelector('[data-promoto-shell]');
277
+ if (shell)
278
+ this.resizeObserver.observe(shell);
279
+ }
280
+ void this.reload();
281
+ this.startRefresh();
282
+ }
283
+ disconnectedCallback() {
284
+ this.querySelector('[data-promoto-previous]')?.removeEventListener('click', this.onPrevious);
285
+ this.querySelector('[data-promoto-next]')?.removeEventListener('click', this.onNext);
286
+ this.querySelector('[data-promoto-pause]')?.removeEventListener('click', this.onPause);
287
+ this.querySelector('[data-promoto-dismiss]')?.removeEventListener('click', this.onDismiss);
288
+ this.querySelector('[data-promoto-progress]')?.removeEventListener('animationend', this.onProgressEnd);
289
+ window.removeEventListener('scroll', this.onLayoutChange);
290
+ window.removeEventListener('resize', this.onLayoutChange);
291
+ document.removeEventListener('visibilitychange', this.onVisibilityChange);
292
+ this.motionQuery?.removeEventListener('change', this.onMotionChange);
293
+ document.removeEventListener('astro:page-load', this.onPageLoad);
294
+ document.removeEventListener(VISIT_EVENT, this.onLayoutChange);
295
+ this.resizeObserver?.disconnect();
296
+ this.stopRotation();
297
+ this.stopRefresh();
298
+ }
299
+ async reload() {
300
+ const previousId = this.cards[this.currentIndex]?.id;
301
+ this.loadedLocale = this.dataset.locale ?? null;
302
+ const remote = await loadActivePromotions({ locale: this.dataset.locale });
303
+ if (!this.isConnected)
304
+ return;
305
+ const fallback = this.readFallback();
306
+ this.cards = selectPromotionCards(remote, fallback);
307
+ const previousIndex = previousId ? this.cards.findIndex((card) => card.id === previousId) : -1;
308
+ this.currentIndex = previousIndex >= 0 ? previousIndex : 0;
309
+ this.renderCards();
310
+ this.updateVisibility();
311
+ this.updateControls();
312
+ this.syncRotation(false);
313
+ }
314
+ readFallback() {
315
+ const serialized = this.dataset.fallback;
316
+ if (!serialized)
317
+ return null;
318
+ try {
319
+ const value = JSON.parse(serialized);
320
+ return isUsableCard(value) ? value : null;
321
+ }
322
+ catch (error) {
323
+ console.warn('Unable to parse the site-provided promotion fallback.', error);
324
+ return null;
325
+ }
326
+ }
327
+ renderCards() {
328
+ const track = this.querySelector('[data-promoto-track]');
329
+ if (!track)
330
+ return;
331
+ const fragment = document.createDocumentFragment();
332
+ this.cards.forEach((card, index) => {
333
+ const slide = document.createElement('article');
334
+ slide.className = 'hagilight-promoto__slide';
335
+ slide.dataset.hasImage = String(Boolean(card.image));
336
+ slide.setAttribute('aria-hidden', index === this.currentIndex ? 'false' : 'true');
337
+ if (index !== this.currentIndex)
338
+ slide.setAttribute('inert', '');
339
+ const copy = document.createElement('div');
340
+ copy.className = 'hagilight-promoto__copy';
341
+ const title = document.createElement('h2');
342
+ title.className = 'hagilight-promoto__title';
343
+ title.textContent = card.title;
344
+ const description = document.createElement('p');
345
+ description.className = 'hagilight-promoto__description';
346
+ description.textContent = card.description;
347
+ copy.append(title, description);
348
+ const link = document.createElement('a');
349
+ link.className = 'hagilight-promoto__cta';
350
+ // Classified and tagged per slide, so rotation never reuses a previous decision.
351
+ const origin = globalThis.location?.origin;
352
+ link.href = withCampaignUtm(card.link, {
353
+ siteId: this.dataset.siteId,
354
+ medium: 'promoto',
355
+ origin,
356
+ });
357
+ link.target = '_blank';
358
+ link.rel = getLinkRel(classifyLink(card.link, { origin }));
359
+ link.textContent = card.ctaLabel;
360
+ for (const [name, value] of Object.entries(gaEventAttributes({
361
+ category: 'promotion',
362
+ label: card.id,
363
+ location: 'promoto_banner',
364
+ })))
365
+ link.setAttribute(name, value);
366
+ slide.append(copy);
367
+ if (card.image) {
368
+ const media = document.createElement('span');
369
+ media.className = 'hagilight-promoto__media';
370
+ const image = document.createElement('img');
371
+ image.className = 'hagilight-promoto__image';
372
+ image.alt = card.image.alt || card.title;
373
+ image.loading = index === this.currentIndex ? 'eager' : 'lazy';
374
+ image.decoding = 'async';
375
+ if (card.image.width)
376
+ image.width = card.image.width;
377
+ if (card.image.height)
378
+ image.height = card.image.height;
379
+ const sources = getPromotionImageSources(card.image);
380
+ if (sources.length) {
381
+ const picture = document.createElement('picture');
382
+ picture.className = 'hagilight-promoto__picture';
383
+ for (const source of sources) {
384
+ const element = document.createElement('source');
385
+ element.type = source.type;
386
+ element.srcset = source.src;
387
+ picture.append(element);
388
+ }
389
+ picture.append(image);
390
+ image.addEventListener('error', () => advancePromotionImageSource(image));
391
+ media.append(picture);
392
+ }
393
+ else {
394
+ media.append(image);
395
+ }
396
+ image.src = card.image.src;
397
+ slide.append(media);
398
+ }
399
+ slide.append(link);
400
+ fragment.append(slide);
401
+ });
402
+ track.replaceChildren(fragment);
403
+ this.updateSlides();
404
+ }
405
+ updateSlides() {
406
+ const track = this.querySelector('[data-promoto-track]');
407
+ if (!track)
408
+ return;
409
+ Array.from(track.children).forEach((slide, index) => {
410
+ const active = index === this.currentIndex;
411
+ slide.setAttribute('aria-hidden', String(!active));
412
+ if (active)
413
+ slide.removeAttribute('inert');
414
+ else
415
+ slide.setAttribute('inert', '');
416
+ });
417
+ track.style.transform = `translateX(-${this.currentIndex * 100}%)`;
418
+ const current = this.cards[this.currentIndex];
419
+ const status = this.querySelector('[data-promoto-status]');
420
+ if (status)
421
+ status.textContent = current?.title ?? '';
422
+ const count = this.querySelector('[data-promoto-count]');
423
+ if (count)
424
+ count.textContent = `${this.currentIndex + 1} / ${this.cards.length}`;
425
+ }
426
+ updateControls() {
427
+ const multiple = this.cards.length > 1;
428
+ this.toggleAttribute('data-multiple', multiple);
429
+ const controls = this.querySelector('[data-promoto-controls]');
430
+ if (controls)
431
+ controls.hidden = !multiple;
432
+ const pause = this.querySelector('[data-promoto-pause]');
433
+ if (pause) {
434
+ const paused = this.paused || this.reducedMotion;
435
+ pause.disabled = this.reducedMotion;
436
+ pause.setAttribute('aria-pressed', String(paused));
437
+ pause.setAttribute('aria-label', paused ? 'Resume automatic promotion rotation' : 'Pause automatic promotion rotation');
438
+ }
439
+ this.updateSlides();
440
+ }
441
+ move(direction) {
442
+ if (this.cards.length < 2)
443
+ return;
444
+ this.currentIndex = getNextPromotionIndex(this.currentIndex, this.cards.length, direction);
445
+ this.dataset.dir = direction < 0 ? 'prev' : 'next';
446
+ this.updateSlides();
447
+ this.syncRotation(true);
448
+ }
449
+ updateVisibility() {
450
+ const signature = getPromotionSetSignature(this.cards);
451
+ const atPageTop = isScrolledToTop(window.scrollY ?? document.documentElement.scrollTop)
452
+ && isPageTopRuleActive(siteVisitCount);
453
+ const state = getPromotionVisibility(this.cards, signature, this.dismissedSignature, this.isFooterVisible(), atPageTop);
454
+ const hidden = state === 'hidden' || state === 'dismissed';
455
+ const shell = this.querySelector('[data-promoto-shell]');
456
+ this.hidden = hidden;
457
+ if (shell) {
458
+ shell.hidden = hidden;
459
+ shell.inert = state !== 'ready';
460
+ }
461
+ this.dataset.state = state;
462
+ }
463
+ isFooterVisible() {
464
+ if (!this.footer)
465
+ return false;
466
+ const bounds = this.footer.getBoundingClientRect();
467
+ return bounds.top < window.innerHeight && bounds.bottom > 0;
468
+ }
469
+ /**
470
+ * Mirrors the rotation state onto the host for CSS: `data-rotating` runs the progress line,
471
+ * `data-paused` holds it. `restart` begins a fresh cycle; without it a running one carries on.
472
+ */
473
+ syncRotation(restart) {
474
+ const rotating = shouldAutoRotate(this.cards.length, false, this.reducedMotion, document.visibilityState !== 'hidden', (this.dataset.state ?? 'hidden')) && !this.dismissing;
475
+ const wasRotating = this.hasAttribute('data-rotating');
476
+ this.toggleAttribute('data-rotating', rotating);
477
+ this.toggleAttribute('data-paused', this.paused);
478
+ if (rotating && (restart || !wasRotating)) {
479
+ this.tick = this.tick === 0 ? 1 : 0;
480
+ this.dataset.tick = String(this.tick);
481
+ }
482
+ }
483
+ stopRotation() {
484
+ this.removeAttribute('data-rotating');
485
+ }
486
+ startRefresh() {
487
+ this.stopRefresh();
488
+ if (document.visibilityState !== 'hidden') {
489
+ this.refreshTimer = window.setInterval(() => void this.reload(), REFRESH_INTERVAL_MS);
490
+ }
491
+ }
492
+ stopRefresh() {
493
+ if (this.refreshTimer !== undefined)
494
+ window.clearInterval(this.refreshTimer);
495
+ this.refreshTimer = undefined;
496
+ }
497
+ }
498
+ customElements.define(ELEMENT_NAME, PromotoBannerElement);
499
+ }
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Browser-safe picker for decorative R2 images (`r2-image-random-display`).
3
+ *
4
+ * A slot keeps its vendored default in the markup and carries a `data-random-pool` JSON list of R2 object paths.
5
+ * After load, one entry is picked uniformly, preloaded with a plain `Image()` (never `fetch`, so the R2 CORS rule
6
+ * does not apply), and swapped in only when it loads within the timeout. Every failure keeps the default. No node:
7
+ * import, no library. The environment is injectable so tests can use a fake image and a seeded random source.
8
+ */
9
+ export declare const R2_IMAGE_ORIGIN = "https://img-gen.hagicode.com";
10
+ export declare const RANDOM_POOL_ATTRIBUTE = "data-random-pool";
11
+ export declare const RANDOM_POOL_MIN_ENTRIES = 12;
12
+ export declare const RANDOM_POOL_MAX_ENTRIES = 200;
13
+ export declare const RANDOM_IMAGE_TIMEOUT_MS = 3000;
14
+ export interface PreloadImage {
15
+ src: string;
16
+ onload: (() => void) | null;
17
+ onerror: (() => void) | null;
18
+ }
19
+ export interface RandomImageEnvironment {
20
+ /** Returns a value in [0, 1). Defaults to `Math.random`. */
21
+ readonly random?: () => number;
22
+ /** Defaults to `new Image()`. */
23
+ readonly createImage?: () => PreloadImage;
24
+ /** Defaults to the global `setTimeout` and `clearTimeout`. */
25
+ readonly setTimer?: (callback: () => void, ms: number) => unknown;
26
+ readonly clearTimer?: (handle: unknown) => void;
27
+ /** Defaults to the browser's data-saver preference. */
28
+ readonly saveData?: () => boolean;
29
+ }
30
+ /**
31
+ * Checks a pool: 12 to 200 unique `<album>/<key>.avif` object paths, all from one album. Throws a `TypeError`, so a
32
+ * bad pool fails the build instead of the page.
33
+ */
34
+ export declare function validateRandomPool(entries: unknown): readonly string[];
35
+ /** Picks one entry uniformly. Out-of-range or non-finite random values clamp to the first or last entry. */
36
+ export declare function pickRandomEntry<T>(entries: readonly T[], random?: () => number): T;
37
+ /** The full image URL of an R2 object path. */
38
+ export declare function poolImageUrl(objectPath: string): string;
39
+ /**
40
+ * Loads `url` in an image element. Resolves `true` only when it loads within the timeout; an error, a timeout or a
41
+ * missing `Image` resolves `false`. Never rejects and never retries.
42
+ */
43
+ export declare function preloadImage(url: string, env?: RandomImageEnvironment): Promise<boolean>;
44
+ /**
45
+ * Swaps a loaded URL into a `<picture>` or `<img>` slot. The `<source>` elements are removed so the browser uses the
46
+ * swapped `src`; the `<img>` keeps its width, height and alt text, so the box does not change.
47
+ */
48
+ export declare function showPoolImage(slot: Element, url: string): void;
49
+ /**
50
+ * Runs the picker for every `[data-random-pool]` slot under `root` that has not been handled yet. Each slot is marked
51
+ * with `data-random-state` so it is picked once per page view. Returns the number of slots that swapped.
52
+ * Never rejects: an invalid pool, a data saver, or a failed load leaves the default in place.
53
+ */
54
+ export declare function mountRandomImages(root: ParentNode, env?: RandomImageEnvironment): Promise<number>;
55
+ export interface RandomPoolManifest {
56
+ readonly album: string;
57
+ readonly maxBytes: number;
58
+ readonly entries: readonly {
59
+ readonly key: string;
60
+ readonly bytes: number;
61
+ }[];
62
+ }
63
+ /**
64
+ * Build-time: turns a manifest pool into validated object paths. An entry over `maxBytes` fails the build, so an
65
+ * oversized object never reaches a page.
66
+ */
67
+ export declare function randomPoolFromManifest(pool: RandomPoolManifest): readonly string[];
68
+ /** Build-time: the `data-random-pool` attribute value for a validated pool. */
69
+ export declare function randomPoolAttribute(entries: readonly string[]): string;