2022-01-10 16:36:28 +00:00
|
|
|
import { Tldraw, TldrawApp, TldrawProps, useFileSystem } from '@tldraw/tldraw'
|
2022-06-09 14:33:35 +00:00
|
|
|
import * as React from 'react'
|
2022-08-02 13:56:12 +00:00
|
|
|
import { useUploadAssets } from '~hooks/useUploadAssets'
|
|
|
|
import * as gtag from '~utils/gtag'
|
2021-11-16 16:01:29 +00:00
|
|
|
|
|
|
|
declare const window: Window & { app: TldrawApp }
|
|
|
|
|
|
|
|
interface EditorProps {
|
|
|
|
id?: string
|
|
|
|
}
|
|
|
|
|
2022-07-23 14:05:48 +00:00
|
|
|
const Editor = ({ id = 'home', ...rest }: EditorProps & Partial<TldrawProps>) => {
|
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()
|
|
|
|
|
2022-05-18 20:46:24 +00:00
|
|
|
const { onAssetUpload } = useUploadAssets()
|
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
return (
|
|
|
|
<div className="tldraw">
|
|
|
|
<Tldraw
|
|
|
|
id={id}
|
|
|
|
autofocus
|
|
|
|
onMount={handleMount}
|
|
|
|
onPersist={handlePersist}
|
2022-05-18 20:46:24 +00:00
|
|
|
onAssetUpload={onAssetUpload}
|
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
|