2022-01-18 15:52:29 +01:00
|
|
|
|
console.info("Config Editor 3.2");
|
2021-12-03 02:46:05 +01:00
|
|
|
|
const LitElement = window.LitElement || Object.getPrototypeOf(customElements.get("hui-masonry-view") );
|
2021-12-01 03:01:04 +01:00
|
|
|
|
const html = LitElement.prototype.html;
|
|
|
|
|
|
|
|
|
|
class ConfigEditor extends LitElement {
|
|
|
|
|
|
|
|
|
|
static get properties() {
|
|
|
|
|
return {
|
2021-12-03 02:46:05 +01:00
|
|
|
|
_hass: {type: Object},
|
2021-12-01 03:01:04 +01:00
|
|
|
|
code: {type: String},
|
|
|
|
|
fileList: {type: Array},
|
|
|
|
|
openedFile: {type: String},
|
2022-01-17 18:54:19 +01:00
|
|
|
|
infoLine: {type: String},
|
|
|
|
|
alertLine: {type: String},
|
|
|
|
|
edit: {},
|
2021-12-03 02:46:05 +01:00
|
|
|
|
};
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.code = '';
|
|
|
|
|
this.fileList = [];
|
|
|
|
|
this.openedFile = '';
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.infoLine = '';
|
|
|
|
|
this.alertLine = '';
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2022-01-18 03:27:10 +01:00
|
|
|
|
const hver=this._hass.states['config_editor.version']
|
2022-01-18 15:52:29 +01:00
|
|
|
|
if(!hver){return html`<ha-card>Missing 'config_editor:' in configuration.yaml
|
|
|
|
|
for github.com/htmltiger/config-editor</ha-card>`;}
|
|
|
|
|
if(hver.state != '3'){console.log(hver);return html`<ha-card>Please upgrade
|
|
|
|
|
github.com/htmltiger/config-editor</ha-card>`;}
|
2021-12-01 03:01:04 +01:00
|
|
|
|
if(this.fileList.length<1){
|
2022-01-08 20:11:27 +01:00
|
|
|
|
this.openedFile = localStorage.getItem('config_editorOpen');
|
2022-01-17 15:37:25 +01:00
|
|
|
|
this.edit.plainBox = localStorage.getItem('config_editorPlain') ? true : false;
|
2022-01-18 03:27:10 +01:00
|
|
|
|
this.edit.ext = localStorage.getItem('config_editorExt');
|
|
|
|
|
if(!this.edit.ext){this.edit.ext='yaml';}
|
2022-01-08 20:11:27 +01:00
|
|
|
|
if(!this.openedFile){
|
|
|
|
|
this.openedFile = '';
|
|
|
|
|
}
|
|
|
|
|
this.List();
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
2022-01-08 20:11:27 +01:00
|
|
|
|
|
2021-12-01 03:01:04 +01:00
|
|
|
|
return html`
|
|
|
|
|
<ha-card>
|
2022-01-11 20:21:14 +01:00
|
|
|
|
<div style="min-height: calc(100vh - var(--header-height));">
|
2022-01-18 03:27:10 +01:00
|
|
|
|
<div style="text-align:right">
|
|
|
|
|
<select @change=${this.extChange}>
|
2022-01-18 15:52:29 +01:00
|
|
|
|
${["yaml","py","json","conf","js","txt","log"].map(value =>
|
|
|
|
|
html`<option ?selected=${value === this.edit.ext }
|
|
|
|
|
value=${value}>${value.toUpperCase()}</option>`)}
|
2022-01-18 03:27:10 +01:00
|
|
|
|
</select>
|
2022-01-18 15:52:29 +01:00
|
|
|
|
<label style="cursor:pointer">Plain text <input type="checkbox"
|
|
|
|
|
?checked=${true === this.edit.plainBox }
|
|
|
|
|
name="plain" value="1" @change=${this.plainChange}></label>
|
2021-12-01 03:01:04 +01:00
|
|
|
|
</div>
|
2022-01-18 15:52:29 +01:00
|
|
|
|
${this.edit.plainBox ?
|
|
|
|
|
html`<textarea
|
|
|
|
|
style="width:98%;height:80vh;padding:5px;overflow-wrap:normal;white-space:pre"
|
|
|
|
|
rows="10" @change=${this.updateText} id="code">${this.code}</textarea>` :
|
|
|
|
|
html`<ha-code-editor id="code" mode="yaml"
|
|
|
|
|
@value-changed=${this.updateText}></ha-code-editor>`}
|
|
|
|
|
</div>
|
|
|
|
|
<div style="position:-webkit-sticky;position:sticky;bottom:0;z-index:2;
|
|
|
|
|
background:var(--app-header-background-color);
|
|
|
|
|
color:var(--app-header-text-color,white)">
|
2022-01-17 18:54:19 +01:00
|
|
|
|
<div>${this.alertLine}</div>
|
2022-01-11 20:21:14 +01:00
|
|
|
|
<div>
|
|
|
|
|
<button @click="${this.List}">Get List</button>
|
|
|
|
|
<select @change=${this.Load}>
|
2022-01-18 15:52:29 +01:00
|
|
|
|
${[''].concat(this.fileList).map(value =>
|
|
|
|
|
html`<option ?selected=${value === this.openedFile}
|
|
|
|
|
value=${value}>${value}</option>`)}
|
2022-01-11 20:21:14 +01:00
|
|
|
|
</select>
|
|
|
|
|
<button @click="${this.Save}">Save</button>
|
|
|
|
|
</div>
|
2022-01-17 18:54:19 +01:00
|
|
|
|
<code>#${this.infoLine}</code>
|
2022-01-04 17:50:54 +01:00
|
|
|
|
</div>
|
2021-12-01 03:01:04 +01:00
|
|
|
|
</ha-card>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 03:27:10 +01:00
|
|
|
|
extChange(e){
|
|
|
|
|
this.edit.ext = e.target.value;
|
|
|
|
|
localStorage.setItem('config_editorExt',this.edit.ext);
|
|
|
|
|
this.openedFile = '';
|
|
|
|
|
this.oldText(this);
|
|
|
|
|
this.List();
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 15:37:25 +01:00
|
|
|
|
plainChange(e){
|
|
|
|
|
if(this.edit.plainBox = !this.edit.plainBox){
|
|
|
|
|
localStorage.setItem('config_editorPlain','1');
|
|
|
|
|
}else{
|
|
|
|
|
localStorage.removeItem('config_editorPlain');
|
|
|
|
|
}
|
|
|
|
|
this.List();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 03:01:04 +01:00
|
|
|
|
updateText(e) {
|
2022-01-17 15:37:25 +01:00
|
|
|
|
this.code = this.edit.plainBox ? e.target.value : e.detail.value;
|
2022-01-11 20:21:14 +01:00
|
|
|
|
if(this.openedFile){localStorage.setItem('config_editorText', this.code);}
|
2022-01-08 20:11:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 22:00:35 +01:00
|
|
|
|
Unsave(){
|
|
|
|
|
this.code = localStorage.getItem('config_editorUnsaved');
|
|
|
|
|
this.renderRoot.querySelector('#code').value=this.code;
|
|
|
|
|
localStorage.removeItem('config_editorUnsaved');
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.alertLine = '';
|
2022-01-10 22:00:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-08 20:11:27 +01:00
|
|
|
|
async oldText(dhis){
|
2022-01-10 22:00:35 +01:00
|
|
|
|
dhis.Load({target:{value:dhis.openedFile}});
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-03 02:46:05 +01:00
|
|
|
|
async Coder(){
|
|
|
|
|
if(customElements.get("developer-tools-event")){return;}
|
|
|
|
|
await customElements.whenDefined("partial-panel-resolver");
|
|
|
|
|
const p = document.createElement('partial-panel-resolver');
|
|
|
|
|
p.hass = {panels: [{url_path: "tmp", component_name: "developer-tools"}]};
|
|
|
|
|
p._updateRoutes();
|
|
|
|
|
await p.routerOptions.routes.tmp.load()
|
|
|
|
|
await customElements.whenDefined("developer-tools-router");
|
|
|
|
|
const d = document.createElement("developer-tools-router");
|
2021-12-03 03:26:04 +01:00
|
|
|
|
await d.routerOptions.routes.event.load();
|
2021-12-03 02:46:05 +01:00
|
|
|
|
}
|
2021-12-01 03:01:04 +01:00
|
|
|
|
async List(){
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.infoLine = 'List Loading...';
|
2022-01-18 15:52:29 +01:00
|
|
|
|
const e=(await this._hass.callWS({type: "config_editor/ws", action: 'list',
|
|
|
|
|
data: '', file: '', ext: this.edit.ext}));
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.infoLine = e.msg;
|
2021-12-30 03:44:32 +01:00
|
|
|
|
this.fileList = e.file.slice().sort();
|
2022-01-18 03:27:10 +01:00
|
|
|
|
if(this.openedFile.endsWith("."+this.edit.ext)){
|
2022-01-10 22:00:35 +01:00
|
|
|
|
setTimeout(this.oldText, 500, this);
|
2022-01-08 20:11:27 +01:00
|
|
|
|
}
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
2022-01-17 18:54:19 +01:00
|
|
|
|
|
2021-12-01 03:01:04 +01:00
|
|
|
|
async Load(x) {
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.code = ''; this.renderRoot.querySelector('#code').value='';this.infoLine = '';
|
2021-12-01 03:01:04 +01:00
|
|
|
|
this.openedFile = x.target.value
|
2022-01-08 20:11:27 +01:00
|
|
|
|
if(this.openedFile){
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.infoLine = 'Loading: '+this.openedFile;
|
2022-01-18 15:52:29 +01:00
|
|
|
|
const e=(await this._hass.callWS({type: "config_editor/ws", action: 'load',
|
|
|
|
|
data: '', file: this.openedFile, ext: this.edit.ext}));
|
2022-01-08 20:11:27 +01:00
|
|
|
|
this.openedFile = e.file;
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.infoLine = e.msg;
|
2022-01-18 15:52:29 +01:00
|
|
|
|
const uns={f:localStorage.getItem('config_editorOpen'),
|
|
|
|
|
d:localStorage.getItem('config_editorText')};
|
2022-01-10 22:00:35 +01:00
|
|
|
|
if(uns.f == this.openedFile && uns.d && uns.d != e.data){
|
|
|
|
|
localStorage.setItem('config_editorUnsaved', uns.d);
|
2022-01-18 15:52:29 +01:00
|
|
|
|
this.alertLine = html`<i style="background:#ff7a81;cursor:pointer"
|
|
|
|
|
@click="${this.Unsave}"> Load unsaved from browser </i>`;
|
2022-01-10 22:00:35 +01:00
|
|
|
|
}else{
|
2022-01-17 18:54:19 +01:00
|
|
|
|
localStorage.removeItem('config_editorText');this.alertLine = '';
|
2022-01-10 22:00:35 +01:00
|
|
|
|
}
|
2022-01-08 20:11:27 +01:00
|
|
|
|
this.renderRoot.querySelector('#code').value=e.data;
|
|
|
|
|
this.code = e.data;
|
|
|
|
|
}
|
|
|
|
|
localStorage.setItem('config_editorOpen', this.openedFile);
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
|
|
|
|
async Save() {
|
2021-12-05 04:28:10 +01:00
|
|
|
|
if(this.renderRoot.querySelector('#code').value != this.code){
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.infoLine='Something not right!';
|
2021-12-01 03:01:04 +01:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-05 04:28:10 +01:00
|
|
|
|
if(!this.openedFile && this.code){
|
2022-01-18 13:21:10 +01:00
|
|
|
|
this.openedFile=prompt("type abc."+this.edit.ext+" or folder/abc."+this.edit.ext);
|
2021-12-05 04:28:10 +01:00
|
|
|
|
}
|
2022-01-18 13:21:10 +01:00
|
|
|
|
if(this.openedFile && this.openedFile.endsWith("."+this.edit.ext)){
|
2022-01-17 18:54:19 +01:00
|
|
|
|
if(!this.code){this.infoLine='';this.infoLine = 'Text is empty!'; return;}
|
|
|
|
|
this.infoLine = 'Saving: '+this.openedFile;
|
2022-01-18 15:52:29 +01:00
|
|
|
|
const e=(await this._hass.callWS({type: "config_editor/ws", action: 'save',
|
|
|
|
|
data: this.code, file: this.openedFile, ext: this.edit.ext}));
|
2022-01-17 18:54:19 +01:00
|
|
|
|
this.infoLine = e.msg;
|
2022-01-10 22:00:35 +01:00
|
|
|
|
localStorage.removeItem('config_editorText');
|
2021-12-05 04:28:10 +01:00
|
|
|
|
}else{this.openedFile='';}
|
2022-01-10 22:00:35 +01:00
|
|
|
|
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getCardSize() {
|
|
|
|
|
return 5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setConfig(config) {
|
2022-01-18 03:27:10 +01:00
|
|
|
|
this.edit = {options: config, plainBox: false, ext: ''};
|
2021-12-03 02:46:05 +01:00
|
|
|
|
this.Coder();
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set hass(hass) {
|
|
|
|
|
this._hass = hass;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shouldUpdate(changedProps) {
|
2022-01-18 15:52:29 +01:00
|
|
|
|
for(const e of ['code','openedFile','fileList','infoLine','alertLine','edit']) {
|
|
|
|
|
if(changedProps.has(e)){return true;}
|
|
|
|
|
}
|
2021-12-01 03:01:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} customElements.define('config-editor-card', ConfigEditor);
|
|
|
|
|
|
|
|
|
|
window.customCards = window.customCards || [];
|
|
|
|
|
window.customCards.push({
|
|
|
|
|
type: 'config-editor-card',
|
|
|
|
|
name: 'Config Editor Card',
|
|
|
|
|
preview: false,
|
|
|
|
|
description: 'Basic editor for configuration.yaml'
|
|
|
|
|
});
|