Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copy encryption key #23515

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading