Skip to content

Commit c16e33a

Browse files
committed
improve typing
1 parent 392a87f commit c16e33a

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

src/panels/lovelace/cards/hui-picture-elements-card.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import { findEntities } from "../common/find-entities";
1111
import type { LovelaceElement, LovelaceElementConfig } from "../elements/types";
1212
import type { LovelaceCard, LovelaceCardEditor } from "../types";
1313
import { createStyledHuiElement } from "./picture-elements/create-styled-hui-element";
14-
import type { PictureElementsCardConfig } from "./types";
14+
import {
15+
PREVIEW_CLICK_CALLBACK,
16+
type PictureElementsCardConfig,
17+
} from "./types";
1518
import type { PersonEntity } from "../../../data/person";
1619

17-
// Symbol for preview click callback - preserved through spreads, not serialized
18-
// This allows the editor to attach a callback that only exists on the edited card's config
19-
export const PREVIEW_CLICK_CALLBACK = Symbol("previewClickCallback");
20-
2120
export type PreviewClickCallback = (x: number, y: number) => void;
2221

2322
@customElement("hui-picture-elements-card")
@@ -230,23 +229,16 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {
230229
}
231230

232231
private _handleImageClick(ev: MouseEvent): void {
233-
if (
234-
!this.preview ||
235-
!this._config ||
236-
!(this._config as any)[PREVIEW_CLICK_CALLBACK]
237-
) {
232+
if (!this.preview || !this._config?.[PREVIEW_CLICK_CALLBACK]) {
238233
return;
239234
}
240235

241236
const rect = (ev.currentTarget as HTMLElement).getBoundingClientRect();
242237
const x = ((ev.clientX - rect.left) / rect.width) * 100;
243238
const y = ((ev.clientY - rect.top) / rect.height) * 100;
244239

245-
// Call the callback if present on config (only the edited card has this)
246-
const callback = (this._config as any)[
247-
PREVIEW_CLICK_CALLBACK
248-
] as PreviewClickCallback;
249-
callback(x, y);
240+
// only the edited card has this callback
241+
this._config[PREVIEW_CLICK_CALLBACK](x, y);
250242
}
251243
}
252244

src/panels/lovelace/cards/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ export interface PictureCardConfig extends LovelaceCardConfig {
483483
alt_text?: string;
484484
}
485485

486+
// Symbol for preview click callback - preserved through spreads, not serialized
487+
// This allows the editor to attach a callback that only exists on the edited card's config
488+
export const PREVIEW_CLICK_CALLBACK = Symbol("previewClickCallback");
489+
486490
export interface PictureElementsCardConfig extends LovelaceCardConfig {
487491
title?: string;
488492
image?: string | MediaSelectorValue;
@@ -497,6 +501,7 @@ export interface PictureElementsCardConfig extends LovelaceCardConfig {
497501
theme?: string;
498502
dark_mode_image?: string | MediaSelectorValue;
499503
dark_mode_filter?: string;
504+
[PREVIEW_CLICK_CALLBACK]?: (x: number, y: number) => void;
500505
}
501506

502507
export interface PictureEntityCardConfig extends LovelaceCardConfig {

src/panels/lovelace/editor/config-elements/hui-picture-elements-card-editor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import "../../../../components/ha-form/ha-form";
2121
import "../../../../components/ha-icon";
2222
import "../../../../components/ha-switch";
2323
import type { HomeAssistant } from "../../../../types";
24-
import type { PictureElementsCardConfig } from "../../cards/types";
24+
import {
25+
PREVIEW_CLICK_CALLBACK,
26+
type PictureElementsCardConfig,
27+
} from "../../cards/types";
2528
import type { LovelaceCardEditor } from "../../types";
2629
import "../hui-sub-element-editor";
2730
import { baseLovelaceCardConfig } from "../structs/base-card-struct";
2831
import type { EditDetailElementEvent, SubElementEditorConfig } from "../types";
2932
import { configElementStyle } from "./config-elements-style";
30-
import { PREVIEW_CLICK_CALLBACK } from "../../cards/hui-picture-elements-card";
3133
import "../hui-picture-elements-card-row-editor";
3234
import type { LovelaceElementConfig } from "../../elements/types";
3335
import type { LocalizeFunc } from "../../../../common/translations/localize";

0 commit comments

Comments
 (0)