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
4 changes: 2 additions & 2 deletions src/panels/config/js/script/call_service.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { h, Component } from "preact";
import "../../../../components/ha-service-picker";

import JSONTextArea from "../json_textarea";
import YAMLTextArea from "../yaml_textarea";

export default class CallServiceAction extends Component<any> {
constructor() {
Expand Down Expand Up @@ -32,7 +32,7 @@ export default class CallServiceAction extends Component<any> {
value={service}
onChange={this.serviceChanged}
/>
<JSONTextArea
<YAMLTextArea
label={localize(
"ui.panel.config.automation.editor.actions.type.service.service_data"
)}
Expand Down
4 changes: 2 additions & 2 deletions src/panels/config/js/script/event.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { h, Component } from "preact";
import "@polymer/paper-input/paper-input";

import JSONTextArea from "../json_textarea";
import YAMLTextArea from "../yaml_textarea";
import { onChangeEvent } from "../../../../common/preact/event";
import { LocalizeFunc } from "../../../../common/translations/localize";
import { EventAction } from "../../../../data/script";
Expand Down Expand Up @@ -45,7 +45,7 @@ export default class EventActionForm extends Component<Props> {
value={event}
onvalue-changed={this.onChange}
/>
<JSONTextArea
<YAMLTextArea
label={localize(
"ui.panel.config.automation.editor.actions.type.event.service_data"
)}
Expand Down
4 changes: 2 additions & 2 deletions src/panels/config/js/trigger/event.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { h, Component } from "preact";
import "@polymer/paper-input/paper-input";

import JSONTextArea from "../json_textarea";
import YAMLTextArea from "../yaml_textarea";
import { onChangeEvent } from "../../../../common/preact/event";

export default class EventTrigger extends Component<any> {
Expand Down Expand Up @@ -34,7 +34,7 @@ export default class EventTrigger extends Component<any> {
value={event_type}
onvalue-changed={this.onChange}
/>
<JSONTextArea
<YAMLTextArea
label={localize(
"ui.panel.config.automation.editor.triggers.type.event.event_data"
)}
Expand Down
80 changes: 80 additions & 0 deletions src/panels/config/js/yaml_textarea.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { h, Component } from "preact";
import yaml from "js-yaml";
import "../../../components/ha-textarea";

const isEmpty = (obj: object) => {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
return false;
}
}
return true;
};

export default class YAMLTextArea extends Component<any, any> {
constructor(props) {
super(props);

let value: string | undefined;
try {
value =
props.value && !isEmpty(props.value)
? yaml.safeDump(props.value)
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.

What if this raises ?

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.

yaml.safeDump(() => {})

: undefined;
} catch (err) {
alert(`There was an error converting to YAML: ${err}`);
}

this.state = {
isvalid: true,
value,
};

this.onChange = this.onChange.bind(this);
}

public onChange(ev) {
const value = ev.target.value;
let parsed;
let isValid = true;

if (value) {
try {
parsed = yaml.safeLoad(value);
isValid = true;
} catch (err) {
// Invalid YAML
isValid = false;
}
} else {
parsed = {};
}

this.setState({
value,
isValid,
});
if (isValid) {
this.props.onChange(parsed);
}
}

public render({ label }, { value, isValid }) {
const style: any = {
minWidth: 300,
width: "100%",
};
if (!isValid) {
style.border = "1px solid red";
}
return (
<ha-textarea
label={label}
value={value}
style={style}
onvalue-changed={this.onChange}
dir="ltr"
/>
);
}
}