-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: yjs collaboration plugin in react strict mode
- Loading branch information
1 parent
0e168d1
commit f6319c4
Showing
4 changed files
with
186 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
180 changes: 180 additions & 0 deletions
180
packages/lexical-react/src/LexicalCollaborationPlugin.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import type {Doc} from 'yjs'; | ||
|
||
import { | ||
type CollaborationContextType, | ||
useCollaborationContext, | ||
} from '@lexical/react/LexicalCollaborationContext'; | ||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; | ||
import { | ||
Binding, | ||
createBinding, | ||
ExcludedProperties, | ||
Provider, | ||
} from '@lexical/yjs'; | ||
import {LexicalEditor} from 'lexical'; | ||
import {useEffect, useState} from 'react'; | ||
|
||
import {InitialEditorStateType} from './LexicalComposer'; | ||
import { | ||
CursorsContainerRef, | ||
useYjsCollaboration, | ||
useYjsFocusTracking, | ||
useYjsHistory, | ||
} from './shared/useYjsCollaboration'; | ||
|
||
type Props = { | ||
id: string; | ||
providerFactory: ( | ||
// eslint-disable-next-line no-shadow | ||
id: string, | ||
yjsDocMap: Map<string, Doc>, | ||
) => Provider; | ||
shouldBootstrap: boolean; | ||
username?: string; | ||
cursorColor?: string; | ||
cursorsContainerRef?: CursorsContainerRef; | ||
initialEditorState?: InitialEditorStateType; | ||
excludedProperties?: ExcludedProperties; | ||
// `awarenessData` parameter allows arbitrary data to be added to the awareness. | ||
awarenessData?: object; | ||
}; | ||
|
||
export function CollaborationPlugin({ | ||
id, | ||
providerFactory, | ||
shouldBootstrap, | ||
username, | ||
cursorColor, | ||
cursorsContainerRef, | ||
initialEditorState, | ||
excludedProperties, | ||
awarenessData, | ||
}: Props): JSX.Element { | ||
const collabContext = useCollaborationContext(username, cursorColor); | ||
|
||
const {yjsDocMap, name, color} = collabContext; | ||
|
||
const [editor] = useLexicalComposerContext(); | ||
|
||
useEffect(() => { | ||
collabContext.isCollabActive = true; | ||
|
||
return () => { | ||
// Reseting flag only when unmount top level editor collab plugin. Nested | ||
// editors (e.g. image caption) should unmount without affecting it | ||
if (editor._parentEditor == null) { | ||
collabContext.isCollabActive = false; | ||
} | ||
}; | ||
}, [collabContext, editor]); | ||
|
||
const [provider, setProvider] = useState<Provider>(); | ||
useEffect(() => { | ||
const newProvider = providerFactory(id, yjsDocMap); | ||
setProvider(newProvider); | ||
|
||
return () => { | ||
newProvider.disconnect(); | ||
}; | ||
}, [id, providerFactory, yjsDocMap]); | ||
|
||
const [doc, setDoc] = useState(yjsDocMap.get(id)); | ||
const [binding, setBinding] = useState<Binding>(); | ||
useEffect(() => { | ||
if (!provider) { | ||
return; | ||
} | ||
const newBinding = createBinding( | ||
editor, | ||
provider, | ||
id, | ||
doc ?? yjsDocMap.get(id), | ||
yjsDocMap, | ||
excludedProperties, | ||
); | ||
setBinding(newBinding); | ||
|
||
return () => { | ||
newBinding.root.destroy(newBinding); | ||
}; | ||
}, [editor, provider, id, yjsDocMap, doc, excludedProperties]); | ||
|
||
if (!provider || !binding) { | ||
return <></>; | ||
} | ||
|
||
return ( | ||
<WrapWithProvider | ||
awarenessData={awarenessData} | ||
binding={binding} | ||
collabContext={collabContext} | ||
color={color} | ||
cursorsContainerRef={cursorsContainerRef} | ||
editor={editor} | ||
id={id} | ||
initialEditorState={initialEditorState} | ||
name={name} | ||
provider={provider} | ||
setDoc={setDoc} | ||
shouldBootstrap={shouldBootstrap} | ||
yjsDocMap={yjsDocMap} | ||
/> | ||
); | ||
} | ||
|
||
function WrapWithProvider({ | ||
editor, | ||
id, | ||
provider, | ||
yjsDocMap, | ||
name, | ||
color, | ||
shouldBootstrap, | ||
cursorsContainerRef, | ||
initialEditorState, | ||
awarenessData, | ||
collabContext, | ||
binding, | ||
setDoc, | ||
}: { | ||
editor: LexicalEditor; | ||
id: string; | ||
provider: Provider; | ||
yjsDocMap: Map<string, Doc>; | ||
name: string; | ||
color: string; | ||
shouldBootstrap: boolean; | ||
binding: Binding; | ||
setDoc: React.Dispatch<React.SetStateAction<Doc | undefined>>; | ||
cursorsContainerRef?: CursorsContainerRef | undefined; | ||
initialEditorState?: InitialEditorStateType | undefined; | ||
awarenessData?: object; | ||
collabContext: CollaborationContextType; | ||
}) { | ||
const [cursors] = useYjsCollaboration( | ||
editor, | ||
id, | ||
provider, | ||
yjsDocMap, | ||
name, | ||
color, | ||
shouldBootstrap, | ||
binding, | ||
setDoc, | ||
cursorsContainerRef, | ||
initialEditorState, | ||
awarenessData, | ||
); | ||
collabContext.clientID = binding.clientID; | ||
useYjsHistory(editor, binding); | ||
useYjsFocusTracking(editor, provider, name, color, awarenessData); | ||
return cursors; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters