/
Editor.tsx
61 lines (52 loc) · 1.33 KB
/
Editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from "react";
import ReactCodeMirror from "@uiw/react-codemirror";
import jsYaml from "js-yaml";
import { linter, lintGutter } from "@codemirror/lint";
import { basicSetup } from "codemirror";
//import { StreamLanguage } from "@codemirror/language"
//import { yaml } from "@codemirror/legacy-modes/mode/yaml"
enum Severity {
ERROR = "error",
INFO = "info",
WARNING = "warning"
}
type Diagnostics = {
from: number;
to: number;
message: string;
severity: Severity;
};
const yamlLinter = linter((view) => {
const diagnostics: Diagnostics[] = [];
try {
jsYaml.load(view.state.doc);
} catch (e) {
const loc = e.mark;
const from = loc ? loc.position : 0;
const to = from;
const severity = Severity.ERROR;
diagnostics.push({
from,
to,
message: e.message,
severity
});
}
return diagnostics;
});
type Props = {
value: string;
onChange: (value: string) => void;
};
const Editor = ({ value, onChange }: Props) => {
return (
<>
<ReactCodeMirror
value={value}
onChange={onChange}
extensions={[lintGutter(), yamlLinter, basicSetup]} //, StreamLanguage.define(yaml)]}
/>
</>
);
};
export default Editor;