Skip to content

Commit 9483529

Browse files
committed
Merge branch 'sandcastle-v2' into window-title-dirty-state
2 parents 7f0775c + 8126080 commit 9483529

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

packages/sandcastle/src/SandcastleEditor.tsx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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";
33
import { RefObject, useImperativeHandle, useRef, useState } from "react";
44
import * as prettier from "prettier";
55
import * as babelPlugin from "prettier/plugins/babel";
@@ -12,6 +12,37 @@ import { Icon } from "@stratakit/foundations";
1212
import { play, textAlignLeft } from "./icons";
1313
import { setupSandcastleSnippets } from "./setupSandcastleSnippets";
1414

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+
1546
const TYPES_URL = `${__PAGE_BASE_URL__}Source/Cesium.d.ts`;
1647
const SANDCASTLE_TYPES_URL = `templates/Sandcastle.d.ts`;
1748

@@ -40,7 +71,7 @@ function SandcastleEditor({
4071
}) {
4172
const [activeTab, setActiveTab] = useState<"js" | "html">("js");
4273

43-
const internalEditorRef = useRef<editor.IStandaloneCodeEditor>(null);
74+
const internalEditorRef = useRef<monaco.editor.IStandaloneCodeEditor>(null);
4475
useImperativeHandle(ref, () => {
4576
return {
4677
formatCode() {
@@ -54,7 +85,7 @@ function SandcastleEditor({
5485
}
5586

5687
function handleEditorDidMount(
57-
editor: editor.IStandaloneCodeEditor,
88+
editor: monaco.editor.IStandaloneCodeEditor,
5889
monaco: Monaco,
5990
) {
6091
internalEditorRef.current = editor;
@@ -66,6 +97,8 @@ function SandcastleEditor({
6697
},
6798
});
6899

100+
const { KeyCode, KeyMod } = monaco;
101+
69102
monaco.editor.addKeybindingRules([
70103
// Remove some default keybindings that get in the way
71104
// https://github.com/microsoft/monaco-editor/issues/102

0 commit comments

Comments
 (0)