import { LitElement, PropertyValues, html, nothing } from "lit"; import { property } from "lit/decorators.js"; import { ifDefined } from "lit/directives/if-defined.js"; import { unsafeHTML } from "lit/directives/unsafe-html.js"; import { ShadowPartsController } from "../controllers/shadow-parts-controller"; import type { EmbedType } from "../types/embeds.types"; import { customElement } from "../utils/decorators/custom-element"; import { EmbedRemoveEventType, EmbedSwapEventType } from "../types/embeds.events"; import { NamedEvent } from "../types/generic.types"; import { partMap } from "../utils/directives/shadow-part-map"; import { repeat } from "lit/directives/repeat.js"; import { openUrl } from "../utils/urls"; import { ActionType } from "../types/action.types"; import { clickOnEnterAndConsumeOnSpace, clickOnSpace } from "../utils/keyboard"; import { createRef, ref, Ref } from "lit/directives/ref.js"; import embedCss from "../scss/components/embed.scss"; import hostContentsCss from "../scss/host-contents.scss"; import "./ui/wy-dropdown"; import "./ui/wy-button"; import "./ui/wy-icon"; import "./ui/wy-item"; import { WeavySubComponent } from "../classes/weavy-sub-component"; declare global { interface HTMLElementTagNameMap { "wy-embed": WyEmbed; "wy-embed-select": WyEmbedSelect; } } /** * Embed card for displaying external content (photo, video, link, rich). * * **Used sub components:** * * - [``](./ui/wy-item.ts) * - [``](./ui/wy-button.ts) * - [``](./ui/wy-icon.ts) * - [``](./ui/wy-dropdown.ts) * * @slot before - Slot rendered before the embed content (e.g. action buttons) * * @csspart wy-embed - Root embed container * @csspart wy-embed-interactive - Modifier when embed is clickable * @csspart wy-disabled - Modifier when embed is disabled * @csspart wy-embed-area - Area containing media (video/photo) * @csspart wy-embed-content - Content wrapper for video/rich embeds * @csspart wy-embed-video - Modifier for video embeds * @csspart wy-embed-rich - Modifier for rich embeds * @csspart wy-embed-photo - Photo variant container * @csspart wy-embed-photo-with-description - Photo variant with text description * @csspart wy-embed-image - Image element for photo embeds * @csspart wy-embed-icon - Thumbnail/icon element * @csspart wy-embed-provider - Provider meta text * * @fires {WyActionEventType} wy-action - Emitted when an action button is triggered. */ @customElement("wy-embed") export class WyEmbed extends WeavySubComponent { static override styles = [hostContentsCss, embedCss]; /** @internal */ protected exportParts = new ShadowPartsController(this); /** * Embed data to render inside the card. */ @property({ attribute: false }) embed!: EmbedType; /** * Whether the embed is disabled (non-interactive). */ @property({ type: Boolean, reflect: true }) disabled: boolean = false; /** * Reference to the embed content container element used for sizing/aspect-ratio adjustments. * * @internal */ protected embedContentRef: Ref = createRef(); override render() { // Array copy const actionList = this.embed.actions ? [...this.embed.actions] : []; const primaryAction = actionList.shift(); const secondaryAction = actionList.length === 1 ? actionList.shift() : undefined; const hasDescription = Boolean((this.embed.type === "photo" || this.embed.type === "link") && (this.embed.title || this.embed.description || (this.embed.type === "photo" && this.embed.provider_name))); return html`
this.embed.url && this.dispatchAction({ action: ActionType.Default, embed: this.embed }) && openUrl(this.embed.url, "_blank")} @keydown=${clickOnEnterAndConsumeOnSpace} @keyup=${clickOnSpace} > ${(this.embed.type === "video" || this.embed.type === "rich") && this.embed.html ? html`
${unsafeHTML(this.embed.html)}
` : nothing} ${this.embed.type === "photo" && this.embed.image && this.embed.thumbnail_url ? html`
${this.embed.provider_name
` : nothing} ${hasDescription ? html` ${this.embed.type === "link" && this.embed.image && this.embed.thumbnail_url ? html` ${this.embed.provider_name ` : nothing} ${this.embed.provider_name ? html`${this.embed.provider_name}` : nothing} ${this.embed.title ? html`${this.embed.title}` : nothing} ${this.embed.description ? html`${this.embed.description}` : nothing} ${secondaryAction ? html` { e.stopPropagation(); this.dispatchAction({ action: secondaryAction, embed: this.embed }); }} > ${secondaryAction} ` : nothing} ${primaryAction ? html` { e.stopPropagation(); this.dispatchAction({ action: primaryAction, embed: this.embed }); }} > ${primaryAction} ` : nothing} ${actionList.length ? html` ${actionList.map( (action) => html` { e.preventDefault(); e.stopPropagation(); this.dispatchAction({ action, embed: this.embed }); }} > ${action} ` )} ` : nothing} ` : nothing}
`; } protected override updated(_changedProperties: PropertyValues): void { if (this.embedContentRef.value) { const embedContent = this.embedContentRef.value.firstElementChild as HTMLElement; const {width, height} = this.embedContentRef.value.dataset; const parsedHeight = Number(height ?? ''); // If height is 128-256 use it as min height, otherwise 128 const minHeight = parsedHeight > 128 && parsedHeight < 256 ? parsedHeight : 128; if (width && height) { // Set aspect ratio to get proper resizing embedContent.style.aspectRatio = `${width} / ${height}`; // Use min of height and --wy-embed-content-max-size (as rendered value) embedContent.style.minHeight = `${minHeight}px`; } } } } /** * Embed selector for multiple embeds (renders `` instances with per-embed actions). * * **Used sub components:** * * - [``](./wy-embed.ts) * - [``](./ui/wy-button.ts) * - [``](./ui/wy-icon.ts) * * @csspart wy-embed-select - Wrapper for embed selections * @csspart wy-embed-actions - Slot for action buttons shown before each embed * * @fires {EmbedRemoveEventType} embed-remove - Emitted when an embed is removed. * @fires {EmbedSwapEventType} embed-swap - Emitted when embeds should be swapped/rotated. */ @customElement("wy-embed-select") export class WyEmbedSelect extends LitElement { static override styles = [hostContentsCss, embedCss]; protected exportParts = new ShadowPartsController(this); /** * Array of embeds to render in the selector. * * @type {EmbedType[]} */ @property({ attribute: false }) embeds!: EmbedType[]; /** * Dispatch a local embed-remove event for the embed with the given id. * * @internal * @param id - Embed id to remove. * @returns {boolean} True if the event was not canceled. */ private dispatchRemove(id: number) { const event: EmbedRemoveEventType = new (CustomEvent as NamedEvent)("embed-remove", { detail: { id: id }, bubbles: false, composed: false, }); return this.dispatchEvent(event); } /** * Dispatch a local embed-swap event to request rotating/swapping embeds. * * @internal * @returns {boolean} True if the event was not canceled. */ private dispatchSwap() { const event: EmbedSwapEventType = new (CustomEvent as NamedEvent)("embed-swap", { detail: {}, bubbles: false, composed: false, }); return this.dispatchEvent(event); } override render() { return html`
${repeat( this.embeds, (embed) => embed.id, (embed) => html`
${this.embeds.length > 1 ? html` this.dispatchSwap()}> ` : html``} this.dispatchRemove(embed.id)} >
` )}
`; } }