Skip to content

Commit

Permalink
[lexical-yjs] Feature: Allow passing in custom syncCursorPositions
Browse files Browse the repository at this point in the history
…function to collab hook (#7053)
  • Loading branch information
amanharwara authored Jan 15, 2025
1 parent 415c576 commit 136a565
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
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

0 comments on commit 136a565

Please sign in to comment.