Skip to content

Commit

Permalink
Fix copy encryption key
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten committed Dec 30, 2024
1 parent 6b039a1 commit 66d2658
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
8 changes: 5 additions & 3 deletions src/common/util/copy-clipboard.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
};
5 changes: 4 additions & 1 deletion src/panels/config/backup/dialogs/dialog-backup-onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
});
Expand All @@ -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"),
});
Expand Down

0 comments on commit 66d2658

Please sign in to comment.