From 7c0d82a3d967b35d0f0afceec77feec6aa8cbcd3 Mon Sep 17 00:00:00 2001 From: Stanislas Gueniffey Date: Tue, 21 Mar 2023 10:05:52 +0000 Subject: [PATCH] [IMP] web_editor: allow pasting HTML tables Previously, when a clipboardData contained both files and html content, we prioritised files. This is correct when the original content is an image (in which case the clipboard html may contain an image tag or be empty, depending on the source). This is however incorrect in at least one known case: copying a table from a spreadsheet app, as those usually generate an image of the table alongside the actual html table. In that case, we would rather keep the table. This commit differentiates the specific case where the HTML represents a table from other cases. In the former, priority is now given to the HTML content. In the latter, we keep the files as we did before. task-3165757 closes odoo/odoo#119862 X-original-commit: 021f2f028b382fe15ca75928d1b30a4eb48322df Signed-off-by: David Monjoie (dmo) --- .../js/editor/odoo-editor/src/OdooEditor.js | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js index 4a3289150b2..9adf0d6aee9 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js @@ -4378,18 +4378,25 @@ export class OdooEditor extends EventTarget { if (fragment.hasChildNodes()) { this._applyCommand('insert', fragment); } - } else if (files.length && targetSupportsHtmlContent) { - this.addImagesFiles(files).then(html => { - const imageNodes = this._applyCommand('insert', this._prepareClipboardData(html)); - if (imageNodes && this.options.dropImageAsAttachment) { - // Mark images as having to be saved as attachments. - for (const imageNode of imageNodes) { - imageNode.classList.add('o_b64_image_to_save'); + } else if ((files.length || clipboardHtml) && targetSupportsHtmlContent) { + const clipboardElem = this._prepareClipboardData(clipboardHtml); + // When copy pasting a table from the outside, a picture of the + // table can be included in the clipboard as an image file. In that + // particular case the html table is given a higher priority than + // the clipboard picture. + if (files.length && !clipboardElem.querySelector('table')) { + this.addImagesFiles(files).then(html => { + const imageNodes = this._applyCommand('insert', this._prepareClipboardData(html)); + if (imageNodes && this.options.dropImageAsAttachment) { + // Mark images as having to be saved as attachments. + for (const imageNode of imageNodes) { + imageNode.classList.add('o_b64_image_to_save'); + } } - } - }); - } else if (clipboardHtml && targetSupportsHtmlContent) { - this._applyCommand('insert', this._prepareClipboardData(clipboardHtml)); + }); + } else { + this._applyCommand('insert', clipboardElem); + } } else { const text = ev.clipboardData.getData('text/plain'); const selectionIsInsideALink = !!closestElement(sel.anchorNode, 'a');