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

[yjs] Add preliminary cursor rendering support #2527

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions packages/yjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"@hocuspocus/provider": "^2.2.1",
"@slate-yjs/core": "^1.0.1",
"@slate-yjs/react": "1.1.0",
"@udecode/plate-common": "22.0.2",
"yjs": "^13.5.42"
},
Expand Down
143 changes: 143 additions & 0 deletions packages/yjs/src/RemoteCursorOverlay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import React, { RefObject } from 'react';
import {
CursorOverlayData,
useRemoteCursorOverlayPositions,
} from '@slate-yjs/react';
import {
getPluginOptions,
usePlateEditorState,
usePlateSelectors,
} from '@udecode/plate-core';
import { UnknownObject } from '@udecode/utils';
import { flushSync } from 'react-dom';

import { KEY_YJS, YjsPlugin } from './createYjsPlugin';
import { useYjsSelectors } from './yjsStore';

export interface CaretPosition {
height: number;
top: number;
left: number;
}

export interface SelectionRect {
width: number;
height: number;

top: number;
left: number;
}

export type RenderSelectionRect<
TCursorData extends UnknownObject = UnknownObject,
> = React.FC<Pick<CursorProps<TCursorData>, 'data' | 'selectionRect'>>;
export type RenderCaret<TCursorData extends UnknownObject = UnknownObject> =
React.FC<Pick<CursorProps<TCursorData>, 'data' | 'caretPosition'>>;

export interface CursorProps<
TCursorData extends UnknownObject = UnknownObject,
> {
data: TCursorData;
selectionRect: SelectionRect;
caretPosition: CaretPosition;
}

export interface CursorOverlayProps<
TCursorData extends UnknownObject = UnknownObject,
> {
onRenderSelectionRect: RenderSelectionRect<TCursorData>;
onRenderCaret: RenderCaret<TCursorData>;
containerRef?: RefObject<HTMLDivElement>;
}

type Renderers<TCursorData extends UnknownObject = UnknownObject> = Pick<
CursorOverlayProps<TCursorData>,
'onRenderSelectionRect' | 'onRenderCaret'
>;

type RemoteSelectionProps<TCursorData extends UnknownObject = UnknownObject> =
CursorOverlayData<TCursorData> & Renderers<TCursorData>;

const RemoteSelection = <TCursorData extends UnknownObject = UnknownObject>({
data,
selectionRects,
caretPosition,
onRenderSelectionRect: RenderSelectionRect,
onRenderCaret: RenderCaret,
}: RemoteSelectionProps<TCursorData>) => {
if (!data) {
return null;
}

return (
<>
{selectionRects.map((rect, i) => {
return <RenderSelectionRect key={i} data={data} selectionRect={rect} />;
})}
{caretPosition && (
<RenderCaret data={data} caretPosition={caretPosition} />
)}
</>
);
};

type RemoteCursorOverlayContentProps<
TCursorData extends UnknownObject = UnknownObject,
> = { containerRef?: RefObject<HTMLDivElement> } & Renderers<TCursorData>;

function RemoteCursorOverlayContent<
TCursorData extends UnknownObject = UnknownObject,
>({
containerRef,
onRenderSelectionRect,
onRenderCaret,
}: RemoteCursorOverlayContentProps<TCursorData>) {
const [cursors] = useRemoteCursorOverlayPositions<TCursorData>({
containerRef,
});

return (
<>
{cursors.map((cursor) => (
<RemoteSelection
key={cursor.clientId}
{...cursor}
onRenderSelectionRect={onRenderSelectionRect}
onRenderCaret={onRenderCaret}
/>
))}
</>
);
}

export function RemoteCursorOverlay<
TCursorData extends UnknownObject = UnknownObject,
>(props: CursorOverlayProps<TCursorData>) {
const editor = usePlateEditorState();
const isRendered = usePlateSelectors().isRendered();
const isSynced = useYjsSelectors.isSynced();
const { disableCursors } = getPluginOptions<YjsPlugin>(editor, KEY_YJS);

console.log("editor", editor);

const hidden =
disableCursors ||
!editor ||
editor.children.length === 0 ||
!isSynced ||
!isRendered;

// existing issue with @slate-yjs/react https://github.com/BitPhinix/slate-yjs/issues/372
if (editor) {
const { onChange } = editor;
editor.onChange = () => {
flushSync(() => {
onChange();
});
};
}

if (hidden) return null;

return <RemoteCursorOverlayContent<TCursorData> {...props} />;
}
1 change: 1 addition & 0 deletions packages/yjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @file Automatically generated by barrelsby.
*/

export * from './RemoteCursorOverlay';
export * from './RenderAboveEditableYjs';
export * from './createYjsPlugin';
export * from './withPlateYjs';
Expand Down