From dcb9afa5ebbcac73f8b5cea1d6ba1ea73615cf60 Mon Sep 17 00:00:00 2001 From: Janina Hiob Date: Tue, 21 Jul 2026 10:37:53 +0200 Subject: [PATCH] Use insertAttachmentPreview instead of insertAttachment on pasting or droping non-image-files Signed-off-by: Janina Hiob Assisted-by: GPT-5.3-Codex --- cypress/e2e/attachments.spec.js | 19 ++++++++++++++++++- src/components/Editor/MediaHandler.vue | 23 ++++++++++++++--------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/cypress/e2e/attachments.spec.js b/cypress/e2e/attachments.spec.js index 778b6bbc307..9d930536b91 100644 --- a/cypress/e2e/attachments.spec.js +++ b/cypress/e2e/attachments.spec.js @@ -252,6 +252,23 @@ describe('Test all attachment insertion methods', () => { }) it('Upload a local media file (file.txt.gz)', () => { + const checkPreviewAttachment = (documentId, fileName, fileId) => { + cy.log( + 'Check the attachment is visible and well formed', + documentId, + fileName, + ) + + // keep track that we have created this attachment in the attachment dir + if (!attachmentFileNameToId[documentId]) { + attachmentFileNameToId[documentId] = {} + } + + attachmentFileNameToId[documentId][fileName] = fileId + + return cy.get('.text-editor__main .widget-file__link').should('contain', `.attachments.${documentId}`) + } + cy.visit('/apps/files') cy.openFile('test.md') // in this case we almost could just attach the file to the input @@ -263,7 +280,7 @@ describe('Test all attachment insertion methods', () => { attachFile('file.txt.gz', requestAlias) - return waitForRequestAndCheckAttachment(requestAlias, undefined, false) + return waitForRequestAndCheckAttachment(requestAlias, undefined, false, checkPreviewAttachment) }) cy.closeFile() }) diff --git a/src/components/Editor/MediaHandler.vue b/src/components/Editor/MediaHandler.vue index 7fc46091426..af93fd1605c 100644 --- a/src/components/Editor/MediaHandler.vue +++ b/src/components/Editor/MediaHandler.vue @@ -151,13 +151,17 @@ export default { return uploadAttachment(this.connection, file) .then((response) => { - this.insertAttachment( - response.data?.name, - response.data?.id, - file.type, - position, - response.data?.dirname, - ) + if (file.type.startsWith('image/')) { + this.insertAttachment( + response.data?.name, + response.data?.id, + file.type, + position, + response.data?.dirname, + ) + } else { + this.insertAttachmentPreview(response.data?.id, position) + } }) .catch((error) => { logger.error('Uploading attachment failed', { error }) @@ -232,10 +236,11 @@ export default { }) }, - insertAttachmentPreview(fileId) { + insertAttachmentPreview(fileId, position = null) { const url = new URL(generateUrl(`/f/${fileId}`), window.origin) const href = url.href.replaceAll(' ', '%20') - this.editor.chain().focus().insertPreview(href).run() + const chain = position ? this.editor.chain().focus(position) : this.editor.chain().focus() + chain.insertPreview(href).run() }, insertAttachment(name, fileId, mimeType, position = null, dirname = '') {