import { DebugFlag, Editor, TLShapePartial, createShapeId, debugFlags, featureFlags, hardResetEditor, track, uniqueId, useEditor, } from '@tldraw/editor' import React from 'react' import { useDialogs } from '../../context/dialogs' import { useToasts } from '../../context/toasts' import { untranslated } from '../../hooks/useTranslation/useTranslation' import { TldrawUiButton } from '../primitives/Button/TldrawUiButton' import { TldrawUiButtonCheck } from '../primitives/Button/TldrawUiButtonCheck' import { TldrawUiButtonLabel } from '../primitives/Button/TldrawUiButtonLabel' import { TldrawUiDialogBody, TldrawUiDialogCloseButton, TldrawUiDialogFooter, TldrawUiDialogHeader, TldrawUiDialogTitle, } from '../primitives/TldrawUiDialog' import { TldrawUiMenuCheckboxItem } from '../primitives/menus/TldrawUiMenuCheckboxItem' import { TldrawUiMenuGroup } from '../primitives/menus/TldrawUiMenuGroup' import { TldrawUiMenuItem } from '../primitives/menus/TldrawUiMenuItem' import { TldrawUiMenuSubmenu } from '../primitives/menus/TldrawUiMenuSubmenu' /** @public */ export function DefaultDebugMenuContent() { const editor = useEditor() const { addToast } = useToasts() const { addDialog } = useDialogs() const [error, setError] = React.useState(false) return ( <> { addToast({ id: uniqueId(), title: 'Something good happened', description: 'Hey, attend to this thing over here. It might be important!', keepOpen: true, severity: 'success', // icon?: string // title?: string // description?: string // actions?: TLUiToastAction[] }) addToast({ id: uniqueId(), title: 'Something happened', description: 'Hey, attend to this thing over here. It might be important!', keepOpen: true, severity: 'info', actions: [ { label: 'Primary', type: 'primary', onClick: () => { void null }, }, { label: 'Normal', type: 'normal', onClick: () => { void null }, }, { label: 'Danger', type: 'danger', onClick: () => { void null }, }, ], // icon?: string // title?: string // description?: string // actions?: TLUiToastAction[] }) addToast({ id: uniqueId(), title: 'Something maybe bad happened', description: 'Hey, attend to this thing over here. It might be important!', keepOpen: true, severity: 'warning', actions: [ { label: 'Primary', type: 'primary', onClick: () => { void null }, }, { label: 'Normal', type: 'normal', onClick: () => { void null }, }, { label: 'Danger', type: 'danger', onClick: () => { void null }, }, ], }) addToast({ id: uniqueId(), title: 'Something bad happened', severity: 'error', keepOpen: true, }) }} label={untranslated('Show toast')} /> { addDialog({ component: ({ onClose }) => ( onClose()} onContinue={() => onClose()} /> ), onClose: () => { void null }, }) }} /> createNShapes(editor, 100)} /> { const selectedShapes = editor.getSelectedShapes() const shapes = selectedShapes.length === 0 ? editor.getRenderingShapes() : selectedShapes window.alert( `Shapes ${shapes.length}, DOM nodes:${document.querySelector('.tl-shapes')!.querySelectorAll('*')?.length}` ) }} /> {(() => { if (error) throw Error('oh no!') return null })()} setError(true)} label={'Throw error'} /> {/* {...children} */} ) } /** @public */ export function DebugFlags() { const items = Object.values(debugFlags) if (!items.length) return null return ( {items.map((flag) => ( ))} ) } /** @public */ export function FeatureFlags() { const items = Object.values(featureFlags) if (!items.length) return null return ( {items.map((flag) => ( ))} ) } /** @public */ export function ExampleDialog({ title = 'title', body = 'hello hello hello', cancel = 'Cancel', confirm = 'Continue', displayDontShowAgain = false, onCancel, onContinue, }: { title?: string body?: string cancel?: string confirm?: string displayDontShowAgain?: boolean onCancel: () => void onContinue: () => void }) { const [dontShowAgain, setDontShowAgain] = React.useState(false) return ( <> {title} {body} {displayDontShowAgain && ( setDontShowAgain(!dontShowAgain)} style={{ marginRight: 'auto' }} > Don't show again )} {cancel} onContinue()}> {confirm} ) } const DebugFlagToggle = track(function DebugFlagToggle({ flag, onChange, }: { flag: DebugFlag onChange?: (newValue: boolean) => void }) { const value = flag.get() return ( `${m[0]} ${m[1].toLowerCase()}`) .replace(/^[a-z]/, (m) => m.toUpperCase())} checked={value} onSelect={() => { flag.set(!value) onChange?.(!value) }} /> ) }) let t = 0 function createNShapes(editor: Editor, n: number) { const shapesToCreate: TLShapePartial[] = Array(n) const cols = Math.floor(Math.sqrt(n)) for (let i = 0; i < n; i++) { t++ shapesToCreate[i] = { id: createShapeId('box' + t), type: 'geo', x: (i % cols) * 132, y: Math.floor(i / cols) * 132, } } editor.createShapes(shapesToCreate).setSelectedShapes(shapesToCreate.map((s) => s.id)) }