2021-10-12 14:59:04 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
2021-10-09 13:57:44 +00:00
|
|
|
import { TLDraw, TLDrawState, Data, TLDrawDocument, TLDrawUser } from '@tldraw/tldraw'
|
2021-10-08 23:05:24 +00:00
|
|
|
import * as React from 'react'
|
2021-10-09 13:57:44 +00:00
|
|
|
import { createClient, Presence } from '@liveblocks/client'
|
2021-10-08 23:05:24 +00:00
|
|
|
import { LiveblocksProvider, RoomProvider, useObject, useErrorListener } from '@liveblocks/react'
|
|
|
|
import { Utils } from '@tldraw/core'
|
|
|
|
|
2021-10-12 14:59:04 +00:00
|
|
|
interface TLDrawUserPresence extends Presence {
|
2021-10-09 13:57:44 +00:00
|
|
|
user: TLDrawUser
|
|
|
|
}
|
|
|
|
|
2021-10-08 23:05:24 +00:00
|
|
|
const client = createClient({
|
2021-10-09 13:57:44 +00:00
|
|
|
publicApiKey: process.env.NEXT_PUBLIC_LIVEBLOCKS_PUBLIC_API_KEY,
|
2021-10-16 20:44:58 +00:00
|
|
|
throttle: 80,
|
2021-10-08 23:05:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export default function MultiplayerEditor({ id }: { id: string }) {
|
|
|
|
return (
|
|
|
|
<LiveblocksProvider client={client}>
|
|
|
|
<RoomProvider id={id}>
|
|
|
|
<Editor id={id} />
|
|
|
|
</RoomProvider>
|
|
|
|
</LiveblocksProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Editor({ id }: { id: string }) {
|
2021-10-09 13:57:44 +00:00
|
|
|
const [docId] = React.useState(() => Utils.uniqueId())
|
|
|
|
|
|
|
|
const [error, setError] = React.useState<Error>()
|
|
|
|
|
2021-10-08 23:05:24 +00:00
|
|
|
const [tlstate, setTlstate] = React.useState<TLDrawState>()
|
|
|
|
|
2021-10-09 13:57:44 +00:00
|
|
|
useErrorListener((err) => setError(err))
|
2021-10-08 23:05:24 +00:00
|
|
|
|
|
|
|
const doc = useObject<{ uuid: string; document: TLDrawDocument }>('doc', {
|
2021-10-09 13:57:44 +00:00
|
|
|
uuid: docId,
|
2021-10-08 23:05:24 +00:00
|
|
|
document: {
|
2021-10-09 13:57:44 +00:00
|
|
|
id: 'test-room',
|
2021-11-04 15:48:39 +00:00
|
|
|
...TLDrawState.defaultDocument,
|
2021-10-08 23:05:24 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Put the tlstate into the window, for debugging.
|
2021-10-27 15:15:01 +00:00
|
|
|
const handleMount = React.useCallback(
|
|
|
|
(tlstate: TLDrawState) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
window.tlstate = tlstate
|
2021-10-16 20:44:58 +00:00
|
|
|
|
2021-10-27 15:15:01 +00:00
|
|
|
tlstate.loadRoom(id)
|
2021-10-16 20:44:58 +00:00
|
|
|
|
2021-10-27 15:15:01 +00:00
|
|
|
setTlstate(tlstate)
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
2021-10-08 23:05:24 +00:00
|
|
|
|
|
|
|
const handleChange = React.useCallback(
|
|
|
|
(_tlstate: TLDrawState, state: Data, reason: string) => {
|
2021-10-16 20:44:58 +00:00
|
|
|
// If the client updates its document, update the room's document
|
|
|
|
if (reason.startsWith('command') || reason.startsWith('undo') || reason.startsWith('redo')) {
|
2021-10-09 13:57:44 +00:00
|
|
|
doc?.update({ uuid: docId, document: state.document })
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the client updates its presence, update the room
|
2021-10-16 20:44:58 +00:00
|
|
|
// if (state.room && (reason === 'patch:room:self:update' || reason === 'patch:selected')) {
|
|
|
|
// const room = client.getRoom(ROOM_ID)
|
|
|
|
// if (!room) return
|
|
|
|
// const { userId, users } = state.room
|
|
|
|
// room.updatePresence({ id: userId, user: users[userId] })
|
|
|
|
// }
|
2021-10-08 23:05:24 +00:00
|
|
|
},
|
2021-10-16 20:44:58 +00:00
|
|
|
[docId, doc]
|
2021-10-08 23:05:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-10-09 13:57:44 +00:00
|
|
|
const room = client.getRoom(id)
|
|
|
|
|
|
|
|
if (!room) return
|
2021-10-08 23:05:24 +00:00
|
|
|
if (!doc) return
|
|
|
|
if (!tlstate) return
|
2021-10-16 20:44:58 +00:00
|
|
|
if (!tlstate.state.room) return
|
2021-10-08 23:05:24 +00:00
|
|
|
|
2021-10-09 13:57:44 +00:00
|
|
|
// Update the user's presence with the user from state
|
|
|
|
const { users, userId } = tlstate.state.room
|
2021-10-16 20:44:58 +00:00
|
|
|
|
|
|
|
room.updatePresence({ id: userId, user: users[userId] })
|
2021-10-09 13:57:44 +00:00
|
|
|
|
|
|
|
// Subscribe to presence changes; when others change, update the state
|
2021-10-12 14:59:04 +00:00
|
|
|
room.subscribe<TLDrawUserPresence>('others', (others) => {
|
2021-10-09 13:57:44 +00:00
|
|
|
tlstate.updateUsers(
|
|
|
|
others
|
|
|
|
.toArray()
|
|
|
|
.filter((other) => other.presence)
|
|
|
|
.map((other) => other.presence!.user)
|
|
|
|
.filter(Boolean)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
room.subscribe('event', (event) => {
|
|
|
|
if (event.event?.name === 'exit') {
|
|
|
|
tlstate.removeUser(event.event.userId)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function handleDocumentUpdates() {
|
|
|
|
if (!doc) return
|
|
|
|
if (!tlstate) return
|
2021-10-16 20:44:58 +00:00
|
|
|
if (!tlstate.state.room) return
|
2021-10-09 13:57:44 +00:00
|
|
|
|
2021-10-08 23:05:24 +00:00
|
|
|
const docObject = doc.toObject()
|
2021-10-09 13:57:44 +00:00
|
|
|
|
|
|
|
// Only merge the change if it caused by someone else
|
|
|
|
if (docObject.uuid !== docId) {
|
|
|
|
tlstate.mergeDocument(docObject.document)
|
2021-10-12 14:59:04 +00:00
|
|
|
} else {
|
|
|
|
tlstate.updateUsers(
|
|
|
|
Object.values(tlstate.state.room.users).map((user) => {
|
2021-10-16 20:44:58 +00:00
|
|
|
// const activeShapes = user.activeShapes
|
|
|
|
// .map((shape) => docObject.document.pages[tlstate.currentPageId].shapes[shape.id])
|
|
|
|
// .filter(Boolean)
|
2021-10-12 14:59:04 +00:00
|
|
|
return {
|
|
|
|
...user,
|
2021-10-16 20:44:58 +00:00
|
|
|
// activeShapes: activeShapes,
|
|
|
|
selectedIds: user.selectedIds, // activeShapes.map((shape) => shape.id),
|
2021-10-12 14:59:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2021-10-09 13:57:44 +00:00
|
|
|
}
|
2021-10-08 23:05:24 +00:00
|
|
|
}
|
|
|
|
|
2021-10-09 13:57:44 +00:00
|
|
|
function handleExit() {
|
2021-10-16 20:44:58 +00:00
|
|
|
if (!(tlstate && tlstate.state.room)) return
|
|
|
|
room?.broadcastEvent({ name: 'exit', userId: tlstate.state.room.userId })
|
2021-10-09 13:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('beforeunload', handleExit)
|
|
|
|
|
|
|
|
// When the shared document changes, update the state
|
|
|
|
doc.subscribe(handleDocumentUpdates)
|
2021-10-08 23:05:24 +00:00
|
|
|
|
2021-10-09 13:57:44 +00:00
|
|
|
// Load the shared document
|
2021-11-04 15:48:39 +00:00
|
|
|
const newDocument = doc.toObject().document
|
|
|
|
|
|
|
|
if (newDocument) {
|
|
|
|
tlstate.loadDocument(newDocument)
|
|
|
|
}
|
2021-10-09 13:57:44 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('beforeunload', handleExit)
|
|
|
|
doc.unsubscribe(handleDocumentUpdates)
|
|
|
|
}
|
|
|
|
}, [doc, docId, tlstate, id])
|
2021-10-08 23:05:24 +00:00
|
|
|
|
2021-10-16 20:44:58 +00:00
|
|
|
const handleUserChange = React.useCallback(
|
|
|
|
(tlstate: TLDrawState, user: TLDrawUser) => {
|
|
|
|
const room = client.getRoom(id)
|
|
|
|
room?.updatePresence({ id: tlstate.state.room?.userId, user })
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
2021-10-08 23:05:24 +00:00
|
|
|
if (error) return <div>Error: {error.message}</div>
|
|
|
|
|
2021-10-09 13:57:44 +00:00
|
|
|
if (doc === null) return <div>Loading...</div>
|
2021-10-08 23:05:24 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="tldraw">
|
2021-10-16 20:44:58 +00:00
|
|
|
<TLDraw
|
|
|
|
onMount={handleMount}
|
|
|
|
onChange={handleChange}
|
|
|
|
onUserChange={handleUserChange}
|
|
|
|
showPages={false}
|
|
|
|
autofocus
|
|
|
|
/>
|
2021-10-08 23:05:24 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|