From 158d0c78fa524692a482e83e9c5eb20264582aa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Geelen=20=28sge=29?= Date: Tue, 6 Apr 2021 11:39:17 +0000 Subject: [PATCH] [FIX] web_editor: override CTRL+A select all to avoid traceback Odoo Task : https://www.odoo.com/web#id=2497783&action=333&active_id=1695&model=project.task&view_type=form&cids=1&menu_id=4720 Bug report from LNA: > In Google Slides, when you press ctrl+a, it selects the content of your container (ex : your shape or title or whatever) which is pretty useful to quickly replace big blobs of text whereas here, it takes the entire email and if I press suppr => traceback https://pastebin.com/KnCzV3Hc https://tinyurl.com/yzen23cc And my mailing is then empty and completely broken https://tinyurl.com/yhhu9zj6 I can't even drop so it's just over since I cannot click on "cancel" https://tinyurl.com/yef5c55s closes odoo/odoo#68819 Signed-off-by: David Monjoie (dmo) --- .../static/src/js/wysiwyg/wysiwyg.js | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js index ff7b4b7345f..acd4f21750f 100644 --- a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js +++ b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js @@ -142,6 +142,7 @@ const Wysiwyg = Widget.extend({ $(this.odooEditor.editable).on('click', this._updateEditorUI.bind(this)); $(this.odooEditor.editable).on('keydown', this._updateEditorUI.bind(this)); + $(this.odooEditor.editable).on('keydown', this._handleShortcuts.bind(this)); // Ensure the Toolbar always have the correct layout in note. this._updateEditorUI(); @@ -848,6 +849,31 @@ const Wysiwyg = Widget.extend({ return rgbToHex(rgbColor); } }, + /** + * Handle custom keyboard shortcuts. + */ + _handleShortcuts: function (e) { + // Open the link modal / tool when CTRL+K is pressed. + if (e && e.key === 'k' && (e.ctrlKey || e.metaKey)) { + e.preventDefault(); + this.toggleLinkTools(); + } + // Override selectAll (CTRL+A) to restrict it to the editable zone / current snippet and prevent traceback. + if (e && e.key === 'a' && (e.ctrlKey || e.metaKey)) { + e.preventDefault(); + const selection = this.odooEditor.document.getSelection(); + const deepestParent = + selection ? + $(selection.anchorNode).parentsUntil('#wrap>*, [contenteditable], .oe_structure>*').last() : + []; + if(deepestParent.length) { + const range = document.createRange(); + range.selectNodeContents(deepestParent.parent()[0]); + selection.removeAllRanges(); + selection.addRange(range); + } + } + }, /** * Update any editor UI that is not handled by the editor itself. */ @@ -908,11 +934,6 @@ const Wysiwyg = Widget.extend({ this.toolbar.$el.find('.only_fa').toggleClass('d-none', !$target.is('.fa')); // Toggle the toolbar arrow. this.toolbar.$el.toggleClass('noarrow', isInMedia); - // open the link modal / tool when CTRL+K is pressed - if (e && e.key === 'k' && (e.ctrlKey || e.metaKey)) { - e.preventDefault(); - this.toggleLinkTools(); - } // Unselect all media. this.$editable.find('.o_we_selected_image').removeClass('o_we_selected_image'); if (isInMedia) {