-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show remote cursors when in collaboration.
- Loading branch information
Showing
6 changed files
with
241 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@udecode/plate-yjs': patch | ||
--- | ||
|
||
Add a small component to show remote 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
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 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,123 @@ | ||
// Lifted from slate-yjs https://github.com/BitPhinix/slate-yjs/blob/main/examples/frontend/src/pages/RemoteCursorOverlay/Overlay.tsx | ||
|
||
import React, { | ||
type CSSProperties, | ||
type PropsWithChildren, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
|
||
import { | ||
type CursorOverlayData, | ||
useRemoteCursorOverlayPositions, | ||
} from '@slate-yjs/react'; | ||
|
||
import { cn } from '@udecode/cn'; | ||
|
||
export function addAlpha(hexColor: string, opacity: number): string { | ||
const normalized = Math.round(Math.min(Math.max(opacity, 0), 1) * 255); | ||
|
||
return hexColor + normalized.toString(16).toUpperCase(); | ||
} | ||
|
||
export type CursorData = { | ||
color: string; | ||
name: string; | ||
}; | ||
|
||
type CaretProps = Pick<CursorOverlayData<CursorData>, 'caretPosition' | 'data'>; | ||
const cursorOpacity = 0.7; | ||
const hoverOpacity = 1; | ||
|
||
function Caret({ caretPosition, data }: CaretProps) { | ||
const [isHover, setIsHover] = useState(false); | ||
|
||
const handleMouseEnter = () => { | ||
setIsHover(true); | ||
}; | ||
const handleMouseLeave = () => { | ||
setIsHover(false); | ||
}; | ||
const caretStyle: CSSProperties = { | ||
...caretPosition, | ||
background: data?.color, | ||
opacity: cursorOpacity, | ||
transition: 'opacity 0.2s', | ||
}; | ||
const caretStyleHover = { ...caretStyle, opacity: hoverOpacity }; | ||
|
||
const labelStyle: CSSProperties = { | ||
background: data?.color, | ||
opacity: cursorOpacity, | ||
transform: 'translateY(-100%)', | ||
transition: 'opacity 0.2s', | ||
}; | ||
const labelStyleHover = { ...labelStyle, opacity: hoverOpacity }; | ||
|
||
return ( | ||
<div | ||
className="absolute w-0.5" | ||
style={isHover ? caretStyleHover : caretStyle} | ||
> | ||
<div | ||
className="absolute top-0 whitespace-nowrap rounded rounded-bl-none px-1.5 py-0.5 text-xs text-white" | ||
style={isHover ? labelStyleHover : labelStyle} | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
> | ||
{data?.name} | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
function RemoteSelection({ | ||
caretPosition, | ||
data, | ||
selectionRects, | ||
}: CursorOverlayData<CursorData>) { | ||
if (!data) { | ||
return null; | ||
} | ||
|
||
const selectionStyle: CSSProperties = { | ||
// Add a opacity to the background color | ||
backgroundColor: addAlpha(data.color, 0.5), | ||
}; | ||
|
||
return ( | ||
<React.Fragment> | ||
{selectionRects.map((position, i) => ( | ||
<div | ||
key={i} | ||
className="pointer-events-none absolute" | ||
style={{ ...selectionStyle, ...position }} | ||
></div> | ||
))} | ||
{caretPosition && <Caret caretPosition={caretPosition} data={data} />} | ||
</React.Fragment> | ||
); | ||
} | ||
|
||
type RemoteCursorsProps = PropsWithChildren<{ | ||
className?: string; | ||
}>; | ||
|
||
export function RemoteCursorOverlay({ | ||
children, | ||
className, | ||
}: RemoteCursorsProps) { | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const [cursors] = useRemoteCursorOverlayPositions<CursorData>({ | ||
containerRef, | ||
}); | ||
|
||
return ( | ||
<div ref={containerRef} className={cn('relative', className)}> | ||
{children} | ||
{cursors.map((cursor) => ( | ||
<RemoteSelection key={cursor.clientId} {...cursor} /> | ||
))} | ||
</div> | ||
); | ||
} |
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
Oops, something went wrong.