Fix markdown card crashing the demo (#5962)

This commit is contained in:
Bram Kragten 2020-05-21 13:00:33 +02:00 committed by GitHub
parent 389b7def0b
commit e4607735ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 23 deletions

View File

@ -41,7 +41,7 @@ export class HuiMarkdownCard extends LitElement implements LovelaceCard {
@property() private _content = ""; @property() private _content = "";
@property() private _unsubRenderTemplate?: Promise<UnsubscribeFunc>; @property() private _unsubRenderTemplate?: UnsubscribeFunc;
public getCardSize(): number { public getCardSize(): number {
return this._config === undefined return this._config === undefined
@ -121,7 +121,8 @@ export class HuiMarkdownCard extends LitElement implements LovelaceCard {
return; return;
} }
this._unsubRenderTemplate = subscribeRenderTemplate( try {
this._unsubRenderTemplate = await subscribeRenderTemplate(
this.hass.connection, this.hass.connection,
(result) => { (result) => {
this._content = result; this._content = result;
@ -135,10 +136,10 @@ export class HuiMarkdownCard extends LitElement implements LovelaceCard {
}, },
} }
); );
this._unsubRenderTemplate.catch(() => { } catch {
this._content = this._config!.content; this._content = this._config!.content;
this._unsubRenderTemplate = undefined; this._unsubRenderTemplate = undefined;
}); }
} }
private async _tryDisconnect(): Promise<void> { private async _tryDisconnect(): Promise<void> {
@ -147,9 +148,8 @@ export class HuiMarkdownCard extends LitElement implements LovelaceCard {
} }
try { try {
const unsub = await this._unsubRenderTemplate; this._unsubRenderTemplate();
this._unsubRenderTemplate = undefined; this._unsubRenderTemplate = undefined;
unsub();
} catch (e) { } catch (e) {
if (e.code === "not_found") { if (e.code === "not_found") {
// If we get here, the connection was probably already closed. Ignore. // If we get here, the connection was probably already closed. Ignore.

View File

@ -115,6 +115,7 @@ class HuiWeatherForecastCard extends LitElement implements LovelaceCard {
oldConfig.theme !== this._config.theme oldConfig.theme !== this._config.theme
) { ) {
applyThemesOnElement(this, this.hass.themes, this._config.theme); applyThemesOnElement(this, this.hass.themes, this._config.theme);
this.requestUpdate();
} }
} }

View File

@ -1,5 +1,5 @@
import { LovelaceCard } from "../types"; import { LovelaceCard } from "../types";
export const computeCardSize = (card: LovelaceCard): number => { export const computeCardSize = (card: LovelaceCard): number => {
return typeof card.getCardSize === "function" ? card.getCardSize() : 1; return typeof card.getCardSize === "function" ? card.getCardSize() : 4;
}; };

View File

@ -284,16 +284,23 @@ export class HuiCardPicker extends LitElement {
"ll-rebuild", "ll-rebuild",
(ev) => { (ev) => {
ev.stopPropagation(); ev.stopPropagation();
element.parentElement!.replaceChild( this._rebuildCard(element, cardConfig);
this._createCardElement(cardConfig),
element
);
}, },
{ once: true } { once: true }
); );
return element; return element;
} }
private _rebuildCard(
cardElToReplace: LovelaceCard,
config: LovelaceCardConfig
): void {
const newCardEl = this._createCardElement(config);
if (cardElToReplace.parentElement) {
cardElToReplace.parentElement!.replaceChild(newCardEl, cardElToReplace);
}
}
private async _renderCardElement(card: Card): Promise<TemplateResult> { private async _renderCardElement(card: Card): Promise<TemplateResult> {
let { type } = card; let { type } = card;
const { showElement, isCustom, name, description } = card; const { showElement, isCustom, name, description } = card;