create-abcart-theme 0.0.0-stage → 0.10.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.
- package/CHANGELOG.md +127 -0
- package/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +64 -2
- package/bin/create-abcart-theme.mjs +4 -0
- package/package.json +47 -4
- package/src/args.mjs +113 -0
- package/src/banner.mjs +21 -0
- package/src/cli.mjs +119 -0
- package/src/create.mjs +82 -0
- package/src/package-manager.mjs +32 -0
- package/src/style.mjs +17 -0
- package/templates/default/.agents/skills/shopify-theme-asset-collector/SKILL.md +125 -0
- package/templates/default/.agents/skills/shopify-theme-asset-collector/scripts/collect_public_assets.py +370 -0
- package/templates/default/.agents/skills/wocart-docs/SKILL.md +146 -0
- package/templates/default/.agents/skills/wocart-theme-from-design/SKILL.md +58 -0
- package/templates/default/.agents/skills/wocart-theme-from-design/references/inputs-and-rights.md +47 -0
- package/templates/default/.agents/skills/wocart-theme-from-design/references/layout-rtl-and-phone.md +36 -0
- package/templates/default/.agents/skills/wocart-theme-from-design/references/measure-the-design.md +60 -0
- package/templates/default/.agents/skills/wocart-theme-from-design/references/tokens-sections-content.md +103 -0
- package/templates/default/.agents/skills/wocart-theme-from-design/references/verify-and-report.md +102 -0
- package/templates/default/.agents/skills/wocart-theme-from-design/scripts/compare-design.cjs +230 -0
- package/templates/default/.agents/skills/wocart-theme-from-design/scripts/qa-pages.cjs +219 -0
- package/templates/default/.dev.vars.example +12 -0
- package/templates/default/AGENTS.md +240 -0
- package/templates/default/README.md +184 -0
- package/templates/default/_gitignore +11 -0
- package/templates/default/astro.config.mjs +85 -0
- package/templates/default/package.json +36 -0
- package/templates/default/public/fonts/OFL.txt +96 -0
- package/templates/default/public/fonts/alexandria-arabic.woff2 +0 -0
- package/templates/default/public/fonts/fraunces-latin.woff2 +0 -0
- package/templates/default/src/actions/index.ts +22 -0
- package/templates/default/src/components/cart/AddToCartButton.astro +45 -0
- package/templates/default/src/components/cart/CartDrawer.astro +47 -0
- package/templates/default/src/components/cart/CartSummary.astro +11 -0
- package/templates/default/src/components/cart/FreeShipping.astro +7 -0
- package/templates/default/src/components/checkout/CheckoutMain.astro +29 -0
- package/templates/default/src/components/checkout/OrderForm.astro +280 -0
- package/templates/default/src/components/checkout/ThankYouMain.astro +68 -0
- package/templates/default/src/components/landing/LandingImages.astro +51 -0
- package/templates/default/src/components/landing/LandingOrder.astro +46 -0
- package/templates/default/src/components/landing/LandingProduct.astro +41 -0
- package/templates/default/src/components/listing/ListingMain.astro +93 -0
- package/templates/default/src/components/listing/PageMain.astro +52 -0
- package/templates/default/src/components/listing/Pagination.astro +31 -0
- package/templates/default/src/components/listing/SortControl.astro +35 -0
- package/templates/default/src/components/product/BuyBox.astro +96 -0
- package/templates/default/src/components/product/Description.astro +24 -0
- package/templates/default/src/components/product/Gallery.astro +66 -0
- package/templates/default/src/components/product/MobileBuyBar.astro +29 -0
- package/templates/default/src/components/product/OfferChoice.astro +66 -0
- package/templates/default/src/components/product/ProductMain.astro +40 -0
- package/templates/default/src/components/product/ProductReviews.astro +72 -0
- package/templates/default/src/components/product/RelatedProducts.astro +30 -0
- package/templates/default/src/components/product/blocks/BenefitsBlock.astro +13 -0
- package/templates/default/src/components/product/blocks/CollapsibleBlock.astro +20 -0
- package/templates/default/src/components/product/blocks/DescriptionBlock.astro +19 -0
- package/templates/default/src/components/product/blocks/DetailsBlock.astro +33 -0
- package/templates/default/src/components/product/blocks/GalleryBlock.astro +12 -0
- package/templates/default/src/components/product/blocks/OrderBlock.astro +30 -0
- package/templates/default/src/components/product/blocks/PriceBlock.astro +23 -0
- package/templates/default/src/components/product/blocks/RatingBlock.astro +16 -0
- package/templates/default/src/components/product/blocks/StockBlock.astro +22 -0
- package/templates/default/src/components/product/blocks/TextBlock.astro +11 -0
- package/templates/default/src/components/product/blocks/TitleBlock.astro +20 -0
- package/templates/default/src/components/reviews/ReviewCard.astro +46 -0
- package/templates/default/src/components/reviews/ReviewForm.astro +113 -0
- package/templates/default/src/components/reviews/VoiceNote.astro +60 -0
- package/templates/default/src/components/sections/CollectionCards.astro +65 -0
- package/templates/default/src/components/sections/Countdown.astro +57 -0
- package/templates/default/src/components/sections/Faq.astro +44 -0
- package/templates/default/src/components/sections/FeaturedProduct.astro +71 -0
- package/templates/default/src/components/sections/Hero.astro +99 -0
- package/templates/default/src/components/sections/ImageBanner.astro +46 -0
- package/templates/default/src/components/sections/ProductGrid.astro +47 -0
- package/templates/default/src/components/sections/RichText.astro +35 -0
- package/templates/default/src/components/sections/Steps.astro +46 -0
- package/templates/default/src/components/sections/Testimonials.astro +57 -0
- package/templates/default/src/components/shell/Footer.astro +80 -0
- package/templates/default/src/components/shell/Header.astro +89 -0
- package/templates/default/src/components/shell/LanguageMenu.astro +33 -0
- package/templates/default/src/components/shell/MobileMenu.astro +65 -0
- package/templates/default/src/components/shell/SearchDialog.astro +19 -0
- package/templates/default/src/components/shell/TopBar.astro +54 -0
- package/templates/default/src/components/shell/WhatsAppWidget.astro +111 -0
- package/templates/default/src/components/store/ContactMain.astro +79 -0
- package/templates/default/src/components/store/SearchMain.astro +56 -0
- package/templates/default/src/components/store/TrackOrderMain.astro +161 -0
- package/templates/default/src/components/surface/Area.astro +20 -0
- package/templates/default/src/components/surface/PreviewBridge.astro +24 -0
- package/templates/default/src/components/surface/Sections.astro +35 -0
- package/templates/default/src/components/ui/Breadcrumbs.astro +36 -0
- package/templates/default/src/components/ui/Button.astro +45 -0
- package/templates/default/src/components/ui/EditorPlaceholder.astro +24 -0
- package/templates/default/src/components/ui/Icon.astro +93 -0
- package/templates/default/src/components/ui/Logo.astro +36 -0
- package/templates/default/src/components/ui/Photo.astro +46 -0
- package/templates/default/src/components/ui/Price.astro +24 -0
- package/templates/default/src/components/ui/ProductCard.astro +41 -0
- package/templates/default/src/components/ui/Rating.astro +22 -0
- package/templates/default/src/components/ui/Section.astro +34 -0
- package/templates/default/src/components/ui/SectionHeading.astro +19 -0
- package/templates/default/src/env.d.ts +23 -0
- package/templates/default/src/layouts/StoreLayout.astro +145 -0
- package/templates/default/src/lib/browser-copy.ts +37 -0
- package/templates/default/src/lib/context.ts +89 -0
- package/templates/default/src/lib/coupon-notice.ts +47 -0
- package/templates/default/src/lib/i18n.ts +32 -0
- package/templates/default/src/lib/images.ts +38 -0
- package/templates/default/src/lib/listing-query.ts +41 -0
- package/templates/default/src/lib/money.ts +16 -0
- package/templates/default/src/lib/motion.ts +16 -0
- package/templates/default/src/lib/nav.ts +26 -0
- package/templates/default/src/lib/offer-tiles.ts +105 -0
- package/templates/default/src/lib/order-outcome.ts +66 -0
- package/templates/default/src/lib/otp-message.ts +21 -0
- package/templates/default/src/lib/products.ts +38 -0
- package/templates/default/src/lib/review-refusals.ts +31 -0
- package/templates/default/src/lib/store.ts +31 -0
- package/templates/default/src/lib/whatsapp.ts +29 -0
- package/templates/default/src/middleware.ts +23 -0
- package/templates/default/src/pages/404.astro +22 -0
- package/templates/default/src/pages/api/abandoned/convert.ts +4 -0
- package/templates/default/src/pages/api/abandoned/upsert.ts +4 -0
- package/templates/default/src/pages/api/cart/validate.ts +4 -0
- package/templates/default/src/pages/api/communes/[wilayaId].ts +4 -0
- package/templates/default/src/pages/api/otp/send.ts +4 -0
- package/templates/default/src/pages/api/otp/verify.ts +4 -0
- package/templates/default/src/pages/api/reviews/[productId].ts +3 -0
- package/templates/default/src/pages/api/reviews/eligibility.ts +3 -0
- package/templates/default/src/pages/api/reviews/submit.ts +3 -0
- package/templates/default/src/pages/api/reviews/upload.ts +3 -0
- package/templates/default/src/pages/category/[slug].astro +20 -0
- package/templates/default/src/pages/checkout.astro +25 -0
- package/templates/default/src/pages/collections/[handle].astro +20 -0
- package/templates/default/src/pages/contact.astro +17 -0
- package/templates/default/src/pages/index.astro +9 -0
- package/templates/default/src/pages/lp/[slug].astro +47 -0
- package/templates/default/src/pages/pages/[slug].astro +16 -0
- package/templates/default/src/pages/products/[handle].astro +37 -0
- package/templates/default/src/pages/products/index.astro +15 -0
- package/templates/default/src/pages/robots.txt.ts +9 -0
- package/templates/default/src/pages/search.astro +19 -0
- package/templates/default/src/pages/sitemap.xml.ts +12 -0
- package/templates/default/src/pages/thank-you.astro +33 -0
- package/templates/default/src/pages/track-order.astro +17 -0
- package/templates/default/src/pages/wocart-preview.astro +43 -0
- package/templates/default/src/pages/wocart-revalidate.ts +10 -0
- package/templates/default/src/pages/wocart-styles.css.ts +5 -0
- package/templates/default/src/pages/wocart-theme.json.ts +5 -0
- package/templates/default/src/scripts/cart.ts +371 -0
- package/templates/default/src/scripts/checkout/coupon.ts +96 -0
- package/templates/default/src/scripts/checkout/delivery.ts +86 -0
- package/templates/default/src/scripts/checkout/form.ts +54 -0
- package/templates/default/src/scripts/checkout/otp.ts +106 -0
- package/templates/default/src/scripts/checkout/picker.ts +169 -0
- package/templates/default/src/scripts/checkout/quote.ts +169 -0
- package/templates/default/src/scripts/checkout/submit.ts +113 -0
- package/templates/default/src/scripts/checkout/tracking.ts +74 -0
- package/templates/default/src/scripts/countdown.ts +29 -0
- package/templates/default/src/scripts/events.ts +4 -0
- package/templates/default/src/scripts/hero.ts +49 -0
- package/templates/default/src/scripts/listing.ts +13 -0
- package/templates/default/src/scripts/order-form.ts +100 -0
- package/templates/default/src/scripts/product.ts +306 -0
- package/templates/default/src/scripts/public-config.ts +79 -0
- package/templates/default/src/scripts/reviews.ts +358 -0
- package/templates/default/src/scripts/site.ts +131 -0
- package/templates/default/src/scripts/thank-you.ts +11 -0
- package/templates/default/src/scripts/voice-note.ts +94 -0
- package/templates/default/src/styles/base.css +1030 -0
- package/templates/default/src/styles/checkout.css +1111 -0
- package/templates/default/src/styles/global.css +12 -0
- package/templates/default/src/styles/product.css +871 -0
- package/templates/default/src/styles/reviews.css +533 -0
- package/templates/default/src/styles/sections.css +931 -0
- package/templates/default/src/styles/shell.css +970 -0
- package/templates/default/src/styles/tokens.css +583 -0
- package/templates/default/src/theme/api-routes.ts +10 -0
- package/templates/default/src/theme/content/catalog.ts +117 -0
- package/templates/default/src/theme/content/checkout.ts +168 -0
- package/templates/default/src/theme/content/editor.ts +21 -0
- package/templates/default/src/theme/content/index.ts +45 -0
- package/templates/default/src/theme/content/reviews.ts +76 -0
- package/templates/default/src/theme/content/shell.ts +59 -0
- package/templates/default/src/theme/content/types.ts +12 -0
- package/templates/default/src/theme/documents/catalog.json +11 -0
- package/templates/default/src/theme/documents/category.json +11 -0
- package/templates/default/src/theme/documents/checkout.json +11 -0
- package/templates/default/src/theme/documents/collection.json +11 -0
- package/templates/default/src/theme/documents/contact.json +11 -0
- package/templates/default/src/theme/documents/footer.area.json +11 -0
- package/templates/default/src/theme/documents/header.area.json +16 -0
- package/templates/default/src/theme/documents/home.json +313 -0
- package/templates/default/src/theme/documents/landing.json +8 -0
- package/templates/default/src/theme/documents/page.json +11 -0
- package/templates/default/src/theme/documents/product.json +125 -0
- package/templates/default/src/theme/documents/search.json +11 -0
- package/templates/default/src/theme/documents/thank-you.json +11 -0
- package/templates/default/src/theme/documents/track-order.json +11 -0
- package/templates/default/src/theme/fields.ts +61 -0
- package/templates/default/src/theme/key.ts +11 -0
- package/templates/default/src/theme/labels.ts +231 -0
- package/templates/default/src/theme/locales.ts +11 -0
- package/templates/default/src/theme/palette.ts +14 -0
- package/templates/default/src/theme/sections/collection-cards.ts +65 -0
- package/templates/default/src/theme/sections/faq.ts +74 -0
- package/templates/default/src/theme/sections/featured-product.ts +42 -0
- package/templates/default/src/theme/sections/hero.ts +47 -0
- package/templates/default/src/theme/sections/image-banner.ts +25 -0
- package/templates/default/src/theme/sections/index.ts +40 -0
- package/templates/default/src/theme/sections/landing.ts +150 -0
- package/templates/default/src/theme/sections/listing.ts +84 -0
- package/templates/default/src/theme/sections/product-grid.ts +42 -0
- package/templates/default/src/theme/sections/product.ts +297 -0
- package/templates/default/src/theme/sections/rich-text.ts +39 -0
- package/templates/default/src/theme/sections/shared.ts +54 -0
- package/templates/default/src/theme/sections/shell.ts +85 -0
- package/templates/default/src/theme/sections/store-pages.ts +101 -0
- package/templates/default/src/theme/sections/testimonials.ts +61 -0
- package/templates/default/src/theme/sections/thank-you.ts +35 -0
- package/templates/default/src/theme/settings.ts +124 -0
- package/templates/default/src/theme/theme.ts +65 -0
- package/templates/default/tests/fixtures/canvas-tote.json +62 -0
- package/templates/default/tests/fixtures/linen-shirt.json +128 -0
- package/templates/default/tests/unit/checkout.test.ts +241 -0
- package/templates/default/tests/unit/contract.test.ts +169 -0
- package/templates/default/tests/unit/contrast.test.ts +68 -0
- package/templates/default/tests/unit/copy.test.ts +101 -0
- package/templates/default/tests/unit/design-values.test.ts +95 -0
- package/templates/default/tests/unit/editability.test.ts +180 -0
- package/templates/default/tests/unit/fixtures.ts +91 -0
- package/templates/default/tests/unit/footer.test.ts +54 -0
- package/templates/default/tests/unit/guards.test.ts +340 -0
- package/templates/default/tests/unit/images.test.ts +26 -0
- package/templates/default/tests/unit/landing.test.ts +151 -0
- package/templates/default/tests/unit/languages.test.ts +42 -0
- package/templates/default/tests/unit/middleware.test.ts +53 -0
- package/templates/default/tests/unit/offer-tiles.test.ts +75 -0
- package/templates/default/tests/unit/render.ts +38 -0
- package/templates/default/tests/unit/reviews.test.ts +25 -0
- package/templates/default/tests/unit/rules.test.ts +59 -0
- package/templates/default/tests/unit/source.ts +75 -0
- package/templates/default/tests/unit/store-pages.test.ts +147 -0
- package/templates/default/tests/unit/studio.test.ts +202 -0
- package/templates/default/tests/unit/testimonials.test.ts +52 -0
- package/templates/default/tests/unit/thank-you.test.ts +52 -0
- package/templates/default/tests/unit/tracking.test.ts +29 -0
- package/templates/default/tests/unit/voice-note.test.ts +42 -0
- package/templates/default/tsconfig.json +13 -0
- package/templates/default/vitest.config.ts +15 -0
- package/templates/default/wrangler.jsonc +17 -0
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reviews: play a voice note, open a photo, show the next page, write one.
|
|
3
|
+
* Writing proves the purchase first, then sends each photo (shrunk in the
|
|
4
|
+
* browser) and the voice note the moment it is added, with its progress and a
|
|
5
|
+
* retry, so sending the review only names what already arrived. Every rule is
|
|
6
|
+
* the server's; this script shows its answers in the page's words.
|
|
7
|
+
*/
|
|
8
|
+
import { REVIEW_TEXT_LIMITS, STOREFRONT_API_PATHS, storefrontApiPath, type Review, type ReviewEligibility } from "@abcart/storefront";
|
|
9
|
+
import { canRecordVoice, createReviewSession, createVoiceRecorder, type VoiceRecorder } from "@abcart/storefront/review-media";
|
|
10
|
+
import { formatMessage } from "@abcart/theme/messages";
|
|
11
|
+
import { bindEach } from "@abcart/theme/lifecycle";
|
|
12
|
+
import { reviewRefusalWords, type ReviewStep } from "@/lib/review-refusals";
|
|
13
|
+
import { readPublicConfig } from "./public-config";
|
|
14
|
+
import { bindVoice, clock } from "./voice-note";
|
|
15
|
+
|
|
16
|
+
const config = readPublicConfig();
|
|
17
|
+
const PAGE = 6;
|
|
18
|
+
|
|
19
|
+
// ── Photos and the next page ──────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
bindEach<HTMLElement>("#reviews", (section) => {
|
|
22
|
+
const dialog = section.querySelector<HTMLDialogElement>("[data-photo-dialog]");
|
|
23
|
+
const full = dialog?.querySelector<HTMLImageElement>("[data-photo-full]");
|
|
24
|
+
section.addEventListener("click", (event) => {
|
|
25
|
+
const photo = (event.target as HTMLElement).closest<HTMLElement>("[data-review-photo]");
|
|
26
|
+
if (!photo || !dialog || !full) return;
|
|
27
|
+
full.src = photo.dataset.reviewPhoto ?? "";
|
|
28
|
+
dialog.showModal();
|
|
29
|
+
});
|
|
30
|
+
dialog?.addEventListener("click", (event) => {
|
|
31
|
+
if (event.target === dialog || (event.target as HTMLElement).closest("[data-photo-close]")) dialog.close();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const list = section.querySelector<HTMLElement>("[data-reviews-list]");
|
|
35
|
+
const template = section.querySelector<HTMLTemplateElement>("[data-review-template]");
|
|
36
|
+
const more = section.querySelector<HTMLButtonElement>("[data-reviews-more]");
|
|
37
|
+
if (!list || !template || !more) return;
|
|
38
|
+
more.addEventListener("click", async () => {
|
|
39
|
+
more.disabled = true;
|
|
40
|
+
try {
|
|
41
|
+
const loaded = Number(list.dataset.loaded) || 0;
|
|
42
|
+
const response = await fetch(`${storefrontApiPath("reviewsList", { productId: list.dataset.productId ?? "" })}?limit=${PAGE}&offset=${loaded}`);
|
|
43
|
+
const answer = (await response.json()) as { data?: Review[]; total?: number };
|
|
44
|
+
for (const review of answer.data ?? []) list.append(card(template, review));
|
|
45
|
+
list.dataset.loaded = String(loaded + (answer.data?.length ?? 0));
|
|
46
|
+
if (Number(list.dataset.loaded) >= (answer.total ?? 0) || (answer.data?.length ?? 0) === 0) more.parentElement?.remove();
|
|
47
|
+
} finally {
|
|
48
|
+
more.disabled = false;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
function card(template: HTMLTemplateElement, review: Review): HTMLElement {
|
|
54
|
+
const node = template.content.firstElementChild!.cloneNode(true) as HTMLElement;
|
|
55
|
+
const set = (selector: string, text: string | null | undefined) => {
|
|
56
|
+
const element = node.querySelector<HTMLElement>(selector);
|
|
57
|
+
if (!element) return;
|
|
58
|
+
element.textContent = text ?? "";
|
|
59
|
+
element.hidden = !text;
|
|
60
|
+
};
|
|
61
|
+
set("[data-review-name]", review.customerName);
|
|
62
|
+
set("[data-review-initial]", review.customerName.slice(0, 1));
|
|
63
|
+
set("[data-review-title]", review.title);
|
|
64
|
+
set("[data-review-body]", review.body);
|
|
65
|
+
const date = node.querySelector<HTMLTimeElement>("[data-review-date]");
|
|
66
|
+
if (date) {
|
|
67
|
+
date.dateTime = review.createdAt;
|
|
68
|
+
date.textContent = new Intl.DateTimeFormat(config.localeTag, { day: "numeric", month: "long", year: "numeric" }).format(new Date(review.createdAt));
|
|
69
|
+
}
|
|
70
|
+
node.querySelector<HTMLElement>("[data-review-verified]")!.hidden = !review.verified;
|
|
71
|
+
const rating = Math.round(review.rating);
|
|
72
|
+
const stars = node.querySelector<HTMLElement>("[data-review-stars]");
|
|
73
|
+
stars?.querySelectorAll("svg").forEach((star, index) => star.classList.toggle("star-empty", index >= rating));
|
|
74
|
+
stars?.setAttribute("aria-label", formatMessage(config.content, "a11y.rating", { rating: String(rating) }, { locale: config.localeTag }));
|
|
75
|
+
const photos = node.querySelector<HTMLElement>("[data-review-photos]");
|
|
76
|
+
const images = review.media.filter((media) => media.kind === "image");
|
|
77
|
+
if (photos) {
|
|
78
|
+
photos.hidden = images.length === 0;
|
|
79
|
+
for (const image of images) {
|
|
80
|
+
const button = document.createElement("button");
|
|
81
|
+
button.type = "button";
|
|
82
|
+
button.className = "review-card__photo";
|
|
83
|
+
button.dataset.reviewPhoto = image.url;
|
|
84
|
+
button.setAttribute("aria-label", config.content["reviews.openPhoto"] ?? "");
|
|
85
|
+
const img = document.createElement("img");
|
|
86
|
+
img.src = image.thumbUrl ?? image.url;
|
|
87
|
+
img.alt = "";
|
|
88
|
+
img.width = 76;
|
|
89
|
+
img.height = 76;
|
|
90
|
+
img.loading = "lazy";
|
|
91
|
+
button.append(img);
|
|
92
|
+
photos.append(button);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const voice = review.media.find((media) => media.kind === "audio");
|
|
96
|
+
const voiceBox = node.querySelector<HTMLElement>("[data-review-voice]");
|
|
97
|
+
if (voiceBox) {
|
|
98
|
+
voiceBox.hidden = !voice;
|
|
99
|
+
if (voice) {
|
|
100
|
+
const player = voiceBox.querySelector<HTMLElement>("[data-voice-note]")!;
|
|
101
|
+
player.querySelector<HTMLAudioElement>("[data-voice-audio]")!.src = voice.url;
|
|
102
|
+
player.dataset.durationMs = String(voice.durationMs ?? 0);
|
|
103
|
+
const time = player.querySelector<HTMLElement>("[data-voice-time]")!;
|
|
104
|
+
time.dataset.length = voice.durationMs ? clock(voice.durationMs / 1000) : "";
|
|
105
|
+
time.textContent = time.dataset.length;
|
|
106
|
+
bindVoice(player);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return node;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ── Writing a review ──────────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
type Copy = Record<string, string>;
|
|
115
|
+
interface Upload {
|
|
116
|
+
kind: "image" | "audio";
|
|
117
|
+
node: HTMLLIElement;
|
|
118
|
+
file: Blob;
|
|
119
|
+
durationMs?: number;
|
|
120
|
+
uploadId: string | null;
|
|
121
|
+
state: "sending" | "sent" | "failed";
|
|
122
|
+
cancel?: () => void;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
bindEach<HTMLDialogElement>("[data-review-dialog]", (dialog) => {
|
|
126
|
+
const root = dialog.querySelector<HTMLElement>("[data-review-form]")!;
|
|
127
|
+
const copy: Copy = JSON.parse(root.dataset.copy ?? "{}");
|
|
128
|
+
const say = (key: string, values?: Record<string, string | number>) => formatMessage(copy, key, values, { locale: config.localeTag });
|
|
129
|
+
const $ = <T extends Element>(selector: string) => root.querySelector<T>(selector)!;
|
|
130
|
+
const find = $<HTMLFormElement>("[data-review-find]");
|
|
131
|
+
const write = $<HTMLFormElement>("[data-review-write]");
|
|
132
|
+
const error = $<HTMLElement>("[data-review-error]");
|
|
133
|
+
const success = $<HTMLElement>("[data-review-success]");
|
|
134
|
+
const fileTemplate = $<HTMLTemplateElement>("[data-review-file-template]");
|
|
135
|
+
const recordButton = $<HTMLButtonElement>("[data-review-record]");
|
|
136
|
+
const stopButton = $<HTMLButtonElement>("[data-review-stop]");
|
|
137
|
+
const timer = $<HTMLElement>("[data-review-timer]");
|
|
138
|
+
const stars = Array.from(root.querySelectorAll<HTMLButtonElement>("[data-star]"));
|
|
139
|
+
|
|
140
|
+
const session = createReviewSession({ routes: { eligibility: STOREFRONT_API_PATHS.reviewEligibility, upload: STOREFRONT_API_PATHS.reviewUpload, submit: STOREFRONT_API_PATHS.reviewSubmit } });
|
|
141
|
+
let rating = 0;
|
|
142
|
+
let files: Upload[] = [];
|
|
143
|
+
let recorder: VoiceRecorder | null = null;
|
|
144
|
+
|
|
145
|
+
const showError = (message: string) => {
|
|
146
|
+
error.textContent = message;
|
|
147
|
+
error.hidden = false;
|
|
148
|
+
error.scrollIntoView({ block: "nearest" });
|
|
149
|
+
};
|
|
150
|
+
const clearError = () => {
|
|
151
|
+
error.hidden = true;
|
|
152
|
+
error.textContent = "";
|
|
153
|
+
};
|
|
154
|
+
const refusal = (code: string | null, step: ReviewStep) => say(reviewRefusalWords(code, step), { count: REVIEW_TEXT_LIMITS.bodyMin });
|
|
155
|
+
const busy = (button: HTMLButtonElement, on: boolean) => {
|
|
156
|
+
button.disabled = on;
|
|
157
|
+
button.toggleAttribute("aria-busy", on);
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
document.querySelectorAll<HTMLElement>("[data-review-open]").forEach((button) => button.addEventListener("click", () => dialog.showModal()));
|
|
161
|
+
dialog.querySelectorAll<HTMLElement>("[data-review-close]").forEach((button) => button.addEventListener("click", () => dialog.close()));
|
|
162
|
+
dialog.addEventListener("click", (event) => event.target === dialog && dialog.close());
|
|
163
|
+
dialog.addEventListener("close", () => recorder?.cancel());
|
|
164
|
+
|
|
165
|
+
find.addEventListener("submit", async (event) => {
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
clearError();
|
|
168
|
+
const data = new FormData(find);
|
|
169
|
+
const orderNumber = String(data.get("orderNumber") ?? "").trim();
|
|
170
|
+
const phone = String(data.get("phone") ?? "").trim();
|
|
171
|
+
if (!orderNumber || !phone) return showError(say("reviews.errorOrder"));
|
|
172
|
+
const button = $<HTMLButtonElement>("[data-review-find-button]");
|
|
173
|
+
busy(button, true);
|
|
174
|
+
const found = await session.find({ orderNumber, phone });
|
|
175
|
+
busy(button, false);
|
|
176
|
+
if (found.status === "refused") return showError(refusal(found.code, "find"));
|
|
177
|
+
if (!found.eligibility.products.some((product) => !product.reviewed)) return showError(say("reviews.nothingLeft"));
|
|
178
|
+
openWriting(found.eligibility);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
function openWriting(eligibility: ReviewEligibility) {
|
|
182
|
+
$<HTMLElement>("[data-review-order]").textContent = say("reviews.orderFound", { order: eligibility.orderNumber });
|
|
183
|
+
const list = $<HTMLElement>("[data-review-product-list]");
|
|
184
|
+
const template = $<HTMLTemplateElement>("[data-review-product-template]");
|
|
185
|
+
list.replaceChildren();
|
|
186
|
+
const preferred =
|
|
187
|
+
eligibility.products.find((product) => product.productId === root.dataset.productId && !product.reviewed) ??
|
|
188
|
+
eligibility.products.find((product) => !product.reviewed);
|
|
189
|
+
for (const product of eligibility.products) {
|
|
190
|
+
const option = template.content.firstElementChild!.cloneNode(true) as HTMLLabelElement;
|
|
191
|
+
const input = option.querySelector("input")!;
|
|
192
|
+
input.value = product.productId;
|
|
193
|
+
input.disabled = product.reviewed;
|
|
194
|
+
input.checked = product.productId === preferred?.productId;
|
|
195
|
+
option.querySelector<HTMLElement>("[data-name]")!.textContent = product.name;
|
|
196
|
+
option.querySelector<HTMLElement>("[data-done]")!.hidden = !product.reviewed;
|
|
197
|
+
list.append(option);
|
|
198
|
+
}
|
|
199
|
+
$<HTMLElement>("[data-review-products]").hidden = eligibility.products.length < 2;
|
|
200
|
+
const media = eligibility.media;
|
|
201
|
+
$<HTMLElement>("[data-review-photos-block]").hidden = !media || media.maxPhotos === 0;
|
|
202
|
+
$<HTMLElement>("[data-review-voice-block]").hidden = !media || media.voiceMaxSeconds === null;
|
|
203
|
+
if (media?.maxPhotos) $<HTMLElement>("[data-review-photos-limit]").textContent = say("reviews.photosLimit", { count: media.maxPhotos });
|
|
204
|
+
if (media?.voiceMaxSeconds) {
|
|
205
|
+
$<HTMLElement>("[data-review-voice-limit]").textContent = say("reviews.voiceLimit", { count: media.voiceMaxSeconds });
|
|
206
|
+
recordButton.hidden = !canRecordVoice();
|
|
207
|
+
}
|
|
208
|
+
find.hidden = true;
|
|
209
|
+
write.hidden = false;
|
|
210
|
+
stars[0]?.focus();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
$<HTMLButtonElement>("[data-review-change]").addEventListener("click", () => {
|
|
214
|
+
recorder?.cancel();
|
|
215
|
+
session.reset();
|
|
216
|
+
files = [];
|
|
217
|
+
$<HTMLElement>("[data-review-photo-list]").replaceChildren();
|
|
218
|
+
$<HTMLElement>("[data-review-voice-list]").replaceChildren();
|
|
219
|
+
write.hidden = true;
|
|
220
|
+
find.hidden = false;
|
|
221
|
+
clearError();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const paintStars = (value: number) => stars.forEach((star) => star.classList.toggle("is-on", Number(star.dataset.star) <= value));
|
|
225
|
+
stars.forEach((star) => {
|
|
226
|
+
star.addEventListener("click", () => {
|
|
227
|
+
rating = Number(star.dataset.star);
|
|
228
|
+
stars.forEach((other) => other.setAttribute("aria-checked", String(other === star)));
|
|
229
|
+
paintStars(rating);
|
|
230
|
+
});
|
|
231
|
+
star.addEventListener("mouseenter", () => paintStars(Number(star.dataset.star)));
|
|
232
|
+
star.addEventListener("mouseleave", () => paintStars(rating));
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const sentVoice = () => files.some((item) => item.kind === "audio" && item.state === "sent");
|
|
236
|
+
const bodyHint = () => ($<HTMLElement>("[data-review-body-hint]").hidden = !files.some((item) => item.kind === "audio"));
|
|
237
|
+
|
|
238
|
+
function show(item: Upload, state: Upload["state"], progress = 0, message?: string) {
|
|
239
|
+
item.state = state;
|
|
240
|
+
item.node.dataset.state = state;
|
|
241
|
+
item.node.querySelector<HTMLElement>("[data-status]")!.textContent = state === "failed" ? message ?? say("reviews.notSent") : state === "sent" ? "" : say("reviews.sending");
|
|
242
|
+
item.node.querySelector<HTMLElement>("[data-progress]")!.style.inlineSize = `${Math.round((state === "sent" ? 1 : progress) * 100)}%`;
|
|
243
|
+
item.node.querySelector<HTMLElement>("[data-retry]")!.hidden = state !== "failed";
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function send(item: Upload) {
|
|
247
|
+
show(item, "sending");
|
|
248
|
+
const upload = session.upload(item.file, { kind: item.kind, ...(item.durationMs ? { durationMs: item.durationMs } : {}), onProgress: (share) => show(item, "sending", share) });
|
|
249
|
+
item.cancel = upload.cancel;
|
|
250
|
+
void upload.result.then((result) => {
|
|
251
|
+
if (result.status === "cancelled" || !files.includes(item)) return;
|
|
252
|
+
if (result.status === "sent") {
|
|
253
|
+
item.uploadId = result.uploadId;
|
|
254
|
+
show(item, "sent");
|
|
255
|
+
} else show(item, "failed", 0, refusal(result.code, "upload"));
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function add(kind: Upload["kind"], file: Blob, durationMs?: number) {
|
|
260
|
+
const node = fileTemplate.content.firstElementChild!.cloneNode(true) as HTMLLIElement;
|
|
261
|
+
const item: Upload = { kind, node, file, ...(durationMs ? { durationMs } : {}), uploadId: null, state: "sending" };
|
|
262
|
+
const preview = URL.createObjectURL(file);
|
|
263
|
+
const media = node.querySelector<HTMLImageElement | HTMLAudioElement>(kind === "image" ? "img" : "audio")!;
|
|
264
|
+
media.src = preview;
|
|
265
|
+
media.hidden = false;
|
|
266
|
+
node.querySelector<HTMLButtonElement>("[data-remove]")!.addEventListener("click", () => {
|
|
267
|
+
item.cancel?.();
|
|
268
|
+
URL.revokeObjectURL(preview);
|
|
269
|
+
node.remove();
|
|
270
|
+
files = files.filter((other) => other !== item);
|
|
271
|
+
bodyHint();
|
|
272
|
+
});
|
|
273
|
+
node.querySelector<HTMLButtonElement>("[data-retry]")!.addEventListener("click", () => send(item));
|
|
274
|
+
files.push(item);
|
|
275
|
+
$<HTMLElement>(kind === "image" ? "[data-review-photo-list]" : "[data-review-voice-list]").append(node);
|
|
276
|
+
bodyHint();
|
|
277
|
+
send(item);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
$<HTMLInputElement>("[data-review-photo-file]").addEventListener("change", (event) => {
|
|
281
|
+
const input = event.target as HTMLInputElement;
|
|
282
|
+
const max = session.eligibility?.media?.maxPhotos ?? 0;
|
|
283
|
+
const room = max - files.filter((item) => item.kind === "image").length;
|
|
284
|
+
const picked = Array.from(input.files ?? []).filter((file) => file.type.startsWith("image/"));
|
|
285
|
+
if (picked.length > room) showError(say("reviews.tooManyPhotos", { count: max }));
|
|
286
|
+
for (const file of picked.slice(0, Math.max(room, 0))) add("image", file);
|
|
287
|
+
input.value = "";
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
const replaceVoice = (blob: Blob, durationMs?: number) => {
|
|
291
|
+
for (const item of files.filter((other) => other.kind === "audio")) item.node.querySelector<HTMLButtonElement>("[data-remove]")!.click();
|
|
292
|
+
add("audio", blob, durationMs);
|
|
293
|
+
};
|
|
294
|
+
$<HTMLInputElement>("[data-review-voice-file]").addEventListener("change", (event) => {
|
|
295
|
+
const input = event.target as HTMLInputElement;
|
|
296
|
+
const file = input.files?.[0];
|
|
297
|
+
if (file) replaceVoice(file);
|
|
298
|
+
input.value = "";
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
const finishRecording = (blob: Blob, durationMs: number) => {
|
|
302
|
+
recorder = null;
|
|
303
|
+
stopButton.hidden = true;
|
|
304
|
+
recordButton.hidden = false;
|
|
305
|
+
replaceVoice(blob, durationMs);
|
|
306
|
+
};
|
|
307
|
+
recordButton.addEventListener("click", async () => {
|
|
308
|
+
const max = session.eligibility?.media?.voiceMaxSeconds;
|
|
309
|
+
if (!max) return;
|
|
310
|
+
recorder = createVoiceRecorder({
|
|
311
|
+
maxSeconds: max,
|
|
312
|
+
onTick: (elapsed) => (timer.textContent = clock(elapsed / 1000)),
|
|
313
|
+
onLimit: (voice) => finishRecording(voice.blob, voice.durationMs),
|
|
314
|
+
});
|
|
315
|
+
try {
|
|
316
|
+
await recorder.start();
|
|
317
|
+
recordButton.hidden = true;
|
|
318
|
+
stopButton.hidden = false;
|
|
319
|
+
timer.textContent = "0:00";
|
|
320
|
+
} catch {
|
|
321
|
+
recorder = null;
|
|
322
|
+
showError(say("reviews.micDenied"));
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
stopButton.addEventListener("click", async () => {
|
|
326
|
+
if (!recorder || recorder.state !== "recording") return;
|
|
327
|
+
const voice = await recorder.stop();
|
|
328
|
+
finishRecording(voice.blob, voice.durationMs);
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
write.addEventListener("submit", async (event) => {
|
|
332
|
+
event.preventDefault();
|
|
333
|
+
clearError();
|
|
334
|
+
const opened = session.eligibility;
|
|
335
|
+
if (!opened) return;
|
|
336
|
+
const productId = new FormData(write).get("productId")?.toString() ?? opened.products.find((product) => !product.reviewed)?.productId;
|
|
337
|
+
if (!productId) return showError(say("reviews.nothingLeft"));
|
|
338
|
+
if (!rating) return showError(say("reviews.ratingRequired"));
|
|
339
|
+
const body = $<HTMLTextAreaElement>("#review-body").value.trim();
|
|
340
|
+
if (body.length < REVIEW_TEXT_LIMITS.bodyMin && !sentVoice()) return showError(say("reviews.tooShort", { count: REVIEW_TEXT_LIMITS.bodyMin }));
|
|
341
|
+
if (files.some((item) => item.state !== "sent")) return showError(say("reviews.waitUploads"));
|
|
342
|
+
const button = $<HTMLButtonElement>("[data-review-submit]");
|
|
343
|
+
busy(button, true);
|
|
344
|
+
const title = $<HTMLInputElement>("#review-title").value.trim();
|
|
345
|
+
const submitted = await session.submit({
|
|
346
|
+
productId,
|
|
347
|
+
rating,
|
|
348
|
+
body,
|
|
349
|
+
...(title ? { title } : {}),
|
|
350
|
+
uploads: files.flatMap((item) => (item.uploadId ? [item.uploadId] : [])),
|
|
351
|
+
});
|
|
352
|
+
busy(button, false);
|
|
353
|
+
if (submitted.status === "refused") return showError(refusal(submitted.code, "submit"));
|
|
354
|
+
$<HTMLElement>("[data-review-success-text]").textContent = say(submitted.result.visibility === "published" ? "reviews.thanksPublished" : "reviews.thanksPending");
|
|
355
|
+
write.hidden = true;
|
|
356
|
+
success.hidden = false;
|
|
357
|
+
});
|
|
358
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shell's behaviour: the sticky header, the phone menu, search, the
|
|
3
|
+
* desktop menus, the announcement ticker and back to top. No commerce lives here.
|
|
4
|
+
*/
|
|
5
|
+
import { bindEach } from "@abcart/theme/lifecycle";
|
|
6
|
+
import { motionMs } from "@/lib/motion";
|
|
7
|
+
|
|
8
|
+
bindEach<HTMLElement>("[data-mobile-menu]", (menu) => {
|
|
9
|
+
const openButtons = document.querySelectorAll<HTMLButtonElement>("[data-menu-open]");
|
|
10
|
+
let lastFocus: HTMLElement | null = null;
|
|
11
|
+
const open = () => {
|
|
12
|
+
lastFocus = document.activeElement as HTMLElement | null;
|
|
13
|
+
menu.dataset.open = "true";
|
|
14
|
+
menu.setAttribute("aria-hidden", "false");
|
|
15
|
+
menu.inert = false;
|
|
16
|
+
document.body.classList.add("has-drawer-open");
|
|
17
|
+
openButtons.forEach((button) => button.setAttribute("aria-expanded", "true"));
|
|
18
|
+
menu.querySelector<HTMLElement>("[data-menu-close]")?.focus();
|
|
19
|
+
};
|
|
20
|
+
const close = () => {
|
|
21
|
+
if (menu.dataset.open !== "true") return;
|
|
22
|
+
menu.dataset.open = "false";
|
|
23
|
+
menu.setAttribute("aria-hidden", "true");
|
|
24
|
+
menu.inert = true;
|
|
25
|
+
document.body.classList.remove("has-drawer-open");
|
|
26
|
+
openButtons.forEach((button) => button.setAttribute("aria-expanded", "false"));
|
|
27
|
+
lastFocus?.focus();
|
|
28
|
+
};
|
|
29
|
+
menu.inert = true;
|
|
30
|
+
openButtons.forEach((button) => button.addEventListener("click", open));
|
|
31
|
+
menu.querySelectorAll<HTMLElement>("[data-menu-close], a[href]").forEach((element) => element.addEventListener("click", close));
|
|
32
|
+
document.addEventListener("keydown", (event) => {
|
|
33
|
+
if (event.key === "Escape") close();
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
bindEach<HTMLDialogElement>("[data-search-dialog]", (dialog) => {
|
|
38
|
+
const close = () => {
|
|
39
|
+
if (dialog.open) dialog.close();
|
|
40
|
+
};
|
|
41
|
+
document.querySelectorAll<HTMLElement>("[data-search-open]").forEach((button) =>
|
|
42
|
+
button.addEventListener("click", () => {
|
|
43
|
+
dialog.showModal();
|
|
44
|
+
document.body.classList.add("has-dialog-open");
|
|
45
|
+
dialog.querySelector<HTMLInputElement>("input[type=search]")?.focus();
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
dialog.querySelectorAll<HTMLElement>("[data-search-close]").forEach((button) => button.addEventListener("click", close));
|
|
49
|
+
dialog.addEventListener("click", (event) => {
|
|
50
|
+
if (event.target === dialog) close();
|
|
51
|
+
});
|
|
52
|
+
dialog.addEventListener("close", () => document.body.classList.remove("has-dialog-open"));
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
bindEach<HTMLElement>("[data-site-nav]", (nav) => {
|
|
56
|
+
const items = Array.from(nav.querySelectorAll<HTMLDetailsElement>("[data-nav-item]"));
|
|
57
|
+
const hover = window.matchMedia("(hover: hover)");
|
|
58
|
+
const closeAll = (except?: HTMLDetailsElement) => items.forEach((item) => item !== except && (item.open = false));
|
|
59
|
+
items.forEach((item) => {
|
|
60
|
+
let timer = 0;
|
|
61
|
+
item.addEventListener("mouseenter", () => {
|
|
62
|
+
if (!hover.matches) return;
|
|
63
|
+
window.clearTimeout(timer);
|
|
64
|
+
closeAll(item);
|
|
65
|
+
item.open = true;
|
|
66
|
+
});
|
|
67
|
+
item.addEventListener("mouseleave", () => {
|
|
68
|
+
if (hover.matches) timer = window.setTimeout(() => (item.open = false), motionMs("--motion-menu-close-delay"));
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
document.addEventListener("click", (event) => {
|
|
72
|
+
if (!items.some((item) => item.contains(event.target as Node))) closeAll();
|
|
73
|
+
});
|
|
74
|
+
document.addEventListener("keydown", (event) => {
|
|
75
|
+
if (event.key === "Escape") closeAll();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
bindEach<HTMLElement>("[data-ticker]", (root) => {
|
|
80
|
+
const items = Array.from(root.querySelectorAll<HTMLElement>("[data-ticker-item]"));
|
|
81
|
+
if (items.length < 2) return;
|
|
82
|
+
let index = 0;
|
|
83
|
+
let timer = 0;
|
|
84
|
+
const show = (next: number) => {
|
|
85
|
+
index = (next + items.length) % items.length;
|
|
86
|
+
items.forEach((item, i) => {
|
|
87
|
+
item.hidden = i !== index;
|
|
88
|
+
item.dataset.active = i === index ? "true" : "false";
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
const stop = () => {
|
|
92
|
+
window.clearInterval(timer);
|
|
93
|
+
timer = 0;
|
|
94
|
+
};
|
|
95
|
+
const start = () => {
|
|
96
|
+
stop();
|
|
97
|
+
if (!window.matchMedia("(prefers-reduced-motion: reduce)").matches) timer = window.setInterval(() => show(index + 1), motionMs("--motion-ticker-interval"));
|
|
98
|
+
};
|
|
99
|
+
root.querySelector("[data-ticker-prev]")?.addEventListener("click", () => {
|
|
100
|
+
show(index - 1);
|
|
101
|
+
start();
|
|
102
|
+
});
|
|
103
|
+
root.querySelector("[data-ticker-next]")?.addEventListener("click", () => {
|
|
104
|
+
show(index + 1);
|
|
105
|
+
start();
|
|
106
|
+
});
|
|
107
|
+
root.addEventListener("mouseenter", stop);
|
|
108
|
+
root.addEventListener("mouseleave", start);
|
|
109
|
+
root.addEventListener("focusin", stop);
|
|
110
|
+
root.addEventListener("focusout", start);
|
|
111
|
+
start();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
bindEach<HTMLElement>("[data-back-to-top]", (button) => {
|
|
115
|
+
const update = () => (button.dataset.visible = window.scrollY > 600 ? "true" : "false");
|
|
116
|
+
update();
|
|
117
|
+
window.addEventListener("scroll", update, { passive: true });
|
|
118
|
+
button.addEventListener("click", () => window.scrollTo({ top: 0, behavior: "smooth" }));
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
bindEach<HTMLDetailsElement>("[data-locale-menu]", (menu) => {
|
|
122
|
+
document.addEventListener("click", (event) => {
|
|
123
|
+
if (!menu.contains(event.target as Node)) menu.open = false;
|
|
124
|
+
});
|
|
125
|
+
menu.addEventListener("keydown", (event) => {
|
|
126
|
+
if (event.key === "Escape") {
|
|
127
|
+
menu.open = false;
|
|
128
|
+
menu.querySelector("summary")?.focus();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The thank-you page: the checkout this order came from is no longer
|
|
3
|
+
* abandoned. Best-effort — it never holds the page.
|
|
4
|
+
*/
|
|
5
|
+
import { STOREFRONT_API_PATHS, convertAbandonedCheckout } from "@abcart/storefront";
|
|
6
|
+
import { bindEach } from "@abcart/theme/lifecycle";
|
|
7
|
+
|
|
8
|
+
bindEach<HTMLElement>("[data-checkout-converted]", (element) => {
|
|
9
|
+
const { orderId, orderNumber } = element.dataset;
|
|
10
|
+
if (orderId && orderNumber) convertAbandonedCheckout({ endpoint: STOREFRONT_API_PATHS.abandonedConvert, orderId, orderNumber });
|
|
11
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A voice note's player. One note plays at a time. The player says what it is
|
|
3
|
+
* doing — waiting, loading, playing, paused, or failed with a retry on the
|
|
4
|
+
* same button — fills its wave as it plays, and gives its track the position
|
|
5
|
+
* in words for a screen reader. The track counts seconds and never mirrors.
|
|
6
|
+
*/
|
|
7
|
+
import { bindEach } from "@abcart/theme/lifecycle";
|
|
8
|
+
import { formatMessage } from "@abcart/theme/messages";
|
|
9
|
+
|
|
10
|
+
type VoiceState = "idle" | "loading" | "playing" | "paused" | "error";
|
|
11
|
+
|
|
12
|
+
/** `HTMLMediaElement.HAVE_FUTURE_DATA`: enough of the note to start playing now. */
|
|
13
|
+
const CAN_PLAY = 3;
|
|
14
|
+
|
|
15
|
+
export const clock = (seconds: number) => `${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`;
|
|
16
|
+
|
|
17
|
+
let playing: HTMLAudioElement | null = null;
|
|
18
|
+
|
|
19
|
+
export function bindVoice(root: HTMLElement): (() => void) | void {
|
|
20
|
+
const audio = root.querySelector<HTMLAudioElement>("[data-voice-audio]");
|
|
21
|
+
const button = root.querySelector<HTMLButtonElement>("[data-voice-play]");
|
|
22
|
+
const seek = root.querySelector<HTMLInputElement>("[data-voice-seek]");
|
|
23
|
+
const time = root.querySelector<HTMLElement>("[data-voice-time]");
|
|
24
|
+
const error = root.querySelector<HTMLElement>("[data-voice-error]");
|
|
25
|
+
const bars = Array.from(root.querySelectorAll<HTMLElement>(".voice-note__bars i"));
|
|
26
|
+
if (!audio || !button || !seek || !time) return;
|
|
27
|
+
|
|
28
|
+
const length = () => (Number.isFinite(audio.duration) && audio.duration > 0 ? audio.duration : Number(root.dataset.durationMs || 0) / 1000);
|
|
29
|
+
|
|
30
|
+
const paint = () => {
|
|
31
|
+
const total = length();
|
|
32
|
+
const at = total > 0 ? Math.min(audio.currentTime, total) : 0;
|
|
33
|
+
const share = total > 0 ? at / total : 0;
|
|
34
|
+
bars.forEach((bar, index) => bar.classList.toggle("is-played", index < Math.round(share * bars.length)));
|
|
35
|
+
seek.max = String(Math.max(1, Math.round(total)));
|
|
36
|
+
seek.value = String(Math.floor(at));
|
|
37
|
+
seek.disabled = total === 0;
|
|
38
|
+
const position = root.dataset.labelPosition ?? "";
|
|
39
|
+
seek.setAttribute("aria-valuetext", formatMessage({ position }, "position", { elapsed: clock(at), total: clock(total) }, { locale: document.documentElement.lang }));
|
|
40
|
+
time.textContent = at > 0 ? clock(at) : time.dataset.length || (total > 0 ? clock(total) : "");
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const setState = (state: VoiceState) => {
|
|
44
|
+
root.dataset.state = state;
|
|
45
|
+
root.setAttribute("aria-busy", state === "loading" ? "true" : "false");
|
|
46
|
+
const label = state === "playing" || state === "loading" ? root.dataset.labelPause : state === "error" ? root.dataset.labelRetry : root.dataset.labelPlay;
|
|
47
|
+
button.setAttribute("aria-label", label ?? "");
|
|
48
|
+
if (error) error.textContent = state === "error" ? (root.dataset.labelError ?? "") : "";
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const play = () => {
|
|
52
|
+
if (playing && playing !== audio) playing.pause();
|
|
53
|
+
playing = audio;
|
|
54
|
+
if (root.dataset.state === "error") audio.load();
|
|
55
|
+
if (audio.readyState < CAN_PLAY) setState("loading");
|
|
56
|
+
audio.play().catch((reason: unknown) => {
|
|
57
|
+
if (!(reason instanceof DOMException && reason.name === "AbortError")) setState("error");
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const listeners: Array<[EventTarget, string, () => void]> = [
|
|
62
|
+
[button, "click", () => (audio.paused ? play() : audio.pause())],
|
|
63
|
+
[seek, "input", () => {
|
|
64
|
+
if (length() > 0) audio.currentTime = Number(seek.value);
|
|
65
|
+
paint();
|
|
66
|
+
}],
|
|
67
|
+
[seek, "focus", paint],
|
|
68
|
+
[audio, "playing", () => setState("playing")],
|
|
69
|
+
[audio, "waiting", () => {
|
|
70
|
+
if (!audio.paused) setState("loading");
|
|
71
|
+
}],
|
|
72
|
+
[audio, "pause", () => {
|
|
73
|
+
if (root.dataset.state !== "error") setState("paused");
|
|
74
|
+
paint();
|
|
75
|
+
}],
|
|
76
|
+
[audio, "ended", () => {
|
|
77
|
+
audio.currentTime = 0;
|
|
78
|
+
setState("idle");
|
|
79
|
+
paint();
|
|
80
|
+
}],
|
|
81
|
+
[audio, "timeupdate", paint],
|
|
82
|
+
[audio, "loadedmetadata", paint],
|
|
83
|
+
[audio, "error", () => {
|
|
84
|
+
if (audio.getAttribute("src")) setState("error");
|
|
85
|
+
}],
|
|
86
|
+
];
|
|
87
|
+
for (const [target, type, listener] of listeners) target.addEventListener(type, listener);
|
|
88
|
+
return () => {
|
|
89
|
+
for (const [target, type, listener] of listeners) target.removeEventListener(type, listener);
|
|
90
|
+
if (!audio.paused) audio.pause();
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
bindEach<HTMLElement>("[data-voice-note]", bindVoice);
|