import { html, nothing } from "lit"; import { customElement } from "../../utils/decorators/custom-element"; import { property } from "lit/decorators.js"; import { styleMap } from "lit/directives/style-map.js"; import { ref } from "lit/directives/ref.js"; import type { FileType } from "../../types/files.types"; import { getExtension } from "../../utils/files"; import { checkImageLoad, imageLoaded } from "../../utils/images"; import { partMap } from "../../utils/directives/shadow-part-map"; import { ShadowPartsController } from "../../controllers/shadow-parts-controller"; import imageGridCss from "../../scss/components/image-grid.scss"; import hostContentsCss from "../../scss/host-contents.scss"; import { isModifiedClick } from "../../utils/dom"; import { WeavySubComponent } from "../../classes/weavy-sub-component"; import { ActionType } from "../../types/action.types"; const MAX_ICON_SIZE = 128; declare global { interface HTMLElementTagNameMap { "wy-image-grid": WyImageGrid; } } /** * Image grid with evenly layouted heights on each row. * * @csspart wy-image-grid - The grid container. * @csspart wy-image-grid-outer - Modifier class for outer styling. * @csspart wy-image - Image wrapper. * @csspart wy-image-filled - Expanding the image wrapper to the area where it's placed. * @csspart wy-image-full-width - Image wrapper in full with. * @csspart wy-image-area - Area to render the image. * @csspart wy-image-content - The image. * @csspart wy-image-icon - The image when it's an icon. * @csspart wy-image-grid-more - Overlay on the last image in the limited span. * * @fires {WyActionEvent} wy-action - When opening a file in preview is requested. */ @customElement("wy-image-grid") export class WyImageGrid extends WeavySubComponent { static override styles = [imageGridCss, hostContentsCss]; protected exportParts = new ShadowPartsController(this); /** * Array of images to display. Displays an overflow graphic on the last image in the limited span. */ @property({ attribute: false }) images: FileType[] = []; /** * The maximum number of images to display. The last image will get an graphic overlay indication there is more. * * Set to `Infinity` to disable the limit. */ @property({ type: Number }) limit: number = 3; /** * Display the image grid with outer styling. */ @property({ type: Boolean, reflect: true }) outer: boolean = false; /** * Dispatch an `wy-action` event. * * @internal * @param e - Event that will be prevented. * @param file - The file to open. */ dispatchFileOpen(e: MouseEvent, file: FileType) { // check for ctrl, meta or middle click if (isModifiedClick(e)) { e.stopPropagation(); return; } e.preventDefault(); if (!file.is_trashed) { this.dispatchAction({ action: ActionType.Preview, file }); } } override render() { const more = this.images.length > this.limit ? this.images.length - this.limit : 0; const images = this.images.slice(0, this.limit); return html`