Do not recreate entities list at each re-order (#13751)

This commit is contained in:
Paul Bottein 2022-09-16 13:38:31 +02:00 committed by GitHub
parent e13c632afa
commit 9e416e829c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 142 additions and 186 deletions

View File

@ -1,14 +1,7 @@
import { mdiDrag } from "@mdi/js";
import {
css,
CSSResultGroup,
html,
LitElement,
PropertyValues,
TemplateResult,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { guard } from "lit/directives/guard";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators";
import { repeat } from "lit/directives/repeat";
import type { SortableEvent } from "sortablejs";
import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/entity/ha-entity-picker";
@ -30,20 +23,20 @@ export class HuiEntityEditor extends LitElement {
@property() protected label?: string;
@state() private _attached = false;
@state() private _renderEmptySortable = false;
private _entityKeys = new WeakMap<EntityConfig, string>();
private _sortable?: SortableInstance;
public connectedCallback() {
super.connectedCallback();
this._attached = true;
public disconnectedCallback() {
this._destroySortable();
}
public disconnectedCallback() {
super.disconnectedCallback();
this._attached = false;
private _getKey(action: EntityConfig) {
if (!this._entityKeys.has(action)) {
this._entityKeys.set(action, Math.random().toString());
}
return this._entityKeys.get(action)!;
}
protected render(): TemplateResult {
@ -60,13 +53,14 @@ export class HuiEntityEditor extends LitElement {
")"}
</h3>
<div class="entities">
${guard([this.entities, this._renderEmptySortable], () =>
this._renderEmptySortable
? ""
: this.entities!.map(
${repeat(
this.entities,
(entityConf) => this._getKey(entityConf),
(entityConf, index) => html`
<div class="entity" data-entity-id=${entityConf.entity}>
<div class="handle">
<ha-svg-icon .path=${mdiDrag}></ha-svg-icon>
</div>
<ha-entity-picker
.hass=${this.hass}
.value=${entityConf.entity}
@ -76,7 +70,6 @@ export class HuiEntityEditor extends LitElement {
></ha-entity-picker>
</div>
`
)
)}
</div>
<ha-entity-picker
@ -87,58 +80,41 @@ export class HuiEntityEditor extends LitElement {
`;
}
protected updated(changedProps: PropertyValues): void {
super.updated(changedProps);
const attachedChanged = changedProps.has("_attached");
const entitiesChanged = changedProps.has("entities");
if (!entitiesChanged && !attachedChanged) {
return;
}
if (attachedChanged && !this._attached) {
// Tear down sortable, if available
this._sortable?.destroy();
this._sortable = undefined;
return;
}
if (!this._sortable && this.entities) {
protected firstUpdated(): void {
this._createSortable();
return;
}
if (entitiesChanged) {
this._handleEntitiesChanged();
}
}
private async _handleEntitiesChanged() {
this._renderEmptySortable = true;
await this.updateComplete;
const container = this.shadowRoot!.querySelector(".entities")!;
while (container.lastElementChild) {
container.removeChild(container.lastElementChild);
}
this._renderEmptySortable = false;
}
private async _createSortable() {
const Sortable = await loadSortable();
this._sortable = new Sortable(
this.shadowRoot!.querySelector(".entities")!,
{
animation: 150,
fallbackClass: "sortable-fallback",
handle: "ha-svg-icon",
handle: ".handle",
dataIdAttr: "data-entity-id",
onEnd: async (evt: SortableEvent) => this._entityMoved(evt),
onChoose: (evt: SortableEvent) => {
(evt.item as any).placeholder =
document.createComment("sort-placeholder");
evt.item.after((evt.item as any).placeholder);
},
onEnd: (evt: SortableEvent) => {
// put back in original location
if ((evt.item as any).placeholder) {
(evt.item as any).placeholder.replaceWith(evt.item);
delete (evt.item as any).placeholder;
}
this._entityMoved(evt);
},
}
);
}
private _destroySortable() {
this._sortable?.destroy();
this._sortable = undefined;
}
private async _addEntity(ev: CustomEvent): Promise<void> {
const value = ev.detail.value;
if (value === "") {
@ -198,9 +174,15 @@ export class HuiEntityEditor extends LitElement {
display: flex;
align-items: center;
}
.entity ha-svg-icon {
.entity .handle {
padding-right: 8px;
cursor: move;
padding-inline-end: 8px;
padding-inline-start: initial;
direction: var(--direction);
}
.entity .handle > * {
pointer-events: none;
}
.entity ha-entity-picker {
flex-grow: 1;

View File

@ -1,14 +1,7 @@
import { mdiClose, mdiDrag, mdiPencil } from "@mdi/js";
import {
css,
CSSResultGroup,
html,
LitElement,
PropertyValues,
TemplateResult,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { guard } from "lit/directives/guard";
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit";
import { customElement, property } from "lit/decorators";
import { repeat } from "lit/directives/repeat";
import type { SortableEvent } from "sortablejs";
import { fireEvent } from "../../../common/dom/fire_event";
import "../../../components/entity/ha-entity-picker";
@ -39,20 +32,20 @@ export class HuiEntitiesCardRowEditor extends LitElement {
@property() protected label?: string;
@state() private _attached = false;
@state() private _renderEmptySortable = false;
private _entityKeys = new WeakMap<LovelaceRowConfig, string>();
private _sortable?: SortableInstance;
public connectedCallback() {
super.connectedCallback();
this._attached = true;
public disconnectedCallback() {
this._destroySortable();
}
public disconnectedCallback() {
super.disconnectedCallback();
this._attached = false;
private _getKey(action: LovelaceRowConfig) {
if (!this._entityKeys.has(action)) {
this._entityKeys.set(action, Math.random().toString());
}
return this._entityKeys.get(action)!;
}
protected render(): TemplateResult {
@ -70,10 +63,9 @@ export class HuiEntitiesCardRowEditor extends LitElement {
)})`}
</h3>
<div class="entities">
${guard([this.entities, this._renderEmptySortable], () =>
this._renderEmptySortable
? ""
: this.entities!.map(
${repeat(
this.entities,
(entityConf) => this._getKey(entityConf),
(entityConf, index) => html`
<div class="entity">
<div class="handle">
@ -126,7 +118,6 @@ export class HuiEntitiesCardRowEditor extends LitElement {
></ha-icon-button>
</div>
`
)
)}
</div>
<ha-entity-picker
@ -137,57 +128,40 @@ export class HuiEntitiesCardRowEditor extends LitElement {
`;
}
protected updated(changedProps: PropertyValues): void {
super.updated(changedProps);
const attachedChanged = changedProps.has("_attached");
const entitiesChanged = changedProps.has("entities");
if (!entitiesChanged && !attachedChanged) {
return;
}
if (attachedChanged && !this._attached) {
// Tear down sortable, if available
this._sortable?.destroy();
this._sortable = undefined;
return;
}
if (!this._sortable && this.entities) {
protected firstUpdated(): void {
this._createSortable();
return;
}
if (entitiesChanged) {
this._handleEntitiesChanged();
}
}
private async _handleEntitiesChanged() {
this._renderEmptySortable = true;
await this.updateComplete;
const container = this.shadowRoot!.querySelector(".entities")!;
while (container.lastElementChild) {
container.removeChild(container.lastElementChild);
}
this._renderEmptySortable = false;
}
private async _createSortable() {
const Sortable = await loadSortable();
this._sortable = new Sortable(
this.shadowRoot!.querySelector(".entities")!,
{
animation: 150,
fallbackClass: "sortable-fallback",
handle: ".handle",
onEnd: async (evt: SortableEvent) => this._rowMoved(evt),
onChoose: (evt: SortableEvent) => {
(evt.item as any).placeholder =
document.createComment("sort-placeholder");
evt.item.after((evt.item as any).placeholder);
},
onEnd: (evt: SortableEvent) => {
// put back in original location
if ((evt.item as any).placeholder) {
(evt.item as any).placeholder.replaceWith(evt.item);
delete (evt.item as any).placeholder;
}
this._rowMoved(evt);
},
}
);
}
private _destroySortable() {
this._sortable?.destroy();
this._sortable = undefined;
}
private async _addEntity(ev: CustomEvent): Promise<void> {
const value = ev.detail.value;
if (value === "") {