This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/web/src/elements/CodeMirror.ts

163 lines
5.1 KiB
TypeScript
Raw Normal View History

import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
import { html as htmlLang } from "@codemirror/lang-html";
import { javascript } from "@codemirror/lang-javascript";
import { python } from "@codemirror/lang-python";
import { xml } from "@codemirror/lang-xml";
import {
LanguageSupport,
StreamLanguage,
defaultHighlightStyle,
syntaxHighlighting,
} from "@codemirror/language";
import * as yamlMode from "@codemirror/legacy-modes/mode/yaml";
import { Compartment, EditorState, Extension } from "@codemirror/state";
import { oneDark } from "@codemirror/theme-one-dark";
import { EditorView, drawSelection, keymap, lineNumbers } from "@codemirror/view";
import { EVENT_THEME_CHANGE } from "@goauthentik/common/constants";
web: re-organise frontend and cleanup common code (#3572) * fix repo in api client Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * web: re-organise files to match their interface Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * core: include version in script tags Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * cleanup maybe broken Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * revert rename Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * web: get rid of Client.ts Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * move more to common Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * more moving Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * format Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * unfuck files that vscode fucked, thanks Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * move more Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * finish moving (maybe) Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * ok more moving Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * fix more stuff that vs code destroyed Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * get rid "web" prefix for virtual package Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * fix locales Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * use custom base element Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * fix css file Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * don't run autoDetectLanguage when importing locale Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * fix circular dependencies Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> * web: fix build Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org> Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
2022-09-14 22:05:21 +00:00
import { AKElement } from "@goauthentik/elements/Base";
import YAML from "yaml";
import { customElement, property } from "lit/decorators.js";
2020-11-21 17:32:34 +00:00
import { UiThemeEnum } from "@goauthentik/api";
2020-12-05 21:08:42 +00:00
@customElement("ak-codemirror")
export class CodeMirrorTextarea<T> extends AKElement {
@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
@property()
name?: string;
@property({ type: Boolean })
parseValue = true;
editor?: EditorView;
_value?: string;
theme: Compartment;
themeLight: Extension;
themeDark: Extension;
@property()
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types
set value(v: T | string) {
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;
}
}
if (this.editor) {
this.editor.dispatch({
changes: { from: 0, to: this.editor.state.doc.length, insert: textValue as string },
});
} else {
this._value = textValue as string;
}
}
get value(): T | string {
if (!this.parseValue) {
return this.getInnerValue();
}
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();
}
}
constructor() {
super();
this.theme = new Compartment();
this.themeLight = EditorView.theme(
{
"&": {
backgroundColor: "var(--pf-global--BackgroundColor--light-300)",
},
},
{ dark: false },
);
this.themeDark = oneDark;
}
private getInnerValue(): string {
if (!this.editor) {
return "";
}
return this.editor.state.doc.toString();
}
2020-11-21 17:32:34 +00:00
getLanguageExtension(): LanguageSupport | undefined {
switch (this.mode.toLowerCase()) {
case "xml":
return xml();
case "javascript":
return javascript();
case "html":
return htmlLang();
case "python":
return python();
case "yaml":
return new LanguageSupport(StreamLanguage.define(yamlMode.yaml));
}
return undefined;
2020-11-21 17:32:34 +00:00
}
firstUpdated(): void {
this.addEventListener(EVENT_THEME_CHANGE, ((ev: CustomEvent<UiThemeEnum>) => {
if (ev.detail === UiThemeEnum.Dark) {
this.editor?.dispatch({
effects: this.theme.reconfigure(this.themeDark),
});
} else {
this.editor?.dispatch({
effects: this.theme.reconfigure(this.themeLight),
});
}
}) as EventListener);
const extensions = [
history(),
keymap.of([...defaultKeymap, ...historyKeymap]),
syntaxHighlighting(defaultHighlightStyle),
this.getLanguageExtension(),
lineNumbers(),
drawSelection(),
EditorView.lineWrapping,
EditorState.readOnly.of(this.readOnly),
EditorState.tabSize.of(2),
this.theme.of(this.activeTheme === UiThemeEnum.Dark ? this.themeLight : this.themeDark),
];
this.editor = new EditorView({
extensions: extensions.filter((p) => p) as Extension[],
root: this.shadowRoot || document,
doc: this._value,
2020-11-21 17:32:34 +00:00
});
this.shadowRoot?.appendChild(this.editor.dom);
}
2020-11-21 17:32:34 +00:00
}