[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) <dmo@odoo.com>
This commit is contained in:
Antoine Guenet
2022-11-07 13:50:01 +01:00
parent 292102bac1
commit b8290bcdd9
@@ -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());
}
});
}