From b8290bcdd933b5408bb8e82c362cadc8eb0d8d4c Mon Sep 17 00:00:00 2001 From: Antoine Guenet Date: Mon, 31 Oct 2022 15:01:21 +0000 Subject: [PATCH] [FIX] web_editor: prevent wrong selection after discard link dialog When discarding the link dialog, we need to restore the selection in the editable area. This failed because the selection was recomputed when the dialog was actually removed from the DOM. This is the same case as a couple of lines above the restoration of the selection, where we need to wait for the element to be removed before triggering a focus on the inserted link. task-3000250 closes odoo/odoo#104847 X-original-commit: 6b86f6c91fc69bec59f39377e322d79242466d42 Signed-off-by: David Monjoie (dmo) --- .../web_editor/static/src/js/wysiwyg/wysiwyg.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js index bfce8c259bc..346e24b61a5 100644 --- a/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js +++ b/addons/web_editor/static/src/js/wysiwyg/wysiwyg.js @@ -1218,17 +1218,24 @@ const Wysiwyg = Widget.extend({ // Focus the link after the dialog element is removed because // if the dialog element is still in the DOM at the time of // doing link.focus(), because there is the attribute tabindex - // on the dialog element, the focus cannot occurs. + // on the dialog element, the focus cannot occur. // Using a microtask to set the focus is hackish and might break - // if another microtask wich focus an elemen in the dom occurs - // at the same time (but this case seems unlikely). + // if another microtask which focuses an element in the dom + // occurs at the same time (but this case seems unlikely). Promise.resolve().then(() => link.focus()); }); linkDialog.on('closed', this, function () { // If the linkDialog content has been saved // the previous selection in not relevant anymore. if (linkDialog.destroyAction !== 'save') { - restoreSelection(); + // Restore the selection after the dialog element isremoved + // because if the dialog element is still in the DOM at the + // time of doing restoreSelection(), it will trigger a new + // selection change which will undo this one. Using a + // microtask to set the focus is hackish and might break if + // another microtask which changes the selection in the dom + // occurs at the same time (but this case seems unlikely). + Promise.resolve().then(() => restoreSelection()); } }); }