[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) <dmo@odoo.com>
This commit is contained in:
Sébastien Geelen (sge)
2021-04-06 12:45:41 +00:00
parent 1b0004d29a
commit 158d0c78fa
@@ -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) {