-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathEditorContainer.tsx
More file actions
64 lines (56 loc) · 2.03 KB
/
EditorContainer.tsx
File metadata and controls
64 lines (56 loc) · 2.03 KB
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
/**
* (c) 2021, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import { useCallback } from "react";
import { useProjectFileText } from "../project/project-hooks";
import { useSettings } from "../settings/settings";
import { WorkbenchSelection } from "../workbench/use-selection";
import Editor from "./codemirror/CodeMirror";
import ModuleOverlay from "./ModuleOverlay";
import { usePersistentProject } from "../project-persistence/persistent-project-hooks";
import * as Y from "yjs";
interface EditorContainerProps {
selection: WorkbenchSelection;
}
/**
* Container for the editor that integrates it with the app settings
* and wires it to the currently open file.
*/
const EditorContainer = ({ selection }: EditorContainerProps) => {
const [settings, setSettings] = useSettings();
const disableV2OnlyFeaturesWarning = useCallback(() => {
setSettings({ ...settings, warnForApiUnsupportedByDevice: false });
}, [setSettings, settings]);
// Note fileInfo is not updated for ordinary text edits.
const [fileInfo, onFileChange] = useProjectFileText(selection.file);
const { ydoc, awareness } = usePersistentProject();
const ytext = ydoc?.getMap("files").get(selection.file) as Y.Text;
if (ytext === null) return null;
if (fileInfo === undefined) {
return null;
}
awareness!.setLocalStateField("user", {
name: "micro:bit tester",
color: "yellow",
});
// TODO: represent fileInfo in project?
return fileInfo.isThirdPartyModule &&
!settings.allowEditingThirdPartyModules ? (
<ModuleOverlay moduleData={fileInfo.moduleData} />
) : (
<Editor
selection={selection}
onChange={onFileChange}
fontSize={settings.fontSize}
codeStructureOption={settings.codeStructureHighlight}
parameterHelpOption={settings.parameterHelp}
warnOnV2OnlyFeatures={settings.warnForApiUnsupportedByDevice}
disableV2OnlyFeaturesWarning={disableV2OnlyFeaturesWarning}
awareness={awareness!}
text={ytext}
/>
);
};
export default EditorContainer;