Add copy YAML (automation & script) fallback without navigator.clipboard (#7900)

This commit is contained in:
Philip Allgaier 2020-12-10 15:55:57 +01:00 committed by GitHub
parent ac0871d0e8
commit f9dece0743
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 8 deletions

View File

@ -1,8 +1,12 @@
export const copyToClipboard = (str) => { export const copyToClipboard = (str) => {
const el = document.createElement("textarea"); if (navigator.clipboard) {
el.value = str; navigator.clipboard.writeText(str);
document.body.appendChild(el); } else {
el.select(); const el = document.createElement("textarea");
document.execCommand("copy"); el.value = str;
document.body.removeChild(el); document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
}
}; };

View File

@ -32,6 +32,7 @@ import "../../../components/ha-svg-icon";
import "../../../components/ha-yaml-editor"; import "../../../components/ha-yaml-editor";
import { showToast } from "../../../util/toast"; import { showToast } from "../../../util/toast";
import type { HaYamlEditor } from "../../../components/ha-yaml-editor"; import type { HaYamlEditor } from "../../../components/ha-yaml-editor";
import { copyToClipboard } from "../../../common/util/copy-clipboard";
import { import {
AutomationConfig, AutomationConfig,
AutomationEntity, AutomationEntity,
@ -396,7 +397,7 @@ export class HaAutomationEditor extends KeyboardShortcutMixin(LitElement) {
private async _copyYaml() { private async _copyYaml() {
if (this._editor?.yaml) { if (this._editor?.yaml) {
navigator.clipboard.writeText(this._editor.yaml); copyToClipboard(this._editor.yaml);
} }
} }

View File

@ -35,6 +35,7 @@ import "../../../components/ha-icon-input";
import "../../../components/ha-svg-icon"; import "../../../components/ha-svg-icon";
import "../../../components/ha-yaml-editor"; import "../../../components/ha-yaml-editor";
import type { HaYamlEditor } from "../../../components/ha-yaml-editor"; import type { HaYamlEditor } from "../../../components/ha-yaml-editor";
import { copyToClipboard } from "../../../common/util/copy-clipboard";
import { import {
Action, Action,
deleteScript, deleteScript,
@ -545,7 +546,7 @@ export class HaScriptEditor extends KeyboardShortcutMixin(LitElement) {
private async _copyYaml() { private async _copyYaml() {
if (this._editor?.yaml) { if (this._editor?.yaml) {
navigator.clipboard.writeText(this._editor.yaml); copyToClipboard(this._editor.yaml);
} }
} }