2021-10-12 14:59:04 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
2021-11-16 16:01:29 +00:00
|
|
|
import { Tldraw, TldrawApp, TDDocument, TDUser, useFileSystem } 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-11-16 16:01:29 +00:00
|
|
|
import { useAccountHandlers } from '-hooks/useAccountHandlers'
|
|
|
|
import { styled } from '-styles'
|
2021-10-08 23:05:24 +00:00
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
declare const window: Window & { app: TldrawApp }
|
|
|
|
|
|
|
|
interface TDUserPresence extends Presence {
|
|
|
|
user: TDUser
|
2021-10-09 13:57:44 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 23:05:24 +00:00
|
|
|
const client = createClient({
|
2021-11-08 14:21:37 +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
|
|
|
})
|
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
export default function MultiplayerEditor({
|
|
|
|
roomId,
|
|
|
|
isUser = false,
|
|
|
|
isSponsor = false,
|
|
|
|
}: {
|
|
|
|
roomId: string
|
|
|
|
isUser: boolean
|
|
|
|
isSponsor: boolean
|
|
|
|
}) {
|
2021-10-08 23:05:24 +00:00
|
|
|
return (
|
|
|
|
<LiveblocksProvider client={client}>
|
2021-11-16 19:41:16 +00:00
|
|
|
<RoomProvider id={roomId} defaultStorageRoot={TldrawApp.defaultDocument}>
|
2021-11-16 16:01:29 +00:00
|
|
|
<Editor roomId={roomId} isSponsor={isSponsor} isUser={isUser} />
|
2021-10-08 23:05:24 +00:00
|
|
|
</RoomProvider>
|
|
|
|
</LiveblocksProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
// Inner Editor
|
|
|
|
|
|
|
|
function Editor({ roomId, isSponsor }: { roomId: string; isUser; isSponsor: boolean }) {
|
2021-10-09 13:57:44 +00:00
|
|
|
const [docId] = React.useState(() => Utils.uniqueId())
|
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
const [app, setApp] = React.useState<TldrawApp>()
|
2021-10-09 13:57:44 +00:00
|
|
|
|
2021-11-08 14:21:37 +00:00
|
|
|
const [error, setError] = React.useState<Error>()
|
2021-10-08 23:05:24 +00:00
|
|
|
|
2021-10-09 13:57:44 +00:00
|
|
|
useErrorListener((err) => setError(err))
|
2021-10-08 23:05:24 +00:00
|
|
|
|
2021-11-08 14:21:37 +00:00
|
|
|
// Setup document
|
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
const doc = useObject<{ uuid: string; document: TDDocument }>('doc', {
|
2021-10-09 13:57:44 +00:00
|
|
|
uuid: docId,
|
2021-10-08 23:05:24 +00:00
|
|
|
document: {
|
2021-11-16 16:01:29 +00:00
|
|
|
...TldrawApp.defaultDocument,
|
2021-11-08 14:21:37 +00:00
|
|
|
id: roomId,
|
2021-10-08 23:05:24 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-11-16 19:41:16 +00:00
|
|
|
// Put the state into the window, for debugging.
|
|
|
|
const handleMount = React.useCallback((app: TldrawApp) => {
|
|
|
|
window.app = app
|
|
|
|
setApp(app)
|
|
|
|
}, [])
|
|
|
|
|
2021-11-08 14:21:37 +00:00
|
|
|
// Setup client
|
2021-10-08 23:05:24 +00:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-11-08 14:21:37 +00:00
|
|
|
const room = client.getRoom(roomId)
|
2021-10-09 13:57:44 +00:00
|
|
|
|
|
|
|
if (!room) return
|
2021-10-08 23:05:24 +00:00
|
|
|
if (!doc) return
|
2021-11-16 19:41:16 +00:00
|
|
|
if (!app) return
|
2021-10-16 20:44:58 +00:00
|
|
|
|
2021-11-16 19:41:16 +00:00
|
|
|
app.loadRoom(roomId)
|
2021-10-09 13:57:44 +00:00
|
|
|
|
|
|
|
// Subscribe to presence changes; when others change, update the state
|
2021-11-16 16:01:29 +00:00
|
|
|
room.subscribe<TDUserPresence>('others', (others) => {
|
|
|
|
app.updateUsers(
|
2021-10-09 13:57:44 +00:00
|
|
|
others
|
|
|
|
.toArray()
|
|
|
|
.filter((other) => other.presence)
|
|
|
|
.map((other) => other.presence!.user)
|
|
|
|
.filter(Boolean)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
room.subscribe('event', (event) => {
|
|
|
|
if (event.event?.name === 'exit') {
|
2021-11-16 16:01:29 +00:00
|
|
|
app.removeUser(event.event.userId)
|
2021-10-09 13:57:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function handleDocumentUpdates() {
|
|
|
|
if (!doc) return
|
2021-11-16 16:01:29 +00:00
|
|
|
if (!app?.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) {
|
2021-11-16 16:01:29 +00:00
|
|
|
app.mergeDocument(docObject.document)
|
2021-10-12 14:59:04 +00:00
|
|
|
} else {
|
2021-11-16 16:01:29 +00:00
|
|
|
app.updateUsers(
|
|
|
|
Object.values(app.room.users).map((user) => {
|
2021-10-12 14:59:04 +00:00
|
|
|
return {
|
|
|
|
...user,
|
2021-11-08 14:21:37 +00:00
|
|
|
selectedIds: user.selectedIds,
|
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-11-16 16:01:29 +00:00
|
|
|
if (!app?.room) return
|
|
|
|
room?.broadcastEvent({ name: 'exit', userId: app.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) {
|
2021-11-16 16:01:29 +00:00
|
|
|
app.loadDocument(newDocument)
|
2021-11-16 19:41:16 +00:00
|
|
|
app.loadRoom(roomId)
|
|
|
|
|
|
|
|
// Update the user's presence with the user from state
|
|
|
|
if (app.state.room) {
|
|
|
|
const { users, userId } = app.state.room
|
|
|
|
room.updatePresence({ id: userId, user: users[userId] })
|
|
|
|
}
|
2021-11-04 15:48:39 +00:00
|
|
|
}
|
2021-10-09 13:57:44 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener('beforeunload', handleExit)
|
|
|
|
doc.unsubscribe(handleDocumentUpdates)
|
|
|
|
}
|
2021-11-16 16:01:29 +00:00
|
|
|
}, [doc, docId, app, roomId])
|
2021-11-08 14:21:37 +00:00
|
|
|
|
|
|
|
const handlePersist = React.useCallback(
|
2021-11-16 16:01:29 +00:00
|
|
|
(app: TldrawApp) => {
|
|
|
|
doc?.update({ uuid: docId, document: app.document })
|
2021-11-08 14:21:37 +00:00
|
|
|
},
|
|
|
|
[docId, doc]
|
|
|
|
)
|
2021-10-08 23:05:24 +00:00
|
|
|
|
2021-10-16 20:44:58 +00:00
|
|
|
const handleUserChange = React.useCallback(
|
2021-11-16 16:01:29 +00:00
|
|
|
(app: TldrawApp, user: TDUser) => {
|
2021-11-08 14:21:37 +00:00
|
|
|
const room = client.getRoom(roomId)
|
2021-11-16 16:01:29 +00:00
|
|
|
room?.updatePresence({ id: app.room?.userId, user })
|
2021-10-16 20:44:58 +00:00
|
|
|
},
|
2021-11-08 14:21:37 +00:00
|
|
|
[roomId]
|
2021-10-16 20:44:58 +00:00
|
|
|
)
|
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
const fileSystemEvents = useFileSystem()
|
|
|
|
|
|
|
|
const { onSignIn, onSignOut } = useAccountHandlers()
|
|
|
|
|
|
|
|
if (error) return <LoadingScreen>Error: {error.message}</LoadingScreen>
|
2021-10-08 23:05:24 +00:00
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
if (doc === null) return <LoadingScreen>Loading...</LoadingScreen>
|
2021-10-08 23:05:24 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="tldraw">
|
2021-11-16 16:01:29 +00:00
|
|
|
<Tldraw
|
|
|
|
autofocus
|
2021-10-16 20:44:58 +00:00
|
|
|
onMount={handleMount}
|
2021-11-08 14:21:37 +00:00
|
|
|
onPersist={handlePersist}
|
2021-10-16 20:44:58 +00:00
|
|
|
onUserChange={handleUserChange}
|
|
|
|
showPages={false}
|
2021-11-16 16:01:29 +00:00
|
|
|
showSponsorLink={isSponsor}
|
|
|
|
onSignIn={isSponsor ? undefined : onSignIn}
|
|
|
|
onSignOut={onSignOut}
|
|
|
|
{...fileSystemEvents}
|
2021-10-16 20:44:58 +00:00
|
|
|
/>
|
2021-10-08 23:05:24 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2021-11-16 16:01:29 +00:00
|
|
|
|
|
|
|
const LoadingScreen = styled('div', {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
})
|