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] Feature: Allow passing in custom syncCursorPositions function to collab hook #7053

Merged
merged 3 commits into from
Jan 15, 2025
Merged
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
14 changes: 11 additions & 3 deletions packages/lexical-react/src/shared/useYjsCollaboration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import type {Binding, Provider} from '@lexical/yjs';
import type {Binding, Provider, SyncCursorPositionsFn} from '@lexical/yjs';
import type {LexicalEditor} from 'lexical';

import {mergeRegister} from '@lexical/utils';
Expand Down Expand Up @@ -54,6 +54,7 @@ export function useYjsCollaboration(
cursorsContainerRef?: CursorsContainerRef,
initialEditorState?: InitialEditorStateType,
awarenessData?: object,
syncCursorPositionsFn: SyncCursorPositionsFn = syncCursorPositions,
): JSX.Element {
const isReloadingDoc = useRef(false);

Expand Down Expand Up @@ -90,7 +91,7 @@ export function useYjsCollaboration(
};

const onAwarenessUpdate = () => {
syncCursorPositions(binding, provider);
syncCursorPositionsFn(binding, provider);
};

const onYjsTreeChanges = (
Expand All @@ -102,7 +103,13 @@ export function useYjsCollaboration(
const origin = transaction.origin;
if (origin !== binding) {
const isFromUndoManger = origin instanceof UndoManager;
syncYjsChangesToLexical(binding, provider, events, isFromUndoManger);
syncYjsChangesToLexical(
binding,
provider,
events,
isFromUndoManger,
syncCursorPositionsFn,
);
}
};

Expand Down Expand Up @@ -191,6 +198,7 @@ export function useYjsCollaboration(
shouldBootstrap,
awarenessData,
setDoc,
syncCursorPositionsFn,
]);
const cursorsContainer = useMemo(() => {
const ref = (element: null | HTMLElement) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/lexical-yjs/src/SyncCursors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,11 @@ function getCollabNodeAndOffset(
return [null, 0];
}

export type SyncCursorPositionsFn = (
binding: Binding,
provider: Provider,
) => void;

export function syncCursorPositions(
binding: Binding,
provider: Provider,
Expand Down
4 changes: 3 additions & 1 deletion packages/lexical-yjs/src/SyncEditorStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {CollabTextNode} from './CollabTextNode';
import {
$syncLocalCursorPosition,
syncCursorPositions,
SyncCursorPositionsFn,
syncLexicalSelectionToYjs,
} from './SyncCursors';
import {
Expand Down Expand Up @@ -83,6 +84,7 @@ export function syncYjsChangesToLexical(
provider: Provider,
events: Array<YEvent<YText>>,
isFromUndoManger: boolean,
syncCursorPositionsFn: SyncCursorPositionsFn = syncCursorPositions,
): void {
const editor = binding.editor;
const currentEditorState = editor._editorState;
Expand Down Expand Up @@ -129,7 +131,7 @@ export function syncYjsChangesToLexical(
},
{
onUpdate: () => {
syncCursorPositions(binding, provider);
syncCursorPositionsFn(binding, provider);
// If there was a collision on the top level paragraph
// we need to re-add a paragraph. To ensure this insertion properly syncs with other clients,
// it must be placed outside of the update block above that has tags 'collaboration' or 'historic'.
Expand Down
1 change: 1 addition & 0 deletions packages/lexical-yjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export function setLocalStateFocus(
export {
getAnchorAndFocusCollabNodesForUserState,
syncCursorPositions,
type SyncCursorPositionsFn,
} from './SyncCursors';
export {
syncLexicalUpdateToYjs,
Expand Down
Loading