Skip to content

A platform for community discussion. Free, open, simple.

UX: better rich editor placeholder for non-image and image uploads (#39102)

Renato Atilioce43d1b18918
diff --git a/app/assets/stylesheets/common/rich-editor/rich-editor.scss b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
index 66dc04b9e47..0a3d49c0860 100644
--- a/app/assets/stylesheets/common/rich-editor/rich-editor.scss
+++ b/app/assets/stylesheets/common/rich-editor/rich-editor.scss
@@ -51,22 +51,82 @@
font-size: var(--font-up-1-rem);
}
- img[data-placeholder="true"] {
- animation: placeholder 1.5s infinite;
-
- @keyframes placeholder {
- 0% {
- opacity: 0.6;
+ .upload-placeholder {
+ &.--image {
+ position: relative;
+ display: inline-block;
+ vertical-align: bottom;
+
+ img {
+ max-width: 350px;
+
+ @include viewport.until(sm) {
+ max-width: 100%;
+ }
+ height: auto;
+ border-radius: var(--d-border-radius);
+ display: block;
+ animation: placeholder 1.5s infinite;
}
+ }
- 50% {
- opacity: 0.4;
- }
+ &.--file {
+ display: inline-flex;
+ align-items: center;
+ gap: var(--space-2);
+ padding: 0 var(--space-1) 0 0.65em;
+ border: 1px solid var(--primary-low-mid);
+ border-radius: var(--d-border-radius);
+ color: var(--primary-high);
+ font-size: var(--font-down-1-rem);
- 100% {
- opacity: 0.6;
+ .d-icon {
+ fill: currentcolor;
}
}
+
+ &__overlay {
+ position: absolute;
+ bottom: var(--space-2);
+ right: var(--space-2);
+ display: flex;
+ align-items: center;
+ gap: var(--space-2);
+ padding: 0 var(--space-1) 0 0.65em;
+ background: var(--primary-very-low);
+ border: 1px solid var(--primary-low-mid);
+ border-radius: var(--d-border-radius);
+ color: var(--primary-high);
+ font-size: var(--font-down-1-rem);
+ }
+
+ &__progress {
+ color: var(--tertiary);
+ font-weight: bold;
+ }
+
+ &__filename,
+ &__icon {
+ animation: placeholder 1.5s infinite;
+ }
+
+ &__cancel {
+ padding: 0.5em 0.65em;
+ }
+ }
+
+ @keyframes placeholder {
+ 0% {
+ opacity: 0.6;
+ }
+
+ 50% {
+ opacity: 0.4;
+ }
+
+ 100% {
+ opacity: 0.6;
+ }
}
ul,
diff --git a/frontend/discourse/app/lib/uppy/composer-upload.js b/frontend/discourse/app/lib/uppy/composer-upload.js
index 948ca11dcec..8ce3187ed67 100644
--- a/frontend/discourse/app/lib/uppy/composer-upload.js
+++ b/frontend/discourse/app/lib/uppy/composer-upload.js
@@ -312,6 +312,10 @@ export default class UppyComposerUpload {
(progress.bytesUploaded / progress.bytesTotal) * 100
);
upload.set("progress", percentage);
+ this.appEvents.trigger(
+ `composer:upload-progress:${file.id}`,
+ percentage
+ );
}
});
});
diff --git a/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs b/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs
index 46d8c2e216d..47f1bf28539 100644
--- a/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs
+++ b/frontend/discourse/app/static/prosemirror/components/image-node-view.gjs
@@ -11,9 +11,12 @@ 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 icon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
import ImageAltTextInput from "./image-alt-text-input";
+const PLACEHOLDER_CLASSES = ["upload-placeholder", "--image"];
+
const MIN_SCALE = 50;
const MAX_SCALE = 100;
const SCALE_STEP = 25;
@@ -87,6 +90,7 @@ class ImageToolbar extends ToolbarBase {
}
export default class ImageNodeView extends Component {
+ @service("app-events") appEvents;
@service menu;
@service siteSettings;
@@ -94,18 +98,35 @@ export default class ImageNodeView extends Component {
@tracked menuInstance;
@tracked altMenuInstance;
@tracked imageLoaded = false;
+ @tracked uploadProgress = 0;
+ #progressEvent;
constructor() {
super(...arguments);
+ if (this.isPlaceholder) {
+ const fileId = this.args.node.attrs.title;
+ this.args.dom.classList.add(...PLACEHOLDER_CLASSES);
+ this.args.dom.dataset.uploadId = fileId;
+ this.#progressEvent = `composer:upload-progress:${fileId}`;
+ this.appEvents.on(this.#progressEvent, this, this.onUploadProgress);
+ }
+
this.args.onSetup?.(this);
}
willDestroy() {
super.willDestroy(...arguments);
+ if (this.#progressEvent) {
+ this.appEvents.off(this.#progressEvent, this, this.onUploadProgress);
+ }
this.closeMenus();
}
+ get isPlaceholder() {
+ return !!this.args.node.attrs.placeholder;
+ }
+
stopEvent(event) {
if (["dragover", "dragend", "drop", "dragleave"].includes(event.type)) {
return false;
@@ -272,14 +293,18 @@ export default class ImageNodeView extends Component {
selectNode() {
this.image.classList.add("ProseMirror-selectednode");
- this.showToolbar();
- this.showAltText();
+ if (!this.isPlaceholder) {
+ this.showToolbar();
+ this.showAltText();
+ }
}
deselectNode() {
this.image.classList.remove("ProseMirror-selectednode");
- this.closeMenus();
+ if (!this.isPlaceholder) {
+ this.closeMenus();
+ }
}
closeMenus() {
@@ -568,6 +593,19 @@ export default class ImageNodeView extends Component {
view.dispatch(tr);
}
+ onUploadProgress(percentage) {
+ this.uploadProgress = percentage;
+ }
+
+ @action
+ cancelUpload(event) {
+ event.preventDefault();
+ event.stopPropagation();
+ this.appEvents.trigger("composer:cancel-upload", {
+ fileId: this.args.node.attrs.title,
+ });
+ }
+
@action
updateImageLoaded() {
this.imageLoaded = true;
@@ -600,6 +638,7 @@ export default class ImageNodeView extends Component {
height={{@node.attrs.height}}
data-orig-src={{@node.attrs.originalSrc}}
data-scale={{@node.attrs.scale}}
+ data-placeholder={{@node.attrs.placeholder}}
data-thumbnail={{if (eq @node.attrs.extras "thumbnail") "true"}}
style={{this.imageStyle}}
role="button"
@@ -607,6 +646,20 @@ export default class ImageNodeView extends Component {
{{on "load" this.updateImageLoaded}}
{{on "click" this.handleImageClick}}
/>
+ {{#if this.isPlaceholder}}
+ <span class="upload-placeholder__overlay">
+ <span
+ class="upload-placeholder__progress"
+ >{{this.uploadProgress}}%</span>
+ <button
+ class="upload-placeholder__cancel btn-transparent no-text"
+ title={{i18n "cancel"}}
+ aria-label={{i18n "cancel"}}
+ contenteditable="false"
+ {{on "click" this.cancelUpload}}
+ >{{icon "xmark"}}</button>
+ </span>
+ {{/if}}
</template>
}
diff --git a/frontend/discourse/app/static/prosemirror/components/upload-placeholder-node-view.gjs b/frontend/discourse/app/static/prosemirror/components/upload-placeholder-node-view.gjs
new file mode 100644
index 00000000000..ffb6249de83
--- /dev/null
+++ b/frontend/discourse/app/static/prosemirror/components/upload-placeholder-node-view.gjs
@@ -0,0 +1,68 @@
+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 icon from "discourse/ui-kit/helpers/d-icon";
+import { i18n } from "discourse-i18n";
+
+export default class UploadPlaceholderNodeView extends Component {
+ @service("app-events") appEvents;
+
+ @tracked progress = 0;
+ #progressEvent;
+
+ constructor() {
+ super(...arguments);
+ this.args.dom?.classList.add("upload-placeholder", "--file");
+ if (this.args.dom) {
+ this.args.dom.dataset.uploadId = this.args.node.attrs.fileId;
+ }
+ this.#progressEvent = `composer:upload-progress:${this.args.node.attrs.fileId}`;
+ this.appEvents.on(this.#progressEvent, this, this.onProgress);
+ this.args.onSetup?.(this);
+ }
+
+ willDestroy() {
+ super.willDestroy(...arguments);
+ this.appEvents.off(this.#progressEvent, this, this.onProgress);
+ }
+
+ onProgress(percentage) {
+ this.progress = percentage;
+ }
+
+ selectNode() {
+ this.args.dom.classList.add("ProseMirror-selectednode");
+ }
+
+ deselectNode() {
+ this.args.dom.classList.remove("ProseMirror-selectednode");
+ }
+
+ get filename() {
+ return this.args.node.attrs.filename;
+ }
+
+ @action
+ cancel(event) {
+ event.preventDefault();
+ event.stopPropagation();
+ this.appEvents.trigger("composer:cancel-upload", {
+ fileId: this.args.node.attrs.fileId,
+ });
+ }
+
+ <template>
+ <span class="upload-placeholder__icon">{{icon "file"}}</span>
+ <span class="upload-placeholder__filename">{{this.filename}}</span>
+ <span class="upload-placeholder__progress">{{this.progress}}%</span>
+ <button
+ class="upload-placeholder__cancel btn-transparent no-text"
+ title={{i18n "cancel"}}
+ aria-label={{i18n "cancel"}}
+ contenteditable="false"
+ {{on "click" this.cancel}}
+ >{{icon "xmark"}}</button>
+ </template>
+}
diff --git a/frontend/discourse/app/static/prosemirror/extensions/image.js b/frontend/discourse/app/static/prosemirror/extensions/image.js
index c3acab9f4db..6b18ee391a6 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/image.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/image.js
@@ -24,9 +24,7 @@ const extension = {
image: {
component: ImageNodeView,
shouldRender: ({ node }) =>
- !node.attrs.placeholder &&
- node.attrs.extras !== "audio" &&
- node.attrs.extras !== "video",
+ node.attrs.extras !== "audio" && node.attrs.extras !== "video",
},
},
@@ -69,6 +67,7 @@ const extension = {
originalSrc,
extras,
scale: dom.getAttribute("data-scale"),
+ placeholder: dom.dataset.placeholder || null,
};
},
},
diff --git a/frontend/discourse/app/static/prosemirror/extensions/register-default.js b/frontend/discourse/app/static/prosemirror/extensions/register-default.js
index e93ef385997..f624bd1c797 100644
--- a/frontend/discourse/app/static/prosemirror/extensions/register-default.js
+++ b/frontend/discourse/app/static/prosemirror/extensions/register-default.js
@@ -27,6 +27,7 @@ import trailingInlineSpace from "./trailing-inline-space";
import trailingParagraph from "./trailing-paragraph";
import typographerReplacements from "./typographer-replacements";
import underline from "./underline";
+import uploadPlaceholder from "./upload-placeholder";
import wrap from "./wrap";
/**
@@ -63,6 +64,7 @@ const defaultExtensions = [
overrideDragGhost,
hardBreak,
grid,
+ uploadPlaceholder,
];
defaultExtensions.forEach(registerRichEditorExtension);
diff --git a/frontend/discourse/app/static/prosemirror/extensions/upload-placeholder.js b/frontend/discourse/app/static/prosemirror/extensions/upload-placeholder.js
new file mode 100644
index 00000000000..5c844f39cd9
--- /dev/null
+++ b/frontend/discourse/app/static/prosemirror/extensions/upload-placeholder.js
@@ -0,0 +1,109 @@
+import UploadPlaceholderNodeView from "../components/upload-placeholder-node-view";
+
+/** @type {RichEditorExtension} */
+const extension = {
+ nodeSpec: {
+ upload_placeholder: {
+ inline: true,
+ atom: true,
+ selectable: true,
+ draggable: true,
+ group: "inline",
+ attrs: {
+ fileId: { default: null },
+ filename: { default: "" },
+ },
+ toDOM(node) {
+ return [
+ "span",
+ {
+ class: "upload-placeholder --file",
+ "data-upload-id": node.attrs.fileId,
+ },
+ node.attrs.filename,
+ ];
+ },
+ parseDOM: [],
+ },
+ },
+
+ nodeViews: {
+ upload_placeholder: {
+ component: UploadPlaceholderNodeView,
+ },
+ },
+
+ serializeNode: {
+ upload_placeholder() {},
+ },
+
+ plugins({ pmState: { Plugin, PluginKey }, getContext }) {
+ function collectPlaceholderIds(doc) {
+ const ids = new Set();
+ doc.descendants((node) => {
+ if (node.type.name === "upload_placeholder") {
+ ids.add(node.attrs.fileId);
+ } else if (node.type.name === "image" && node.attrs.placeholder) {
+ ids.add(node.attrs.title);
+ }
+ });
+ return ids;
+ }
+
+ return new Plugin({
+ key: new PluginKey("uploadPlaceholder"),
+
+ appendTransaction(transactions, oldState, newState) {
+ const isUserAction = transactions.some(
+ (tr) => tr.docChanged && tr.getMeta("addToHistory") !== false
+ );
+ if (!isUserAction) {
+ return;
+ }
+
+ const oldIds = collectPlaceholderIds(oldState.doc);
+ if (oldIds.size === 0) {
+ return;
+ }
+
+ const newIds = collectPlaceholderIds(newState.doc);
+ for (const id of oldIds) {
+ if (!newIds.has(id)) {
+ getContext().appEvents.trigger("composer:cancel-upload", {
+ fileId: id,
+ });
+ }
+ }
+
+ // Remove duplicated placeholders (e.g. from Option+drag)
+ const seen = new Set();
+ const dupes = [];
+ newState.doc.descendants((node, pos) => {
+ const id =
+ node.type.name === "upload_placeholder"
+ ? node.attrs.fileId
+ : node.type.name === "image" && node.attrs.placeholder
+ ? node.attrs.title
+ : null;
+ if (id) {
+ if (seen.has(id)) {
+ dupes.push({ pos, size: node.nodeSize });
+ } else {
+ seen.add(id);
+ }
+ }
+ });
+
+ if (dupes.length) {
+ const tr = newState.tr;
+ for (const { pos, size } of dupes.reverse()) {
+ tr.delete(pos, pos + size);
+ }
+ return tr.setMeta("addToHistory", false);
+ }
+ },
+ });
+ },
+};
+
+export default extension;
diff --git a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js
index a82b1d35199..398e3906eff 100644
--- a/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js
+++ b/frontend/discourse/app/static/prosemirror/lib/text-manipulation.js
@@ -3,6 +3,8 @@ import { getOwner, setOwner } from "@ember/owner";
import { trackedObject } from "@ember/reactive/collections";
import { next } from "@ember/runloop";
import { isEmpty } from "@ember/utils";
+// @ts-ignore — pretty-text has no type declarations
+import { lookupCachedUploadUrl } from "pretty-text/upload-short-url";
import { lift, setBlockType, toggleMark, wrapIn } from "prosemirror-commands";
import { Slice } from "prosemirror-model";
import {
@@ -10,7 +12,7 @@ import {
sinkListItem,
wrapInList,
} from "prosemirror-schema-list";
-import { Selection, TextSelection } from "prosemirror-state";
+import { NodeSelection, Selection, TextSelection } from "prosemirror-state";
import { bind } from "discourse/lib/decorators";
import escapeRegExp from "discourse/lib/escape-regexp";
import dAutocomplete from "discourse/ui-kit/modifiers/d-autocomplete";
@@ -491,9 +493,7 @@ export default class ProsemirrorTextManipulation {
const imagesToWrapGrid = new Set(consecutiveImages);
const placeholderNodes = [];
- // Find all placeholder image nodes in the document that match our consecutive images
this.view.state.doc.descendants((node, pos) => {
- // Skip traversing grids
if (node.type === this.schema.nodes.grid) {
return false;
}
@@ -503,7 +503,6 @@ export default class ProsemirrorTextManipulation {
node.attrs.placeholder &&
node.attrs.alt
) {
- // Extract filename from the alt text (which contains the upload placeholder text)
const uploadingText = i18n("uploading_filename", {
filename: "%placeholder%",
});
@@ -524,43 +523,38 @@ export default class ProsemirrorTextManipulation {
}
});
- // Check if we found all consecutive images and they are adjacent
- if (placeholderNodes.length === consecutiveImages.length) {
- // Sort by position to ensure correct order
- placeholderNodes.sort((a, b) => a.pos - b.pos);
-
- // Check if nodes are consecutive (adjacent)
- let areConsecutive = true;
- for (let i = 1; i < placeholderNodes.length; i++) {
- const prevNode = placeholderNodes[i - 1];
- const currNode = placeholderNodes[i];
- const expectedNextPos = prevNode.pos + prevNode.node.nodeSize;
-
- // Allow some flexibility for whitespace between nodes
- if (currNode.pos > expectedNextPos + 2) {
- areConsecutive = false;
- break;
- }
+ if (placeholderNodes.length !== consecutiveImages.length) {
+ return;
+ }
+
+ placeholderNodes.sort((a, b) => a.pos - b.pos);
+
+ let areConsecutive = true;
+ for (let i = 1; i < placeholderNodes.length; i++) {
+ const prevNode = placeholderNodes[i - 1];
+ const currNode = placeholderNodes[i];
+ if (currNode.pos > prevNode.pos + prevNode.node.nodeSize + 2) {
+ areConsecutive = false;
+ break;
}
+ }
- if (areConsecutive) {
- const firstNode = placeholderNodes[0];
- const lastNode = placeholderNodes[placeholderNodes.length - 1];
- const startPos = firstNode.pos;
- const endPos = lastNode.pos + lastNode.node.nodeSize;
+ if (!areConsecutive) {
+ return;
+ }
- // Replace the placeholder content with the actual placeholder nodes inside a grid
- const tr = this.view.state.tr;
- const content = tr.doc.slice(startPos, endPos).content;
+ const firstNode = placeholderNodes[0];
+ const lastNode = placeholderNodes[placeholderNodes.length - 1];
+ const startPos = firstNode.pos;
+ const endPos = lastNode.pos + lastNode.node.nodeSize;
- // Create grid node and put the content inside it
- const gridNode = this.schema.nodes.grid.createAndFill(null, content);
+ const tr = this.view.state.tr;
+ const content = tr.doc.slice(startPos, endPos).content;
+ const gridNode = this.schema.nodes.grid.createAndFill(null, content);
- if (gridNode) {
- tr.replaceWith(startPos, endPos, gridNode);
- this.view.dispatch(tr);
- }
- }
+ if (gridNode) {
+ tr.replaceWith(startPos, endPos, gridNode);
+ this.view.dispatch(tr);
}
}
@@ -702,26 +696,59 @@ class ProsemirrorPlaceholderHandler {
this.convertFromMarkdown = convertFromMarkdown;
}
+ #revokeBlobUrl(node) {
+ if (node.attrs.src?.startsWith("blob:")) {
+ URL.revokeObjectURL(node.attrs.src);
+ }
+ }
+
+ #findPlaceholder(fileId) {
+ let result = null;
+ this.view.state.doc.descendants((node, pos) => {
+ if (result) {
+ return false;
+ }
+ if (
+ (node.type === this.schema.nodes.image &&
+ node.attrs.placeholder &&
+ node.attrs.title === fileId) ||
+ (node.type === this.schema.nodes.upload_placeholder &&
+ node.attrs.fileId === fileId)
+ ) {
+ result = { node, pos };
+ return false;
+ }
+ });
+ return result;
+ }
+
insert(file) {
+ const isImage = file.data?.type?.startsWith("image/");
const isEmptyParagraph =
this.view.state.selection.$from.parent.type.name === "paragraph" &&
this.view.state.selection.$from.parent.nodeSize === 2;
- const imageNode = this.schema.nodes.image.create({
- src: URL.createObjectURL(file.data),
- alt: i18n("uploading_filename", { filename: file.name }),
- title: file.id,
- width: 120,
- placeholder: true,
- });
+ const node = isImage
+ ? this.schema.nodes.image.create({
+ src: URL.createObjectURL(file.data),
+ alt: i18n("uploading_filename", { filename: file.name }),
+ title: file.id,
+ placeholder: true,
+ })
+ : this.schema.nodes.upload_placeholder.create({
+ fileId: file.id,
+ filename: file.name,
+ });
this.view.dispatch(
- this.view.state.tr.insert(
- this.view.state.selection.from,
- isEmptyParagraph
- ? imageNode
- : this.schema.nodes.paragraph.create(null, imageNode)
- )
+ this.view.state.tr
+ .insert(
+ this.view.state.selection.from,
+ isEmptyParagraph
+ ? node
+ : this.schema.nodes.paragraph.create(null, node)
+ )
+ .setMeta("addToHistory", false)
);
}
@@ -730,53 +757,84 @@ class ProsemirrorPlaceholderHandler {
progressComplete() {}
cancelAll() {
+ const toDelete = [];
this.view.state.doc.descendants((node, pos) => {
if (node.type === this.schema.nodes.image && node.attrs.placeholder) {
- this.view.dispatch(this.view.state.tr.delete(pos, pos + node.nodeSize));
+ this.#revokeBlobUrl(node);
+ toDelete.push({ pos, size: node.nodeSize });
+ } else if (node.type === this.schema.nodes.upload_placeholder) {
+ toDelete.push({ pos, size: node.nodeSize });
}
});
+
+ if (toDelete.length) {
+ const tr = this.view.state.tr;
+ for (const { pos, size } of toDelete.reverse()) {
+ tr.delete(pos, pos + size);
+ }
+ this.view.dispatch(tr.setMeta("addToHistory", false));
+ }
}
cancel(file) {
- this.view.state.doc.descendants((node, pos) => {
- if (
- node.type === this.schema.nodes.image &&
- node.attrs.placeholder &&
- node.attrs.title === file.id
- ) {
- this.view.dispatch(this.view.state.tr.delete(pos, pos + node.nodeSize));
- }
- });
+ const found = this.#findPlaceholder(file.id);
+ if (found) {
+ this.#revokeBlobUrl(found.node);
+ this.view.dispatch(
+ this.view.state.tr
+ .delete(found.pos, found.pos + found.node.nodeSize)
+ .setMeta("addToHistory", false)
+ );
+ }
}
success(file, markdown) {
- /** @type {null | { node: import("prosemirror-model").Node, pos: number }} */
- let nodeToReplace = null;
- this.view.state.doc.descendants((node, pos) => {
- if (
- node.type === this.schema.nodes.image &&
- node.attrs.placeholder &&
- node.attrs.title === file.id
- ) {
- nodeToReplace = { node, pos };
- return false;
- }
- return true;
- });
-
- if (!nodeToReplace) {
+ const found = this.#findPlaceholder(file.id);
+ if (!found) {
return;
}
+ const wasSelected = this.view.state.selection.from === found.pos;
+
// keeping compatibility with plugins that change the upload markdown
const doc = this.convertFromMarkdown(markdown);
+ const tr = this.view.state.tr;
+ const replacement = doc.content.firstChild.content;
- this.view.dispatch(
- this.view.state.tr.replaceWith(
- nodeToReplace.pos,
- nodeToReplace.pos + nodeToReplace.node.nodeSize,
- doc.content.firstChild.content
- )
- );
+ if (found.node.type === this.schema.nodes.image) {
+ this.#revokeBlobUrl(found.node);
+ }
+
+ tr.replaceWith(found.pos, found.pos + found.node.nodeSize, replacement);
+
+ // resolve transparent.png placeholders using the upload URL cache,
+ // which was populated before success() was called
+ if (found.node.type === this.schema.nodes.image) {
+ tr.doc.nodesBetween(
+ found.pos,
+ found.pos + replacement.size,
+ (node, pos) => {
+ if (
+ node.type.name === "image" &&
+ node.attrs.originalSrc &&
+ node.attrs.src?.includes("transparent.png")
+ ) {
+ const cached = lookupCachedUploadUrl(node.attrs.originalSrc);
+ if (cached?.url) {
+ tr.setNodeMarkup(pos, null, { ...node.attrs, src: cached.url });
+ }
+ }
+ }
+ );
+ }
+
+ if (wasSelected) {
+ const resolved = tr.doc.resolve(found.pos);
+ if (resolved.nodeAfter) {
+ tr.setSelection(NodeSelection.create(tr.doc, found.pos));
+ }
+ }
+
+ this.view.dispatch(tr);
}
}
diff --git a/spec/support/shared_contexts/prosemirror_editor_context.rb b/spec/support/shared_contexts/prosemirror_editor_context.rb
index 18cdc392d49..18ffaaae44d 100644
--- a/spec/support/shared_contexts/prosemirror_editor_context.rb
+++ b/spec/support/shared_contexts/prosemirror_editor_context.rb
@@ -36,9 +36,10 @@ RSpec.shared_context "with prosemirror editor" do
cdp.copy_test_image
cdp.paste
+ expect(composer).to have_no_in_progress_uploads
expect(rich).to have_css(".composer-image-node img", count: 1)
expect(rich).to have_no_css(".composer-image-node img[src='/images/transparent.png']")
- expect(rich).to have_no_css(".composer-image-node img[data-placeholder='true']")
+ expect(rich).to have_no_css("img[data-placeholder='true']")
rich.find(".composer-image-node img").click
diff --git a/spec/system/composer/prosemirror_uploads_spec.rb b/spec/system/composer/prosemirror_uploads_spec.rb
new file mode 100644
index 00000000000..5fc49ac0b2f
--- /dev/null
+++ b/spec/system/composer/prosemirror_uploads_spec.rb
@@ -0,0 +1,167 @@
+# frozen_string_literal: true
+
+describe "Composer - ProseMirror - Uploads" do
+ include_context "with prosemirror editor"
+
+ describe "image uploads" do
+ it "replaces the placeholder with the uploaded image without a transparent.png flash" do
+ open_composer
+
+ file_path = file_from_fixtures("logo.png", "images").path
+ attach_file("file-uploader", file_path, make_visible: true)
+
+ expect(composer).to have_no_in_progress_uploads
+ expect(rich).to have_css(".composer-image-node img")
+ expect(rich).to have_no_css("img[src*='transparent.png']")
+ end
+
+ it "produces correct markdown after upload completes" do
+ open_composer
+
+ file_path = file_from_fixtures("logo.png", "images").path
+ attach_file("file-uploader", file_path, make_visible: true)
+
+ expect(composer).to have_no_in_progress_uploads
+ expect(rich).to have_css(".composer-image-node img")
+
+ composer.toggle_rich_editor
+ expect(composer.composer_input.value).not_to include("blob:")
+ expect(composer.composer_input.value).not_to include("Uploading:")
+ expect(composer.composer_input.value).to match(%r{!\[.*\]\(upload://})
+ end
+
+ it "uploads multiple images at once" do
+ open_composer
+
+ file_path_1 = file_from_fixtures("logo.png", "images").path
+ file_path_2 = file_from_fixtures("logo.jpg", "images").path
+ attach_file("file-uploader", [file_path_1, file_path_2], make_visible: true)
+
+ expect(composer).to have_no_in_progress_uploads
+ expect(rich).to have_css(".composer-image-node img", count: 2)
+ end
+ end
+
+ describe "non-image uploads" do
+ before { SiteSetting.authorized_extensions += "|pdf" }
+
+ it "replaces the placeholder with a link after upload" do
+ open_composer
+
+ file_path = file_from_fixtures("small.pdf", "pdf").path
+ attach_file("file-uploader", file_path, make_visible: true)
+
+ expect(composer).to have_no_in_progress_uploads
+ expect(rich).to have_no_css(".upload-placeholder.--file")
+ expect(rich).to have_css("a[href]")
+ end
+
+ it "uploads mixed image and non-image files" do
+ open_composer
+
+ file_path_1 = file_from_fixtures("logo.png", "images").path
+ file_path_2 = file_from_fixtures("small.pdf", "pdf").path
+ attach_file("file-uploader", [file_path_1, file_path_2], make_visible: true)
+
+ expect(composer).to have_no_in_progress_uploads
+ expect(rich).to have_css(".composer-image-node img", count: 1)
+ expect(rich).to have_css("a[href]")
+ end
+
+ it "shows both placeholders simultaneously during mixed upload" do
+ open_composer
+
+ file_path_1 = file_from_fixtures("logo.png", "images").path
+ file_path_2 = file_from_fixtures("large.pdf", "pdf").path
+ cdp.with_slow_upload do
+ attach_file("file-uploader", [file_path_1, file_path_2], make_visible: true)
+ expect(composer).to have_in_progress_uploads
+ expect(rich).to have_css(".upload-placeholder.--image")
+ expect(rich).to have_css(".upload-placeholder.--file")
+ end
+ end
+ end
+
+ describe "upload cancellation" do
+ it "removes image placeholder when upload is cancelled" do
+ open_composer
+
+ file_path = file_from_fixtures("logo.png", "images").path
+ cdp.with_slow_upload do
+ attach_file("file-uploader", file_path, make_visible: true)
+ expect(composer).to have_in_progress_uploads
+ find("#cancel-file-upload").click
+
+ expect(composer).to have_no_in_progress_uploads
+ expect(rich).to have_no_css(".upload-placeholder.--image")
+ end
+ end
+
+ it "removes non-image placeholder when upload is cancelled" do
+ SiteSetting.authorized_extensions += "|pdf"
+ open_composer
+
+ file_path = file_from_fixtures("large.pdf", "pdf").path
+ cdp.with_slow_upload do
+ attach_file("file-uploader", file_path, make_visible: true)
+ expect(composer).to have_in_progress_uploads
+ find("#cancel-file-upload").click
+
+ expect(composer).to have_no_in_progress_uploads
+ expect(rich).to have_no_css(".upload-placeholder.--file")
+ end
+ end
+
+ it "cancels individual non-image upload via inline cancel button" do
+ SiteSetting.authorized_extensions += "|pdf"
+ open_composer
+
+ file_path = file_from_fixtures("large.pdf", "pdf").path
+ cdp.with_slow_upload do
+ attach_file("file-uploader", file_path, make_visible: true)
+ expect(rich).to have_css(".upload-placeholder.--file")
+
+ find(".upload-placeholder__cancel").click
+
+ expect(rich).to have_no_css(".upload-placeholder.--file")
+ end
+ end
+
+ it "cancels individual image upload via overlay cancel button" do
+ open_composer
+
+ file_path = file_from_fixtures("logo.png", "images").path
+ cdp.with_slow_upload do
+ attach_file("file-uploader", file_path, make_visible: true)
+ expect(rich).to have_css(".upload-placeholder__overlay")
+
+ find(".upload-placeholder__overlay .upload-placeholder__cancel").click
+
+ expect(rich).to have_no_css(".upload-placeholder.--image")
+ end
+ end
+ end
+
+ describe "progress indicator" do
+ it "shows progress overlay on image placeholder" do
+ open_composer
+
+ file_path = file_from_fixtures("logo.png", "images").path
+ cdp.with_slow_upload do
+ attach_file("file-uploader", file_path, make_visible: true)
+ expect(rich).to have_css(".upload-placeholder__overlay .upload-placeholder__progress")
+ end
+ end
+
+ it "shows progress on non-image placeholder" do
+ SiteSetting.authorized_extensions += "|pdf"
+ open_composer
+
+ file_path = file_from_fixtures("large.pdf", "pdf").path
+ cdp.with_slow_upload do
+ attach_file("file-uploader", file_path, make_visible: true)
+ expect(rich).to have_css(".upload-placeholder.--file .upload-placeholder__progress")
+ end
+ end
+ end
+end