From 71243d62467fbdd78c2d46be5cfe050032ba8658 Mon Sep 17 00:00:00 2001
From: dhba-odoo <dhba@odoo.com>
Date: Tue, 16 May 2023 15:06:10 +0530
Subject: [PATCH] [FIX] web_editor: shift paste of vs code content looses
 indentation

Before this commit:

when performing a shift paste of copied VS Code content, the indentation was
lost.

After this commit:

Now, when you perform a shift paste of copied VS Code content, the indentation
is preserved

Task-2884491

closes odoo/odoo#122828

Signed-off-by: David Monjoie (dmo) <dmo@odoo.com>
---
 .../static/lib/odoo-editor/src/OdooEditor.js          | 11 ++++++++++-
 .../lib/odoo-editor/test/spec/copyPaste.test.js       |  4 ++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js
index e895cae603e4..d8b79ecc4cae 100644
--- a/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js
+++ b/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js
@@ -3407,7 +3407,16 @@ export class OdooEditor extends EventTarget {
                         const textFragments = splitAroundUrl[i].split(/\r?\n/);
                         let textIndex = 1;
                         for (const textFragment of textFragments) {
-                            this._applyCommand('insertText', textFragment);
+                            // Replace consecutive spaces by alternating nbsp.
+                            const modifiedTextFragment = textFragment.replace(/( {2,})/g, match => {
+                                let alertnateValue = false;
+                                return match.replace(/ /g, () => {
+                                    alertnateValue = !alertnateValue;
+                                    const replaceContent = alertnateValue ? '\u00A0' : ' ';
+                                    return replaceContent;
+                                });
+                            });
+                            this._applyCommand('insertText', modifiedTextFragment);
                             if (textIndex < textFragments.length) {
                                 this._applyCommand('oShiftEnter');
                             }
diff --git a/addons/web_editor/static/lib/odoo-editor/test/spec/copyPaste.test.js b/addons/web_editor/static/lib/odoo-editor/test/spec/copyPaste.test.js
index d9565b37a215..8c58d7c612c7 100644
--- a/addons/web_editor/static/lib/odoo-editor/test/spec/copyPaste.test.js
+++ b/addons/web_editor/static/lib/odoo-editor/test/spec/copyPaste.test.js
@@ -132,7 +132,7 @@ describe('Copy and paste', () => {
                     stepFunction: async editor => {
                         await pasteText(editor, 'x    y');
                     },
-                    contentAfter: '<p>abx    y[]cd</p>',
+                    contentAfter: '<p>abx&nbsp; &nbsp; y[]cd</p>',
                 });
             });
             it('should paste a text in a span', async () => {
@@ -186,7 +186,7 @@ describe('Copy and paste', () => {
                     stepFunction: async editor => {
                         await pasteText(editor, 'x    y');
                     },
-                    contentAfter: '<p>ax    y[]d</p>',
+                    contentAfter: '<p>ax&nbsp; &nbsp; y[]d</p>',
                 });
             });
             it('should paste a text in a span', async () => {
-- 
GitLab