@@ -34,10 +34,15 @@ import { useSplitCellCallback } from "../useSplitCell";
34
34
import { invariant } from "@/utils/invariant" ;
35
35
import { connectionAtom } from "@/core/network/connection" ;
36
36
import { WebSocketState } from "@/core/websocket/types" ;
37
- import { realTimeCollaboration } from "@/core/codemirror/rtc/extension" ;
37
+ import {
38
+ connectedDocAtom ,
39
+ realTimeCollaboration ,
40
+ } from "@/core/codemirror/rtc/extension" ;
38
41
import { store } from "@/core/state/jotai" ;
39
42
import { useDeleteCellCallback } from "../useDeleteCell" ;
40
43
import { useSaveNotebook } from "@/core/saving/save-component" ;
44
+ import { DelayMount } from "@/components/utils/delay-mount" ;
45
+ import { Button } from "@/components/ui/button" ;
41
46
42
47
export interface CellEditorProps
43
48
extends Pick < CellRuntimeState , "status" > ,
@@ -234,9 +239,10 @@ const CellEditorInternal = ({
234
239
saveOrNameNotebook ,
235
240
] ) ;
236
241
242
+ const rtcEnabled = getFeatureFlag ( "rtc_v2" ) ;
237
243
const handleInitializeEditor = useEvent ( ( ) => {
238
244
// If rtc is enabled, use collaborative editing
239
- if ( getFeatureFlag ( "rtc_v2" ) ) {
245
+ if ( rtcEnabled ) {
240
246
const rtc = realTimeCollaboration (
241
247
cellId ,
242
248
( code ) => {
@@ -259,13 +265,15 @@ const CellEditorInternal = ({
259
265
} ) ;
260
266
setEditorView ( ev ) ;
261
267
// Initialize the language adapter
262
- switchLanguage ( ev , getInitialLanguageAdapter ( ev . state ) . type ) ;
268
+ if ( ! rtcEnabled ) {
269
+ switchLanguage ( ev , getInitialLanguageAdapter ( ev . state ) . type ) ;
270
+ }
263
271
} ) ;
264
272
265
273
const handleReconfigureEditor = useEvent ( ( ) => {
266
274
invariant ( editorViewRef . current !== null , "Editor view is not initialized" ) ;
267
275
// If rtc is enabled, use collaborative editing
268
- if ( getFeatureFlag ( "rtc_v2" ) ) {
276
+ if ( rtcEnabled ) {
269
277
const rtc = realTimeCollaboration ( cellId , ( code ) => {
270
278
// It's not really a formatting change,
271
279
// but this means it won't be marked as stale
@@ -292,7 +300,7 @@ const CellEditorInternal = ({
292
300
293
301
const handleDeserializeEditor = useEvent ( ( ) => {
294
302
invariant ( serializedEditorState , "Editor view is not initialized" ) ;
295
- if ( getFeatureFlag ( "rtc_v2" ) ) {
303
+ if ( rtcEnabled ) {
296
304
const rtc = realTimeCollaboration (
297
305
cellId ,
298
306
( code ) => {
@@ -303,6 +311,7 @@ const CellEditorInternal = ({
303
311
code ,
304
312
) ;
305
313
extensions . push ( rtc . extension ) ;
314
+ code = rtc . code ;
306
315
}
307
316
308
317
const ev = new EditorView ( {
@@ -316,7 +325,9 @@ const CellEditorInternal = ({
316
325
) ,
317
326
} ) ;
318
327
// Initialize the language adapter
319
- switchLanguage ( ev , getInitialLanguageAdapter ( ev . state ) . type ) ;
328
+ if ( ! rtcEnabled ) {
329
+ switchLanguage ( ev , getInitialLanguageAdapter ( ev . state ) . type ) ;
330
+ }
320
331
setEditorView ( ev ) ;
321
332
// Clear the serialized state so that we don't re-create the editor next time
322
333
cellActions . clearSerializedEditorState ( { cellId } ) ;
@@ -507,9 +518,27 @@ function WithWaitUntilConnected<T extends {}>(
507
518
) {
508
519
const WaitUntilConnectedComponent = ( props : T ) => {
509
520
const connection = useAtomValue ( connectionAtom ) ;
521
+ const [ rtcDoc , setRtcDoc ] = useAtom ( connectedDocAtom ) ;
510
522
511
- if ( connection . state === WebSocketState . CONNECTING ) {
512
- return null ;
523
+ if (
524
+ connection . state === WebSocketState . CONNECTING ||
525
+ rtcDoc === undefined
526
+ ) {
527
+ return (
528
+ < div className = "flex h-full w-full items-baseline p-4" >
529
+ < DelayMount milliseconds = { 1000 } fallback = { null } >
530
+ < span > Waiting for real-time collaboration connection...</ span >
531
+ < Button
532
+ variant = "link"
533
+ onClick = { ( ) => {
534
+ setRtcDoc ( "disabled" ) ;
535
+ } }
536
+ >
537
+ Turn off real-time collaboration
538
+ </ Button >
539
+ </ DelayMount >
540
+ </ div >
541
+ ) ;
513
542
}
514
543
515
544
return < Component { ...props } /> ;
0 commit comments