"vscode:/vscode.git/clone" did not exist on "adcbffb50febff9353cbb129ea1b3b63ed9450f1"
index.tsx 1.51 KB
Newer Older
dechen lin's avatar
dechen lin committed
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
62
import React, { PropsWithChildren } from "react";
import ReactCodeMirror, { EditorView, Extension } from "@uiw/react-codemirror";
import { loadLanguage } from "@uiw/codemirror-extensions-langs";
import cls from "classnames";
import style from "./index.module.scss";
// import { scrollPastEnd } from "@codemirror/view";

interface IProps {
  className?: string;
  editable?: boolean;
  language?: "json" | "markdown" | "yaml";
  value: string;
  onChange?: (value: string) => void;
  lineWrapping?: boolean;
  onBeforeChange?: (editor: any, data: any, value: any) => void;
}

const CodeMirror: React.FC<PropsWithChildren<IProps>> = ({
  language = "markdown",
  value,
  className,
  onChange,
  lineWrapping,
  onBeforeChange,
  editable,
}) => {
  //   const noScrollPastEnd = scrollPastEnd();
  const extensions = [
    {
      ext: EditorView.lineWrapping,
      on: lineWrapping,
    },
    {
      ext: loadLanguage(language),
      on: true,
    },
  ]
    .map((i) => (i.on ? i.ext : null))
    .filter(Boolean) as Extension[];

  return (
    <ReactCodeMirror
      className={cls("odl-code-mirror", className, style.codeMirror)}
      value={value}
      theme="light"
      basicSetup={{
        lineNumbers: false,
        highlightActiveLineGutter: false,
        foldGutter: false,

        highlightActiveLine: false,
        // syntaxHighlighting: true,
      }}
      editable={editable}
      extensions={extensions}
      onChange={(v) => {
        onChange?.(v);
      }}
    />
  );
};
export default React.memo(CodeMirror);