From 82f79977712c65927a6337ef56f7a91ad969a3a9 Mon Sep 17 00:00:00 2001 From: maxgfr <25312957+maxgfr@users.noreply.github.com> Date: Thu, 19 Oct 2023 18:13:50 +0200 Subject: [PATCH] fix: copy --- .../src/web-components/tooltip.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/code-du-travail-frontend/src/web-components/tooltip.js b/packages/code-du-travail-frontend/src/web-components/tooltip.js index e60eb4370d..d20a52a30f 100644 --- a/packages/code-du-travail-frontend/src/web-components/tooltip.js +++ b/packages/code-du-travail-frontend/src/web-components/tooltip.js @@ -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 {