|
| 1 | +import { UmbMediaUrlRepository } from '../../media/repository/index.js'; |
| 2 | +import { css, customElement, html, nothing, property, state, when } from '@umbraco-cms/backoffice/external/lit'; |
| 3 | +import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; |
| 4 | +import { UmbTextStyles } from '@umbraco-cms/backoffice/style'; |
| 5 | + |
| 6 | +@customElement('umb-media-image') |
| 7 | +export class UmbMediaImageElement extends UmbLitElement { |
| 8 | + /** |
| 9 | + * The unique identifier for the media item. |
| 10 | + * @description This is also known as the media key and is used to fetch the resource. |
| 11 | + */ |
| 12 | + @property() |
| 13 | + unique?: string; |
| 14 | + |
| 15 | + /** |
| 16 | + * The alt text for the thumbnail. |
| 17 | + */ |
| 18 | + @property() |
| 19 | + alt?: string; |
| 20 | + |
| 21 | + /** |
| 22 | + * The fallback icon for the thumbnail. |
| 23 | + */ |
| 24 | + @property() |
| 25 | + icon = 'icon-picture'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The `loading` state of the thumbnail. |
| 29 | + * @enum {'lazy' | 'eager'} |
| 30 | + * @default 'lazy' |
| 31 | + */ |
| 32 | + @property() |
| 33 | + loading: (typeof HTMLImageElement)['prototype']['loading'] = 'lazy'; |
| 34 | + |
| 35 | + @state() |
| 36 | + private _isLoading = true; |
| 37 | + |
| 38 | + @state() |
| 39 | + private _imageUrl = ''; |
| 40 | + |
| 41 | + #mediaRepository = new UmbMediaUrlRepository(this); |
| 42 | + |
| 43 | + #intersectionObserver?: IntersectionObserver; |
| 44 | + |
| 45 | + override connectedCallback() { |
| 46 | + super.connectedCallback(); |
| 47 | + |
| 48 | + if (this.loading === 'lazy') { |
| 49 | + this.#intersectionObserver = new IntersectionObserver((entries) => { |
| 50 | + if (entries[0].isIntersecting) { |
| 51 | + this.#generateThumbnailUrl(); |
| 52 | + this.#intersectionObserver?.disconnect(); |
| 53 | + } |
| 54 | + }); |
| 55 | + this.#intersectionObserver.observe(this); |
| 56 | + } else { |
| 57 | + this.#generateThumbnailUrl(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + override disconnectedCallback() { |
| 62 | + super.disconnectedCallback(); |
| 63 | + this.#intersectionObserver?.disconnect(); |
| 64 | + } |
| 65 | + |
| 66 | + async #generateThumbnailUrl() { |
| 67 | + if (!this.unique) throw new Error('Unique is missing'); |
| 68 | + const { data } = await this.#mediaRepository.requestItems([this.unique]); |
| 69 | + |
| 70 | + this._imageUrl = data?.[0]?.url ?? ''; |
| 71 | + this._isLoading = false; |
| 72 | + } |
| 73 | + |
| 74 | + override render() { |
| 75 | + return html` ${this.#renderThumbnail()} ${when(this._isLoading, () => this.#renderLoading())} `; |
| 76 | + } |
| 77 | + |
| 78 | + #renderLoading() { |
| 79 | + return html`<div id="loader"><uui-loader></uui-loader></div>`; |
| 80 | + } |
| 81 | + |
| 82 | + #renderThumbnail() { |
| 83 | + if (this._isLoading) return nothing; |
| 84 | + |
| 85 | + return when( |
| 86 | + this._imageUrl, |
| 87 | + () => |
| 88 | + html`<img |
| 89 | + part="img" |
| 90 | + src="${this._imageUrl}" |
| 91 | + alt="${this.alt ?? ''}" |
| 92 | + loading="${this.loading}" |
| 93 | + draggable="false" />`, |
| 94 | + () => html`<umb-icon id="icon" name="${this.icon}"></umb-icon>`, |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + static override styles = [ |
| 99 | + UmbTextStyles, |
| 100 | + css` |
| 101 | + :host { |
| 102 | + display: contents; |
| 103 | + } |
| 104 | +
|
| 105 | + #loader { |
| 106 | + display: flex; |
| 107 | + justify-content: center; |
| 108 | + align-items: center; |
| 109 | + height: 100%; |
| 110 | + width: 100%; |
| 111 | + } |
| 112 | +
|
| 113 | + #icon { |
| 114 | + width: 100%; |
| 115 | + height: 100%; |
| 116 | + font-size: var(--uui-size-8); |
| 117 | + } |
| 118 | + `, |
| 119 | + ]; |
| 120 | +} |
| 121 | + |
| 122 | +declare global { |
| 123 | + interface HTMLElementTagNameMap { |
| 124 | + 'umb-media-image': UmbMediaImageElement; |
| 125 | + } |
| 126 | +} |
0 commit comments