UI Editor for markdown card (#2231)

* UI Editor for `markdown` card

* Remove `id`
This commit is contained in:
Paulus Schoutsen 2018-12-11 21:21:38 +01:00 committed by GitHub
commit 65cf2feb7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 106 additions and 2 deletions

View File

@ -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<LovelaceCardEditor> {
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 {

View File

@ -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}
<div class="card-config">
<paper-input
label="Title"
value="${this._title}"
.configValue="${"title"}"
@value-changed="${this._valueChanged}"
></paper-input>
<paper-textarea
label="Content"
value="${this._content}"
.configValue="${"content"}"
@value-changed="${this._valueChanged}"
autocapitalize="none"
autocomplete="off"
spellcheck="false"
></paper-textarea>
</div>
`;
}
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);