From d7e59935015062e97d7f5d871d9e8e63d0fb396e Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Fri, 7 Dec 2018 21:39:27 -0600 Subject: [PATCH 1/2] UI Editor for `markdown` card --- .../lovelace/cards/hui-markdown-card.ts | 12 ++- .../hui-markdown-card-editor.ts | 97 +++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts diff --git a/src/panels/lovelace/cards/hui-markdown-card.ts b/src/panels/lovelace/cards/hui-markdown-card.ts index e30fadaa40..4c33ebf4bc 100644 --- a/src/panels/lovelace/cards/hui-markdown-card.ts +++ b/src/panels/lovelace/cards/hui-markdown-card.ts @@ -4,16 +4,24 @@ import { classMap } from "lit-html/directives/classMap"; import "../../../components/ha-card"; import "../../../components/ha-markdown"; -import { LovelaceCard } from "../types"; +import { LovelaceCard, LovelaceCardEditor } from "../types"; import { LovelaceCardConfig } from "../../../data/lovelace"; import { TemplateResult } from "lit-html"; -interface Config extends LovelaceCardConfig { +export interface Config extends LovelaceCardConfig { content: string; title?: string; } export class HuiMarkdownCard extends LitElement implements LovelaceCard { + public static async getConfigElement(): Promise { + await import("../editor/config-elements/hui-markdown-card-editor"); + return document.createElement("hui-markdown-card-editor"); + } + public static getStubConfig(): object { + return { content: " " }; + } + private _config?: Config; static get properties(): PropertyDeclarations { diff --git a/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts new file mode 100644 index 0000000000..94753ef548 --- /dev/null +++ b/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts @@ -0,0 +1,97 @@ +import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element"; +import { TemplateResult } from "lit-html"; +import { struct } from "../../common/structs/struct"; +import "@polymer/paper-input/paper-input"; +import "@polymer/paper-input/paper-textarea"; + +import { EntitiesEditorEvent, EditorTarget } from "../types"; +import { hassLocalizeLitMixin } from "../../../../mixins/lit-localize-mixin"; +import { HomeAssistant } from "../../../../types"; +import { LovelaceCardEditor } from "../../types"; +import { fireEvent } from "../../../../common/dom/fire_event"; +import { Config } from "../../cards/hui-glance-card"; +import { configElementStyle } from "./config-elements-style"; + +const cardConfigStruct = struct({ + type: "string", + id: "string|number", + title: "string?", + content: "string", +}); + +export class HuiMarkdownCardEditor extends hassLocalizeLitMixin(LitElement) + implements LovelaceCardEditor { + public hass?: HomeAssistant; + private _config?: Config; + + public setConfig(config: Config): void { + config = cardConfigStruct(config); + + this._config = { type: "markdown", ...config }; + } + + static get properties(): PropertyDeclarations { + return { hass: {}, _config: {} }; + } + + get _title(): string { + return this._config!.title || ""; + } + + get _content(): string { + return this._config!.content || ""; + } + + protected render(): TemplateResult { + if (!this.hass) { + return html``; + } + + return html` + ${configElementStyle} +
+ + +
+ `; + } + + private _valueChanged(ev: EntitiesEditorEvent): void { + if (!this._config || !this.hass) { + return; + } + const target = ev.target! as EditorTarget; + + if (this[`_${target.configValue}`] === target.value) { + return; + } + if (target.configValue) { + this._config = { + ...this._config, + [target.configValue!]: target.value, + }; + } + fireEvent(this, "config-changed", { config: this._config }); + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-markdown-card-editor": HuiMarkdownCardEditor; + } +} + +customElements.define("hui-markdown-card-editor", HuiMarkdownCardEditor); From 0319fd23c5f3e49a096b4464e7ded49de03ad44a Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Mon, 10 Dec 2018 23:13:26 -0600 Subject: [PATCH 2/2] Remove `id` --- .../lovelace/editor/config-elements/hui-markdown-card-editor.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts b/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts index 94753ef548..3c5a8e37e9 100644 --- a/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts +++ b/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts @@ -14,7 +14,6 @@ import { configElementStyle } from "./config-elements-style"; const cardConfigStruct = struct({ type: "string", - id: "string|number", title: "string?", content: "string", });