@@ -15,20 +15,23 @@ import {
1515} from "superstruct" ;
1616import type { HASSDomEvent } from "../../../../common/dom/fire_event" ;
1717import { fireEvent } from "../../../../common/dom/fire_event" ;
18+ import "../../../../components/ha-alert" ;
1819import "../../../../components/ha-card" ;
1920import "../../../../components/ha-form/ha-form" ;
2021import "../../../../components/ha-icon" ;
2122import "../../../../components/ha-switch" ;
2223import type { HomeAssistant } from "../../../../types" ;
23- import type { PictureElementsCardConfig } from "../../cards/types" ;
24+ import {
25+ PREVIEW_CLICK_CALLBACK ,
26+ type PictureElementsCardConfig ,
27+ } from "../../cards/types" ;
2428import type { LovelaceCardEditor } from "../../types" ;
2529import "../hui-sub-element-editor" ;
2630import { baseLovelaceCardConfig } from "../structs/base-card-struct" ;
2731import type { EditDetailElementEvent , SubElementEditorConfig } from "../types" ;
2832import { configElementStyle } from "./config-elements-style" ;
2933import "../hui-picture-elements-card-row-editor" ;
3034import type { LovelaceElementConfig } from "../../elements/types" ;
31- import type { LovelaceCardConfig } from "../../../../data/lovelace/config/card" ;
3235import type { LocalizeFunc } from "../../../../common/translations/localize" ;
3336
3437const genericElementConfigStruct = type ( {
@@ -66,6 +69,44 @@ export class HuiPictureElementsCardEditor
6669 this . _config = config ;
6770 }
6871
72+ private _onPreviewClick = ( x : number , y : number ) : void => {
73+ if ( this . _subElementEditorConfig ?. type === "element" ) {
74+ this . _handlePositionClick ( x , y ) ;
75+ }
76+ } ;
77+
78+ private _handlePositionClick ( x : number , y : number ) : void {
79+ if (
80+ ! this . _subElementEditorConfig ?. elementConfig ||
81+ this . _subElementEditorConfig . type !== "element" ||
82+ this . _subElementEditorConfig . elementConfig . type === "conditional"
83+ ) {
84+ return ;
85+ }
86+
87+ const elementConfig = this . _subElementEditorConfig
88+ . elementConfig as LovelaceElementConfig ;
89+ const currentPosition = ( elementConfig . style as Record < string , string > )
90+ ?. position ;
91+ if ( currentPosition && currentPosition !== "absolute" ) {
92+ return ;
93+ }
94+
95+ const newElement = {
96+ ...elementConfig ,
97+ style : {
98+ ...( ( elementConfig . style as Record < string , string > ) || { } ) ,
99+ left : `${ Math . round ( x ) } %` ,
100+ top : `${ Math . round ( y ) } %` ,
101+ } ,
102+ } ;
103+
104+ const updateEvent = new CustomEvent ( "config-changed" , {
105+ detail : { config : newElement } ,
106+ } ) ;
107+ this . _handleSubElementChanged ( updateEvent ) ;
108+ }
109+
69110 private _schema = memoizeOne (
70111 ( localize : LocalizeFunc ) =>
71112 [
@@ -138,6 +179,16 @@ export class HuiPictureElementsCardEditor
138179
139180 if ( this . _subElementEditorConfig ) {
140181 return html `
182+ ${ this . _subElementEditorConfig . type === "element" &&
183+ this . _subElementEditorConfig . elementConfig ?. type !== "conditional"
184+ ? html `
185+ < ha-alert alert-type ="info ">
186+ ${ this . hass . localize (
187+ "ui.panel.lovelace.editor.card.picture-elements.position_hint"
188+ ) }
189+ </ ha-alert >
190+ `
191+ : nothing }
141192 < hui-sub-element-editor
142193 .hass =${ this . hass }
143194 .config =${ this . _subElementEditorConfig }
@@ -181,6 +232,7 @@ export class HuiPictureElementsCardEditor
181232 return ;
182233 }
183234
235+ // no need to attach the preview click callback here, no element is being edited
184236 fireEvent ( this , "config-changed" , { config : ev . detail . value } ) ;
185237 }
186238
@@ -191,7 +243,8 @@ export class HuiPictureElementsCardEditor
191243 const config = {
192244 ...this . _config ,
193245 elements : ev . detail . elements as LovelaceElementConfig [ ] ,
194- } as LovelaceCardConfig ;
246+ [ PREVIEW_CLICK_CALLBACK ] : this . _onPreviewClick ,
247+ } as PictureElementsCardConfig ;
195248
196249 fireEvent ( this , "config-changed" , { config } ) ;
197250
@@ -232,7 +285,12 @@ export class HuiPictureElementsCardEditor
232285 elementConfig : value ,
233286 } ;
234287
235- fireEvent ( this , "config-changed" , { config : this . _config } ) ;
288+ fireEvent ( this , "config-changed" , {
289+ config : {
290+ ...this . _config ,
291+ [ PREVIEW_CLICK_CALLBACK ] : this . _onPreviewClick ,
292+ } ,
293+ } ) ;
236294 }
237295
238296 private _editDetailElement ( ev : HASSDomEvent < EditDetailElementEvent > ) : void {
0 commit comments