Skip to content

Commit 5c9e052

Browse files
committed
improve typing
1 parent 392a87f commit 5c9e052

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +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-
21-
export type PreviewClickCallback = (x: number, y: number) => void;
22-
2320
@customElement("hui-picture-elements-card")
2421
class HuiPictureElementsCard extends LitElement implements LovelaceCard {
2522
public static async getConfigElement(): Promise<LovelaceCardEditor> {
@@ -230,23 +227,16 @@ class HuiPictureElementsCard extends LitElement implements LovelaceCard {
230227
}
231228

232229
private _handleImageClick(ev: MouseEvent): void {
233-
if (
234-
!this.preview ||
235-
!this._config ||
236-
!(this._config as any)[PREVIEW_CLICK_CALLBACK]
237-
) {
230+
if (!this.preview || !this._config?.[PREVIEW_CLICK_CALLBACK]) {
238231
return;
239232
}
240233

241234
const rect = (ev.currentTarget as HTMLElement).getBoundingClientRect();
242235
const x = ((ev.clientX - rect.left) / rect.width) * 100;
243236
const y = ((ev.clientY - rect.top) / rect.height) * 100;
244237

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);
238+
// only the edited card has this callback
239+
this._config[PREVIEW_CLICK_CALLBACK](x, y);
250240
}
251241
}
252242

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)