2022-01-10 16:36:28 +00:00
|
|
|
import { Tldraw, TldrawApp, TldrawProps, useFileSystem } from '@tldraw/tldraw'
|
2021-11-26 15:14:10 +00:00
|
|
|
import { useAccountHandlers } from 'hooks/useAccountHandlers'
|
2022-03-24 09:05:35 +00:00
|
|
|
import React, { FC } from 'react'
|
2022-01-10 16:36:28 +00:00
|
|
|
import { exportToImage } from 'utils/export'
|
2022-03-24 09:05:35 +00:00
|
|
|
import * as gtag from 'utils/gtag'
|
2021-11-16 16:01:29 +00:00
|
|
|
|
|
|
|
declare const window: Window & { app: TldrawApp }
|
|
|
|
|
|
|
|
interface EditorProps {
|
|
|
|
id?: string
|
|
|
|
isUser?: boolean
|
|
|
|
isSponsor?: boolean
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:05:35 +00:00
|
|
|
const Editor: FC<EditorProps & Partial<TldrawProps>> = ({
|
2022-01-10 16:36:28 +00:00
|
|
|
id = 'home',
|
|
|
|
isUser = false,
|
|
|
|
isSponsor = false,
|
|
|
|
...rest
|
2022-03-24 09:05:35 +00:00
|
|
|
}) => {
|
2021-11-16 16:01:29 +00:00
|
|
|
const handleMount = React.useCallback((app: TldrawApp) => {
|
|
|
|
window.app = app
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
// Send events to gtag as actions.
|
|
|
|
const handlePersist = React.useCallback((_app: TldrawApp, reason?: string) => {
|
|
|
|
gtag.event({
|
2021-11-26 15:14:10 +00:00
|
|
|
action: reason ?? '',
|
2021-11-16 16:01:29 +00:00
|
|
|
category: 'editor',
|
2021-11-26 15:14:10 +00:00
|
|
|
label: reason ?? 'persist',
|
2021-11-16 16:01:29 +00:00
|
|
|
value: 0,
|
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
const fileSystemEvents = useFileSystem()
|
|
|
|
|
|
|
|
const { onSignIn, onSignOut } = useAccountHandlers()
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="tldraw">
|
|
|
|
<Tldraw
|
|
|
|
id={id}
|
|
|
|
autofocus
|
|
|
|
onMount={handleMount}
|
|
|
|
onPersist={handlePersist}
|
|
|
|
showSponsorLink={!isSponsor}
|
|
|
|
onSignIn={isSponsor ? undefined : onSignIn}
|
2021-11-19 12:59:08 +00:00
|
|
|
onSignOut={isUser ? onSignOut : undefined}
|
2022-01-10 16:36:28 +00:00
|
|
|
onExport={exportToImage}
|
2021-11-16 16:01:29 +00:00
|
|
|
{...fileSystemEvents}
|
2022-01-10 16:36:28 +00:00
|
|
|
{...rest}
|
2021-11-16 16:01:29 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2022-03-24 09:05:35 +00:00
|
|
|
|
|
|
|
export default Editor
|