From 7411fc970b8ff73eddf32aece18e7ff9c10abc1b Mon Sep 17 00:00:00 2001
From: Antoine Guenet <age@odoo.com>
Date: Fri, 25 Feb 2022 08:41:50 +0000
Subject: [PATCH] [FIX] web_editor: use styling commands on shortcut too

When pressing the italic or underline buttons, we pass through a command
in the editor. Their corresponding keyboard shortcuts however were not
intercepted by the editor and were therefore triggering the default
browser behavior. As a result we had different behaviors when using the
buttons and the shortcuts.

X-original-commit: 2e3c3b3a15fa6ff4fa209fc7dbac10864f8898a2
Part-of: odoo/odoo#86930
---
 .../static/lib/odoo-editor/src/OdooEditor.js         | 12 ++++++++++++
 1 file changed, 12 insertions(+)

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 d8ebf330aa0b..8d355df69262 100644
--- a/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js
+++ b/addons/web_editor/static/lib/odoo-editor/src/OdooEditor.js
@@ -70,6 +70,8 @@ const KEYBOARD_TYPES = { VIRTUAL: 'VIRTUAL', PHYSICAL: 'PHYSICAL', UNKNOWN: 'UKN
 const IS_KEYBOARD_EVENT_UNDO = ev => ev.key === 'z' && (ev.ctrlKey || ev.metaKey);
 const IS_KEYBOARD_EVENT_REDO = ev => ev.key === 'y' && (ev.ctrlKey || ev.metaKey);
 const IS_KEYBOARD_EVENT_BOLD = ev => ev.key === 'b' && (ev.ctrlKey || ev.metaKey);
+const IS_KEYBOARD_EVENT_ITALIC = ev => ev.key === 'i' && (ev.ctrlKey || ev.metaKey);
+const IS_KEYBOARD_EVENT_UNDERLINE = ev => ev.key === 'u' && (ev.ctrlKey || ev.metaKey);
 const IS_KEYBOARD_EVENT_STRIKETHROUGH = ev => ev.key === '5' && (ev.ctrlKey || ev.metaKey);
 
 const CLIPBOARD_BLACKLISTS = {
@@ -2362,6 +2364,16 @@ export class OdooEditor extends EventTarget {
             ev.preventDefault();
             ev.stopPropagation();
             this.execCommand('bold');
+        } else if (IS_KEYBOARD_EVENT_ITALIC(ev)) {
+            // Ctrl-I
+            ev.preventDefault();
+            ev.stopPropagation();
+            this.execCommand('italic');
+        } else if (IS_KEYBOARD_EVENT_UNDERLINE(ev)) {
+            // Ctrl-U
+            ev.preventDefault();
+            ev.stopPropagation();
+            this.execCommand('underline');
         } else if (IS_KEYBOARD_EVENT_STRIKETHROUGH(ev)) {
             // Ctrl-5 / Ctrl-shift-(
             ev.preventDefault();
-- 
GitLab