The Monaco Editor is the code editor that powers VS Code, packaged for use in web applications. It provides a familiar, feature-rich editing experience with syntax highlighting, keyboard shortcuts, and extensive customization options.
Monaco Editor is the same editor you use in Visual Studio Code, but running directly in the browser.
The magic happens when we bind the Monaco Editor to a Yjs shared type:
import { MonacoBinding } from 'y-monaco'import * as Y from 'yjs'import { WebsocketProvider } from 'y-websocket'// Create Yjs document and shared text typeconst doc = new Y.Doc()const provider = new WebsocketProvider(serverWsUrl, wsID, doc)const type = doc.getText('monaco')// Bind to Monaco Editorconst binding = new MonacoBinding( type, // Yjs shared text type editorRef.current.getModel()!, // Monaco editor model new Set([editorRef.current]), // Set of editors to bind provider.awareness // Awareness for user presence)
The MonacoBinding automatically synchronizes changes between the editor and the Yjs document, handling all the complexity of collaborative editing.
A subtle but important detail for collaboration across different operating systems:
// Normalize line endings to LF (Unix-style)const newModel = editorRef.current.getModel()if (newModel === null) return// Set EOL to 0 (LF) instead of 1 (CRLF)netModel.setEOL(0)editorRef.current.setModel(newModel)
This ensures that users on Windows, macOS, and Linux can collaborate without issues caused by different line ending conventions.