-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheditor.js
More file actions
131 lines (116 loc) · 4.73 KB
/
Copy patheditor.js
File metadata and controls
131 lines (116 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Super Text Input Editor Component
* Provides a configuration interface for the Super Text Input card
*
* Key features:
* - Form-based configuration for basic settings
* - YAML editors for complex configurations
* - Optimized update cycle to prevent YAML editor state loss
*/
import BaseCardEditor from "./base-editor.js?v=0.3.22";
// Get LitElement base class from Home Assistant frontend
const LitElement = customElements.get("home-assistant-main")
? Object.getPrototypeOf(customElements.get("home-assistant-main"))
: Object.getPrototypeOf(customElements.get("hui-view"));
const html = LitElement.prototype.html;
const css = LitElement.prototype.css;
class SuperTextInputEditor extends BaseCardEditor {
/**
* Define reactive properties
* Note: We only track _config as other properties are handled by BaseCardEditor
*/
static get properties() {
return {
_config: { type: Object }
};
}
/**
* Critical update cycle control
* Prevents loss of YAML editor state by blocking rapid re-renders
* Uses timestamp tracking to create a 100ms window where updates are blocked
* after YAML changes while allowing other updates to proceed normally
*/
shouldUpdate(changedProps) {
if (changedProps.has('_config')) {
const lastYamlUpdate = this._lastYamlUpdate || 0;
return Date.now() - lastYamlUpdate > 100;
}
return true;
}
/**
* Known configuration properties that receive special handling
* Used to filter unknown props into the "other" YAML editor
*/
static HANDLED_PROPS = new Set([
"type", "name", "entity", "label", "placeholder",
"update_mode", "debounce_time", "hide_label", "compact_buttons",
"style", "buttons", "change_action",
]);
/**
* Available update mode options for the text input
*/
static UPDATE_MODE_OPTIONS = [
{ value: "blur", label: "On Blur" },
{ value: "realtime", label: "Real-time" },
];
/**
* Filters configuration object for unknown properties
* These properties are displayed in the "other" YAML editor
*/
_getOtherProps() {
return Object.entries(this._config)
.filter(([key, value]) =>
!SuperTextInputEditor.HANDLED_PROPS.has(key) &&
value !== undefined &&
typeof key === "string" &&
isNaN(parseInt(key))
)
.reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {});
}
/**
* Initialize the editor with configuration
*/
setConfig(config) {
this._config = config;
this.loadEntityPicker();
}
/**
* Render the configuration interface
* Layout:
* 1. Basic form fields for common settings
* 2. Conditional fields based on update mode
* 3. YAML editors for complex configurations
*/
render() {
if (!this._config) {
return html``;
}
const otherProps = this._getOtherProps();
return html`
<div class="card-config">
${this.buildEntityPickerField("Entity (Required)", "entity", this._config.entity, ["input_text", "text"])}
${this.buildTextField("Name (Optional)", "name", this._config.name)}
${this.buildTextField("Label (Optional)", "label", this._config.label)}
${this.buildTextField("Placeholder (Optional)", "placeholder", this._config.placeholder)}
${this.buildSwitchField("Hide Label (slim mode)", "hide_label", this._config.hide_label, false)}
${this.buildSwitchField("Compact Buttons (tighter spacing)", "compact_buttons", this._config.compact_buttons, false)}
${this.buildSelectField(
"Update Mode",
"update_mode",
SuperTextInputEditor.UPDATE_MODE_OPTIONS,
this._config.update_mode,
"blur"
)}
${this._config.update_mode === "realtime"
? this.buildNumberField("Update Frequency (ms)", "debounce_time", this._config.debounce_time, 1000, 100)
: ""}
${this.buildYamlEditor("Styles (optional)", "style", this._config.style)}
${this.buildYamlEditor("Buttons/Icons (optional)", "buttons", this._config.buttons)}
${this.buildYamlEditor("Additional Text change action (optional)", "change_action", this._config.change_action)}
${this.buildYamlEditor("Other Configuration Props (danger!)", "other", otherProps)}
</div>
`;
}
}
// Register the editor component
customElements.define("super-text-input-editor", SuperTextInputEditor);