Files
marketplaces/src/app/features/project-editor/sections/widgets-section.component.ts

217 lines
7.6 KiB
TypeScript
Raw Normal View History

import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core';
2026-07-10 13:43:53 +04:00
import { FormsModule } from '@angular/forms';
import { ProjectEditorFacade } from '../facade/project-editor.facade';
import { TranslatePipe } from '../../../i18n/translate.pipe';
import { TranslateService } from '../../../i18n/translate.service';
import { InputComponent } from '../../../shared/ui/input/input.component';
feat(builder): visual homepage blocks, merchant-language widget settings, real carousel arrows Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> P0 user feedback: homepage builder showed raw section.id like 'section-hero'/'section-categories'; hero widget exposed 'full-bleed'/'boxed' and raw px/vh as free text with no explanation; Overlay/Autoplay toggles made no sense without a slides concept; product carousel widgets rendered arrows nowhere near a working carousel. Homepage section (sections list -> visual blocks): - Replaced raw section.id display with a merchant-facing block catalog (icon + name + one-line explanation) for hero/categories/featured-products/product-carousel/recently-viewed/banner/partners/custom-html - Added block catalog picker to append new blocks (was fixed at whatever the seed data had - task asked 'what if we add manually? not fixed 3') - Added duplicate and remove per block, alongside the existing drag-to-reorder - Verified in browser: labels render correctly, add-block and duplicate both confirmed working end-to-end Widgets section (hero widget): - 'Layout' free-text replaced with a select (Full width / Boxed) instead of typing 'full-bleed'/'boxed' blind - 'Height' free-text replaced with a select (Compact/Medium/Tall/Full screen) mapped to real vh values - New Slides editor: title/subtitle pairs an admin can add/remove: this is the actual multi-slide data the Overlay/Autoplay toggles were referring to with nothing to point at before - HeroWidgetData contract gains slides[]/autoplay; HeroWidgetComponent now renders a real rotator (dots, click-to-jump, autoplay interval) when more than one slide exists - previously autoplay/overlay props existed but there was no slideshow behavior anywhere to control Carousel arrows root cause and fix: - widget-manifest.json offers 'carousel' as a layout option for product-collection/product-carousel widgets, and the admin UI let you select it, but ProductCarouselWidgetComponent always rendered a static CSS grid regardless - there was no carousel implementation to have arrows in the first place - Now renders a real horizontally-scrollable strip with working prev/next buttons (native scrollBy, disabled at each end) when section.layout.strategy === 'carousel'; falls back to the existing grid otherwise - Confirmed src/app/components/items-carousel (a PrimeNG p-carousel) is dead code, not wired into any route or widget - not the source of the reported bug New builder.* i18n keys (en/ru/hy), zero duplicate-key collisions verified via scan
2026-07-19 14:14:29 +04:00
import { ButtonComponent } from '../../../shared/ui/button/button.component';
import { SectionCardComponent } from '../../../shared/ui/section-card/section-card.component';
import { ToggleComponent } from '../../../shared/ui/toggle/toggle.component';
import { WidgetConfig } from '../../../shared/models/config';
import { IconComponent } from '../../../shared/ui/icon/icon.component';
import { AppIconName } from '../../../shared/ui/icon/icon-registry';
feat(builder): visual homepage blocks, merchant-language widget settings, real carousel arrows Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> P0 user feedback: homepage builder showed raw section.id like 'section-hero'/'section-categories'; hero widget exposed 'full-bleed'/'boxed' and raw px/vh as free text with no explanation; Overlay/Autoplay toggles made no sense without a slides concept; product carousel widgets rendered arrows nowhere near a working carousel. Homepage section (sections list -> visual blocks): - Replaced raw section.id display with a merchant-facing block catalog (icon + name + one-line explanation) for hero/categories/featured-products/product-carousel/recently-viewed/banner/partners/custom-html - Added block catalog picker to append new blocks (was fixed at whatever the seed data had - task asked 'what if we add manually? not fixed 3') - Added duplicate and remove per block, alongside the existing drag-to-reorder - Verified in browser: labels render correctly, add-block and duplicate both confirmed working end-to-end Widgets section (hero widget): - 'Layout' free-text replaced with a select (Full width / Boxed) instead of typing 'full-bleed'/'boxed' blind - 'Height' free-text replaced with a select (Compact/Medium/Tall/Full screen) mapped to real vh values - New Slides editor: title/subtitle pairs an admin can add/remove: this is the actual multi-slide data the Overlay/Autoplay toggles were referring to with nothing to point at before - HeroWidgetData contract gains slides[]/autoplay; HeroWidgetComponent now renders a real rotator (dots, click-to-jump, autoplay interval) when more than one slide exists - previously autoplay/overlay props existed but there was no slideshow behavior anywhere to control Carousel arrows root cause and fix: - widget-manifest.json offers 'carousel' as a layout option for product-collection/product-carousel widgets, and the admin UI let you select it, but ProductCarouselWidgetComponent always rendered a static CSS grid regardless - there was no carousel implementation to have arrows in the first place - Now renders a real horizontally-scrollable strip with working prev/next buttons (native scrollBy, disabled at each end) when section.layout.strategy === 'carousel'; falls back to the existing grid otherwise - Confirmed src/app/components/items-carousel (a PrimeNG p-carousel) is dead code, not wired into any route or widget - not the source of the reported bug New builder.* i18n keys (en/ru/hy), zero duplicate-key collisions verified via scan
2026-07-19 14:14:29 +04:00
interface HeroSlideDraft {
title: string;
subtitle: string;
}
const WIDGET_ICONS: Record<string, AppIconName> = {
hero: 'image',
categories: 'layoutGrid',
'product-collection': 'package',
};
const WIDGET_LABEL_KEYS: Record<string, string> = {
hero: 'builder.widgetTypeHero',
categories: 'builder.widgetTypeCategories',
'product-collection': 'builder.widgetTypeProducts',
};
2026-07-10 13:43:53 +04:00
@Component({
selector: 'app-project-editor-widgets-section',
standalone: true,
imports: [FormsModule, TranslatePipe, InputComponent, ButtonComponent, SectionCardComponent, ToggleComponent, IconComponent],
2026-07-10 13:43:53 +04:00
templateUrl: './widgets-section.component.html',
styleUrls: ['./section.shared.scss', './widgets-section.component.scss'],
2026-07-10 13:43:53 +04:00
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ProjectEditorWidgetsSectionComponent {
private readonly facade = inject(ProjectEditorFacade);
private readonly translate = inject(TranslateService);
2026-07-10 13:43:53 +04:00
readonly widgets = this.facade.homepageWidgets;
readonly fieldError = (key: string): string | null => {
const messageKey = this.facade.fieldError(key);
return messageKey ? this.translate.t(messageKey) : null;
};
2026-07-10 13:43:53 +04:00
widgetIcon(type: string): AppIconName {
return WIDGET_ICONS[type] ?? 'stop';
}
widgetLabel(widget: WidgetConfig): string {
if (widget.title) {
return widget.title;
}
const key = WIDGET_LABEL_KEYS[widget.type];
return key ? this.translate.t(key) : widget.type;
}
isVisible(widget: WidgetConfig): boolean {
return widget.visible !== false;
}
toggleVisible(widgetId: string): void {
this.updateWidgetConfig(widgetId, widget => ({ ...widget, visible: !this.isVisible(widget) }));
}
moveWidget(widgetId: string, direction: -1 | 1): void {
this.facade.updateBootstrap(current => ({
...current,
pages: current.pages.map(page => ({
...page,
sections: page.sections.map(section => {
const index = section.widgets.findIndex(w => w.id === widgetId);
if (index === -1) {
return section;
}
const targetIndex = index + direction;
if (targetIndex < 0 || targetIndex >= section.widgets.length) {
return section;
}
const widgets = [...section.widgets];
[widgets[index], widgets[targetIndex]] = [widgets[targetIndex], widgets[index]];
return { ...section, widgets };
})
}))
}));
}
duplicateWidget(widgetId: string): void {
this.facade.updateBootstrap(current => ({
...current,
pages: current.pages.map(page => ({
...page,
sections: page.sections.map(section => {
const index = section.widgets.findIndex(w => w.id === widgetId);
if (index === -1) {
return section;
}
const original = section.widgets[index];
const copy: WidgetConfig = { ...original, id: `${original.id}-copy-${Date.now()}` };
const widgets = [...section.widgets];
widgets.splice(index + 1, 0, copy);
return { ...section, widgets };
})
}))
}));
}
removeWidget(widgetId: string): void {
if (!confirm(this.translate.t('builder.widgetRemoveConfirm'))) {
return;
}
this.facade.updateBootstrap(current => ({
...current,
pages: current.pages.map(page => ({
...page,
sections: page.sections.map(section => ({
...section,
widgets: section.widgets.filter(w => w.id !== widgetId)
}))
}))
}));
}
private updateWidgetConfig(widgetId: string, updater: (widget: WidgetConfig) => WidgetConfig): void {
2026-07-10 13:43:53 +04:00
this.facade.updateBootstrap(current => ({
...current,
pages: current.pages.map(page => ({
...page,
sections: page.sections.map(section => ({
...section,
widgets: section.widgets.map(widget => widget.id !== widgetId ? widget : updater(widget))
2026-07-10 13:43:53 +04:00
}))
}))
}));
}
updateWidget(widgetId: string, updater: (props: Record<string, unknown>) => Record<string, unknown>): void {
this.updateWidgetConfig(widgetId, widget => ({ ...widget, props: updater(widget.props ?? {}) }));
}
2026-07-10 13:43:53 +04:00
updateProp(widgetId: string, key: string, value: unknown): void {
this.updateWidget(widgetId, props => ({ ...props, [key]: value }));
}
feat(builder): visual homepage blocks, merchant-language widget settings, real carousel arrows Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> P0 user feedback: homepage builder showed raw section.id like 'section-hero'/'section-categories'; hero widget exposed 'full-bleed'/'boxed' and raw px/vh as free text with no explanation; Overlay/Autoplay toggles made no sense without a slides concept; product carousel widgets rendered arrows nowhere near a working carousel. Homepage section (sections list -> visual blocks): - Replaced raw section.id display with a merchant-facing block catalog (icon + name + one-line explanation) for hero/categories/featured-products/product-carousel/recently-viewed/banner/partners/custom-html - Added block catalog picker to append new blocks (was fixed at whatever the seed data had - task asked 'what if we add manually? not fixed 3') - Added duplicate and remove per block, alongside the existing drag-to-reorder - Verified in browser: labels render correctly, add-block and duplicate both confirmed working end-to-end Widgets section (hero widget): - 'Layout' free-text replaced with a select (Full width / Boxed) instead of typing 'full-bleed'/'boxed' blind - 'Height' free-text replaced with a select (Compact/Medium/Tall/Full screen) mapped to real vh values - New Slides editor: title/subtitle pairs an admin can add/remove: this is the actual multi-slide data the Overlay/Autoplay toggles were referring to with nothing to point at before - HeroWidgetData contract gains slides[]/autoplay; HeroWidgetComponent now renders a real rotator (dots, click-to-jump, autoplay interval) when more than one slide exists - previously autoplay/overlay props existed but there was no slideshow behavior anywhere to control Carousel arrows root cause and fix: - widget-manifest.json offers 'carousel' as a layout option for product-collection/product-carousel widgets, and the admin UI let you select it, but ProductCarouselWidgetComponent always rendered a static CSS grid regardless - there was no carousel implementation to have arrows in the first place - Now renders a real horizontally-scrollable strip with working prev/next buttons (native scrollBy, disabled at each end) when section.layout.strategy === 'carousel'; falls back to the existing grid otherwise - Confirmed src/app/components/items-carousel (a PrimeNG p-carousel) is dead code, not wired into any route or widget - not the source of the reported bug New builder.* i18n keys (en/ru/hy), zero duplicate-key collisions verified via scan
2026-07-19 14:14:29 +04:00
/** Extra slides beyond the widget's primary title/subtitle (which stay editable as "slide 1" via the existing fields above this list). */
heroSlides(widget: WidgetConfig): HeroSlideDraft[] {
const raw = widget.props?.['slides'];
if (!Array.isArray(raw)) {
return [];
}
return raw.map(slide => ({
title: typeof slide?.title === 'string' ? slide.title : '',
subtitle: typeof slide?.subtitle === 'string' ? slide.subtitle : '',
}));
}
addHeroSlide(widgetId: string): void {
this.updateWidget(widgetId, props => ({
...props,
slides: [...this.slidesArray(props), { title: '', subtitle: '' }],
}));
}
updateHeroSlide(widgetId: string, index: number, field: 'title' | 'subtitle', value: string): void {
this.updateWidget(widgetId, props => ({
...props,
slides: this.slidesArray(props).map((slide, i) => i === index ? { ...slide, [field]: value } : slide),
}));
}
removeHeroSlide(widgetId: string, index: number): void {
this.updateWidget(widgetId, props => ({
...props,
slides: this.slidesArray(props).filter((_, i) => i !== index),
}));
}
private slidesArray(props: Record<string, unknown>): HeroSlideDraft[] {
const raw = props['slides'];
return Array.isArray(raw) ? raw : [];
}
private readonly jsonDrafts = signal<Record<string, string>>({});
private readonly jsonErrors = signal<Record<string, string>>({});
/**
* While the textarea holds invalid JSON, keep showing the user's own draft
* (not the last-committed props) so their in-progress edit isn't silently
* overwritten on the next change-detection pass.
*/
widgetJsonValue(widgetId: string, props: Record<string, unknown>): string {
return this.jsonDrafts()[widgetId] ?? this.propsJson(props);
}
widgetJsonError(widgetId: string): string | null {
return this.jsonErrors()[widgetId] ?? null;
}
2026-07-10 13:43:53 +04:00
updateJson(widgetId: string, raw: string): void {
try {
const parsed = JSON.parse(raw);
this.updateWidget(widgetId, () => parsed);
this.jsonDrafts.update(({ [widgetId]: _removed, ...rest }) => rest);
this.jsonErrors.update(({ [widgetId]: _removed, ...rest }) => rest);
2026-07-10 13:43:53 +04:00
} catch {
this.jsonDrafts.update(drafts => ({ ...drafts, [widgetId]: raw }));
this.jsonErrors.update(errors => ({ ...errors, [widgetId]: this.translate.t('builder.widgetJsonInvalid') }));
2026-07-10 13:43:53 +04:00
}
}
propsJson(props: Record<string, unknown>): string {
return JSON.stringify(props ?? {}, null, 2);
}
}