-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathindex.tsx
142 lines (130 loc) · 4.33 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import type { Extension } from "@codemirror/state";
import type { KeyBinding } from "@codemirror/view";
import { forwardRef } from "react";
import { useActiveCode } from "../../hooks/useActiveCode";
import { useSandpack } from "../../hooks/useSandpack";
import type { CustomLanguage, SandpackInitMode } from "../../types";
import { useClassNames } from "../../utils/classNames";
import { useSandpackId } from "../../utils/useAsyncSandpackId";
import { FileTabs } from "../FileTabs";
import { RunButton } from "../common/RunButton";
import { SandpackStack } from "../common/Stack";
import { CodeMirror } from "./CodeMirror";
import type { CodeMirrorRef } from "./CodeMirror";
import { editorClassName } from "./styles";
export type CodeEditorRef = CodeMirrorRef;
export interface CodeEditorProps {
style?: React.CSSProperties;
className?: string;
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?: KeyBinding[];
/**
* This disables editing of the editor content by the user.
*/
readOnly?: boolean;
/**
* Controls the visibility of Read-only label, which will only
* appears when `readOnly` is `true`
*/
showReadOnly?: boolean;
/**
* Provides a way to add custom language modes by supplying a language
* type, applicable file extensions, and a LanguageSupport instance
* for that syntax mode
*/
additionalLanguages?: CustomLanguage[];
}
export { CodeMirror as CodeEditor };
export const SandpackCodeEditor = forwardRef<CodeMirrorRef, CodeEditorProps>(
(
{
showTabs,
showLineNumbers = false,
showInlineErrors = false,
showRunButton = true,
wrapContent = false,
closableTabs = false,
initMode,
extensions,
extensionsKeymap,
readOnly,
showReadOnly,
additionalLanguages,
className,
...props
},
ref
) => {
const { sandpack } = useSandpack();
const { code, updateCode, readOnly: readOnlyFile } = useActiveCode();
const { activeFile, status, editorState } = sandpack;
const shouldShowTabs = showTabs ?? sandpack.visibleFiles.length > 1;
const classNames = useClassNames();
const handleCodeUpdate = (
newCode: string,
shouldUpdatePreview = true
): void => {
updateCode(newCode, shouldUpdatePreview);
};
const activeFileUniqueId = useSandpackId();
return (
<SandpackStack className={classNames("editor", [className])} {...props}>
{shouldShowTabs && (
<FileTabs
activeFileUniqueId={activeFileUniqueId}
closableTabs={closableTabs}
/>
)}
<div
aria-labelledby={`${activeFile}-${activeFileUniqueId}-tab`}
className={classNames("code-editor", [editorClassName])}
id={`${activeFile}-${activeFileUniqueId}-tab-panel`}
role="tabpanel"
>
<CodeMirror
key={activeFile}
ref={ref}
additionalLanguages={additionalLanguages}
code={code}
editorState={editorState}
extensions={extensions}
extensionsKeymap={extensionsKeymap}
filePath={activeFile}
initMode={initMode || sandpack.initMode}
onCodeUpdate={(newCode: string) =>
handleCodeUpdate(newCode, sandpack.autoReload ?? true)
}
readOnly={readOnly || readOnlyFile}
showInlineErrors={showInlineErrors}
showLineNumbers={showLineNumbers}
showReadOnly={showReadOnly}
wrapContent={wrapContent}
/>
{showRunButton && (!sandpack.autoReload || status === "idle") ? (
<RunButton />
) : null}
</div>
</SandpackStack>
);
}
);