mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-29 04:06:35 +00:00
Add code editor to raw config editor
This commit is contained in:
parent
7a8c9d7c12
commit
9f4ae5d932
@ -26,10 +26,16 @@ export class HuiCodeEditor extends HTMLElement {
|
|||||||
shadowRoot.innerHTML = `
|
shadowRoot.innerHTML = `
|
||||||
<style>
|
<style>
|
||||||
${codeMirrorCSS}
|
${codeMirrorCSS}
|
||||||
|
.CodeMirror {
|
||||||
|
height: var(--code-mirror-height, 300px);
|
||||||
|
color: var(--code-mirror-color, black);
|
||||||
|
direction: var(--code-mirror-direction, ltr);
|
||||||
|
}
|
||||||
</style>`;
|
</style>`;
|
||||||
}
|
}
|
||||||
|
|
||||||
set value(value: string) {
|
set value(value: string) {
|
||||||
|
console.log(value);
|
||||||
if (this.cm) {
|
if (this.cm) {
|
||||||
if (value !== this.cm.getValue()) {
|
if (value !== this.cm.getValue()) {
|
||||||
this.cm.setValue(value);
|
this.cm.setValue(value);
|
||||||
@ -38,6 +44,10 @@ export class HuiCodeEditor extends HTMLElement {
|
|||||||
this._value = value;
|
this._value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get value() {
|
||||||
|
return this.cm.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
public connectedCallback() {
|
public connectedCallback() {
|
||||||
if (!this.cm) {
|
if (!this.cm) {
|
||||||
this.cm = CodeMirror(this.shadowRoot, {
|
this.cm = CodeMirror(this.shadowRoot, {
|
||||||
|
@ -14,8 +14,10 @@ import { Lovelace } from "./types";
|
|||||||
|
|
||||||
import "../../components/ha-icon";
|
import "../../components/ha-icon";
|
||||||
import { haStyle } from "../../resources/ha-style";
|
import { haStyle } from "../../resources/ha-style";
|
||||||
|
import "./components/hui-code-editor";
|
||||||
const TAB_INSERT = " ";
|
// This is not a duplicate import, one is for types, one is for element.
|
||||||
|
// tslint:disable-next-line
|
||||||
|
import { HuiCodeEditor } from "./components/hui-code-editor";
|
||||||
|
|
||||||
const lovelaceStruct = struct.interface({
|
const lovelaceStruct = struct.interface({
|
||||||
title: "string?",
|
title: "string?",
|
||||||
@ -67,45 +69,31 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
</app-toolbar>
|
</app-toolbar>
|
||||||
</app-header>
|
</app-header>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<textarea
|
<hui-code-editor @code-changed="${this._yamlChanged}">
|
||||||
autocomplete="off"
|
</hui-code-editor>
|
||||||
autocorrect="off"
|
|
||||||
autocapitalize="off"
|
|
||||||
spellcheck="false"
|
|
||||||
@input="${this._yamlChanged}"
|
|
||||||
></textarea>
|
|
||||||
</div>
|
</div>
|
||||||
</app-header-layout>
|
</app-header-layout>
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected firstUpdated() {
|
protected firstUpdated() {
|
||||||
const textArea = this.textArea;
|
const codeEditor = this.codeEditor;
|
||||||
textArea.value = yaml.safeDump(this.lovelace!.config);
|
codeEditor.value = yaml.safeDump(this.lovelace!.config);
|
||||||
textArea.addEventListener("keydown", (e) => {
|
codeEditor.addEventListener("keydown", (e) => {
|
||||||
if (e.keyCode === 51) {
|
if (e.keyCode === 51) {
|
||||||
this._hashAdded = true;
|
this._hashAdded = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
});
|
||||||
if (e.keyCode !== 9) {
|
codeEditor.addEventListener("code-changed", (e) => {
|
||||||
|
this._hash = this._hashAdded || this.codeEditor.value.includes("#");
|
||||||
|
if (this._changed) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
window.onbeforeunload = () => {
|
||||||
e.preventDefault();
|
return true;
|
||||||
|
};
|
||||||
// tab was pressed, get caret position/selection
|
this._changed = true;
|
||||||
const val = textArea.value;
|
|
||||||
const start = textArea.selectionStart;
|
|
||||||
const end = textArea.selectionEnd;
|
|
||||||
|
|
||||||
// set textarea value to: text before caret + tab + text after caret
|
|
||||||
textArea.value =
|
|
||||||
val.substring(0, start) + TAB_INSERT + val.substring(end);
|
|
||||||
|
|
||||||
// put caret at right position again
|
|
||||||
textArea.selectionStart = textArea.selectionEnd =
|
|
||||||
start + TAB_INSERT.length;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,6 +101,10 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
return [
|
return [
|
||||||
haStyle,
|
haStyle,
|
||||||
css`
|
css`
|
||||||
|
:host {
|
||||||
|
--code-mirror-height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
app-header-layout {
|
app-header-layout {
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
}
|
}
|
||||||
@ -132,16 +124,8 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
height: calc(100vh - 68px);
|
height: calc(100vh - 68px);
|
||||||
}
|
}
|
||||||
|
|
||||||
textarea {
|
hui-code-editor {
|
||||||
box-sizing: border-box;
|
|
||||||
height: 100%;
|
height: 100%;
|
||||||
width: 100%;
|
|
||||||
resize: none;
|
|
||||||
border: 0;
|
|
||||||
outline: 0;
|
|
||||||
font-size: 12pt;
|
|
||||||
font-family: "Courier New", Courier, monospace;
|
|
||||||
padding: 8px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.save-button {
|
.save-button {
|
||||||
@ -185,7 +169,7 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
|
|
||||||
let value;
|
let value;
|
||||||
try {
|
try {
|
||||||
value = yaml.safeLoad(this.textArea.value);
|
value = yaml.safeLoad(this.codeEditor.value);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
alert(`Unable to parse YAML: ${err}`);
|
alert(`Unable to parse YAML: ${err}`);
|
||||||
this._saving = false;
|
this._saving = false;
|
||||||
@ -208,19 +192,8 @@ class LovelaceFullConfigEditor extends LitElement {
|
|||||||
this._hashAdded = false;
|
this._hashAdded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _yamlChanged() {
|
private get codeEditor(): HuiCodeEditor {
|
||||||
this._hash = this._hashAdded || this.textArea.value.includes("#");
|
return this.shadowRoot!.querySelector("hui-code-editor")! as HuiCodeEditor;
|
||||||
if (this._changed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
window.onbeforeunload = () => {
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
this._changed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private get textArea(): HTMLTextAreaElement {
|
|
||||||
return this.shadowRoot!.querySelector("textarea")!;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user