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..3c5a8e37e9 --- /dev/null +++ b/src/panels/lovelace/editor/config-elements/hui-markdown-card-editor.ts @@ -0,0 +1,96 @@ +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", + 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);