Skip to content

Commit ceb9967

Browse files
karwostsMindFreeze
andauthored
Make picture elements editor sortable (#51563)
* Make picture elements editor sortable * Update src/panels/lovelace/editor/hui-picture-elements-card-row-editor.ts Co-authored-by: Petar Petrov <MindFreeze@users.noreply.github.com> * Update src/panels/lovelace/editor/hui-picture-elements-card-row-editor.ts --------- Co-authored-by: Petar Petrov <MindFreeze@users.noreply.github.com>
1 parent b201546 commit ceb9967

File tree

1 file changed

+79
-43
lines changed

1 file changed

+79
-43
lines changed

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

Lines changed: 79 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
mdiClose,
33
mdiContentDuplicate,
4+
mdiDragHorizontalVariant,
45
mdiPencil,
56
mdiPlaylistPlus,
67
} from "@mdi/js";
@@ -13,6 +14,7 @@ import "../../../components/ha-dropdown";
1314
import type { HaDropdownSelectEvent } from "../../../components/ha-dropdown";
1415
import "../../../components/ha-dropdown-item";
1516
import "../../../components/ha-icon-button";
17+
import "../../../components/ha-sortable";
1618
import "../../../components/ha-svg-icon";
1719
import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box";
1820
import type { HomeAssistant } from "../../../types";
@@ -64,49 +66,61 @@ export class HuiPictureElementsCardRowEditor extends LitElement {
6466
)}
6567
</h3>
6668
<div class="elements">
67-
${this.elements.map(
68-
(element, index) => html`
69-
<div class="element">
70-
${element.type
71-
? html`
72-
<div class="element-row">
73-
<div>
74-
<span>
75-
${this.hass?.localize(
76-
`ui.panel.lovelace.editor.card.picture-elements.element_types.${element.type}`
77-
) || element.type}
78-
</span>
79-
<span class="secondary"
80-
>${this._getSecondaryDescription(element)}</span
81-
>
82-
</div>
83-
</div>
84-
`
85-
: nothing}
86-
<ha-icon-button
87-
.label=${this.hass!.localize("ui.common.delete")}
88-
.path=${mdiClose}
89-
class="remove-icon"
90-
.index=${index}
91-
@click=${this._removeRow}
92-
></ha-icon-button>
93-
<ha-icon-button
94-
.label=${this.hass!.localize("ui.common.edit")}
95-
.path=${mdiPencil}
96-
class="edit-icon"
97-
.index=${index}
98-
@click=${this._editRow}
99-
></ha-icon-button>
100-
<ha-icon-button
101-
.label=${this.hass!.localize("ui.common.duplicate")}
102-
.path=${mdiContentDuplicate}
103-
class="duplicate-icon"
104-
.index=${index}
105-
@click=${this._duplicateRow}
106-
></ha-icon-button>
107-
</div>
108-
`
109-
)}
69+
<ha-sortable
70+
handle-selector=".handle"
71+
@item-moved=${this._elementMoved}
72+
>
73+
<div>
74+
${this.elements.map(
75+
(element, index) => html`
76+
<div class="element">
77+
<div class="handle">
78+
<ha-svg-icon
79+
.path=${mdiDragHorizontalVariant}
80+
></ha-svg-icon>
81+
</div>
82+
${element.type
83+
? html`
84+
<div class="element-row">
85+
<div>
86+
<span>
87+
${this.hass?.localize(
88+
`ui.panel.lovelace.editor.card.picture-elements.element_types.${element.type}`
89+
) || element.type}
90+
</span>
91+
<span class="secondary"
92+
>${this._getSecondaryDescription(element)}</span
93+
>
94+
</div>
95+
</div>
96+
`
97+
: nothing}
98+
<ha-icon-button
99+
.label=${this.hass!.localize("ui.common.delete")}
100+
.path=${mdiClose}
101+
class="remove-icon"
102+
.index=${index}
103+
@click=${this._removeRow}
104+
></ha-icon-button>
105+
<ha-icon-button
106+
.label=${this.hass!.localize("ui.common.edit")}
107+
.path=${mdiPencil}
108+
class="edit-icon"
109+
.index=${index}
110+
@click=${this._editRow}
111+
></ha-icon-button>
112+
<ha-icon-button
113+
.label=${this.hass!.localize("ui.common.duplicate")}
114+
.path=${mdiContentDuplicate}
115+
class="duplicate-icon"
116+
.index=${index}
117+
@click=${this._duplicateRow}
118+
></ha-icon-button>
119+
</div>
120+
`
121+
)}
122+
</div>
123+
</ha-sortable>
110124
<ha-dropdown @wa-select=${this._addElement}>
111125
<ha-button size="small" slot="trigger" appearance="filled">
112126
<ha-svg-icon slot="start" .path=${mdiPlaylistPlus}></ha-svg-icon>
@@ -193,6 +207,17 @@ export class HuiPictureElementsCardRowEditor extends LitElement {
193207
fireEvent(this, "elements-changed", { elements: newElements });
194208
}
195209

210+
private _elementMoved(ev: CustomEvent): void {
211+
ev.stopPropagation();
212+
const { oldIndex, newIndex } = ev.detail;
213+
214+
const newElements = this.elements!.concat();
215+
216+
newElements.splice(newIndex, 0, newElements.splice(oldIndex, 1)[0]);
217+
218+
fireEvent(this, "elements-changed", { elements: newElements });
219+
}
220+
196221
private _removeRow(ev: CustomEvent): void {
197222
const index = (ev.currentTarget as any).index;
198223
const element = this.elements?.[index];
@@ -242,6 +267,17 @@ export class HuiPictureElementsCardRowEditor extends LitElement {
242267
display: flex;
243268
align-items: center;
244269
}
270+
.handle {
271+
padding-right: var(--ha-space-2);
272+
cursor: move;
273+
cursor: grab;
274+
padding-inline-end: var(--ha-space-2);
275+
padding-inline-start: initial;
276+
direction: var(--direction);
277+
}
278+
.handle > * {
279+
pointer-events: none;
280+
}
245281
246282
.element-row {
247283
height: 60px;

0 commit comments

Comments
 (0)