Skip to content

Commit 9aad7dc

Browse files
committed
editor object selector cache
1 parent bdf03cb commit 9aad7dc

4 files changed

Lines changed: 20 additions & 158 deletions

File tree

dist/universal-remote-card.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "universal-remote-card",
3-
"version": "4.11.4",
3+
"version": "4.11.5",
44
"description": "Universal Remote Card",
55
"main": "./dist/universal-remote-card.min.js",
66
"type": "module",

src/universal-remote-card-editor.ts

Lines changed: 15 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { hasTemplate, renderTemplate } from 'ha-nunjucks';
22
import { LitElement, TemplateResult, css, html } from 'lit';
33
import { property, state } from 'lit/decorators.js';
44

5-
import { dump, load } from 'js-yaml';
5+
import { load } from 'js-yaml';
66
import { Action, HomeAssistant } from './models/interfaces';
77

88
import {
@@ -26,7 +26,6 @@ import {
2626
IAction,
2727
IBasicActions,
2828
IConfig,
29-
IData,
3029
IElementConfig,
3130
IIconConfig,
3231
ITarget,
@@ -54,8 +53,7 @@ export class UniversalRemoteCardEditor extends LitElement {
5453
@state() errors?: string[];
5554
@state() searchQuery: string = '';
5655

57-
yamlString?: string;
58-
yamlStringsCache: Record<string, string> = {};
56+
yamlCache: Record<string, object> = {};
5957
people: Record<string, string>[] = [];
6058

6159
BASE_TABS = ['general', 'layout', 'elements', 'icons'];
@@ -156,7 +154,7 @@ export class UniversalRemoteCardEditor extends LitElement {
156154
}
157155

158156
toggleGuiMode(_e: Event) {
159-
this.yamlString = undefined;
157+
this.yamlCache = {};
160158
this.configChanged(this.config);
161159
this.guiMode = !this.guiMode;
162160
}
@@ -196,147 +194,8 @@ export class UniversalRemoteCardEditor extends LitElement {
196194
}
197195
}
198196

199-
get yaml(): string {
200-
if (this.yamlString == undefined) {
201-
let yaml = '';
202-
switch (this.baseTabIndex) {
203-
case 3:
204-
case 2:
205-
yaml = dump(this.activeEntry);
206-
break;
207-
case 1:
208-
yaml = dump(this.config.rows);
209-
break;
210-
default:
211-
break;
212-
}
213-
this.yamlString = ['{}', '[]'].includes(yaml.trim()) ? '' : yaml;
214-
}
215-
return this.yamlString ?? '';
216-
}
217-
218-
set yaml(yaml: string | undefined) {
219-
this.yamlString = yaml;
220-
try {
221-
const yamlObj = load(this.yaml);
222-
switch (this.baseTabIndex) {
223-
case 3: {
224-
const entries = structuredClone(this.config.custom_icons ?? []);
225-
entries[this.entryIndex] = yamlObj as IIconConfig;
226-
this.entriesChanged(entries);
227-
break;
228-
}
229-
case 2: {
230-
const entries = structuredClone(this.config.custom_actions ?? []);
231-
switch (
232-
this.renderTemplate(
233-
entries[this.entryIndex].type || 'button',
234-
this.getEntryContext(yamlObj as IElementConfig),
235-
)
236-
) {
237-
case 'touchpad':
238-
case 'circlepad':
239-
if (this.directionTabIndex != 2) {
240-
entries[this.entryIndex] = {
241-
...entries[this.entryIndex],
242-
[this.DIRECTION_TABS[
243-
this.directionTabIndex
244-
] as DirectionAction]: yamlObj,
245-
};
246-
break;
247-
}
248-
// falls through
249-
case 'slider':
250-
case 'button':
251-
default:
252-
entries[this.entryIndex] = yamlObj as IElementConfig;
253-
}
254-
this.entriesChanged(entries);
255-
break;
256-
}
257-
case 1:
258-
this.configChanged({
259-
...this.config,
260-
rows: yamlObj as Row[],
261-
});
262-
break;
263-
default:
264-
break;
265-
}
266-
this.errors = undefined;
267-
} catch (e) {
268-
this.errors = [(e as Error).message];
269-
}
270-
}
271-
272-
handleYamlCodeChanged(e: Event) {
273-
e.stopPropagation();
274-
const yaml = e.detail.value;
275-
if (yaml != this.yaml) {
276-
this.yaml = yaml;
277-
}
278-
}
279-
280-
handleStyleCodeChanged(e: Event) {
281-
e.stopPropagation();
282-
const css = e.detail.value;
283-
if (this.entryIndex > -1 && this.activeEntry) {
284-
if (css != (this.activeEntry as IElementConfig)?.styles) {
285-
this.entryChanged({
286-
...this.activeEntry,
287-
styles: css,
288-
} as IElementConfig);
289-
}
290-
} else {
291-
if (css != this.config.styles) {
292-
this.configChanged({
293-
...this.config,
294-
styles: css,
295-
});
296-
}
297-
}
298-
}
299-
300-
handleActionCodeChanged(e: Event) {
301-
e.stopPropagation();
302-
const actionType = (e.target as HTMLElement).id as ActionType;
303-
const actionYaml = e.detail.value;
304-
this.yamlStringsCache[actionType] = actionYaml;
305-
if (this.activeEntry) {
306-
try {
307-
const actionObj = load(actionYaml) as IData;
308-
if (JSON.stringify(actionObj ?? {}).includes('null')) {
309-
return;
310-
}
311-
this.entryChanged({
312-
...this.activeEntry,
313-
[actionType]: actionObj,
314-
} as unknown as IElementConfig);
315-
this.errors = undefined;
316-
} catch (e) {
317-
this.errors = [(e as Error).message];
318-
}
319-
}
320-
}
321-
322-
handleEvalCodeChanged(e: Event) {
323-
e.stopPropagation();
324-
const actionType = (e.target as HTMLElement).id as ActionType;
325-
const evalString = e.detail.value;
326-
if (this.activeEntry) {
327-
this.entryChanged({
328-
...this.activeEntry,
329-
[actionType]: {
330-
...(this.activeEntry as IElementConfig)[actionType],
331-
eval: evalString,
332-
},
333-
} as IElementConfig);
334-
}
335-
}
336-
337197
handleBaseTabSelected(e: Event) {
338-
this.yamlStringsCache = {};
339-
this.yamlString = undefined;
198+
this.yamlCache = {};
340199
this.entryIndex = -1;
341200
this.guiMode = true;
342201
const i = this.BASE_TABS.indexOf(e.detail.name);
@@ -347,7 +206,7 @@ export class UniversalRemoteCardEditor extends LitElement {
347206
}
348207

349208
handleActionsTabSelected(e: Event) {
350-
this.yamlStringsCache = {};
209+
this.yamlCache = {};
351210
const i = this.ACTION_TABS.indexOf(e.detail.name);
352211
if (this.actionsTabIndex == i) {
353212
return;
@@ -356,8 +215,7 @@ export class UniversalRemoteCardEditor extends LitElement {
356215
}
357216

358217
handleDirectionTabSelected(e: Event) {
359-
this.yamlString = undefined;
360-
this.yamlStringsCache = {};
218+
this.yamlCache = {};
361219
const i = this.DIRECTION_TABS.indexOf(e.detail.name);
362220
if (this.directionTabIndex == i) {
363221
return;
@@ -367,7 +225,6 @@ export class UniversalRemoteCardEditor extends LitElement {
367225
}
368226

369227
handleSelectorChange(e: Event) {
370-
this.yamlStringsCache = {};
371228
const key = (e.target as HTMLElement).id;
372229
let value = e.detail.value;
373230
if (key == 'config_entry_id' && this.baseTabIndex == 0) {
@@ -530,8 +387,7 @@ export class UniversalRemoteCardEditor extends LitElement {
530387
}
531388

532389
editEntry(e: Event) {
533-
this.yamlStringsCache = {};
534-
this.yamlString = undefined;
390+
this.yamlCache = {};
535391
const i = (e.currentTarget as unknown as Event & Record<'index', number>)
536392
.index;
537393
switch (this.baseTabIndex) {
@@ -547,12 +403,12 @@ export class UniversalRemoteCardEditor extends LitElement {
547403
}
548404

549405
exitEditEntry(_e: Event) {
550-
this.yamlStringsCache = {};
551-
this.yamlString = undefined;
406+
this.yamlCache = {};
552407
this.entryIndex = -1;
553408
}
554409

555410
setActionsTab(i: number) {
411+
this.yamlCache = {};
556412
let entry = this.config.custom_actions?.[i] ?? {
557413
name: '',
558414
};
@@ -626,6 +482,8 @@ export class UniversalRemoteCardEditor extends LitElement {
626482
) != 'none'
627483
) {
628484
this.actionsTabIndex = 1;
485+
} else {
486+
this.actionsTabIndex = 0;
629487
}
630488
break;
631489
}
@@ -897,6 +755,10 @@ export class UniversalRemoteCardEditor extends LitElement {
897755
} else if (key == 'this') {
898756
value = this.baseTabIndex > 1 ? this.activeEntry : this.config;
899757
}
758+
if (Object.keys(selector)[0] == 'object') {
759+
this.yamlCache[key] ??= value as object;
760+
value = this.yamlCache[key];
761+
}
900762

901763
return html`<ha-selector
902764
.hass=${hass}

0 commit comments

Comments
 (0)