Skip to content

Commit

Permalink
fix: change edit user
Browse files Browse the repository at this point in the history
  • Loading branch information
ClanEver committed Feb 5, 2025
1 parent 9c1facd commit 3c753b1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 48 deletions.
6 changes: 3 additions & 3 deletions common/themes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Extension } from '@uiw/react-codemirror'
import { catppuccin } from 'codemirror-theme-catppuccin'
import { vscodeDark, vscodeLight } from '@uiw/codemirror-theme-vscode'

const darkCodeMirrorTheme = catppuccin('macchiato')
const lightCodeMirrorTheme = catppuccin('latte')
const lightCodeMirrorTheme = vscodeLight
const darkCodeMirrorTheme = vscodeDark

export const themeMap = {
light: lightCodeMirrorTheme,
Expand Down
2 changes: 1 addition & 1 deletion components/CollaborativeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default function CollaborativeEditor({
/>
<div className='mt-4 flex justify-between items-center'>
<p>
You are User {userId} in Room {roomId}.
You are User {userId} in Room {roomId}.{' '}
{isReadOnly
? `User ${editingUser} is currently editing.`
: 'You can edit now. Press Enter to switch control.'}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"@codemirror/lang-php": "^6.0.1",
"@codemirror/language": "^6.10.8",
"@codemirror/legacy-modes": "^6.4.2",
"@uiw/codemirror-theme-vscode": "^4.23.8",
"@uiw/react-codemirror": "^4.23.7",
"clsx": "^2.1.1",
"codemirror-lang-elixir": "^4.0.0",
"codemirror-theme-catppuccin": "^0.3.0",
"next": "15.1.2",
"next-themes": "^0.4.4",
"react": "^19.0.0",
Expand Down
57 changes: 29 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 11 additions & 15 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface Room {
editingUser: number | null
lastEditPosition: number
settings: Settings
lastSwitchTime: number
}

const rooms = new Map<string, Room>()
Expand Down Expand Up @@ -90,6 +91,7 @@ io.on('connection', (socket) => {
switchTrigger: '[ \n]',
cursorAtEnd: true,
},
lastSwitchTime: Date.now(), // 初始化切换时间戳
})
}

Expand All @@ -116,26 +118,20 @@ io.on('connection', (socket) => {

socket.on(
'contentChange',
({
roomId,
content,
cursorPosition,
newChar,
}: {
roomId: string
content: string
cursorPosition: number
newChar: string | null
}) => {
console.log('contentChange', roomId, content, cursorPosition)
({ roomId, content, cursorPosition, newChar }) => {
const room = rooms.get(roomId)
if (room && room.editingUser === userId) {
// const oldContent = room.content
room.content = content
socket.to(roomId).emit('contentChange', content)

const switchTriggerRe = new RegExp(room.settings.switchTrigger)
if (newChar && switchTriggerRe.test(newChar)) {
const now = Date.now()
// 确保距离上次切换至少200毫秒
if (
newChar &&
now - room.lastSwitchTime >= 200 &&
new RegExp(room.settings.switchTrigger).test(newChar)
) {
room.lastSwitchTime = now // 更新切换时间戳
room.editingUser = arrNext(room.users, userId)
io.to(roomId).emit('setEditingUser', room.editingUser)
}
Expand Down

0 comments on commit 3c753b1

Please sign in to comment.