Skip to content

Commit

Permalink
fix(tooltip): remove breaking space when copy to editor (#5400)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgfr authored Oct 20, 2023
1 parent 43e8ad3 commit d4d468b
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/code-du-travail-frontend/src/web-components/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ class WebComponentsTooltip extends LitElement {
this.visible = false;
}

connectedCallback() {
super.connectedCallback();
window.addEventListener("copy", this._handleCopy);
}
disconnectedCallback() {
window.removeEventListener("copy", this._handleCopy);
super.disconnectedCallback();
}

_handleCopy = (e) => {
const selection = document.getSelection();
const text = selection.toString();
const fragmentElt = document.createElement("div");
fragmentElt.appendChild(selection.getRangeAt(0).cloneContents());
fragmentElt.setAttribute("style", "white-space: pre-wrap;");
e.clipboardData.setData("text/plain", text);
e.clipboardData.setData("text/html", fragmentElt.outerHTML);
e.preventDefault();
};

static get styles() {
return css`
:host {
Expand Down

0 comments on commit d4d468b

Please sign in to comment.