From a45c2b5d41961367c2e7dbabe42e7cf1bd527d43 Mon Sep 17 00:00:00 2001 From: Hiral Bhavsar Date: Tue, 24 Jan 2023 12:33:41 +0000 Subject: [PATCH] [FIX] web_editor: cursor position wrong after link in safari Before this commit: For the safari browser, when typing any URL and pressing space, and continuing typing, the cursor jumps before the URL. After this commit: Inserting an element into a range clears the selection in Safari. Hence, use the cloned range to reselect it and now the cursor position remains at the end of the URL. Task-3089091 closes odoo/odoo#114192 X-original-commit: fa532d1391aed313a9ea13ac8a4ab9df31c313e3 Signed-off-by: David Monjoie (dmo) --- .../static/src/js/editor/odoo-editor/src/OdooEditor.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js index 5149097a760..7d772d10a82 100644 --- a/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js +++ b/addons/web_editor/static/src/js/editor/odoo-editor/src/OdooEditor.js @@ -4204,6 +4204,9 @@ export class OdooEditor extends EventTarget { * @param {int} length */ _createLinkWithUrlInTextNode(textNode, url, index, length) { + const selection = this.document.getSelection(); + const cloneRange = selection.getRangeAt(0).cloneRange(); + const link = this.document.createElement('a'); link.setAttribute('href', url); for (const [param, value] of Object.entries(this.options.defaultLinkAttributes)) { @@ -4214,6 +4217,10 @@ export class OdooEditor extends EventTarget { range.setEnd(textNode, index + length); link.appendChild(range.extractContents()); range.insertNode(link); + // Inserting an element into a range clears the selection in Safari + // Hence, use the cloned range to reselect it. + selection.removeAllRanges(); + selection.addRange(cloneRange); } /**