2021-03-31 20:18:40 +00:00
|
|
|
import { css, CSSResult, customElement, html, LitElement, property, TemplateResult } from "lit-element";
|
2020-11-21 17:32:34 +00:00
|
|
|
|
|
|
|
import CodeMirror from "codemirror";
|
|
|
|
import "codemirror/addon/display/autorefresh";
|
|
|
|
import "codemirror/mode/xml/xml.js";
|
|
|
|
import "codemirror/mode/yaml/yaml.js";
|
2021-02-03 20:58:30 +00:00
|
|
|
import "codemirror/mode/javascript/javascript.js";
|
2020-11-21 17:32:34 +00:00
|
|
|
import "codemirror/mode/python/python.js";
|
2021-03-29 10:16:32 +00:00
|
|
|
import CodeMirrorStyle from "codemirror/lib/codemirror.css";
|
|
|
|
import CodeMirrorTheme from "codemirror/theme/monokai.css";
|
|
|
|
import { ifDefined } from "lit-html/directives/if-defined";
|
2021-03-30 16:20:48 +00:00
|
|
|
import YAML from "yaml";
|
2020-11-21 17:32:34 +00:00
|
|
|
|
2020-12-05 21:08:42 +00:00
|
|
|
@customElement("ak-codemirror")
|
2020-11-21 17:32:34 +00:00
|
|
|
export class CodeMirrorTextarea extends LitElement {
|
2020-12-02 14:44:40 +00:00
|
|
|
@property({type: Boolean})
|
2020-12-01 08:15:41 +00:00
|
|
|
readOnly = false;
|
2020-11-21 17:32:34 +00:00
|
|
|
|
|
|
|
@property()
|
2020-12-01 08:15:41 +00:00
|
|
|
mode = "yaml";
|
2020-11-21 17:32:34 +00:00
|
|
|
|
2021-03-29 10:16:32 +00:00
|
|
|
@property()
|
|
|
|
name?: string;
|
|
|
|
|
2021-03-30 16:20:48 +00:00
|
|
|
editor?: CodeMirror.EditorFromTextArea;
|
|
|
|
|
2021-04-03 10:08:46 +00:00
|
|
|
_value?: string;
|
|
|
|
|
2021-03-29 10:16:32 +00:00
|
|
|
@property()
|
2021-03-30 16:20:48 +00:00
|
|
|
set value(v: string) {
|
2021-03-30 17:56:00 +00:00
|
|
|
if (v === null) return;
|
2021-03-31 20:18:40 +00:00
|
|
|
if (this.editor) {
|
|
|
|
this.editor.setValue(v);
|
2021-04-03 10:08:46 +00:00
|
|
|
} else {
|
|
|
|
this._value = v;
|
2021-03-31 20:18:40 +00:00
|
|
|
}
|
2021-03-30 16:20:48 +00:00
|
|
|
}
|
2021-03-29 10:16:32 +00:00
|
|
|
|
2021-03-30 16:20:48 +00:00
|
|
|
get value(): string {
|
|
|
|
switch (this.mode.toLowerCase()) {
|
|
|
|
case "yaml":
|
|
|
|
return YAML.parse(this.getInnerValue());
|
|
|
|
case "javascript":
|
|
|
|
return JSON.parse(this.getInnerValue());
|
|
|
|
default:
|
|
|
|
return this.getInnerValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private getInnerValue(): string {
|
2021-03-31 20:32:28 +00:00
|
|
|
if (!this.editor) {
|
2021-03-30 16:20:48 +00:00
|
|
|
return "";
|
|
|
|
}
|
2021-03-31 20:32:28 +00:00
|
|
|
return this.editor.getValue();
|
2021-03-30 16:20:48 +00:00
|
|
|
}
|
2020-11-21 17:32:34 +00:00
|
|
|
|
2021-03-29 10:16:32 +00:00
|
|
|
static get styles(): CSSResult[] {
|
2021-03-31 20:18:40 +00:00
|
|
|
return [CodeMirrorStyle, CodeMirrorTheme, css`
|
|
|
|
.CodeMirror-wrap pre {
|
|
|
|
word-break: break-word !important;
|
|
|
|
}
|
|
|
|
`];
|
2020-11-21 17:32:34 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:27:19 +00:00
|
|
|
firstUpdated(): void {
|
2021-03-29 10:16:32 +00:00
|
|
|
const textarea = this.shadowRoot?.querySelector("textarea");
|
2020-11-21 17:32:34 +00:00
|
|
|
if (!textarea) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.editor = CodeMirror.fromTextArea(textarea, {
|
|
|
|
mode: this.mode,
|
2020-11-21 19:48:49 +00:00
|
|
|
theme: "monokai",
|
2020-11-21 17:32:34 +00:00
|
|
|
lineNumbers: false,
|
|
|
|
readOnly: this.readOnly,
|
|
|
|
autoRefresh: true,
|
2021-03-31 20:18:40 +00:00
|
|
|
lineWrapping: true,
|
2021-04-03 10:08:46 +00:00
|
|
|
value: this._value
|
2020-11-21 17:32:34 +00:00
|
|
|
});
|
2020-12-01 16:27:19 +00:00
|
|
|
this.editor.on("blur", () => {
|
2020-11-21 17:32:34 +00:00
|
|
|
this.editor?.save();
|
|
|
|
});
|
|
|
|
}
|
2021-03-29 10:16:32 +00:00
|
|
|
|
|
|
|
render(): TemplateResult {
|
2021-04-03 10:08:46 +00:00
|
|
|
return html`<textarea name=${ifDefined(this.name)}>${ifDefined(this._value)}</textarea>`;
|
2021-03-29 10:16:32 +00:00
|
|
|
}
|
2020-11-21 17:32:34 +00:00
|
|
|
}
|