Skip to content

Commit 8ab7b26

Browse files
Merge pull request #8 from bridgedxyz/ui/use-monaco-editor
UI/use monaco editor
2 parents 3f2a821 + 04f998b commit 8ab7b26

File tree

6 files changed

+138
-15
lines changed

6 files changed

+138
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// dynamic code editor. supports codemirror & monaco
2+
import React from "react";
3+
import CodeMirror from "./code-mirror";
4+
import { MonacoEditor } from "./monaco";
5+
6+
interface DynamicEdotorProps {
7+
host?: _Host;
8+
}
9+
10+
type _Host = "codemirror" | "monaco" | "auto";
11+
// uses monaco by default. when set auto or host not provided.
12+
const fallbackAutoHost = "monaco";
13+
14+
export function CodeEditor(props: DynamicEdotorProps) {
15+
const _editorname = getTargetEditorName(props.host);
16+
17+
switch (_editorname) {
18+
case "codemirror":
19+
return <CodeMirror />;
20+
case "monaco":
21+
return <MonacoEditor />;
22+
}
23+
}
24+
25+
function getTargetEditorName(host?: _Host): "codemirror" | "monaco" {
26+
if (!host) {
27+
return fallbackAutoHost;
28+
}
29+
30+
switch (host) {
31+
case "auto":
32+
return fallbackAutoHost;
33+
case "codemirror":
34+
return "codemirror";
35+
case "monaco":
36+
return "monaco";
37+
}
38+
39+
throw `invalid host option provided - "${host}"`;
40+
}
+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export * from "./code-editor";
12
export * from "./code-mirror";
3+
export * from "./monaco";
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React, { useEffect } from "react";
2+
import Editor, { useMonaco } from "@monaco-editor/react";
3+
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
4+
interface EditorProps {
5+
defaultValue?: string;
6+
width?: number | string;
7+
height?: number | string;
8+
options?: monaco.editor.IStandaloneEditorConstructionOptions;
9+
}
10+
11+
export function MonacoEditor(props: EditorProps) {
12+
const monaco = useMonaco();
13+
useEffect(() => {
14+
// adding jsx support - https://github.com/microsoft/monaco-editor/issues/264
15+
if (monaco) {
16+
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
17+
target: monaco.languages.typescript.ScriptTarget.Latest,
18+
allowNonTsExtensions: true,
19+
moduleResolution:
20+
monaco.languages.typescript.ModuleResolutionKind.NodeJs,
21+
module: monaco.languages.typescript.ModuleKind.CommonJS,
22+
noEmit: true,
23+
esModuleInterop: true,
24+
jsx: monaco.languages.typescript.JsxEmit.React,
25+
reactNamespace: "React",
26+
allowJs: true,
27+
typeRoots: ["node_modules/@types"],
28+
});
29+
30+
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
31+
noSemanticValidation: false,
32+
noSyntaxValidation: false,
33+
});
34+
35+
monaco.languages.typescript.typescriptDefaults.addExtraLib(
36+
"https://cdn.jsdelivr.net/npm/@types/react@16.9.41/index.d.ts"
37+
);
38+
39+
// monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
40+
// jsx: monaco.languages.typescript.JsxEmit.ReactJSX,
41+
// });
42+
43+
// monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
44+
// noSemanticValidation: false,
45+
// noSyntaxValidation: false,
46+
// });
47+
}
48+
}, [monaco]);
49+
50+
return (
51+
<Editor
52+
width={props.width}
53+
height={props.height}
54+
defaultLanguage="typescript"
55+
defaultValue={props.defaultValue ?? "// no content"}
56+
theme="vs-dark"
57+
options={props.options}
58+
/>
59+
);
60+
}
61+
62+
export { useMonaco } from "@monaco-editor/react";

editor/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
"@babel/runtime": "^7.14.0",
1212
"@base-sdk/base": "^0.1.0-5",
1313
"@designto/code": "0.0.1",
14-
"@reflect-ui/editor-ui": "0.0.1",
1514
"@emotion/react": "^11.1.5",
1615
"@emotion/styled": "^11.1.5",
1716
"@material-ui/core": "^4.11.4",
1817
"@material-ui/icons": "^4.11.2",
1918
"@material-ui/lab": "^4.0.0-alpha.58",
2019
"@monaco-editor/react": "^4.1.3",
20+
"@reflect-ui/editor-ui": "0.0.1",
2121
"@visx/gradient": "^1.7.0",
2222
"@visx/group": "^1.7.0",
2323
"@visx/hierarchy": "^1.7.0",
@@ -27,6 +27,7 @@
2727
"cuid": "^2.1.8",
2828
"dart-style": "^1.3.2-dev",
2929
"idb": "^6.0.0",
30+
"monaco-editor": "^0.24.0",
3031
"next": "10.0.3",
3132
"react": "17.0.1",
3233
"react-codemirror2": "^7.2.1",
@@ -47,4 +48,4 @@
4748
"next-transpile-modules": "^7.0.0",
4849
"typescript": "^4.2.3"
4950
}
50-
}
51+
}

editor/pages/figma/to-react.tsx

+26-13
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,21 @@ import {
1717
} from "../../layout/panel";
1818
import { WorkspaceBottomPanelDockLayout } from "../../layout/panel/workspace-bottom-panel-dock-layout";
1919
import { JsonTree } from "../../components/visualization/json-visualization/json-tree";
20+
import { MonacoEditor, useMonaco } from "../../components/code-editor";
2021

2122
// set image repo for figma platform
2223
MainImageRepository.instance = new ImageRepositories();
2324

24-
const CodemirrorEditor = dynamic(
25-
import("../../components/code-editor/code-mirror"),
26-
{
27-
ssr: false,
28-
}
29-
);
25+
// const CodemirrorEditor = dynamic(
26+
// import("../../components/code-editor/code-mirror"),
27+
// {
28+
// ssr: false,
29+
// }
30+
// );
31+
32+
// const MonacoEdotor = dynamic(import("@monaco-editor/react"), {
33+
// ssr: false,
34+
// });
3035

3136
export default function FigmaToReactDemoPage() {
3237
const [reflect, setReflect] = useState<ReflectSceneNode>();
@@ -41,6 +46,14 @@ export default function FigmaToReactDemoPage() {
4146
setTargetnodeConfig(target);
4247
};
4348

49+
const monaco = useMonaco();
50+
51+
useEffect(() => {
52+
if (monaco) {
53+
// do something with editor
54+
}
55+
}, [monaco]);
56+
4457
let widgetCode: string;
4558
let widgetTree;
4659
if (reflect) {
@@ -77,17 +90,17 @@ export default function FigmaToReactDemoPage() {
7790
</WorkspaceContentPanel>
7891
<WorkspaceContentPanel>
7992
<InspectionPanelContentWrap>
80-
<CodemirrorEditor
81-
value={
93+
<MonacoEditor
94+
key={widgetCode}
95+
height="100vh"
96+
options={{
97+
automaticLayout: true,
98+
}}
99+
defaultValue={
82100
widgetCode
83101
? widgetCode
84102
: "// No input design provided to be converted.."
85103
}
86-
options={{
87-
mode: "javascript",
88-
theme: "monokai",
89-
lineNumbers: true,
90-
}}
91104
/>
92105
</InspectionPanelContentWrap>
93106
</WorkspaceContentPanel>

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -9848,6 +9848,11 @@ moment@^2.18.1:
98489848
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
98499849
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
98509850

9851+
monaco-editor@^0.24.0:
9852+
version "0.24.0"
9853+
resolved "https://registry.yarnpkg.com/monaco-editor/-/monaco-editor-0.24.0.tgz#990b55096bcc95d08d8d28e55264c6eb17707269"
9854+
integrity sha512-o1f0Lz6ABFNTtnEqqqvlY9qzNx24rQZx1RgYNQ8SkWkE+Ka63keHH/RqxQ4QhN4fs/UYOnvAtEUZsPrzccH++A==
9855+
98519856
move-concurrently@^1.0.1:
98529857
version "1.0.1"
98539858
resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"

0 commit comments

Comments
 (0)