Spaces:
Running
Running
Fix dynamic import of y-monaco.
Browse files- lynxkite-app/web/src/Code.tsx +31 -16
lynxkite-app/web/src/Code.tsx
CHANGED
|
@@ -14,39 +14,53 @@ import Backspace from "~icons/tabler/backspace.jsx";
|
|
| 14 |
import Close from "~icons/tabler/x.jsx";
|
| 15 |
import favicon from "./assets/favicon.ico";
|
| 16 |
import theme from "./code-theme.ts";
|
| 17 |
-
// For some reason y-monaco is gigantic. The other Monaco packages are small.
|
| 18 |
-
const MonacoBinding = await import("y-monaco").then((m) => m.MonacoBinding);
|
| 19 |
|
| 20 |
export default function Code() {
|
| 21 |
const { path } = useParams();
|
| 22 |
const parentDir = path!.split("/").slice(0, -1).join("/");
|
| 23 |
-
const
|
| 24 |
-
const
|
| 25 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
function beforeMount(monaco: Monaco) {
|
| 27 |
monaco.editor.defineTheme("lynxkite", theme);
|
| 28 |
}
|
| 29 |
function onMount(_editor: editor.IStandaloneCodeEditor) {
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
const proto = location.protocol === "https:" ? "wss:" : "ws:";
|
| 33 |
-
|
| 34 |
`${proto}//${location.host}/ws/code/crdt`,
|
| 35 |
path!,
|
| 36 |
-
|
| 37 |
);
|
| 38 |
-
|
| 39 |
text,
|
| 40 |
-
|
| 41 |
-
new Set([
|
| 42 |
-
|
| 43 |
);
|
| 44 |
}
|
| 45 |
useEffect(() => {
|
| 46 |
return () => {
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
};
|
| 51 |
});
|
| 52 |
return (
|
|
@@ -74,6 +88,7 @@ export default function Code() {
|
|
| 74 |
path={path}
|
| 75 |
beforeMount={beforeMount}
|
| 76 |
onMount={onMount}
|
|
|
|
| 77 |
options={{
|
| 78 |
cursorStyle: "block",
|
| 79 |
cursorBlinking: "solid",
|
|
|
|
| 14 |
import Close from "~icons/tabler/x.jsx";
|
| 15 |
import favicon from "./assets/favicon.ico";
|
| 16 |
import theme from "./code-theme.ts";
|
|
|
|
|
|
|
| 17 |
|
| 18 |
export default function Code() {
|
| 19 |
const { path } = useParams();
|
| 20 |
const parentDir = path!.split("/").slice(0, -1).join("/");
|
| 21 |
+
const yDocRef = useRef<any>();
|
| 22 |
+
const wsProviderRef = useRef<any>();
|
| 23 |
+
const monacoBindingRef = useRef<any>();
|
| 24 |
+
const yMonacoRef = useRef<any>();
|
| 25 |
+
const editorRef = useRef<any>();
|
| 26 |
+
useEffect(() => {
|
| 27 |
+
const loadMonaco = async () => {
|
| 28 |
+
// y-monaco is gigantic. The other Monaco packages are small.
|
| 29 |
+
yMonacoRef.current = await import("y-monaco");
|
| 30 |
+
initCRDT();
|
| 31 |
+
};
|
| 32 |
+
loadMonaco();
|
| 33 |
+
}, []);
|
| 34 |
function beforeMount(monaco: Monaco) {
|
| 35 |
monaco.editor.defineTheme("lynxkite", theme);
|
| 36 |
}
|
| 37 |
function onMount(_editor: editor.IStandaloneCodeEditor) {
|
| 38 |
+
editorRef.current = _editor;
|
| 39 |
+
initCRDT();
|
| 40 |
+
}
|
| 41 |
+
function initCRDT() {
|
| 42 |
+
if (!yMonacoRef.current || !editorRef.current) return;
|
| 43 |
+
if (yDocRef.current) return;
|
| 44 |
+
yDocRef.current = new Y.Doc();
|
| 45 |
+
const text = yDocRef.current.getText("text");
|
| 46 |
const proto = location.protocol === "https:" ? "wss:" : "ws:";
|
| 47 |
+
wsProviderRef.current = new WebsocketProvider(
|
| 48 |
`${proto}//${location.host}/ws/code/crdt`,
|
| 49 |
path!,
|
| 50 |
+
yDocRef.current,
|
| 51 |
);
|
| 52 |
+
monacoBindingRef.current = new yMonacoRef.current.MonacoBinding(
|
| 53 |
text,
|
| 54 |
+
editorRef.current.getModel()!,
|
| 55 |
+
new Set([editorRef.current]),
|
| 56 |
+
wsProviderRef.current.awareness,
|
| 57 |
);
|
| 58 |
}
|
| 59 |
useEffect(() => {
|
| 60 |
return () => {
|
| 61 |
+
yDocRef.current?.destroy();
|
| 62 |
+
wsProviderRef.current?.destroy();
|
| 63 |
+
monacoBindingRef.current?.destroy();
|
| 64 |
};
|
| 65 |
});
|
| 66 |
return (
|
|
|
|
| 88 |
path={path}
|
| 89 |
beforeMount={beforeMount}
|
| 90 |
onMount={onMount}
|
| 91 |
+
loading={null}
|
| 92 |
options={{
|
| 93 |
cursorStyle: "block",
|
| 94 |
cursorBlinking: "solid",
|