Fix yaml card editor (#6275)

This commit is contained in:
Bram Kragten 2020-06-29 22:19:20 +02:00 committed by GitHub
parent 31a0c53855
commit 7b0e743eca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 31 deletions

View File

@ -8,6 +8,7 @@ import {
LitElement, LitElement,
property, property,
TemplateResult, TemplateResult,
query,
} from "lit-element"; } from "lit-element";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import { computeRTL } from "../../../../common/util/compute_rtl"; import { computeRTL } from "../../../../common/util/compute_rtl";
@ -22,6 +23,7 @@ import { getCardElementClass } from "../../create-element/create-card-element";
import type { EntityConfig } from "../../entity-rows/types"; import type { EntityConfig } from "../../entity-rows/types";
import type { LovelaceCardEditor } from "../../types"; import type { LovelaceCardEditor } from "../../types";
import type { GUIModeChangedEvent } from "../types"; import type { GUIModeChangedEvent } from "../types";
import "../../../../components/ha-circular-progress";
export interface ConfigChangedEvent { export interface ConfigChangedEvent {
config: LovelaceCardConfig; config: LovelaceCardConfig;
@ -69,6 +71,8 @@ export class HuiCardEditor extends LitElement {
@property() private _loading = false; @property() private _loading = false;
@query("ha-code-editor") _yamlEditor?: HaCodeEditor;
public get yaml(): string { public get yaml(): string {
return this._yaml || ""; return this._yaml || "";
} }
@ -119,17 +123,18 @@ export class HuiCardEditor extends LitElement {
}); });
} }
private get _yamlEditor(): HaCodeEditor {
return this.shadowRoot!.querySelector("ha-code-editor")! as HaCodeEditor;
}
public toggleMode() { public toggleMode() {
this.GUImode = !this.GUImode; this.GUImode = !this.GUImode;
} }
public connectedCallback() { public refreshYamlEditor(focus = false) {
super.connectedCallback(); if (!this._yamlEditor?.codemirror) {
this._refreshYamlEditor(); return;
}
this._yamlEditor.codemirror.refresh();
if (focus) {
this._yamlEditor.codemirror.focus();
}
} }
protected render(): TemplateResult { protected render(): TemplateResult {
@ -155,7 +160,7 @@ export class HuiCardEditor extends LitElement {
mode="yaml" mode="yaml"
autofocus autofocus
.value=${this.yaml} .value=${this.yaml}
.error=${this._error} .error=${Boolean(this._error)}
.rtl=${computeRTL(this.hass)} .rtl=${computeRTL(this.hass)}
@value-changed=${this._handleYAMLChanged} @value-changed=${this._handleYAMLChanged}
@keydown=${this._ignoreKeydown} @keydown=${this._ignoreKeydown}
@ -182,14 +187,6 @@ export class HuiCardEditor extends LitElement {
protected updated(changedProperties) { protected updated(changedProperties) {
super.updated(changedProperties); super.updated(changedProperties);
if (changedProperties.has("_GUImode")) {
if (this.GUImode === false) {
// Refresh code editor when switching to yaml mode
this._refreshYamlEditor(true);
}
}
if (this._configElement && changedProperties.has("hass")) { if (this._configElement && changedProperties.has("hass")) {
this._configElement.hass = this.hass; this._configElement.hass = this.hass;
} }
@ -198,18 +195,6 @@ export class HuiCardEditor extends LitElement {
} }
} }
private _refreshYamlEditor(focus = false) {
// wait on render
setTimeout(() => {
if (this._yamlEditor && this._yamlEditor.codemirror) {
this._yamlEditor.codemirror.refresh();
if (focus) {
this._yamlEditor.codemirror.focus();
}
}
}, 1);
}
private _handleUIConfigChanged(ev: UIConfigChangedEvent) { private _handleUIConfigChanged(ev: UIConfigChangedEvent) {
ev.stopPropagation(); ev.stopPropagation();
const config = ev.detail.config; const config = ev.detail.config;
@ -298,10 +283,10 @@ export class HuiCardEditor extends LitElement {
padding: 8px 0px; padding: 8px 0px;
} }
.error { .error {
color: #ef5350; color: var(--error-color);
} }
.warning { .warning {
color: #ffa726; color: var(--warning-color);
} }
ha-circular-progress { ha-circular-progress {
display: block; display: block;

View File

@ -122,6 +122,7 @@ export class HuiDialogEditCard extends LitElement {
scrimClickAction scrimClickAction
@keydown=${this._ignoreKeydown} @keydown=${this._ignoreKeydown}
@closed=${this._close} @closed=${this._close}
@opened=${this._opened}
.heading=${html`${heading} .heading=${html`${heading}
${this._documentationURL !== undefined ${this._documentationURL !== undefined
? html` ? html`
@ -360,6 +361,10 @@ export class HuiDialogEditCard extends LitElement {
this._cardEditorEl?.toggleMode(); this._cardEditorEl?.toggleMode();
} }
private _opened() {
this._cardEditorEl?.refreshYamlEditor();
}
private _close(): void { private _close(): void {
this._params = undefined; this._params = undefined;
this._cardConfig = undefined; this._cardConfig = undefined;

View File

@ -34,7 +34,7 @@ documentContainer.innerHTML = `<custom-style>
--scrollbar-thumb-color: rgb(194, 194, 194); --scrollbar-thumb-color: rgb(194, 194, 194);
--error-color: #db4437; --error-color: #db4437;
--warning-color: f4b400; --warning-color: #f4b400;
--success-color: #0f9d58; --success-color: #0f9d58;
--info-color: #4285f4; --info-color: #4285f4;