c54c800675
* local images use assets for local copy add menu options * clean up packages * cleanup unused content, move file handling into app.paste * Add tldraw-assets.json to other files. * add path to editor * Update build.mjs * add export to server example with link to gist * Fix onAssetCreate and onAssetDelete APIs * Update yarn.lock * fix bugs on paste, adjust api for getting images, fix readonly on cut, copy, paste * re-enable swc * paste svg strings as svg images * cleanup * fix string case for tldraw json
78 lines
2 KiB
TypeScript
78 lines
2 KiB
TypeScript
import { createClient } from '@liveblocks/client'
|
|
import { LiveblocksProvider, RoomProvider } from '@liveblocks/react'
|
|
import { Tldraw, useFileSystem } from '@tldraw/tldraw'
|
|
import { useAccountHandlers } from 'hooks/useAccountHandlers'
|
|
import { useMultiplayerAssets } from 'hooks/useMultiplayerAssets'
|
|
import { useMultiplayerState } from 'hooks/useMultiplayerState'
|
|
import { FC } from 'react'
|
|
import { styled } from 'styles'
|
|
|
|
const client = createClient({
|
|
publicApiKey: process.env.NEXT_PUBLIC_LIVEBLOCKS_PUBLIC_API_KEY || '',
|
|
throttle: 80,
|
|
})
|
|
|
|
interface Props {
|
|
roomId: string
|
|
isUser: boolean
|
|
isSponsor: boolean
|
|
}
|
|
|
|
const MultiplayerEditor: FC<Props> = ({
|
|
roomId,
|
|
isUser = false,
|
|
isSponsor = false,
|
|
}: {
|
|
roomId: string
|
|
isUser: boolean
|
|
isSponsor: boolean
|
|
}) => {
|
|
return (
|
|
<LiveblocksProvider client={client}>
|
|
<RoomProvider id={roomId}>
|
|
<Editor roomId={roomId} isSponsor={isSponsor} isUser={isUser} />
|
|
</RoomProvider>
|
|
</LiveblocksProvider>
|
|
)
|
|
}
|
|
|
|
// Inner Editor
|
|
|
|
function Editor({ roomId, isUser, isSponsor }: Props) {
|
|
const fileSystemEvents = useFileSystem()
|
|
const { onSignIn, onSignOut } = useAccountHandlers()
|
|
const { error, ...events } = useMultiplayerState(roomId)
|
|
const { onAssetCreate, onAssetDelete } = useMultiplayerAssets()
|
|
|
|
if (error) return <LoadingScreen>Error: {error.message}</LoadingScreen>
|
|
|
|
return (
|
|
<div className="tldraw">
|
|
<Tldraw
|
|
autofocus
|
|
disableAssets={false}
|
|
showPages={false}
|
|
showSponsorLink={!isSponsor}
|
|
onSignIn={isSponsor ? undefined : onSignIn}
|
|
onSignOut={isUser ? onSignOut : undefined}
|
|
onAssetCreate={onAssetCreate}
|
|
onAssetDelete={onAssetDelete}
|
|
{...fileSystemEvents}
|
|
{...events}
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default MultiplayerEditor
|
|
|
|
const LoadingScreen = styled('div', {
|
|
position: 'absolute',
|
|
top: 0,
|
|
left: 0,
|
|
width: '100%',
|
|
height: '100%',
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
})
|