-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathCodeEditor.tsx
More file actions
55 lines (48 loc) · 1.54 KB
/
Copy pathCodeEditor.tsx
File metadata and controls
55 lines (48 loc) · 1.54 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
'use client'
import React, { useEffect, useState, useCallback, useMemo } from 'react'
import { Editor } from '@monaco-editor/react'
import { MonacoBinding } from 'y-monaco'
import { useYDoc, useAwareness, useYjsProvider } from '@y-sweet/react'
import { editor } from 'monaco-editor'
import Header from '@/components/Header'
export function CodeEditor() {
const yDoc = useYDoc()
const awareness = useAwareness()
const [editorRef, setEditorRef] = useState<editor.IStandaloneCodeEditor | null>(null)
const sharedText = useMemo(() => yDoc.getText('content'), [yDoc])
useEffect(() => {
if (!editorRef) return
const binding = new MonacoBinding(
sharedText,
editorRef.getModel() as editor.ITextModel,
new Set([editorRef]),
awareness,
)
return () => binding.destroy()
}, [editorRef, sharedText, awareness])
const handleEditorMount = useCallback((editor: editor.IStandaloneCodeEditor) => {
setEditorRef(editor)
}, [])
return (
<div className="p-4 lg:p-8 space-y-4">
<Header
title="Monaco Code Editor"
githubLink="https://github.com/jamsocket/y-sweet/tree/main/examples/nextjs/src/app/(demos)/monaco"
/>
<div>
<Editor
defaultLanguage="javascript"
defaultValue="// Start typing here..."
theme="vs-dark"
className="h-[600px] w-full"
onMount={handleEditorMount}
options={{
tabSize: 2,
automaticLayout: true,
cursorStyle: 'line',
}}
/>
</div>
</div>
)
}