Skip to content

Commit

Permalink
fix: 修复光标清除
Browse files Browse the repository at this point in the history
  • Loading branch information
coderz-w committed Oct 29, 2024
1 parent b143759 commit 704253a
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/components/cooperation/cooperationEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,19 @@ const CooperationEditor: React.FC<CooperationEditorProps> = ({ roomId, userInfo
);
setProvider(provider);

const handleBeforeUnload = () => {
provider.awareness.setLocalStateField('cursorLocation', {
x: undefined,
y: undefined,
});
};

window.addEventListener('beforeunload', handleBeforeUnload);

return () => {
provider?.destroy();
ydoc.destroy();
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, [ydoc, roomId]);

Expand All @@ -73,12 +83,24 @@ const CooperationEditor: React.FC<CooperationEditorProps> = ({ roomId, userInfo
setBinding(binding);

const handleMouseMove = throttle((e: MouseEvent) => {
provider.awareness.setLocalStateField('cursorLocation', {
x: e.clientX,
y: e.clientY,
});
}, 5);
const { clientX, clientY } = e;
const { innerWidth, innerHeight } = window;

const isNearEdge =
clientX < 10 || clientX > innerWidth - 10 || clientY < 10 || clientY > innerHeight - 10;

if (isNearEdge) {
provider.awareness.setLocalStateField('cursorLocation', {
x: undefined,
y: undefined,
});
} else {
provider.awareness.setLocalStateField('cursorLocation', {
x: clientX,
y: clientY,
});
}
}, 10);
const handleMouseout = () => {
provider.awareness.setLocalStateField('cursorLocation', {
x: undefined,
Expand Down Expand Up @@ -107,6 +129,7 @@ const CooperationEditor: React.FC<CooperationEditorProps> = ({ roomId, userInfo

let awarenessState = provider.awareness.getStates() as UserAwarenessData;
setAwareness(Array.from(awarenessState));
console.log('awarenessState', awarenessState);

let newStyles = '';
// 如果一个用户打开2个标签也要处理为一个
Expand Down

0 comments on commit 704253a

Please sign in to comment.