Skip to content

[FIX] html_editor: link popover not hidden on clicking other element #4762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master-mysterious-egg-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions addons/html_editor/static/src/main/link/link_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ export class LinkPlugin extends Plugin {
}
}
});
// Some elements can be clicked on without the selection being changed
// In those cases the overlay closing and opening must be done manually
this.addDomListener(this.editable, "click", (ev) => {
Comment on lines +276 to +278

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, I think this is not the right place to fix the issue. The root cause is that when clicking some elements, the selection is not changed. Most likely, they prevented default on these elements. This influences all the selection change handlers.

For example, with the current fix, when you select text from Home button and the tool bar will popup, then you click the Administrator drop down, the toolbar stays open.

IMO, we should check why the selection change is prevented and make the fix there 🤔

Also we already have a click event listener, so if you still need to do the changes at click, you can probably move the code there :)

const currentSelection = this.document.getSelection();
if (!ev.target.contains(currentSelection.anchorNode)) {
this.currentOverlay.close();
this.linkInDocument = null;
} else if (
this.lastSelectionData &&
this.lastSelectionData.documentSelection.anchorNode ===
currentSelection.anchorNode &&
this.lastSelectionData.documentSelection.anchorOffset ===
currentSelection.anchorOffset
) {
this.handleSelectionChange(this.lastSelectionData);
}
});
// link creation is added to the command service because of a shortcut conflict,
// as ctrl+k is used for invoking the command palette
this.unregisterLinkCommandCallback = this.services.command.add(
Expand Down Expand Up @@ -643,6 +660,7 @@ export class LinkPlugin extends Plugin {
}

handleSelectionChange(selectionData) {
this.lastSelectionData = selectionData;
const selection = selectionData.editableSelection;
if (
this._isNavigatingByMouse &&
Expand Down
11 changes: 11 additions & 0 deletions addons/html_editor/static/tests/link/popover.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ describe("should open a popover", () => {
});
});

describe("should close a popover", () => {
test("clicking an element that doesn't contain an editable link should not leave a previous popover open", async () => {
await setupEditor('<p>this is a <a href="#">li[]nk</a></p><div id="outside_div"></div>');
await waitFor(".o-we-linkpopover");
// click outside the link without modifying the selection
await click("div#outside_div");
await waitForNone(".o-we-linkpopover");
expect(".o-we-linkpopover").toHaveCount(0);
});
});

describe("popover should switch UI depending on editing state", () => {
test("after clicking on edit button, the popover should switch to editing mode", async () => {
await setupEditor('<p>this is a <a href="http://test.com/">li[]nk</a></p>');
Expand Down