From 76e1e44bd7b9d88558493475c8a151a4df0ccec1 Mon Sep 17 00:00:00 2001
From: Antoine Guenet <age@odoo.com>
Date: Tue, 29 Nov 2022 11:12:51 +0000
Subject: [PATCH] [FIX] web_editor: properly apply alignment to links

When the selection was in a link, the alignment buttons didn't work.
That is because they are supposed to align the parent paragraph, which
is temporarily in a contenteditable=false context when the selection is
in a link. This restores the context before applying an editor command,
and restores it afterwards.

task-3083748

X-original-commit: 0a4eb266b198e5e02a3325d063d07c0dd8af0148
Part-of: odoo/odoo#107150
---
 .../src/js/editor/odoo-editor/src/OdooEditor.js   | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

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 2611e6a6aa1f..b3a63a41ab61 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
@@ -1674,6 +1674,7 @@ export class OdooEditor extends EventTarget {
         this._stopContenteditable();
 
         this._fixLinkMutatedElements = {
+            link,
             wasContenteditableTrue: [...editableChildren],
             wasContenteditableFalse: [],
             wasContenteditableNull: [],
@@ -2074,7 +2075,19 @@ export class OdooEditor extends EventTarget {
             }
         }
         if (editorCommands[method]) {
-            return editorCommands[method](this, ...args);
+            // Make sure to restore the content editable before applying an
+            // editor command, as it might have been temporarily disabled for
+            // browser behaviors which should not concern editor commands.
+            const link = this._fixLinkMutatedElements && this._fixLinkMutatedElements.link;
+            if (this._fixLinkMutatedElements) {
+                this.resetContenteditableLink();
+                this._activateContenteditable();
+            }
+            const returnValue = editorCommands[method](this, ...args);
+            if (link) {
+                this.setContenteditableLink(link);
+            }
+            return returnValue;
         }
         if (method.startsWith('justify')) {
             const mode = method.split('justify').join('').toLocaleLowerCase();
-- 
GitLab