A platform for community discussion. Free, open, simple.
UX: Open chat composer uploads in a lightbox (#44958)
Renato Atiliod13c37453c26
diff --git a/app/assets/stylesheets/common/base/lightbox.scss b/app/assets/stylesheets/common/base/lightbox.scssindex 2526afcd40f..7c0cc14088d 100644--- a/app/assets/stylesheets/common/base/lightbox.scss+++ b/app/assets/stylesheets/common/base/lightbox.scss@@ -165,6 +165,14 @@z-index: z("header") - 1;}+.pswp__video {+ position: absolute;+ top: 0;+ left: 0;+ object-fit: contain;+ background: rgb(var(--always-black-rgb));+}+// photoswipe custom caption.pswp__caption {left: 0;diff --git a/frontend/discourse/app/lib/lightbox.js b/frontend/discourse/app/lib/lightbox.jsindex 442b576c9ed..8c4c91661fb 100644--- a/frontend/discourse/app/lib/lightbox.js+++ b/frontend/discourse/app/lib/lightbox.js@@ -73,7 +73,6 @@ async function initLightbox(elem, additionalData = {}) {const { default: PhotoSwipeLightbox } = await waitForPromise(import(/* dynamicChunkName: "photoswipe-lightbox" */ "photoswipe/lightbox"));- const isTestEnv = isTesting() || isRailsTesting();const canDownload =!siteSettings.prevent_anons_from_downloading_files || !!currentUser;const canQuoteImage = !!currentUser;@@ -95,47 +94,21 @@ async function initLightbox(elem, additionalData = {}) {});const lightboxEl = new PhotoSwipeLightbox({+ ...sharedOptions(() => lightboxEl.pswp),dataSource: items,- arrowPrevTitle: i18n("lightbox.previous"),- arrowNextTitle: i18n("lightbox.next"),- closeTitle: i18n("lightbox.close"),- zoomTitle: i18n("lightbox.zoom"),- errorMsg: i18n("lightbox.error"),- showHideAnimationType: isTestEnv ? "none" : "zoom",counter: false,- escKey: false,- tapAction,paddingFn,- pswpModule: async () =>- await waitForPromise(- import(/* dynamicChunkName: "photoswipe" */ "photoswipe")- ),- appendToEl: isTesting() && document.getElementById("ember-testing"),});- const keyDownHandler = function (event) {- if (event.key !== "Escape") {- return;- }-- event.stopPropagation();- event.preventDefault();-- lightboxEl.pswp.close();- };+ closeOnEscape(lightboxEl);lightboxEl.on("afterInit", () => {const el = lightboxEl.pswp.currSlide.data.element;el.querySelector(".meta")?.classList.add("open");-- lightboxEl.pswp.element.addEventListener("keydown", (event) =>- keyDownHandler(event)- );});lightboxEl.on("close", function () {lightboxEl.pswp.element.classList.add("pswp--behind-header");- lightboxEl.pswp.element.removeEventListener("keydown", keyDownHandler);});lightboxEl.on("destroy", () => {@@ -411,15 +384,6 @@ async function initLightbox(elem, additionalData = {}) {})));- function tapAction(pt, event) {- const pswp = lightboxEl.pswp;- if (event.target.classList.contains("pswp__img")) {- pswp?.element?.classList.toggle("pswp--ui-visible");- } else {- pswp?.close();- }- }-function paddingFn(viewportSize, itemData) {if (viewportSize.x < 1200 || caps.isMobileDevice) {return { top: 0, bottom: 0, left: 0, right: 0 };@@ -434,3 +398,208 @@ async function initLightbox(elem, additionalData = {}) {lightboxEl.init();}++/**+ * Opens a lightbox over standalone media elements, for content that isn't+ * rendered as `.lightbox` links (editor images, pending uploads).+ *+ * @param {Array<{type?: "image"|"video", src: string, element?: HTMLImageElement|HTMLVideoElement, alt?: string}>} items+ * `alt` labels the slide and its caption; images default to the element's alt+ * @param {{index?: number}} [options]+ * @returns {Promise<void>} resolves when the lightbox closes, or right away+ * when another lightbox is already open+ */+export async function openLightbox(items, { index = 0 } = {}) {+ const dataSource = items.map(buildSlideData);++ // loaded up front: with a lazily loaded core, two quick opens race and one never settles+ const [{ default: PhotoSwipeLightbox }, { default: PhotoSwipe }] =+ await waitForPromise(+ Promise.all([+ import(+ /* dynamicChunkName: "photoswipe-lightbox" */ "photoswipe/lightbox"+ ),+ import(/* dynamicChunkName: "photoswipe" */ "photoswipe"),+ ])+ );+ const lightboxEl = new PhotoSwipeLightbox({+ ...sharedOptions(() => lightboxEl.pswp),+ dataSource,+ pswpModule: PhotoSwipe,+ });++ closeOnEscape(lightboxEl);++ lightboxEl.on("uiRegister", function () {+ lightboxEl.pswp.ui.registerElement({+ name: "caption",+ order: 11,+ isButton: false,+ appendTo: "root",+ html: "",+ onInit: (caption, pswp) => {+ const titleEl = document.createElement("div");+ titleEl.className = "pswp__caption-title";+ caption.appendChild(titleEl);++ pswp.on("change", () => {+ titleEl.textContent = pswp.currSlide?.data.alt || "";+ });+ },+ });+ });++ lightboxEl.on("contentLoad", (event) => {+ const { content } = event;+ if (content.type !== "video") {+ return;+ }++ event.preventDefault();++ const video = document.createElement("video");+ video.className = "pswp__video";+ video.src = content.data.src;+ video.controls = true;+ video.playsInline = true;+ video.preload = "metadata";+ if (content.data.alt) {+ video.setAttribute("aria-label", content.data.alt);+ }+ video.addEventListener(+ "loadedmetadata",+ () => syncSlideSize(content, video.videoWidth, video.videoHeight),+ { once: true }+ );+ // native controls swallow key and pointer events while focused, so Escape+ // would never reach the lightbox; media events are all they let through+ const refocus = () => video.focus({ preventScroll: true });+ for (const type of ["play", "pause", "seeked", "volumechange"]) {+ video.addEventListener(type, refocus);+ }+ content.element = video;+ });++ lightboxEl.on("contentLoadImage", ({ content }) => {+ const image = content.element;+ image.addEventListener(+ "load",+ () => syncSlideSize(content, image.naturalWidth, image.naturalHeight),+ { once: true }+ );+ });++ lightboxEl.on("contentDeactivate", ({ content }) => {+ if (content.type === "video") {+ content.element?.pause();+ }+ });++ lightboxEl.on("pointerDown", (event) => {+ if (event.originalEvent.target.closest?.(".pswp__video")) {+ event.preventDefault();+ }+ });++ return new Promise((resolve) => {+ lightboxEl.addFilter("thumbEl", (thumbEl, itemData) =>+ itemData.element?.isConnected ? itemData.element : undefined+ );+ lightboxEl.on("close", resolve);+ lightboxEl.on("closingAnimationEnd", () => lightboxEl.destroy());++ lightboxEl.init();+ if (!lightboxEl.loadAndOpen(index)) {+ lightboxEl.destroy();+ resolve();+ }+ });+}++function sharedOptions(getPswp) {+ return {+ arrowPrevTitle: i18n("lightbox.previous"),+ arrowNextTitle: i18n("lightbox.next"),+ closeTitle: i18n("lightbox.close"),+ zoomTitle: i18n("lightbox.zoom"),+ errorMsg: i18n("lightbox.error"),+ showHideAnimationType: isTesting() || isRailsTesting() ? "none" : "zoom",+ escKey: false,+ tapAction: (pt, event) => {+ if (event.target.closest(".pswp__video")) {+ return;+ }++ const pswp = getPswp();+ if (event.target.classList.contains("pswp__img")) {+ pswp?.element?.classList.toggle("pswp--ui-visible");+ } else {+ pswp?.close();+ }+ },+ pswpModule: () =>+ waitForPromise(import(/* dynamicChunkName: "photoswipe" */ "photoswipe")),+ appendToEl: isTesting() && document.getElementById("ember-testing"),+ };+}++// keeps Escape from also reaching handlers underneath the overlay+function closeOnEscape(lightboxEl) {+ const onKeyDown = (event) => {+ if (event.key !== "Escape") {+ return;+ }++ event.stopPropagation();+ event.preventDefault();+ lightboxEl.pswp.close();+ };++ lightboxEl.on("afterInit", () => {+ lightboxEl.pswp.element.addEventListener("keydown", onKeyDown);+ });+}++// initial sizes come from thumbnails, which may not have loaded yet+function syncSlideSize(content, width, height) {+ if (!width || !height) {+ return;+ }+ if (content.width === width && content.height === height) {+ return;+ }++ content.width = content.data.width = width;+ content.height = content.data.height = height;++ if (content.slide) {+ content.slide.width = width;+ content.slide.height = height;+ content.slide.resize();+ // resize() skips this when the slide is zoomed in+ content.slide.updateContentSize(true);+ }+}++function buildSlideData({ type = "image", src, element, alt }) {+ if (type === "video") {+ return {+ type,+ src,+ element,+ alt: alt || "",+ width: element?.videoWidth || 1280,+ height: element?.videoHeight || 720,+ };+ }++ return {+ src,+ msrc: element?.currentSrc || src,+ element,+ width: element?.naturalWidth || 800,+ height: element?.naturalHeight || 600,+ alt: alt ?? (element?.alt || ""),+ thumbCropped: true,+ };+}diff --git a/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs b/frontend/discourse/app/static/prosemirror/components/image-node-view.gjsindex 50b7e395bb1..866bdaedc26 100644--- a/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs+++ b/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs@@ -5,11 +5,10 @@ import { action } from "@ember/object";import didInsert from "@ember/render-modifiers/modifiers/did-insert";import { service } from "@ember/service";import { trustHTML } from "@ember/template";-import { waitForPromise } from "@ember/test-waiters";import { NodeSelection } from "prosemirror-state";import ToolbarButtons from "discourse/components/composer/toolbar-buttons";import { ToolbarBase } from "discourse/lib/composer/toolbar";-import { isRailsTesting, isTesting } from "discourse/lib/environment";+import { openLightbox } from "discourse/lib/lightbox";import icon from "discourse/ui-kit/helpers/d-icon";import { i18n } from "discourse-i18n";import ImageAltTextInput from "./image-alt-text-input.gjs";@@ -495,7 +494,23 @@ export default class ImageNodeView extends Component {event?.preventDefault();event?.stopPropagation();- await openLightbox(this.args.view.dom, this.image);+ // image nodes also hold videos and audio; only rendered images belong in the gallery+ const { view } = this.args;+ const slides = [];+ view.state.doc.descendants((node, pos) => {+ if (node.type.name !== "image") {+ return;+ }++ const element = view.nodeDOM(pos)?.querySelector(":scope > img");+ if (element) {+ slides.push({ src: node.attrs.src, element });+ }+ });++ await openLightbox(slides, {+ index: slides.findIndex((slide) => slide.element === this.image),+ });const pos = this.args.getPos();if (pos !== null && pos >= 0) {@@ -691,74 +706,3 @@ export default class ImageNodeView extends Component {{{/if}}</template>}--async function openLightbox(editorElement, currentImage) {- const allImages = [- ...editorElement.querySelectorAll(".composer-image-node img"),- ];- const currentIndex = allImages.indexOf(currentImage);-- const dataSource = allImages.map((img) => {- return {- src: img.src,- msrc: img.currentSrc || img.src,- width: img.naturalWidth || 800,- height: img.naturalHeight || 600,- alt: img.alt || "",- element: img,- thumbCropped: true,- };- });-- const { default: PhotoSwipeLightbox } = await waitForPromise(- import(/* dynamicChunkName: "photoswipe-lightbox" */ "photoswipe/lightbox")- );- const isTestEnv = isTesting() || isRailsTesting();-- const lightbox = new PhotoSwipeLightbox({- dataSource,- showHideAnimationType: isTestEnv ? "none" : "zoom",- closeTitle: i18n("lightbox.close"),- zoomTitle: i18n("lightbox.zoom"),- arrowPrevTitle: i18n("lightbox.previous"),- arrowNextTitle: i18n("lightbox.next"),- pswpModule: () =>- waitForPromise(import(/* dynamicChunkName: "photoswipe" */ "photoswipe")),- tapAction: (pt, e) => {- if (e.target.classList.contains("pswp__img")) {- lightbox.pswp?.element?.classList.toggle("pswp--ui-visible");- } else {- lightbox.pswp?.close();- }- },- });-- lightbox.on("uiRegister", function () {- lightbox.pswp.ui.registerElement({- name: "caption",- order: 11,- isButton: false,- appendTo: "root",- html: "",- onInit: (caption, pswp) => {- const titleEl = document.createElement("div");- titleEl.className = "pswp__caption-title";- caption.appendChild(titleEl);-- pswp.on("change", () => {- const slideData = pswp.getItemData(pswp.currIndex);- titleEl.textContent = slideData?.alt || "";- });- },- });- });-- return new Promise((resolve) => {- lightbox.addFilter("thumbEl", (thumbEl, itemData) => itemData.element);- lightbox.on("close", resolve);- lightbox.on("closingAnimationEnd", () => lightbox.destroy());-- lightbox.init();- lightbox.loadAndOpen(currentIndex);- });-}diff --git a/frontend/discourse/tests/helpers/media-fixtures.js b/frontend/discourse/tests/helpers/media-fixtures.jsnew file mode 100644index 00000000000..e7e51052acc--- /dev/null+++ b/frontend/discourse/tests/helpers/media-fixtures.js@@ -0,0 +1,13 @@+// smallest payloads browsers actually decode, for tests that render real media+export const PNG_BYTES = Uint8Array.from(+ atob(+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEX/AAAZ4gk3AAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg=="+ ),+ (c) => c.charCodeAt(0)+);+export const WEBM_BYTES = Uint8Array.from(+ atob(+ "GkXfo59ChoEBQveBAULygQRC84EIQoKEd2VibUKHgQJChYECGFOAZwEAAAAAAAIDEU2bdLpNu4tTq4QVSalmU6yBoU27i1OrhBZUrmtTrIHYTbuMU6uEElTDZ1OsggElTbuMU6uEHFO7a1OsggHt7AEAAAAAAABZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAVSalmsirXsYMPQkBNgI1MYXZmNjIuMTIuMTAyV0GNTGF2ZjYyLjEyLjEwMkSJiECPQAAAAAAAFlSua8iuAQAAAAAAAD/XgQFzxYgkYveXOuew6ZyBACK1nIN1bmSIgQCGhVZfVlA4g4EBI+ODhDuaygDgkLCBELqBEJqBAlWwhFW5gQESVMNn/HNzoGPAgGfImkWjh0VOQ09ERVJEh41MYXZmNjIuMTIuMTAyc3PWY8CLY8WIJGL3lzrnsOlnyKFFo4dFTkNPREVSRIeUTGF2YzYyLjI4LjEwMiBsaWJ2cHhnyKFFo4hEVVJBVElPTkSHkzAwOjAwOjAxLjAwMDAwMDAwMAAfQ7Z1wueBAKO9gQAAgPACAJ0BKhAAEAAARwiFhYiZhIgCAgJ1qgP4A/oCDU3uAP79bvP/5G/j50b/yOP//Eqe44nj//xGgBxTu2uRu4+zgQC3iveBAfGCAabwgQM="+ ),+ (c) => c.charCodeAt(0)+);diff --git a/frontend/discourse/tests/integration/components/prosemirror-editor/image-test.js b/frontend/discourse/tests/integration/components/prosemirror-editor/image-test.jsindex 01a051cd30a..4275f491064 100644--- a/frontend/discourse/tests/integration/components/prosemirror-editor/image-test.js+++ b/frontend/discourse/tests/integration/components/prosemirror-editor/image-test.js@@ -1,4 +1,11 @@-import { click, find, settled } from "@ember/test-helpers";+import {+ click,+ find,+ settled,+ triggerKeyEvent,+ waitFor,+ waitUntil,+} from "@ember/test-helpers";import { NodeSelection, TextSelection } from "prosemirror-state";import { module, test } from "qunit";import sinon from "sinon";@@ -232,6 +239,38 @@ module(.doesNotExist("a missing thumbnail is not an error");});+ test("lightbox shows the document's images, skipping videos and audio", async function (assert) {+ pretender.post("/uploads/lookup-urls", (request) =>+ videoLookupResponse(request, "galleryVideoHash")+ );+ const [editor] = await setupRichEditor(+ assert,+ "\n\n\n\n\n\n"+ );+ const { view } = editor;+ let secondImagePos;+ view.state.doc.descendants((node, pos) => {+ if (node.attrs.alt === "two") {+ secondImagePos = pos;+ }+ });++ view.dispatch(+ view.state.tr.setSelection(+ NodeSelection.create(view.state.doc, secondImagePos)+ )+ );+ await settled();+ await click(view.nodeDOM(secondImagePos).querySelector("img"));+ await waitFor(".pswp--open");++ assert.dom(".pswp__counter", document.body).hasText("2 / 2");+ assert.dom(".pswp__caption-title", document.body).hasText("two");++ await triggerKeyEvent(".pswp", "keydown", "Escape");+ await waitUntil(() => !document.querySelector(".pswp"));+ });+test("video player can be selected before and after playback", async function (assert) {pretender.post("/uploads/lookup-urls", (request) =>videoLookupResponse(request, "selectableVideoHash")diff --git a/frontend/discourse/tests/integration/lib/open-lightbox-test.gjs b/frontend/discourse/tests/integration/lib/open-lightbox-test.gjsnew file mode 100644index 00000000000..2cbef89af0f--- /dev/null+++ b/frontend/discourse/tests/integration/lib/open-lightbox-test.gjs@@ -0,0 +1,134 @@+import {+ render,+ triggerKeyEvent,+ waitFor,+ waitUntil,+} from "@ember/test-helpers";+import { module, test } from "qunit";+import { openLightbox } from "discourse/lib/lightbox";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import { WEBM_BYTES } from "discourse/tests/helpers/media-fixtures";++module("Integration | Lib | openLightbox", function (hooks) {+ setupRenderingTest(hooks);++ hooks.afterEach(async function () {+ if (document.querySelector(".pswp")) {+ await triggerKeyEvent(".pswp", "keydown", "Escape");+ await waitUntil(() => !document.querySelector(".pswp"));+ }+ });++ test("opens image slides at the requested index", async function (assert) {+ await render(+ <template>+ <img alt="First" class="first" src="/images/avatar.png" />+ <img alt="Second" class="second" src="/images/avatar.png?2" />+ </template>+ );++ const images = [...this.element.querySelectorAll("img")];+ openLightbox(+ images.map((element) => ({ src: element.src, element })),+ { index: 1 }+ );+ await waitFor(".pswp--open");++ assert+ .dom(+ ".pswp__item[aria-hidden='false'] .pswp__img:not(.pswp__img--placeholder)"+ )+ .hasAttribute("src", /avatar\.png\?2$/);+ assert.dom(".pswp__caption-title").hasText("Second");+ });++ test("renders video slides with native controls", async function (assert) {+ openLightbox([{ type: "video", src: "/uploads/clip.mp4" }]);+ await waitFor("video.pswp__video");++ assert+ .dom("video.pswp__video", document.body)+ .hasAttribute("src", /\/uploads\/clip\.mp4$/);+ assert+ .dom("video.pswp__video", document.body)+ .hasProperty("controls", true);+ });++ test("refocuses the video after its controls are used", async function (assert) {+ openLightbox([{ type: "video", src: "/uploads/clip.mp4" }]);+ await waitFor("video.pswp__video");++ const video = document.querySelector("video.pswp__video");+ video.blur();+ video.dispatchEvent(new Event("play"));++ assert.strictEqual(+ document.activeElement,+ video,+ "keeps Escape reachable after native controls take focus"+ );+ });++ test("resizes an image slide to the loaded image", async function (assert) {+ // no element, so the slide starts from the 800x600 fallback+ openLightbox([{ src: "/images/avatar.png" }]);+ await waitUntil(() => window.pswp?.currSlide?.content.state === "loaded");++ const img = document.querySelector(+ ".pswp__img:not(.pswp__img--placeholder)"+ );+ assert.strictEqual(+ img.style.width,+ img.style.height,+ "uses the square image's aspect ratio"+ );+ });++ test("resizes a video slide once its metadata loads", async function (assert) {+ const src = URL.createObjectURL(+ new Blob([WEBM_BYTES], { type: "video/webm" })+ );++ try {+ // no element, so the slide starts from the 1280x720 fallback+ openLightbox([{ type: "video", src }]);+ await waitUntil(+ () => document.querySelector("video.pswp__video")?.readyState >= 1+ );++ const video = document.querySelector("video.pswp__video");+ assert.strictEqual(+ video.style.width,+ video.style.height,+ "uses the square video's aspect ratio"+ );+ } finally {+ URL.revokeObjectURL(src);+ }+ });++ test("settles every call when opened twice quickly", async function (assert) {+ let settled = 0;+ openLightbox([{ src: "/images/avatar.png" }]).then(() => settled++);+ openLightbox([{ src: "/images/avatar.png?2" }]).then(() => settled++);+ await waitUntil(() => window.pswp?.currSlide?.content.state === "loaded");++ assert.dom(".pswp", document.body).exists({ count: 1 });++ await triggerKeyEvent(".pswp", "keydown", "Escape");+ await waitUntil(() => !document.querySelector(".pswp"));+ await waitUntil(() => settled === 2, { timeout: 1000 });+ assert.strictEqual(settled, 2, "neither open is left pending");+ });++ test("resolves when closed with Escape", async function (assert) {+ const closed = openLightbox([{ src: "/images/avatar.png" }]);+ await waitFor(".pswp--open");++ await triggerKeyEvent(".pswp", "keydown", "Escape");+ await closed;+ await waitUntil(() => !document.querySelector(".pswp"));++ assert.dom(".pswp").doesNotExist();+ });+});diff --git a/frontend/discourse/tests/setup-tests.js b/frontend/discourse/tests/setup-tests.jsindex e9f36396021..83b2acb21a3 100644--- a/frontend/discourse/tests/setup-tests.js+++ b/frontend/discourse/tests/setup-tests.js@@ -340,6 +340,9 @@ export default async function setupTests(config) {// A pending ceremony never settles in headless Chrome, so it would retain the appWebauthnAbortHandler.controller?.abort();+ // An open lightbox is a window global that would stop later tests opening one+ window.pswp?.destroy();+// Test modules can keep fakes after the test; their unformatted error stacks,// call history and pending timers would retain the test's ownerfor (const fake of sinon.getFakes()) {diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-composer-upload.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-composer-upload.gjsindex 047549c7035..e9d5d153192 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-composer-upload.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-composer-upload.gjs@@ -1,5 +1,9 @@import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { on } from "@ember/modifier";+import { action } from "@ember/object";import { service } from "@ember/service";+import { modifier } from "ember-modifier";import { getURLWithCDN } from "discourse/lib/get-url";import { isAudio, isImage, isVideo } from "discourse/lib/uploads";import DButton from "discourse/ui-kit/d-button";@@ -10,7 +14,16 @@ import { i18n } from "discourse-i18n";export default class ChatComposerUpload extends Component {@service capabilities;+ registerPreview = modifier((element) =>+ this.args.registerPreview?.({+ type: this.isVideo ? "video" : "image",+ element,+ alt: this.fileName,+ })+ );+#localPreviewUrl = null;+ @tracked _previewFailed = false;constructor() {super(...arguments);@@ -23,6 +36,7 @@ export default class ChatComposerUpload extends Component {willDestroy() {super.willDestroy(...arguments);+if (this.#localPreviewUrl) {URL.revokeObjectURL(this.#localPreviewUrl);}@@ -64,7 +78,17 @@ export default class ChatComposerUpload extends Component {}get hasPreview() {- return this.isMedia && Boolean(this.args.isDone || this.#localPreviewUrl);+ return (+ this.isMedia &&+ !this._previewFailed &&+ Boolean(this.args.isDone || this.#localPreviewUrl)+ );+ }++ // files the browser cannot decode (e.g. HEIC outside Safari) fall back to the icon+ @action+ handlePreviewError() {+ this._previewFailed = true;}<template>@@ -78,32 +102,51 @@ export default class ChatComposerUpload extends Component {(unless @isDone "chat-composer-upload--in-progress")}}>- <div class="preview">- {{#if this.isImage}}- {{#if this.hasPreview}}- <img class="preview-img" src={{this.previewImageSrc}} />+ {{#if this.hasPreview}}+ <DButton+ class="btn-transparent preview chat-composer-upload__preview-btn"+ @action={{@onPreview}}+ @translatedAriaLabel={{i18n+ "chat.preview_upload"+ filename=this.fileName+ }}+ >+ {{#if this.isImage}}+ <img+ alt=""+ class="preview-img"+ src={{this.previewImageSrc}}+ {{on "error" this.handlePreviewError}}+ {{this.registerPreview}}+ />{{else}}- {{dIcon "far-image"}}- {{/if}}- {{else if this.isVideo}}- {{#if this.hasPreview}}<videoclass="preview-video"mutedplaysinlinepreload="metadata"src={{this.previewVideoSrc}}+ {{on "error" this.handlePreviewError}}+ {{this.registerPreview}}></video>- {{dIcon "play" class="preview-video__badge"}}- {{else}}- {{dIcon "file-video"}}{{/if}}- {{else if (isAudio this.fileName)}}- {{dIcon "file-audio"}}- {{else}}- {{dIcon "file-lines"}}+ </DButton>+ {{#if this.isVideo}}+ {{dIcon "play" class="preview-video__badge"}}{{/if}}- </div>+ {{else}}+ <div class="preview">+ {{#if this.isImage}}+ {{dIcon "far-image"}}+ {{else if this.isVideo}}+ {{dIcon "file-video"}}+ {{else if (isAudio this.fileName)}}+ {{dIcon "file-audio"}}+ {{else}}+ {{dIcon "file-lines"}}+ {{/if}}+ </div>+ {{/if}}<span class="data">{{#unless this.hasPreview}}diff --git a/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs b/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjsindex 475f531c313..abc31f7937e 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat-composer-uploads.gjs@@ -6,6 +6,7 @@ import { service } from "@ember/service";import { tagName } from "@ember-decorators/component";import { removeValueFromArray } from "discourse/lib/array-tools";import { bind } from "discourse/lib/decorators";+import { openLightbox } from "discourse/lib/lightbox";import { cloneJSON } from "discourse/lib/object";import { autoTrackedArray } from "discourse/lib/tracked-tools";import UppyUpload from "discourse/lib/uppy/uppy-upload";@@ -72,6 +73,8 @@ export default class ChatComposerUploads extends Component {existingUploads = null;uploadDropZone = null;+ #previews = new WeakMap();+get inProgressUploads() {return this.uppyUpload.inProgressUploads;}@@ -118,6 +121,39 @@ export default class ChatComposerUploads extends Component {this._triggerUploadsChanged();}+ @action+ openPreview(upload) {+ const shown = [...this.uploads, ...this.inProgressUploads].filter((item) =>+ this.#previews.has(item)+ );++ // own links to unfinished files, so tiles can release theirs at any time+ const localUrls = [];+ const slides = shown.map((item) => {+ const { type, element, alt } = this.#previews.get(item);+ let src = element.currentSrc || element.src;+ if (item.data instanceof Blob) {+ src = URL.createObjectURL(item.data);+ localUrls.push(src);+ }+ return { type, src, element, alt };+ });++ openLightbox(slides, { index: shown.indexOf(upload) }).finally(() =>+ localUrls.forEach((url) => URL.revokeObjectURL(url))+ );+ }++ @bind+ registerPreview(upload, preview) {+ this.#previews.set(upload, preview);+ return () => {+ if (this.#previews.get(upload) === preview) {+ this.#previews.delete(upload);+ }+ };+ }+@bind_pasteEventListener(event) {if (document.activeElement !== this.composerInputEl) {@@ -154,6 +190,8 @@ export default class ChatComposerUploads extends Component {<ChatComposerUpload@isDone={{true}}@onCancel={{fn this.removeUpload upload}}+ @onPreview={{fn this.openPreview upload}}+ @registerPreview={{fn this.registerPreview upload}}@upload={{upload}}/>{{/each}}@@ -161,6 +199,8 @@ export default class ChatComposerUploads extends Component {{{#each this.inProgressUploads as |upload|}}<ChatComposerUpload@onCancel={{fn this.cancelUploading upload}}+ @onPreview={{fn this.openPreview upload}}+ @registerPreview={{fn this.registerPreview upload}}@upload={{upload}}/>{{/each}}diff --git a/plugins/chat/assets/stylesheets/common/chat-composer-upload.scss b/plugins/chat/assets/stylesheets/common/chat-composer-upload.scssindex 55a592c9e08..5684d9bdbe3 100644--- a/plugins/chat/assets/stylesheets/common/chat-composer-upload.scss+++ b/plugins/chat/assets/stylesheets/common/chat-composer-upload.scss@@ -48,19 +48,59 @@&:hover {.chat-composer-upload__remove-btn {visibility: visible;- background: rgb(var(--always-black-rgb), 0.9);- padding: 5px;- border-radius: 100%;- font-size: var(--font-down-2);}}&__remove-btn {- border: 1px solid var(--primary-medium);+ --d-button-flat-bg-color: rgb(var(--always-black-rgb), 0.9);+ --d-button-flat-bg-color--hover: rgb(var(--always-black-rgb), 0.9);+ --d-button-flat-bg-color--focus: rgb(var(--always-black-rgb), 0.9);+ --d-button-flat-icon-color: #fff;+ --d-button-flat-icon-color--hover: #fff;position: absolute;top: -8px;right: -8px;+ display: flex;+ align-items: center;+ justify-content: center;+ width: 1.5rem;+ height: 1.5rem;+ min-height: 0;+ padding: 0;+ border: 1px solid var(--primary-medium);+ border-radius: 50%;+ font-size: var(--font-down-2);+ line-height: 1;visibility: hidden;++ .d-icon {+ margin: 0;+ }+ }++ &__preview-btn {+ padding: 0;+ cursor: zoom-in;+ }++ // beats the shared button focus style, which removes the outline+ .chat-composer-upload__preview-btn:focus-visible {+ outline: 2px solid var(--tertiary);+ outline-offset: 2px;+ }++ // outside the button, so button icon colours don't apply to it+ .preview-video__badge {+ position: absolute;+ top: 50%;+ left: 50%;+ transform: translate(-50%, -50%);+ font-size: var(--font-up-1);+ color: #fff;+ background: rgb(var(--always-black-rgb), 0.6);+ border-radius: 100%;+ padding: 0.35em;+ pointer-events: none;}.preview {@@ -82,20 +122,6 @@object-fit: cover;border-radius: var(--d-border-radius);}-- .preview-video__badge {- position: absolute;- top: 50%;- left: 50%;- transform: translate(-50%, -50%);- font-size: var(--font-up-1);- color: #fff;- background: rgb(var(--always-black-rgb), 0.6);- border-radius: 100%;- padding: 0.35em;- margin: 0;- pointer-events: none;- }}.data {diff --git a/plugins/chat/assets/stylesheets/mobile/chat-composer-upload.scss b/plugins/chat/assets/stylesheets/mobile/chat-composer-upload.scssindex a16325e2fe5..1df668cc298 100644--- a/plugins/chat/assets/stylesheets/mobile/chat-composer-upload.scss+++ b/plugins/chat/assets/stylesheets/mobile/chat-composer-upload.scss@@ -1,11 +1,5 @@.chat-composer-upload {&__remove-btn {visibility: visible;- background: rgb(var(--always-black-rgb), 0.9);- border-radius: 100%;-- // overwrite ios style- font-size: var(--font-down-2) !important;- padding: 5px !important;}}diff --git a/plugins/chat/config/locales/client.en.yml b/plugins/chat/config/locales/client.en.ymlindex e5f849465c2..b9d2610b0b3 100644--- a/plugins/chat/config/locales/client.en.yml+++ b/plugins/chat/config/locales/client.en.yml@@ -313,6 +313,7 @@ en:read_only: "Channel is read only, you cannot send new messages right now."placeholder_silenced: "You cannot send messages at this time."remove_upload: "Remove file"+ preview_upload: "Preview %{filename}"react: "React with emoji"reply: "Reply"message_actions: "Message actions"diff --git a/plugins/chat/test/javascripts/components/chat-composer-upload-test.gjs b/plugins/chat/test/javascripts/components/chat-composer-upload-test.gjsindex 66bc265939f..7674aa8ae20 100644--- a/plugins/chat/test/javascripts/components/chat-composer-upload-test.gjs+++ b/plugins/chat/test/javascripts/components/chat-composer-upload-test.gjs@@ -1,9 +1,16 @@import { fn } from "@ember/helper";-import { clearRender, click, render } from "@ember/test-helpers";+import {+ clearRender,+ click,+ render,+ settled,+ waitFor,+} from "@ember/test-helpers";import { module, test } from "qunit";import sinon from "sinon";import { restoreBaseUri, setupURL } from "discourse/lib/get-url";import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import { PNG_BYTES, WEBM_BYTES } from "discourse/tests/helpers/media-fixtures";import { createFile } from "discourse/tests/helpers/qunit-helpers";import { i18n } from "discourse-i18n";import ChatComposerUpload from "discourse/plugins/chat/discourse/components/chat-composer-upload";@@ -47,7 +54,7 @@ module("Component | ChatComposerUpload", function (hooks) {extension: ".png",progress: 78,fileName: "test.png",- data: createFile("test.png", "image/png"),+ data: createFile("test.png", "image/png", PNG_BYTES),});await render(@@ -66,7 +73,7 @@ module("Component | ChatComposerUpload", function (hooks) {extension: ".png",progress: 10,fileName: "test.png",- data: createFile("test.png", "image/png"),+ data: createFile("test.png", "image/png", PNG_BYTES),});await render(@@ -81,10 +88,10 @@ module("Component | ChatComposerUpload", function (hooks) {test("video - uploading in progress with local file previews it", async function (assert) {this.set("upload", {- extension: ".mp4",+ extension: ".webm",progress: 20,- fileName: "clip.mp4",- data: createFile("clip.mp4", "video/mp4"),+ fileName: "clip.webm",+ data: createFile("clip.webm", "video/webm", WEBM_BYTES),});await render(@@ -134,6 +141,68 @@ module("Component | ChatComposerUpload", function (hooks) {assert.dom(".extension-pill").doesNotExist();});+ test("image - falls back to the icon when the preview cannot be decoded", async function (assert) {+ this.set("upload", {+ extension: ".heic",+ progress: 10,+ fileName: "photo.heic",+ data: createFile("photo.heic", "image/heic"),+ });++ await render(+ <template><ChatComposerUpload @upload={{this.upload}} /></template>+ );++ await waitFor(".d-icon-far-image");++ assert.dom("img.preview-img").doesNotExist();+ assert.dom(".d-icon-far-image").exists();+ assert.dom(".file-name").hasText("photo.heic");+ });++ test("registers its preview and asks to open it when clicked", async function (assert) {+ const unregister = sinon.spy();+ this.set("registerPreview", sinon.stub().returns(unregister));+ this.set("onPreview", sinon.spy());+ this.set("shown", true);+ this.set("upload", {+ original_filename: "photo.png",+ extension: "png",+ url: "/images/avatar.png",+ });++ await render(+ <template>+ {{#if this.shown}}+ <ChatComposerUpload+ @isDone={{true}}+ @onPreview={{this.onPreview}}+ @registerPreview={{this.registerPreview}}+ @upload={{this.upload}}+ />+ {{/if}}+ </template>+ );++ assert.true(+ this.registerPreview.calledOnceWith({+ type: "image",+ element: document.querySelector("img.preview-img"),+ alt: "photo.png",+ })+ );+ assert+ .dom(".chat-composer-upload__preview-btn")+ .hasAria("label", i18n("chat.preview_upload", { filename: "photo.png" }));++ await click(".chat-composer-upload__preview-btn");+ assert.true(this.onPreview.calledOnce);++ this.set("shown", false);+ await settled();+ assert.true(unregister.calledOnce, "unregisters when removed");+ });+test("image - preprocessing upload in progress", async function (assert) {this.set("upload", {extension: ".png",diff --git a/plugins/chat/test/javascripts/components/chat-composer-uploads-test.gjs b/plugins/chat/test/javascripts/components/chat-composer-uploads-test.gjsindex 83d58ae7305..63ea594b1dc 100644--- a/plugins/chat/test/javascripts/components/chat-composer-uploads-test.gjs+++ b/plugins/chat/test/javascripts/components/chat-composer-uploads-test.gjs@@ -1,7 +1,15 @@-import { click, render, waitFor } from "@ember/test-helpers";+import {+ click,+ render,+ triggerKeyEvent,+ waitFor,+ waitUntil,+} from "@ember/test-helpers";import { module, test } from "qunit";+import sinon from "sinon";import { setupRenderingTest } from "discourse/tests/helpers/component-test";import pretender from "discourse/tests/helpers/create-pretender";+import { PNG_BYTES, WEBM_BYTES } from "discourse/tests/helpers/media-fixtures";import {createFile,silenceConsoleErrorsMatching,@@ -44,6 +52,120 @@ function setupUploadPretender() {module("Component | ChatComposerUploads", function (hooks) {setupRenderingTest(hooks);+ hooks.afterEach(async function () {+ if (document.querySelector(".pswp")) {+ await triggerKeyEvent(".pswp", "keydown", "Escape");+ await waitUntil(() => !document.querySelector(".pswp"));+ }+ });++ test("opens previewed uploads in a lightbox from the clicked one", async function (assert) {+ // decodable media, so only the broken image falls back to the icon+ const videoUrl = URL.createObjectURL(+ new Blob([WEBM_BYTES], { type: "video/webm" })+ );+ const brokenImageUrl = URL.createObjectURL(+ new Blob(["not an image"], { type: "image/png" })+ );+ this.existingUploads = [+ {+ original_filename: "photo.png",+ extension: "png",+ url: "/images/avatar.png",+ },+ {+ original_filename: "broken.png",+ extension: "png",+ url: brokenImageUrl,+ },+ { original_filename: "clip.webm", extension: "webm", url: videoUrl },+ ];++ try {+ await render(+ <template>+ <ChatComposerUploads+ @existingUploads={{this.existingUploads}}+ @fileUploadElementId="chat-widget-uploader"+ />+ </template>+ );+ await waitUntil(+ () =>+ document.querySelectorAll(".chat-composer-upload__preview-btn")+ .length === 2+ );++ await click(+ ".chat-composer-upload--video .chat-composer-upload__preview-btn"+ );+ await waitFor(".pswp--open");++ assert+ .dom(".pswp__counter", document.body)+ .hasText("2 / 2", "skips the broken image and opens at the video");+ assert+ .dom(+ ".pswp__item[aria-hidden='false'] video.pswp__video",+ document.body+ )+ .hasAttribute("src", new RegExp(`^${videoUrl}`));+ assert.dom(".pswp__caption-title", document.body).hasText("clip.webm");+ } finally {+ URL.revokeObjectURL(videoUrl);+ URL.revokeObjectURL(brokenImageUrl);+ }+ });++ test("an upload that finishes keeps its slide while the lightbox is open", async function (assert) {+ setupUploadPretender();+ const revoke = sinon.spy(URL, "revokeObjectURL");++ await render(+ <template>+ <ChatComposerUploads @fileUploadElementId="chat-widget-uploader" />+ </template>+ );++ this.container+ .lookup("service:app-events")+ .trigger(+ "upload-mixin:chat-composer-uploader:add-files",+ createFile("avatar.png", "image/png", PNG_BYTES)+ );+ await waitFor(".chat-composer-upload--in-progress .preview-img");+ const tileUrl = document+ .querySelector(".chat-composer-upload--in-progress .preview-img")+ .getAttribute("src");++ await click(+ ".chat-composer-upload--in-progress .chat-composer-upload__preview-btn"+ );+ await waitFor(".pswp--open");+ const slideUrl = window.pswp.currSlide.data.src;+ assert.true(slideUrl.startsWith("blob:"), "shows the unfinished file");+ assert.notStrictEqual(slideUrl, tileUrl, "uses its own link to it");++ await waitFor(+ ".chat-composer-upload:not(.chat-composer-upload--in-progress)",+ {+ timeout: 5000,+ }+ );+ assert.true(+ revoke.calledWith(tileUrl),+ "the finished tile releases its link"+ );+ assert.false(revoke.calledWith(slideUrl), "the lightbox keeps its own");++ await triggerKeyEvent(".pswp", "keydown", "Escape");+ await waitUntil(() => revoke.calledWith(slideUrl));+ assert.true(+ revoke.calledWith(slideUrl),+ "released once the lightbox closes"+ );+ });+test("loading uploads from an outside source (e.g. draft or editing message)", async function (assert) {this.existingUploads = [fakeUpload];