2021-09-22 12:17:52 +00:00
|
|
|
import { TLDraw, TLDrawState, Data } from '@tldraw/tldraw'
|
|
|
|
import * as gtag from '-utils/gtag'
|
2021-09-06 13:04:32 +00:00
|
|
|
import React from 'react'
|
2021-08-10 16:12:55 +00:00
|
|
|
|
2021-09-04 12:02:13 +00:00
|
|
|
interface EditorProps {
|
|
|
|
id?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function Editor({ id = 'home' }: EditorProps) {
|
2021-09-22 12:17:52 +00:00
|
|
|
// Put the tlstate into the window, for debugging.
|
|
|
|
const handleMount = React.useCallback((tlstate: TLDrawState) => {
|
2021-09-06 13:04:32 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
window.tlstate = tlstate
|
|
|
|
}, [])
|
|
|
|
|
2021-09-22 12:17:52 +00:00
|
|
|
// Send events to gtag as actions.
|
2021-10-08 23:05:24 +00:00
|
|
|
const handleChange = React.useCallback(
|
|
|
|
(_tlstate: TLDrawState, _state: Data, reason: string) => {
|
|
|
|
if (reason.startsWith('command')) {
|
|
|
|
gtag.event({
|
|
|
|
action: reason,
|
|
|
|
category: 'editor',
|
|
|
|
label: `page:${id}`,
|
|
|
|
value: 0,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
2021-09-22 12:17:52 +00:00
|
|
|
|
2021-09-09 22:22:22 +00:00
|
|
|
return (
|
|
|
|
<div className="tldraw">
|
2021-09-22 12:17:52 +00:00
|
|
|
<TLDraw id={id} onMount={handleMount} onChange={handleChange} autofocus />
|
2021-09-09 22:22:22 +00:00
|
|
|
</div>
|
|
|
|
)
|
2021-08-10 16:12:55 +00:00
|
|
|
}
|