Skip to content

Commit

Permalink
Fix yaml card editor (#6275)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramkragten authored Jun 29, 2020
1 parent 31a0c53 commit 7b0e743
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 31 deletions.
45 changes: 15 additions & 30 deletions src/panels/lovelace/editor/card-editor/hui-card-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
LitElement,
property,
TemplateResult,
query,
} from "lit-element";
import { fireEvent } from "../../../../common/dom/fire_event";
import { computeRTL } from "../../../../common/util/compute_rtl";
Expand All @@ -22,6 +23,7 @@ import { getCardElementClass } from "../../create-element/create-card-element";
import type { EntityConfig } from "../../entity-rows/types";
import type { LovelaceCardEditor } from "../../types";
import type { GUIModeChangedEvent } from "../types";
import "../../../../components/ha-circular-progress";

export interface ConfigChangedEvent {
config: LovelaceCardConfig;
Expand Down Expand Up @@ -69,6 +71,8 @@ export class HuiCardEditor extends LitElement {

@property() private _loading = false;

@query("ha-code-editor") _yamlEditor?: HaCodeEditor;

public get yaml(): string {
return this._yaml || "";
}
Expand Down Expand Up @@ -119,17 +123,18 @@ export class HuiCardEditor extends LitElement {
});
}

private get _yamlEditor(): HaCodeEditor {
return this.shadowRoot!.querySelector("ha-code-editor")! as HaCodeEditor;
}

public toggleMode() {
this.GUImode = !this.GUImode;
}

public connectedCallback() {
super.connectedCallback();
this._refreshYamlEditor();
public refreshYamlEditor(focus = false) {
if (!this._yamlEditor?.codemirror) {
return;
}
this._yamlEditor.codemirror.refresh();
if (focus) {
this._yamlEditor.codemirror.focus();
}
}

protected render(): TemplateResult {
Expand All @@ -155,7 +160,7 @@ export class HuiCardEditor extends LitElement {
mode="yaml"
autofocus
.value=${this.yaml}
.error=${this._error}
.error=${Boolean(this._error)}
.rtl=${computeRTL(this.hass)}
@value-changed=${this._handleYAMLChanged}
@keydown=${this._ignoreKeydown}
Expand All @@ -182,14 +187,6 @@ export class HuiCardEditor extends LitElement {

protected 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")) {
this._configElement.hass = this.hass;
}
Expand All @@ -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) {
ev.stopPropagation();
const config = ev.detail.config;
Expand Down Expand Up @@ -298,10 +283,10 @@ export class HuiCardEditor extends LitElement {
padding: 8px 0px;
}
.error {
color: #ef5350;
color: var(--error-color);
}
.warning {
color: #ffa726;
color: var(--warning-color);
}
ha-circular-progress {
display: block;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class HuiDialogEditCard extends LitElement {
scrimClickAction
@keydown=${this._ignoreKeydown}
@closed=${this._close}
@opened=${this._opened}
.heading=${html`${heading}
${this._documentationURL !== undefined
? html`
Expand Down Expand Up @@ -360,6 +361,10 @@ export class HuiDialogEditCard extends LitElement {
this._cardEditorEl?.toggleMode();
}

private _opened() {
this._cardEditorEl?.refreshYamlEditor();
}

private _close(): void {
this._params = undefined;
this._cardConfig = undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/resources/ha-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ documentContainer.innerHTML = `<custom-style>
--scrollbar-thumb-color: rgb(194, 194, 194);
--error-color: #db4437;
--warning-color: f4b400;
--warning-color: #f4b400;
--success-color: #0f9d58;
--info-color: #4285f4;
Expand Down

0 comments on commit 7b0e743

Please sign in to comment.