mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-02 14:07:55 +00:00
Check config on save (#2346)
* Check config on save Untested... * views is list of objects, resources not required * Lint * Only warn on "# " to prevent warnings on colors Could miss some comments... * Improve # check Warn on # at the start of a line or when followed by white space * Faster * Multiline * Check if # is added * Also handle paste and just show the warning on every #
This commit is contained in:
parent
25c788871f
commit
88d23eb9dd
@ -10,6 +10,7 @@ import "@polymer/paper-button/paper-button";
|
|||||||
import "@polymer/paper-icon-button/paper-icon-button";
|
import "@polymer/paper-icon-button/paper-icon-button";
|
||||||
import "@polymer/paper-spinner/paper-spinner";
|
import "@polymer/paper-spinner/paper-spinner";
|
||||||
|
|
||||||
|
import { struct } from "./common/structs/struct";
|
||||||
import { Lovelace } from "./types";
|
import { Lovelace } from "./types";
|
||||||
import { hassLocalizeLitMixin } from "../../mixins/lit-localize-mixin";
|
import { hassLocalizeLitMixin } from "../../mixins/lit-localize-mixin";
|
||||||
|
|
||||||
@ -17,18 +18,27 @@ import "../../components/ha-icon";
|
|||||||
|
|
||||||
const TAB_INSERT = " ";
|
const TAB_INSERT = " ";
|
||||||
|
|
||||||
|
const lovelaceStruct = struct.partial({
|
||||||
|
title: "string?",
|
||||||
|
views: ["object"],
|
||||||
|
});
|
||||||
|
|
||||||
class LovelaceFullConfigEditor extends hassLocalizeLitMixin(LitElement) {
|
class LovelaceFullConfigEditor extends hassLocalizeLitMixin(LitElement) {
|
||||||
public lovelace?: Lovelace;
|
public lovelace?: Lovelace;
|
||||||
public closeEditor?: () => void;
|
public closeEditor?: () => void;
|
||||||
private _haStyle?: DocumentFragment;
|
private _haStyle?: DocumentFragment;
|
||||||
private _saving?: boolean;
|
private _saving?: boolean;
|
||||||
private _changed?: boolean;
|
private _changed?: boolean;
|
||||||
|
private _hashAdded?: boolean;
|
||||||
|
private _hash?: boolean;
|
||||||
|
|
||||||
static get properties() {
|
static get properties() {
|
||||||
return {
|
return {
|
||||||
lovelace: {},
|
lovelace: {},
|
||||||
_saving: {},
|
_saving: {},
|
||||||
_changed: {},
|
_changed: {},
|
||||||
|
_hashAdded: {},
|
||||||
|
_hash: {},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,13 +50,26 @@ class LovelaceFullConfigEditor extends hassLocalizeLitMixin(LitElement) {
|
|||||||
<app-toolbar>
|
<app-toolbar>
|
||||||
<paper-icon-button
|
<paper-icon-button
|
||||||
icon="hass:close"
|
icon="hass:close"
|
||||||
@click="${this.closeEditor}"
|
@click="${this._closeEditor}"
|
||||||
></paper-icon-button>
|
></paper-icon-button>
|
||||||
<div main-title>Edit Config</div>
|
<div main-title>Edit Config</div>
|
||||||
|
${
|
||||||
|
this._hash
|
||||||
|
? html`
|
||||||
|
<span class="comments">Comments will be not be saved!</span>
|
||||||
|
`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
<paper-button @click="${this._handleSave}">Save</paper-button>
|
<paper-button @click="${this._handleSave}">Save</paper-button>
|
||||||
<ha-icon class="save-button ${classMap({
|
<ha-icon
|
||||||
saved: this._saving! === false && !this._changed!,
|
class="save-button
|
||||||
})}" icon="hass:check">
|
${
|
||||||
|
classMap({
|
||||||
|
saved: this._saving! === false || this._changed === true,
|
||||||
|
})
|
||||||
|
}"
|
||||||
|
icon="${this._changed ? "hass:circle-medium" : "hass:check"}"
|
||||||
|
></ha-icon>
|
||||||
</app-toolbar>
|
</app-toolbar>
|
||||||
</app-header>
|
</app-header>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@ -66,6 +89,11 @@ class LovelaceFullConfigEditor extends hassLocalizeLitMixin(LitElement) {
|
|||||||
const textArea = this.textArea;
|
const textArea = this.textArea;
|
||||||
textArea.value = yaml.safeDump(this.lovelace!.config);
|
textArea.value = yaml.safeDump(this.lovelace!.config);
|
||||||
textArea.addEventListener("keydown", (e) => {
|
textArea.addEventListener("keydown", (e) => {
|
||||||
|
if (e.keyCode === 51) {
|
||||||
|
this._hashAdded = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (e.keyCode !== 9) {
|
if (e.keyCode !== 9) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -110,6 +138,10 @@ class LovelaceFullConfigEditor extends hassLocalizeLitMixin(LitElement) {
|
|||||||
color: var(--dark-text-color);
|
color: var(--dark-text-color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.comments {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
.content {
|
.content {
|
||||||
height: calc(100vh - 68px);
|
height: calc(100vh - 68px);
|
||||||
}
|
}
|
||||||
@ -140,8 +172,31 @@ class LovelaceFullConfigEditor extends hassLocalizeLitMixin(LitElement) {
|
|||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private _closeEditor() {
|
||||||
|
if (this._changed) {
|
||||||
|
if (
|
||||||
|
!confirm("You have unsafed changes, are you sure you want to exit?")
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.onbeforeunload = null;
|
||||||
|
this.closeEditor!();
|
||||||
|
}
|
||||||
|
|
||||||
private async _handleSave() {
|
private async _handleSave() {
|
||||||
this._saving = true;
|
this._saving = true;
|
||||||
|
|
||||||
|
if (this._hashAdded) {
|
||||||
|
if (
|
||||||
|
!confirm(
|
||||||
|
"Your config might contain comments, these will not be saved. Do you want to continue?"
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let value;
|
let value;
|
||||||
try {
|
try {
|
||||||
value = yaml.safeLoad(this.textArea.value);
|
value = yaml.safeLoad(this.textArea.value);
|
||||||
@ -150,20 +205,31 @@ class LovelaceFullConfigEditor extends hassLocalizeLitMixin(LitElement) {
|
|||||||
this._saving = false;
|
this._saving = false;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
value = lovelaceStruct(value);
|
||||||
|
} catch (err) {
|
||||||
|
alert(`Your config is not valid: ${err}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
await this.lovelace!.saveConfig(value);
|
await this.lovelace!.saveConfig(value);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(`Unable to save YAML: ${err}`);
|
alert(`Unable to save YAML: ${err}`);
|
||||||
}
|
}
|
||||||
|
window.onbeforeunload = null;
|
||||||
this._saving = false;
|
this._saving = false;
|
||||||
this._changed = false;
|
this._changed = false;
|
||||||
|
this._hashAdded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _yamlChanged() {
|
private _yamlChanged() {
|
||||||
|
this._hash = this._hashAdded || this.textArea.value.includes("#");
|
||||||
if (this._changed) {
|
if (this._changed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
window.onbeforeunload = () => {
|
||||||
|
return true;
|
||||||
|
};
|
||||||
this._changed = true;
|
this._changed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user