1
- import { Editor , Monaco , OnChange } from "@monaco-editor/react" ;
2
- import { editor , KeyCode , KeyMod } from "monaco-editor" ;
1
+ import { Editor , Monaco , OnChange , loader } from "@monaco-editor/react" ;
2
+ import * as monaco from "monaco-editor" ;
3
3
import { RefObject , useImperativeHandle , useRef , useState } from "react" ;
4
4
import * as prettier from "prettier" ;
5
5
import * as babelPlugin from "prettier/plugins/babel" ;
@@ -12,6 +12,37 @@ import { Icon } from "@stratakit/foundations";
12
12
import { play , textAlignLeft } from "./icons" ;
13
13
import { setupSandcastleSnippets } from "./setupSandcastleSnippets" ;
14
14
15
+ import EditorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker" ;
16
+ import JsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker" ;
17
+ import CssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker" ;
18
+ import HtmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker" ;
19
+ import TsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker" ;
20
+
21
+ // this setup is needed for Vite to properly build/load the workers
22
+ // see the readme https://github.com/suren-atoyan/monaco-react#loader-config
23
+ self . MonacoEnvironment = {
24
+ getWorker ( _ , label ) {
25
+ if ( label === "json" ) {
26
+ return new JsonWorker ( ) ;
27
+ }
28
+ if ( label === "css" || label === "scss" || label === "less" ) {
29
+ return new CssWorker ( ) ;
30
+ }
31
+ if ( label === "html" || label === "handlebars" || label === "razor" ) {
32
+ return new HtmlWorker ( ) ;
33
+ }
34
+ if ( label === "typescript" || label === "javascript" ) {
35
+ return new TsWorker ( ) ;
36
+ }
37
+ return new EditorWorker ( ) ;
38
+ } ,
39
+ } ;
40
+
41
+ // This ensures we bundle monaco and load locally instead of from the CDN. This allows
42
+ // Sandcastle to work fully "offline" on a local network or in an environment without
43
+ // open network access
44
+ loader . config ( { monaco } ) ;
45
+
15
46
const TYPES_URL = `${ __PAGE_BASE_URL__ } Source/Cesium.d.ts` ;
16
47
const SANDCASTLE_TYPES_URL = `templates/Sandcastle.d.ts` ;
17
48
@@ -40,7 +71,7 @@ function SandcastleEditor({
40
71
} ) {
41
72
const [ activeTab , setActiveTab ] = useState < "js" | "html" > ( "js" ) ;
42
73
43
- const internalEditorRef = useRef < editor . IStandaloneCodeEditor > ( null ) ;
74
+ const internalEditorRef = useRef < monaco . editor . IStandaloneCodeEditor > ( null ) ;
44
75
useImperativeHandle ( ref , ( ) => {
45
76
return {
46
77
formatCode ( ) {
@@ -54,7 +85,7 @@ function SandcastleEditor({
54
85
}
55
86
56
87
function handleEditorDidMount (
57
- editor : editor . IStandaloneCodeEditor ,
88
+ editor : monaco . editor . IStandaloneCodeEditor ,
58
89
monaco : Monaco ,
59
90
) {
60
91
internalEditorRef . current = editor ;
@@ -66,6 +97,8 @@ function SandcastleEditor({
66
97
} ,
67
98
} ) ;
68
99
100
+ const { KeyCode, KeyMod } = monaco ;
101
+
69
102
monaco . editor . addKeybindingRules ( [
70
103
// Remove some default keybindings that get in the way
71
104
// https://github.com/microsoft/monaco-editor/issues/102
0 commit comments