diff --git a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.html b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.html new file mode 100644 index 0000000000..f69257719b --- /dev/null +++ b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.html @@ -0,0 +1,36 @@ + + + +
+
+ + + + +
+
+
+ + +
+
+ + + +
+
+
+ + + + + + + + +
+
+ Image +
+
+
diff --git a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.ts b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.ts new file mode 100644 index 0000000000..f599172c14 --- /dev/null +++ b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.ts @@ -0,0 +1,67 @@ +import { Component, OnInit, OnDestroy, ViewChild, TemplateRef } from '@angular/core'; +import { Subscription } from 'rxjs'; +import { EditMode, ImageField } from '@sitecore-jss/sitecore-jss-angular'; +import { SxaComponent } from '../sxa.component'; +import { JssContextService } from '../../jss-context.service'; + +@Component({ + selector: 'app-image', + templateUrl: './image.component.html', +}) +export class ImageComponent extends SxaComponent implements OnInit, OnDestroy { + @ViewChild('default', { static: true }) defaultVariant: TemplateRef; + @ViewChild('banner', { static: true }) bannerVariant: TemplateRef; + classHeroBannerEmpty = ''; + backgroundStyle = {}; + modifyImageProps = {}; + isEditing = false; + private contextSubscription: Subscription; + + constructor(private jssContext: JssContextService) { + super(); + } + + ngOnInit() { + super.ngOnInit(); + + const imageField = this.rendering.fields?.Image as ImageField; + this.backgroundStyle = imageField?.value?.src && { + 'background-image': `url('${imageField.value.src}')`, + }; + + this.contextSubscription = this.jssContext.state.subscribe((newState) => { + this.isEditing = newState.sitecore && newState.sitecore.context.pageEditing; + + this.classHeroBannerEmpty = + this.isEditing && imageField?.value?.class === 'scEmptyImage' ? 'hero-banner-empty' : ''; + + const isMetadataMode = newState.sitecore?.context?.editMode === EditMode.Metadata; + this.modifyImageProps = !isMetadataMode + ? { + ...imageField, + editable: imageField?.editable + ?.replace(`width="${imageField?.value?.width}"`, 'width="100%"') + .replace(`height="${imageField?.value?.height}"`, 'height="100%"'), + } + : { + ...imageField, + value: { + ...imageField?.value, + style: { width: '100%', height: '100%' }, + }, + }; + }); + } + + ngOnDestroy() { + if (this.contextSubscription) { + this.contextSubscription.unsubscribe(); + } + } + + public get variant(): TemplateRef { + return this.rendering.params?.FieldNames === 'Banner' + ? this.bannerVariant + : this.defaultVariant; + } +} diff --git a/packages/sitecore-jss-angular/src/public_api.ts b/packages/sitecore-jss-angular/src/public_api.ts index 4944328bac..da7ec5f344 100644 --- a/packages/sitecore-jss-angular/src/public_api.ts +++ b/packages/sitecore-jss-angular/src/public_api.ts @@ -56,6 +56,7 @@ export { ComponentFields, ComponentParams, getContentStylesheetLink, + EditMode, } from '@sitecore-jss/sitecore-jss/layout'; export { RetryStrategy,