Fix copy encryption key (#23515)

This commit is contained in:
Bram Kragten 2024-12-30 17:42:31 +01:00
parent d0123b2cce
commit f3705a7e1d
3 changed files with 17 additions and 6 deletions

View File

@ -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);
};

View File

@ -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"),
});

View File

@ -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"),
});