tldraw/www/components/editor.tsx

38 lines
960 B
TypeScript
Raw Normal View History

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) {
// 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
}, [])
// Send events to gtag as actions.
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-09 22:22:22 +00:00
return (
<div className="tldraw">
<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
}