Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lexical-yjs][lexical-react] Feature: Allow custom Yjs XmlText #6483

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/lexical-react/src/LexicalCollaborationPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import type {Doc} from 'yjs';
import type {Doc, XmlText} from 'yjs';

import {
type CollaborationContextType,
Expand Down Expand Up @@ -45,6 +45,7 @@ type Props = {
excludedProperties?: ExcludedProperties;
// `awarenessData` parameter allows arbitrary data to be added to the awareness.
awarenessData?: object;
getXmlText?: (doc: Doc) => XmlText;
};

export function CollaborationPlugin({
Expand All @@ -57,6 +58,7 @@ export function CollaborationPlugin({
initialEditorState,
excludedProperties,
awarenessData,
getXmlText,
}: Props): JSX.Element {
const isBindingInitialized = useRef(false);
const isProviderInitialized = useRef(false);
Expand Down Expand Up @@ -117,13 +119,14 @@ export function CollaborationPlugin({
doc || yjsDocMap.get(id),
yjsDocMap,
excludedProperties,
getXmlText,
);
setBinding(newBinding);

return () => {
newBinding.root.destroy(newBinding);
};
}, [editor, provider, id, yjsDocMap, doc, excludedProperties]);
}, [editor, provider, id, yjsDocMap, doc, excludedProperties, getXmlText]);

if (!provider || !binding) {
return <></>;
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-yjs/flow/LexicalYjs.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ declare export function createBinding(
id: string,
doc: ?Doc,
docMap: Map<string, Doc>,
getXmlText: ?(ydoc: Doc) => XmlText
): Binding;

declare export function syncCursorPositions(
Expand Down
3 changes: 2 additions & 1 deletion packages/lexical-yjs/src/Bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ export function createBinding(
doc: Doc | null | undefined,
docMap: Map<string, Doc>,
excludedProperties?: ExcludedProperties,
getXmlText = (ydoc: Doc) => ydoc.get('root', XmlText) as XmlText,
): Binding {
invariant(
doc !== undefined && doc !== null,
'createBinding: doc is null or undefined',
);
const rootXmlText = doc.get('root', XmlText) as XmlText;
const rootXmlText = getXmlText(doc);
const root: CollabElementNode = $createCollabElementNode(
rootXmlText,
null,
Expand Down
Loading