mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES ## What kind of change does this PR introduce? Adds a new Realtime Monaco component to the UI Library, enabling collaborative code editing with Supabase Realtime synchronization using Monaco Editor and Yjs. ## Additional context This is WIP and used for discuss further changes to the y-supabase provider. ## Demo https://github.com/user-attachments/assets/84a761e5-73bb-478e-979a-682121ffee89 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **New Features** * Added a new Realtime Monaco collaborative code editor component with real-time synchronization support across multiple frameworks (Next.js, React, React Router, Tanstack). * **Documentation** * Added comprehensive documentation and usage guides for the Realtime Monaco component across all supported frameworks. * **Dependencies** * Added Monaco editor, Yjs, y-monaco, and Supabase collaboration packages. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
122 lines
3.8 KiB
TypeScript
122 lines
3.8 KiB
TypeScript
'use client'
|
|
|
|
import { SupabasePersistenceOptions, SupabaseProvider } from '@supabase-labs/y-supabase'
|
|
import type { editor as MonacoEditor } from 'monaco-editor'
|
|
import { useCallback, useEffect, useRef } from 'react'
|
|
import { MonacoBinding } from 'y-monaco'
|
|
import { Awareness } from 'y-protocols/awareness'
|
|
import * as Y from 'yjs'
|
|
|
|
import { createClient } from '@/registry/default/clients/nextjs/lib/supabase/client'
|
|
|
|
type UseConnectOnMountOptions = {
|
|
channel: string
|
|
persistence?: boolean | SupabasePersistenceOptions
|
|
awareness?: boolean | Awareness
|
|
}
|
|
|
|
const generateRandomColor = () => `hsl(${Math.floor(Math.random() * 360)}, 80%, 60%)`
|
|
|
|
export function useConnectOnMount({
|
|
channel,
|
|
persistence,
|
|
awareness = true,
|
|
}: UseConnectOnMountOptions) {
|
|
const docRef = useRef<Y.Doc | null>(null)
|
|
const providerRef = useRef<SupabaseProvider | null>(null)
|
|
const bindingRef = useRef<MonacoBinding | null>(null)
|
|
const userRef = useRef<{ color: string } | null>(null)
|
|
const styleRef = useRef<HTMLStyleElement | null>(null)
|
|
const awarenessHandlerRef = useRef<(() => void) | null>(null)
|
|
|
|
const connectOnMount = useCallback(
|
|
(editor: MonacoEditor.IStandaloneCodeEditor) => {
|
|
if (bindingRef.current) return
|
|
|
|
const model = editor.getModel()
|
|
if (!model) return
|
|
|
|
const doc = new Y.Doc()
|
|
const yText = doc.getText('monaco')
|
|
const supabase = createClient()
|
|
const provider = new SupabaseProvider(channel, doc, supabase as any, {
|
|
awareness,
|
|
persistence,
|
|
})
|
|
const providerAwareness = provider.getAwareness()
|
|
|
|
if (providerAwareness) {
|
|
if (!userRef.current) {
|
|
userRef.current = {
|
|
color: generateRandomColor(),
|
|
}
|
|
}
|
|
providerAwareness.setLocalStateField('user', userRef.current)
|
|
|
|
const applyAwarenessStyles = () => {
|
|
if (!styleRef.current) {
|
|
styleRef.current = document.createElement('style')
|
|
styleRef.current.setAttribute('data-monaco-y-cursors', 'true')
|
|
document.head.appendChild(styleRef.current)
|
|
}
|
|
|
|
let css = `
|
|
.yRemoteSelection {
|
|
background-color: var(--y-remote-selection-color, rgba(0, 0, 0, 0.2));
|
|
opacity: 0.2;
|
|
}
|
|
.yRemoteSelectionHead {
|
|
border-left: 2px solid var(--y-remote-selection-color, rgba(0, 0, 0, 0.7));
|
|
margin-left: -1px;
|
|
box-sizing: border-box;
|
|
}
|
|
`
|
|
|
|
providerAwareness.getStates().forEach((state, clientId) => {
|
|
const color = state?.user?.color
|
|
const isValidHsl = /^hsl\(\d{1,3},\s*\d{1,3}%,\s*\d{1,3}%\)$/.test(color)
|
|
|
|
if (!isValidHsl) return
|
|
if (!color) return
|
|
|
|
css += `
|
|
.yRemoteSelection-${clientId}, .yRemoteSelectionHead-${clientId} {
|
|
--y-remote-selection-color: ${color};
|
|
}
|
|
`
|
|
})
|
|
|
|
styleRef.current.textContent = css
|
|
}
|
|
|
|
awarenessHandlerRef.current = applyAwarenessStyles
|
|
applyAwarenessStyles()
|
|
providerAwareness.on('update', applyAwarenessStyles)
|
|
}
|
|
|
|
docRef.current = doc
|
|
providerRef.current = provider
|
|
bindingRef.current = new MonacoBinding(yText, model, new Set([editor]), providerAwareness)
|
|
},
|
|
[channel]
|
|
)
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (awarenessHandlerRef.current && providerRef.current) {
|
|
const awareness = providerRef.current.getAwareness()
|
|
awareness?.off('update', awarenessHandlerRef.current)
|
|
}
|
|
styleRef.current?.remove()
|
|
bindingRef.current?.destroy()
|
|
bindingRef.current = null
|
|
providerRef.current?.destroy()
|
|
providerRef.current = null
|
|
docRef.current?.destroy()
|
|
docRef.current = null
|
|
}
|
|
}, [])
|
|
|
|
return { connectOnMount }
|
|
}
|