Skip to content
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
16 changes: 10 additions & 6 deletions src/common/util/copy-clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export const copyToClipboard = (str) => {
const el = document.createElement("textarea");
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
if (navigator.clipboard) {
navigator.clipboard.writeText(str);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns a promise, we should await it and catch any errors so we can fallback

} else {
const el = document.createElement("textarea");
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
}
};
3 changes: 2 additions & 1 deletion src/panels/config/automation/ha-automation-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import "../../../components/ha-svg-icon";
import "../../../components/ha-yaml-editor";
import { showToast } from "../../../util/toast";
import type { HaYamlEditor } from "../../../components/ha-yaml-editor";
import { copyToClipboard } from "../../../common/util/copy-clipboard";
import {
AutomationConfig,
AutomationEntity,
Expand Down Expand Up @@ -396,7 +397,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {

private async _copyYaml() {
if (this._editor?.yaml) {
navigator.clipboard.writeText(this._editor.yaml);
copyToClipboard(this._editor.yaml);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/panels/config/script/ha-script-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import "../../../components/ha-icon-input";
import "../../../components/ha-svg-icon";
import "../../../components/ha-yaml-editor";
import type { HaYamlEditor } from "../../../components/ha-yaml-editor";
import { copyToClipboard } from "../../../common/util/copy-clipboard";
import {
Action,
deleteScript,
Expand Down Expand Up @@ -545,7 +546,7 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {

private async _copyYaml() {
if (this._editor?.yaml) {
navigator.clipboard.writeText(this._editor.yaml);
copyToClipboard(this._editor.yaml);
}
}

Expand Down