2020-11-21 17:32:34 +00:00
|
|
|
import CodeMirror from "codemirror";
|
2021-09-21 09:31:37 +00:00
|
|
|
import "codemirror/addon/dialog/dialog";
|
2020-11-21 17:32:34 +00:00
|
|
|
import "codemirror/addon/display/autorefresh";
|
2021-09-21 09:31:37 +00:00
|
|
|
import "codemirror/addon/hint/show-hint";
|
2021-04-09 12:19:31 +00:00
|
|
|
import "codemirror/addon/search/search";
|
|
|
|
import "codemirror/addon/search/searchcursor";
|
2021-10-07 16:34:37 +00:00
|
|
|
import "codemirror/mode/htmlmixed/htmlmixed.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-09-21 09:31:37 +00:00
|
|
|
import "codemirror/mode/xml/xml.js";
|
|
|
|
import "codemirror/mode/yaml/yaml.js";
|
|
|
|
import YAML from "yaml";
|
|
|
|
|
2021-10-28 07:48:51 +00:00
|
|
|
import { CSSResult, LitElement, TemplateResult, css, html } from "lit";
|
2021-11-04 21:34:48 +00:00
|
|
|
import { customElement, property } from "lit/decorators.js";
|
|
|
|
import { ifDefined } from "lit/directives/if-defined.js";
|
2021-09-21 09:31:37 +00:00
|
|
|
|
2021-04-09 12:19:31 +00:00
|
|
|
import CodeMirrorDialogStyle from "codemirror/addon/dialog/dialog.css";
|
2021-08-03 15:52:21 +00:00
|
|
|
import CodeMirrorShowHintStyle from "codemirror/addon/hint/show-hint.css";
|
2021-09-21 09:31:37 +00:00
|
|
|
import CodeMirrorStyle from "codemirror/lib/codemirror.css";
|
|
|
|
import CodeMirrorTheme from "codemirror/theme/monokai.css";
|
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 {
|
2021-08-03 15:52:21 +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-04-09 12:19:31 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types
|
|
|
|
set value(v: any) {
|
|
|
|
if (v === null || v === undefined) return;
|
|
|
|
// Value might be an object if within an iron-form, as that calls the getter of value
|
|
|
|
// in the beginning and the calls this setter on reset
|
|
|
|
let textValue = v;
|
|
|
|
if (!(typeof v === "string" || v instanceof String)) {
|
|
|
|
switch (this.mode.toLowerCase()) {
|
|
|
|
case "yaml":
|
|
|
|
textValue = YAML.stringify(v);
|
|
|
|
break;
|
|
|
|
case "javascript":
|
|
|
|
textValue = JSON.stringify(v);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
textValue = v.toString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 20:18:40 +00:00
|
|
|
if (this.editor) {
|
2021-04-09 12:19:31 +00:00
|
|
|
this.editor.setValue(textValue);
|
2021-04-03 10:08:46 +00:00
|
|
|
} else {
|
2021-04-09 12:19:31 +00:00
|
|
|
this._value = textValue;
|
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-04-09 12:19:31 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
get value(): any {
|
|
|
|
try {
|
|
|
|
switch (this.mode.toLowerCase()) {
|
|
|
|
case "yaml":
|
|
|
|
return YAML.parse(this.getInnerValue());
|
|
|
|
case "javascript":
|
|
|
|
return JSON.parse(this.getInnerValue());
|
|
|
|
default:
|
|
|
|
return this.getInnerValue();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return this.getInnerValue();
|
2021-03-30 16:20:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-03 15:52:21 +00:00
|
|
|
return [
|
|
|
|
CodeMirrorStyle,
|
|
|
|
CodeMirrorTheme,
|
|
|
|
CodeMirrorDialogStyle,
|
|
|
|
CodeMirrorShowHintStyle,
|
|
|
|
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",
|
2022-01-03 20:30:28 +00:00
|
|
|
lineNumbers: true,
|
2020-11-21 17:32:34 +00:00
|
|
|
readOnly: this.readOnly,
|
|
|
|
autoRefresh: true,
|
2021-03-31 20:18:40 +00:00
|
|
|
lineWrapping: true,
|
2021-08-03 15:52:21 +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
|
|
|
}
|