diff --git a/addons/website/static/src/js/widgets/link_popover_widget.js b/addons/website/static/src/js/widgets/link_popover_widget.js index e39e3857fbb..ebac893de25 100644 --- a/addons/website/static/src/js/widgets/link_popover_widget.js +++ b/addons/website/static/src/js/widgets/link_popover_widget.js @@ -2,7 +2,12 @@ import weWidgets from 'wysiwyg.widgets'; import {_t} from 'web.core'; +import { browser } from "@web/core/browser/browser"; + weWidgets.LinkPopoverWidget.include({ + events: Object.assign({}, weWidgets.LinkPopoverWidget.prototype.events, { + 'click .o_we_full_url, .o_we_url_link': '_onPreviewLinkClick', + }), /** * @override */ @@ -19,6 +24,27 @@ weWidgets.LinkPopoverWidget.include({ return this._super(...arguments); }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * Opens website page links in backend mode by forcing the '/@/' controller. + * + * @private + * @param {Event} ev + */ + async _onPreviewLinkClick(ev) { + if (this.target.href) { + const currentUrl = new URL(this.target.href); + if (window.location.hostname === currentUrl.hostname && !currentUrl.pathname.startsWith('/@/')) { + ev.preventDefault(); + currentUrl.pathname = `/@${currentUrl.pathname}`; + browser.open(currentUrl); + } + } + } }); const NavbarLinkPopoverWidget = weWidgets.LinkPopoverWidget.extend({ diff --git a/addons/website/static/tests/tours/edit_link_popover.js b/addons/website/static/tests/tours/edit_link_popover.js index a14f5950933..e56ef33ad47 100644 --- a/addons/website/static/tests/tours/edit_link_popover.js +++ b/addons/website/static/tests/tours/edit_link_popover.js @@ -1,6 +1,8 @@ /** @odoo-module **/ import wTourUtils from "website.tour_utils"; +import { browser } from "@web/core/browser/browser"; +import { patch } from "@web/core/utils/patch"; const FIRST_PARAGRAPH = 'iframe #wrap .s_text_image p:nth-child(2)'; @@ -184,20 +186,24 @@ wTourUtils.registerWebsitePreviewTour('edit_link_popover', { run: function () {}, // it's a check }, { - content: "Ensure that a click on the link popover link opens a new window", + content: "Ensure that a click on the link popover link opens a new window in edit mode", trigger: 'iframe .o_edit_menu_popover a.o_we_url_link[target="_blank"]', extra_trigger: 'iframe .o_edit_menu_popover a.o_we_full_url[target="_blank"]', - run: function (actions) { - this.$anchor[0].addEventListener('click', (ev) => { - const originalPreventDefault = ev.preventDefault.bind(ev); - ev.preventDefault = () => { - throw new Error( - "The link popover should not be default prevented as to open a new tab" - ); - }; - originalPreventDefault(); // We do not want to open a new tab in a tour - }, {once: true, capture: true}); + run: (actions) => { + // We do not want to open a link in a tour + patch(browser, 'window_open_action', { + open: (url) => { + if (window.location.hostname === url.hostname && url.pathname.startsWith('/@/')) { + document.querySelector('body').classList.add('new_backend_window_opened'); + } + } + }, { pure: true }); actions.click(); }, }, + { + content: "Ensure that link is opened correctly in edit mode", + trigger: '.new_backend_window_opened', + run: () => {}, // it's a check + }, ]);