From aca1ecf1eea6fa52dc0b13a2c2bb36b46a4c3f4a Mon Sep 17 00:00:00 2001 From: Ian Richardson Date: Sun, 21 Oct 2018 13:09:11 -0500 Subject: [PATCH] Convert hui-markdown-card to TypeScript/LitElement (#1808) * Convert hui-markdown-card to TypeScript/LitElement * Addressed review comments * Addressed review comments * Addressed review comments --- .../lovelace/cards/hui-markdown-card.js | 67 ------------- .../lovelace/cards/hui-markdown-card.ts | 93 +++++++++++++++++++ .../lovelace/common/create-card-element.js | 2 +- 3 files changed, 94 insertions(+), 68 deletions(-) delete mode 100644 src/panels/lovelace/cards/hui-markdown-card.js create mode 100644 src/panels/lovelace/cards/hui-markdown-card.ts diff --git a/src/panels/lovelace/cards/hui-markdown-card.js b/src/panels/lovelace/cards/hui-markdown-card.js deleted file mode 100644 index cdfd2aaa5f..0000000000 --- a/src/panels/lovelace/cards/hui-markdown-card.js +++ /dev/null @@ -1,67 +0,0 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag.js"; -import { PolymerElement } from "@polymer/polymer/polymer-element.js"; - -import "../../../components/ha-card.js"; -import "../../../components/ha-markdown.js"; - -class HuiMarkdownCard extends PolymerElement { - static get template() { - return html` - - - - - `; - } - - static get properties() { - return { - _config: Object, - noTitle: { - type: Boolean, - reflectToAttribute: true, - computed: "_computeNoTitle(_config.title)", - }, - }; - } - - setConfig(config) { - this._config = config; - } - - getCardSize() { - return this._config.content.split("\n").length; - } - - _computeNoTitle(title) { - return !title; - } -} - -customElements.define("hui-markdown-card", HuiMarkdownCard); diff --git a/src/panels/lovelace/cards/hui-markdown-card.ts b/src/panels/lovelace/cards/hui-markdown-card.ts new file mode 100644 index 0000000000..c701f0e166 --- /dev/null +++ b/src/panels/lovelace/cards/hui-markdown-card.ts @@ -0,0 +1,93 @@ +import { html, LitElement } from "@polymer/lit-element"; +import { classMap } from "lit-html/directives/classMap.js"; + +import "../../../components/ha-card.js"; +import "../../../components/ha-markdown.js"; + +import { LovelaceCard, LovelaceConfig } from "../types.js"; + +interface Config extends LovelaceConfig { + content: string; + title?: string; +} + +export class HuiMarkdownCard extends LitElement + implements LovelaceCard { + protected config?: Config; + + static get properties() { + return { + config: {}, + }; + } + + public getCardSize() { + return this.config!.content.split("\n").length; + } + + public setConfig(config: Config) { + if (!config.content) { + throw new Error("Invalid Configuration: Content Required"); + } + + this.config = config; + } + + protected render() { + if (!this.config) { + return html``; + } + + return html` + ${this.renderStyle()} + + + + `; + } + + private renderStyle() { + return html` + + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "hui-markdown-card": HuiMarkdownCard; + } +} + +customElements.define("hui-markdown-card", HuiMarkdownCard); diff --git a/src/panels/lovelace/common/create-card-element.js b/src/panels/lovelace/common/create-card-element.js index a29faf1b9b..5a2da19af9 100644 --- a/src/panels/lovelace/common/create-card-element.js +++ b/src/panels/lovelace/common/create-card-element.js @@ -11,7 +11,7 @@ import "../cards/hui-history-graph-card.js"; import "../cards/hui-horizontal-stack-card.js"; import "../cards/hui-iframe-card.ts"; import "../cards/hui-map-card.js"; -import "../cards/hui-markdown-card.js"; +import "../cards/hui-markdown-card.ts"; import "../cards/hui-media-control-card.js"; import "../cards/hui-picture-card.js"; import "../cards/hui-picture-elements-card";