Skip to content

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

FIX: Image paste upload in form template composer fields (#39798)

Bannon Tanner8399e15d8418
diff --git a/frontend/discourse/app/components/composer-editor.gjs b/frontend/discourse/app/components/composer-editor.gjs
index 299bb53770a..aa0c6cc6a56 100644
--- a/frontend/discourse/app/components/composer-editor.gjs
+++ b/frontend/discourse/app/components/composer-editor.gjs
@@ -129,6 +129,11 @@ export default class ComposerEditor extends Component {
});
}
+ willDestroyElement() {
+ super.willDestroyElement(...arguments);
+ this.uppyComposerUpload.teardown();
+ }
+
get topic() {
return this.composer.get("model.topic");
}
@@ -288,6 +293,13 @@ export default class ComposerEditor extends Component {
}
}
+ @action
+ _composerEditorInitFormTemplate(formEl) {
+ if (this.composer.allowUpload) {
+ this.uppyComposerUpload.setup(formEl);
+ }
+ }
+
/**
* Sets up the editor with the given text manipulation instance
*
@@ -971,55 +983,11 @@ export default class ComposerEditor extends Component {
this._refreshOneboxes(preview);
this._expandShortUrls(preview);
- // Only apply image size adjustments for form template previews
- // Regular composer handles this through ProseMirror
- if (this.composer.formTemplateIds?.length > 0) {
- this._applyImageSizes(preview);
- }
-
this._decorateCookedElement(preview, helper);
this.composer.afterRefresh(preview);
}
- _applyImageSizes(preview) {
- // Apply sizing to images to match regular composer behavior
- // where percentage scales are relative to the max_image_width site setting
- // not the original image dimensions
- const maxImageWidth = this.siteSettings.max_image_width;
-
- const images = preview.querySelectorAll("img.resizable[width][height]");
-
- images.forEach((img) => {
- const processImage = () => {
- const width = parseInt(img.getAttribute("width"), 10);
- const height = parseInt(img.getAttribute("height"), 10);
- const naturalWidth = img.naturalWidth;
-
- if (!naturalWidth || naturalWidth === 0) {
- return;
- }
-
- if (width && height) {
- const percentage = width / naturalWidth;
-
- const constrainedBase = Math.min(naturalWidth, maxImageWidth);
- const displayWidth = Math.round(constrainedBase * percentage);
- const displayHeight = Math.round(height * (displayWidth / width));
-
- img.setAttribute("width", displayWidth);
- img.setAttribute("height", displayHeight);
- }
- };
-
- if (img.complete && img.naturalWidth > 0) {
- processImage();
- } else {
- img.addEventListener("load", processImage, { once: true });
- }
- });
- }
-
@computed("composer.formTemplateIds")
get selectedFormTemplateId() {
if (this._selectedFormTemplateId) {
@@ -1114,12 +1082,16 @@ export default class ComposerEditor extends Component {
class="composer-select-form-template"
/>
{{/if}}
- <form id="form-template-form">
+ <form
+ id="form-template-form"
+ {{didInsert this._composerEditorInitFormTemplate}}
+ >
<Wrapper
@id={{this.selectedFormTemplateId}}
@initialValues={{this.composer.formTemplateInitialValues}}
@onSelectFormTemplate={{this.composer.onSelectFormTemplate}}
@onChange={{this.updateFormPreview}}
+ @uppyComposerUpload={{this.uppyComposerUpload}}
/>
</form>
</div>
diff --git a/frontend/discourse/app/components/form-template-field/composer.gjs b/frontend/discourse/app/components/form-template-field/composer.gjs
index fe9addea507..e77786610f7 100644
--- a/frontend/discourse/app/components/form-template-field/composer.gjs
+++ b/frontend/discourse/app/components/form-template-field/composer.gjs
@@ -1,14 +1,44 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { action } from "@ember/object";
-import { next } from "@ember/runloop";
+import { next, schedule } from "@ember/runloop";
+import { service } from "@ember/service";
import { trustHTML } from "@ember/template";
import DEditor from "discourse/ui-kit/d-editor";
import dIcon from "discourse/ui-kit/helpers/d-icon";
export default class FormTemplateFieldComposer extends Component {
+ @service composer;
+ @service appEvents;
+
@tracked composerValue = this.args.value || "";
+ _claimAbortController = null;
+
+ constructor() {
+ super(...arguments);
+ this.appEvents.on("composer:replace-text", this, this.handleReplaceText);
+ }
+
+ willDestroy() {
+ super.willDestroy(...arguments);
+ this.appEvents.off("composer:replace-text", this, this.handleReplaceText);
+ this._claimAbortController?.abort();
+ }
+
+ @action
+ handleReplaceText(oldVal, newVal) {
+ if (!this.composerValue?.includes(oldVal)) {
+ return;
+ }
+
+ this.composerValue = this.composerValue.replace(oldVal, newVal ?? "");
+
+ schedule("afterRender", () => {
+ this.args.onChange?.();
+ });
+ }
+
@action
handleInput(event) {
this.composerValue = event.target.value;
@@ -17,6 +47,33 @@ export default class FormTemplateFieldComposer extends Component {
});
}
+ @action
+ onEditorSetup(textManipulation) {
+ if (!this.args.uppyComposerUpload || !this.composer.allowUpload) {
+ return;
+ }
+
+ this.args.uppyComposerUpload.textManipulation = textManipulation;
+
+ const editorTarget =
+ textManipulation.textarea || textManipulation.view?.dom;
+ if (!editorTarget) {
+ return;
+ }
+
+ this._claimAbortController?.abort();
+ this._claimAbortController = new AbortController();
+ const { signal } = this._claimAbortController;
+
+ const claimUploadTarget = () => {
+ this.args.uppyComposerUpload.textManipulation = textManipulation;
+ };
+
+ for (const event of ["focusin", "dragenter", "dragover"]) {
+ editorTarget.addEventListener(event, claimUploadTarget, { signal });
+ }
+ }
+
<template>
<div class="control-group form-template-field" data-field-type="composer">
{{#if @attributes.label}}
@@ -39,6 +96,7 @@ export default class FormTemplateFieldComposer extends Component {
@value={{this.composerValue}}
@change={{this.handleInput}}
@placeholder={{@attributes.placeholder}}
+ @onSetup={{this.onEditorSetup}}
/>
</div>
</template>
diff --git a/frontend/discourse/app/components/form-template-field/textarea.gjs b/frontend/discourse/app/components/form-template-field/textarea.gjs
index 2073c91b2bb..390c4edf6af 100644
--- a/frontend/discourse/app/components/form-template-field/textarea.gjs
+++ b/frontend/discourse/app/components/form-template-field/textarea.gjs
@@ -25,15 +25,15 @@ export default class FormTemplateFieldTextarea extends Component {
@action
handleReplaceText(oldVal, newVal) {
- if (this.value?.includes(oldVal)) {
- const escapedOldVal = oldVal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
- const regex = new RegExp(escapedOldVal, "g");
- this.value = this.value.replace(regex, newVal ?? "");
-
- schedule("afterRender", () => {
- this.args.onChange?.();
- });
+ if (!this.value?.includes(oldVal)) {
+ return;
}
+
+ this.value = this.value.replace(oldVal, newVal ?? "");
+
+ schedule("afterRender", () => {
+ this.args.onChange?.();
+ });
}
@action
diff --git a/frontend/discourse/app/components/form-template-field/upload.gjs b/frontend/discourse/app/components/form-template-field/upload.gjs
index 3a2fadcff7b..07b6fea4974 100644
--- a/frontend/discourse/app/components/form-template-field/upload.gjs
+++ b/frontend/discourse/app/components/form-template-field/upload.gjs
@@ -40,22 +40,21 @@ export default class FormTemplateFieldUpload extends Component {
@action
handleReplaceText(oldVal, newVal) {
- if (this.uploadValue?.includes(oldVal)) {
- const escapedOldVal = oldVal.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
- const regex = new RegExp(escapedOldVal, "g");
- this.uploadValue = this.uploadValue.replace(regex, newVal ?? "");
-
- // If it was a deletion, try to find and remove the file from uploadedFiles list
- if (!newVal) {
- this.uploadedFiles = this.uploadedFiles.filter((file) => {
- return !oldVal.includes(file.short_url);
- });
- }
-
- schedule("afterRender", () => {
- this.args.onChange?.();
+ if (!this.uploadValue?.includes(oldVal)) {
+ return;
+ }
+
+ this.uploadValue = this.uploadValue.replace(oldVal, newVal ?? "");
+
+ if (!newVal) {
+ this.uploadedFiles = this.uploadedFiles.filter((file) => {
+ return !oldVal.includes(file.short_url);
});
}
+
+ schedule("afterRender", () => {
+ this.args.onChange?.();
+ });
}
get uploadStatusLabel() {
diff --git a/frontend/discourse/app/components/form-template-field/wrapper.gjs b/frontend/discourse/app/components/form-template-field/wrapper.gjs
index 29b994949cd..14fdc3f5649 100644
--- a/frontend/discourse/app/components/form-template-field/wrapper.gjs
+++ b/frontend/discourse/app/components/form-template-field/wrapper.gjs
@@ -23,6 +23,7 @@ const FormTemplateField = <template>
@validations={{@content.validations}}
@value={{@initialValue}}
@onChange={{@onChange}}
+ @uppyComposerUpload={{@uppyComposerUpload}}
/>
</template>;
@@ -109,6 +110,7 @@ export default class FormTemplateFieldWrapper extends Component {
@content={{content}}
@initialValue={{get this.initialValues content.id}}
@onChange={{this.onChange}}
+ @uppyComposerUpload={{@uppyComposerUpload}}
/>
{{/each}}
</div>
diff --git a/frontend/discourse/app/lib/uppy/composer-upload.js b/frontend/discourse/app/lib/uppy/composer-upload.js
index 8ce3187ed67..5f5bf81893f 100644
--- a/frontend/discourse/app/lib/uppy/composer-upload.js
+++ b/frontend/discourse/app/lib/uppy/composer-upload.js
@@ -135,6 +135,10 @@ export default class UppyComposerUpload {
}
setup(element) {
+ if (this.#uploadTargetBound) {
+ this.teardown();
+ }
+
this.#editorEl = element;
this.#fileInputEl = document.getElementById(this.fileUploadElementId);
@@ -554,9 +558,7 @@ export default class UppyComposerUpload {
@bind
_pasteEventListener(event) {
- if (
- !document.querySelector(this.editorInputClass)?.contains(event.target)
- ) {
+ if (!event.target.closest(this.editorInputClass)) {
return;
}
diff --git a/frontend/discourse/tests/integration/components/form-template-field/composer-test.gjs b/frontend/discourse/tests/integration/components/form-template-field/composer-test.gjs
new file mode 100644
index 00000000000..7a880275bf0
--- /dev/null
+++ b/frontend/discourse/tests/integration/components/form-template-field/composer-test.gjs
@@ -0,0 +1,361 @@
+import { getOwner } from "@ember/owner";
+import { render, settled, triggerEvent, waitUntil } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import sinon from "sinon";
+import FormComposer from "discourse/components/form-template-field/composer";
+import noop from "discourse/helpers/noop";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+
+function fakeUppy() {
+ return { textManipulation: null };
+}
+
+module(
+ "Integration | Component | form-template-field | composer",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ hooks.afterEach(function () {
+ sinon.restore();
+ });
+
+ function stubAllowUpload(context, value) {
+ const composerService = getOwner(context).lookup("service:composer");
+ sinon.stub(composerService, "allowUpload").value(value);
+ }
+
+ test("renders the markdown editor", async function (assert) {
+ stubAllowUpload(this, true);
+
+ await render(<template><FormComposer @onChange={{noop}} /></template>);
+
+ assert
+ .dom("[data-field-type='composer'] .d-editor-input")
+ .exists("the markdown editor renders");
+ });
+
+ test("assigns textManipulation when uppyComposerUpload is provided", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const uppy = fakeUppy();
+ this.set("uppy", uppy);
+
+ await render(
+ <template>
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ </template>
+ );
+
+ await waitUntil(() => uppy.textManipulation);
+
+ assert.true(
+ uppy.textManipulation?.textarea instanceof HTMLTextAreaElement,
+ "textManipulation reflects the active editor's text manipulation"
+ );
+ });
+
+ test("does not assign textManipulation when allowUpload is false", async function (assert) {
+ stubAllowUpload(this, false);
+
+ const uppy = fakeUppy();
+ this.set("uppy", uppy);
+
+ await render(
+ <template>
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ </template>
+ );
+
+ assert.strictEqual(
+ uppy.textManipulation,
+ null,
+ "textManipulation is left untouched"
+ );
+ });
+
+ test("re-claims the upload target on focusin", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const uppy = fakeUppy();
+ this.set("uppy", uppy);
+
+ await render(
+ <template>
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ </template>
+ );
+
+ await waitUntil(() => uppy.textManipulation);
+
+ const initialTextManipulation = uppy.textManipulation;
+ uppy.textManipulation = "trampled-by-another-field";
+
+ await triggerEvent(".d-editor-input", "focusin");
+
+ assert.strictEqual(
+ uppy.textManipulation,
+ initialTextManipulation,
+ "focusin restores this field's textManipulation as the upload target"
+ );
+ });
+
+ test("re-claims the upload target on dragenter", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const uppy = fakeUppy();
+ this.set("uppy", uppy);
+
+ await render(
+ <template>
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ </template>
+ );
+
+ await waitUntil(() => uppy.textManipulation);
+
+ const initialTextManipulation = uppy.textManipulation;
+ uppy.textManipulation = "trampled-by-another-field";
+
+ await triggerEvent(".d-editor-input", "dragenter");
+
+ assert.strictEqual(
+ uppy.textManipulation,
+ initialTextManipulation,
+ "dragenter restores this field's textManipulation as the upload target"
+ );
+ });
+
+ test("re-claims the upload target on dragover", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const uppy = fakeUppy();
+ this.set("uppy", uppy);
+
+ await render(
+ <template>
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ </template>
+ );
+
+ await waitUntil(() => uppy.textManipulation);
+
+ const initialTextManipulation = uppy.textManipulation;
+ uppy.textManipulation = "trampled-by-another-field";
+
+ await triggerEvent(".d-editor-input", "dragover");
+
+ assert.strictEqual(
+ uppy.textManipulation,
+ initialTextManipulation,
+ "dragover restores this field's textManipulation as the upload target"
+ );
+ });
+
+ test("dragging onto an unfocused field claims its upload target", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const uppy = fakeUppy();
+ this.set("uppy", uppy);
+
+ await render(
+ <template>
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ </template>
+ );
+
+ await waitUntil(() => uppy.textManipulation);
+
+ const textareas = document.querySelectorAll(
+ "[data-field-type='composer'] .d-editor-input"
+ );
+
+ await triggerEvent(textareas[0], "focusin");
+ const fieldATextManipulation = uppy.textManipulation;
+
+ await triggerEvent(textareas[1], "dragenter");
+
+ assert.notStrictEqual(
+ uppy.textManipulation,
+ fieldATextManipulation,
+ "dragenter on field B switches the upload target away from field A without requiring focus"
+ );
+ });
+
+ test("removes claim listeners when the component is destroyed", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const uppy = fakeUppy();
+ this.set("uppy", uppy);
+ this.set("show", true);
+
+ await render(
+ <template>
+ {{#if this.show}}
+ <FormComposer
+ @onChange={{noop}}
+ @uppyComposerUpload={{this.uppy}}
+ />
+ {{/if}}
+ </template>
+ );
+
+ await waitUntil(() => uppy.textManipulation);
+
+ const textarea = document.querySelector(
+ "[data-field-type='composer'] .d-editor-input"
+ );
+
+ this.set("show", false);
+ await settled();
+
+ uppy.textManipulation = "sentinel";
+
+ for (const event of ["focusin", "dragenter", "dragover"]) {
+ textarea.dispatchEvent(new Event(event, { bubbles: true }));
+ }
+
+ assert.strictEqual(
+ uppy.textManipulation,
+ "sentinel",
+ "claim listeners no longer fire after the component is destroyed"
+ );
+ });
+
+ test("multiple fields route uploads to the focused field", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const uppy = fakeUppy();
+ this.set("uppy", uppy);
+
+ await render(
+ <template>
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ <FormComposer @onChange={{noop}} @uppyComposerUpload={{this.uppy}} />
+ </template>
+ );
+
+ await waitUntil(() => uppy.textManipulation);
+
+ const textareas = document.querySelectorAll(
+ "[data-field-type='composer'] .d-editor-input"
+ );
+ assert.strictEqual(textareas.length, 2, "both fields render textareas");
+
+ await triggerEvent(textareas[0], "focusin");
+ const fieldATextManipulation = uppy.textManipulation;
+
+ await triggerEvent(textareas[1], "focusin");
+ const fieldBTextManipulation = uppy.textManipulation;
+
+ assert.notStrictEqual(
+ fieldATextManipulation,
+ fieldBTextManipulation,
+ "each field has its own textManipulation"
+ );
+
+ await triggerEvent(textareas[0], "focusin");
+ assert.strictEqual(
+ uppy.textManipulation,
+ fieldATextManipulation,
+ "focusing field A again restores its textManipulation"
+ );
+ });
+
+ test("composer:replace-text replaces in composerValue and fires onChange", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const onChange = sinon.spy();
+ this.set("onChange", onChange);
+ this.set("initialValue", "before ![image|10x10](upload://abc.png) after");
+
+ await render(
+ <template>
+ <FormComposer
+ @id="field-1"
+ @value={{this.initialValue}}
+ @onChange={{this.onChange}}
+ />
+ </template>
+ );
+
+ const appEvents = getOwner(this).lookup("service:app-events");
+ appEvents.trigger(
+ "composer:replace-text",
+ "![image|10x10](upload://abc.png)",
+ "![image|10x10, 75%](upload://abc.png)"
+ );
+
+ await settled();
+
+ const expected = "before ![image|10x10, 75%](upload://abc.png) after";
+ assert.dom("input[name='field-1']").hasValue(expected);
+ assert.dom(".d-editor-input").hasValue(expected);
+ assert.true(onChange.called, "onChange fires after the replacement");
+ });
+
+ test("composer:replace-text only replaces the first occurrence", async function (assert) {
+ stubAllowUpload(this, true);
+
+ this.set(
+ "initialValue",
+ "![image|10x10](upload://abc.png) and ![image|10x10](upload://abc.png)"
+ );
+
+ await render(
+ <template>
+ <FormComposer
+ @id="field-1"
+ @value={{this.initialValue}}
+ @onChange={{noop}}
+ />
+ </template>
+ );
+
+ const appEvents = getOwner(this).lookup("service:app-events");
+ appEvents.trigger(
+ "composer:replace-text",
+ "![image|10x10](upload://abc.png)",
+ "![image|10x10, 75%](upload://abc.png)"
+ );
+
+ await settled();
+
+ const expected =
+ "![image|10x10, 75%](upload://abc.png) and ![image|10x10](upload://abc.png)";
+ assert.dom("input[name='field-1']").hasValue(expected);
+ assert.dom(".d-editor-input").hasValue(expected);
+ });
+
+ test("composer:replace-text is a no-op when the field does not contain the markdown", async function (assert) {
+ stubAllowUpload(this, true);
+
+ const onChange = sinon.spy();
+ this.set("onChange", onChange);
+ this.set("initialValue", "no images here");
+
+ await render(
+ <template>
+ <FormComposer
+ @id="field-1"
+ @value={{this.initialValue}}
+ @onChange={{this.onChange}}
+ />
+ </template>
+ );
+
+ const appEvents = getOwner(this).lookup("service:app-events");
+ appEvents.trigger(
+ "composer:replace-text",
+ "![image|10x10](upload://abc.png)",
+ "![image|10x10, 75%](upload://abc.png)"
+ );
+
+ await settled();
+
+ assert.dom("input[name='field-1']").hasValue("no images here");
+ assert.dom(".d-editor-input").hasValue("no images here");
+ assert.false(onChange.called, "onChange does not fire");
+ });
+ }
+);
diff --git a/frontend/discourse/tests/unit/lib/uppy/composer-upload-test.js b/frontend/discourse/tests/unit/lib/uppy/composer-upload-test.js
new file mode 100644
index 00000000000..23fe07463e1
--- /dev/null
+++ b/frontend/discourse/tests/unit/lib/uppy/composer-upload-test.js
@@ -0,0 +1,139 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import sinon from "sinon";
+import UppyComposerUpload from "discourse/lib/uppy/composer-upload";
+import { createFile } from "discourse/tests/helpers/qunit-helpers";
+
+function fakeComposerModel() {
+ return { privateMessage: false };
+}
+
+function buildUploader(context) {
+ return new UppyComposerUpload(getOwner(context), {
+ composerEventPrefix: "composer",
+ composerModel: fakeComposerModel(),
+ uploadMarkdownResolvers: [],
+ uploadPreProcessors: [],
+ uploadHandlers: [],
+ fileUploadElementId: "file-uploader",
+ });
+}
+
+function buildPasteEvent(target, files) {
+ const event = new Event("paste", { cancelable: true, bubbles: true });
+ Object.defineProperty(event, "target", { value: target });
+ event.clipboardData = {
+ types: ["Files"],
+ files,
+ items: [],
+ getData: () => "",
+ };
+ return event;
+}
+
+module("Unit | Lib | uppy/composer-upload", function (hooks) {
+ setupTest(hooks);
+
+ hooks.beforeEach(function () {
+ this.fileInput = document.createElement("input");
+ this.fileInput.type = "file";
+ this.fileInput.id = "file-uploader";
+ document.body.appendChild(this.fileInput);
+
+ this.formEl = document.createElement("form");
+ this.editorInput = document.createElement("textarea");
+ this.editorInput.className = "d-editor-input";
+ this.formEl.appendChild(this.editorInput);
+ document.body.appendChild(this.formEl);
+ });
+
+ hooks.afterEach(function () {
+ this.fileInput.remove();
+ this.formEl.remove();
+ sinon.restore();
+ });
+
+ test("_pasteEventListener accepts paste from descendants of .d-editor-input", function (assert) {
+ const uploader = buildUploader(this);
+ uploader.uppyWrapper.uppyInstance = { addFiles: sinon.spy() };
+
+ const nested = document.createElement("span");
+ this.editorInput.appendChild(nested);
+
+ const file = createFile("paste.png", "image/png", "data");
+ const event = buildPasteEvent(nested, [file]);
+ uploader._pasteEventListener(event);
+
+ assert.true(
+ event.defaultPrevented,
+ "paste from a nested element inside .d-editor-input is captured"
+ );
+ });
+
+ test("_pasteEventListener accepts paste from a non-first .d-editor-input", function (assert) {
+ const earlierInput = document.createElement("textarea");
+ earlierInput.className = "d-editor-input";
+ document.body.insertBefore(earlierInput, this.formEl);
+
+ try {
+ const uploader = buildUploader(this);
+ uploader.uppyWrapper.uppyInstance = { addFiles: sinon.spy() };
+
+ const file = createFile("paste.png", "image/png", "data");
+ const event = buildPasteEvent(this.editorInput, [file]);
+ uploader._pasteEventListener(event);
+
+ assert.true(
+ event.defaultPrevented,
+ "paste from the second .d-editor-input is captured (closest() walks up from target, not document.querySelector)"
+ );
+ } finally {
+ earlierInput.remove();
+ }
+ });
+
+ test("_pasteEventListener ignores paste outside .d-editor-input", function (assert) {
+ const uploader = buildUploader(this);
+ uploader.uppyWrapper.uppyInstance = { addFiles: sinon.spy() };
+
+ const outside = document.createElement("div");
+ document.body.appendChild(outside);
+
+ const file = createFile("paste.png", "image/png", "data");
+ const event = buildPasteEvent(outside, [file]);
+ uploader._pasteEventListener(event);
+
+ assert.false(
+ event.defaultPrevented,
+ "paste outside .d-editor-input is not captured"
+ );
+
+ outside.remove();
+ });
+
+ test("setup() called twice tears down the first binding", function (assert) {
+ const uploader = buildUploader(this);
+
+ uploader.setup(this.formEl);
+ const firstUppy = uploader.uppyWrapper.uppyInstance;
+ assert.notStrictEqual(
+ firstUppy,
+ null,
+ "first setup creates an Uppy instance"
+ );
+
+ const teardownSpy = sinon.spy(uploader, "teardown");
+
+ uploader.setup(this.formEl);
+ assert.true(teardownSpy.calledOnce, "teardown is called once on re-setup");
+
+ assert.notStrictEqual(
+ firstUppy,
+ uploader.uppyWrapper.uppyInstance,
+ "second setup replaces the Uppy instance"
+ );
+
+ uploader.teardown();
+ });
+});