From 66d26585c85a685e9dbef900b98a47e487b03bd8 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Mon, 30 Dec 2024 17:27:43 +0100 Subject: [PATCH] Fix copy encryption key --- src/common/util/copy-clipboard.ts | 8 +++++--- .../config/backup/dialogs/dialog-backup-onboarding.ts | 5 ++++- .../dialogs/dialog-change-backup-encryption-key.ts | 10 ++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/common/util/copy-clipboard.ts b/src/common/util/copy-clipboard.ts index 1708858c85ac..c50ad0241834 100644 --- a/src/common/util/copy-clipboard.ts +++ b/src/common/util/copy-clipboard.ts @@ -1,4 +1,4 @@ -export const copyToClipboard = async (str) => { +export const copyToClipboard = async (str, rootEl?: HTMLElement) => { if (navigator.clipboard) { try { await navigator.clipboard.writeText(str); @@ -8,10 +8,12 @@ export const copyToClipboard = async (str) => { } } + const root = rootEl ?? document.body; + const el = document.createElement("textarea"); el.value = str; - document.body.appendChild(el); + root.appendChild(el); el.select(); document.execCommand("copy"); - document.body.removeChild(el); + root.removeChild(el); }; diff --git a/src/panels/config/backup/dialogs/dialog-backup-onboarding.ts b/src/panels/config/backup/dialogs/dialog-backup-onboarding.ts index 7719ebd066fe..cb5b798c50fa 100644 --- a/src/panels/config/backup/dialogs/dialog-backup-onboarding.ts +++ b/src/panels/config/backup/dialogs/dialog-backup-onboarding.ts @@ -396,7 +396,10 @@ class DialogBackupOnboarding extends LitElement implements HassDialog { } private async _copyKeyToClipboard() { - await copyToClipboard(this._config!.create_backup.password!); + await copyToClipboard( + this._config!.create_backup.password!, + this.renderRoot.querySelector("div")! + ); showToast(this, { message: this.hass.localize("ui.common.copied_clipboard"), }); diff --git a/src/panels/config/backup/dialogs/dialog-change-backup-encryption-key.ts b/src/panels/config/backup/dialogs/dialog-change-backup-encryption-key.ts index 1b500b34f925..3e140c60f72d 100644 --- a/src/panels/config/backup/dialogs/dialog-change-backup-encryption-key.ts +++ b/src/panels/config/backup/dialogs/dialog-change-backup-encryption-key.ts @@ -206,7 +206,10 @@ class DialogChangeBackupEncryptionKey extends LitElement implements HassDialog { } private async _copyKeyToClipboard() { - await copyToClipboard(this._newEncryptionKey); + await copyToClipboard( + this._newEncryptionKey, + this.renderRoot.querySelector("div")! + ); showToast(this, { message: this.hass.localize("ui.common.copied_clipboard"), }); @@ -216,7 +219,10 @@ class DialogChangeBackupEncryptionKey extends LitElement implements HassDialog { if (!this._params?.currentKey) { return; } - await copyToClipboard(this._params.currentKey); + await copyToClipboard( + this._params.currentKey, + this.renderRoot.querySelector("div")! + ); showToast(this, { message: this.hass.localize("ui.common.copied_clipboard"), });