-
Notifications
You must be signed in to change notification settings - Fork 373
/
index.tsx
106 lines (96 loc) · 3.02 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { useClasser } from "@code-hike/classer";
import type { Extension } from "@codemirror/state";
import type { KeyBinding } from "@codemirror/view";
import * as React from "react";
import { RunButton } from "../../common/RunButton";
import { SandpackStack } from "../../common/Stack";
import { useActiveCode } from "../../hooks/useActiveCode";
import { useSandpack } from "../../hooks/useSandpack";
import type { SandpackInitMode } from "../../types";
import { FileTabs } from "../FileTabs";
import { CodeMirror } from "./CodeMirror";
import type { CodeMirrorRef } from "./CodeMirror";
export type CodeEditorRef = CodeMirrorRef;
export interface CodeEditorProps {
customStyle?: React.CSSProperties;
showTabs?: boolean;
showLineNumbers?: boolean;
showInlineErrors?: boolean;
showRunButton?: boolean;
wrapContent?: boolean;
closableTabs?: boolean;
/**
* This provides a way to control how some components are going to
* be initialized on the page. The CodeEditor and the Preview components
* are quite expensive and might overload the memory usage, so this gives
* a certain control of when to initialize them.
*/
initMode?: SandpackInitMode;
/**
* CodeMirror extensions for the editor state, which can
* provide extra features and functionalities to the editor component.
*/
extensions?: Extension[];
/**
* Property to register CodeMirror extension keymap.
*/
extensionsKeymap?: Array<readonly KeyBinding[]>;
id?: string;
}
export { CodeMirror as CodeEditor };
/**
* @category Components
*/
export const SandpackCodeEditor = React.forwardRef<
CodeMirrorRef,
CodeEditorProps
>(
(
{
customStyle,
showTabs,
showLineNumbers = false,
showInlineErrors = false,
showRunButton = true,
wrapContent = false,
closableTabs = false,
initMode,
extensions,
extensionsKeymap,
id,
},
ref
) => {
const { sandpack } = useSandpack();
const { code, updateCode } = useActiveCode();
const { activePath, status, editorState } = sandpack;
const shouldShowTabs = showTabs ?? sandpack.openPaths.length > 1;
const c = useClasser("sp");
const handleCodeUpdate = (newCode: string): void => {
updateCode(newCode);
};
return (
<SandpackStack customStyle={customStyle}>
{shouldShowTabs ? <FileTabs closableTabs={closableTabs} /> : null}
<div className={c("code-editor")}>
<CodeMirror
key={activePath}
ref={ref}
code={code}
editorState={editorState}
extensions={extensions}
extensionsKeymap={extensionsKeymap}
filePath={activePath}
id={id}
initMode={initMode || sandpack.initMode}
onCodeUpdate={handleCodeUpdate}
showInlineErrors={showInlineErrors}
showLineNumbers={showLineNumbers}
wrapContent={wrapContent}
/>
{showRunButton && status === "idle" ? <RunButton /> : null}
</div>
</SandpackStack>
);
}
);