A platform for community discussion. Free, open, simple.
FEATURE: lightbox for rich editor image nodes (#36926)
Renato Atilioabb8ed05598c
diff --git a/app/assets/stylesheets/common/rich-editor/rich-editor.scss b/app/assets/stylesheets/common/rich-editor/rich-editor.scssindex 8b66cbf694f..90fece64f12 100644--- a/app/assets/stylesheets/common/rich-editor/rich-editor.scss+++ b/app/assets/stylesheets/common/rich-editor/rich-editor.scss@@ -421,6 +421,7 @@&.ProseMirror-selectednode {user-select: none;+ cursor: zoom-in;}}}diff --git a/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs b/frontend/discourse/app/static/prosemirror/components/image-node-view.gjsindex f3ce257a22e..f879ee563a1 100644--- a/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs+++ b/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs@@ -8,7 +8,9 @@ import { htmlSafe } from "@ember/template";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 { eq } from "discourse/truth-helpers";+import { i18n } from "discourse-i18n";import ImageAltTextInput from "./image-alt-text-input";const MIN_SCALE = 50;@@ -570,6 +572,24 @@ export default class ImageNodeView extends Component {this.imageLoaded = true;}+ @action+ async handleImageClick(event) {+ if (this.image.classList.contains("ProseMirror-selectednode")) {+ event?.preventDefault();+ event?.stopPropagation();++ await openLightbox(this.args.view.dom, this.image);++ const pos = this.args.getPos();+ if (pos !== null && pos >= 0) {+ const tr = this.args.view.state.tr.setSelection(+ NodeSelection.create(this.args.view.state.doc, pos)+ );+ this.args.view.dispatch(tr);+ }+ }+ }+<template><imgsrc={{@node.attrs.src}}@@ -581,8 +601,79 @@ export default class ImageNodeView extends Component {data-scale={{@node.attrs.scale}}data-thumbnail={{if (eq @node.attrs.extras "thumbnail") "true"}}style={{this.imageStyle}}+ role="button"{{didInsert this.setupImage}}{{on "load" this.updateImageLoaded}}+ {{on "click" this.handleImageClick}}/></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 import("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: () => import("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) => {+ pswp.on("change", () => {+ const slideData = pswp.getItemData(pswp.currIndex);+ const alt = slideData?.alt;+ if (alt) {+ caption.innerHTML = `<div class='pswp__caption-title'>${alt}</div>`;+ } else {+ caption.innerHTML = "";+ }+ });+ },+ });+ });++ 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/spec/system/composer/prosemirror_images_spec.rb b/spec/system/composer/prosemirror_images_spec.rbindex 0481067b345..8b732a097b4 100644--- a/spec/system/composer/prosemirror_images_spec.rb+++ b/spec/system/composer/prosemirror_images_spec.rb@@ -38,7 +38,6 @@ describe "Composer - ProseMirror - Images", type: :system doopen_composerimage = Fabricate(:image_upload)composer.type_content("")- find(".composer-image-node img").clickexpect(rich).to have_no_css(".composer-image-node img[width]")expect(rich).to have_no_css(".composer-image-node img[height]")find(".composer-image-toolbar__zoom-out").click@@ -152,7 +151,6 @@ describe "Composer - ProseMirror - Images", type: :system doit "moves image outside grid" doopen_composercomposer.type_content("[grid]")- composer.image_grid.select_first_grid_imagecomposer.image_grid.move_image_outside_gridexpect(composer.image_grid).to have_grid_images(1)expect(composer.image_grid).to have_images(2) # One in grid, one standalone@@ -193,4 +191,71 @@ describe "Composer - ProseMirror - Images", type: :system doexpect(composer.image_grid).to have_single_grid_with_images(5)endend++ describe "image lightbox" do+ let(:lightbox) { PageObjects::Components::PhotoSwipe.new }++ def click_selected_image_to_open_lightbox+ page.execute_script(<<~JS)+ document.querySelector('.composer-image-node img.ProseMirror-selectednode')?.click();+ JS+ end++ it "opens lightbox with single image" do+ open_composer+ paste_and_click_image++ click_selected_image_to_open_lightbox++ expect(lightbox).to be_visible+ expect(lightbox).to have_no_counter+ expect(lightbox).to have_no_next_button+ expect(lightbox).to have_no_prev_button++ lightbox.close_button.click+ expect(lightbox).to be_hidden++ expect(rich).to have_css(".composer-image-node img.ProseMirror-selectednode")+ expect(page).to have_css("[data-identifier='composer-image-toolbar']")+ end++ it "opens lightbox with gallery when multiple images are present" do+ open_composer+ composer.type_content("\n\n")++ first_image = rich.all(".composer-image-node img").first+ first_image.click+ expect(first_image[:class]).to include("ProseMirror-selectednode")++ click_selected_image_to_open_lightbox++ expect(lightbox).to be_visible+ expect(lightbox).to have_css(".pswp__counter", text: "1 / 2")+ expect(lightbox).to have_next_button+ expect(lightbox).to have_prev_button++ lightbox.close_button.click+ expect(lightbox).to be_hidden++ expect(rich).to have_css(".composer-image-node img.ProseMirror-selectednode")+ expect(page).to have_css("[data-identifier='composer-image-toolbar']")+ end++ it "navigates between images using prev/next buttons" do+ open_composer+ composer.type_content("\n\n")++ rich.find(".composer-image-node img[alt='first']").click+ click_selected_image_to_open_lightbox++ expect(lightbox).to be_visible+ expect(lightbox).to have_caption_title("first")++ lightbox.next_button.click+ expect(lightbox).to have_caption_title("second")++ lightbox.prev_button.click+ expect(lightbox).to have_caption_title("first")+ end+ endend