You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editor.tsx 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import * as React from 'react'
  2. import { ColorStyle, DashStyle, SizeStyle, TLDrawShapeType, TLDrawState } from '@tldraw/tldraw'
  3. import { TLDraw, TLDrawDocument } from '@tldraw/tldraw'
  4. import { usePersistence } from '../hooks/usePersistence'
  5. const initialDoc: TLDrawDocument = {
  6. id: 'doc',
  7. pages: {
  8. page1: {
  9. id: 'page1',
  10. shapes: {
  11. rect1: {
  12. id: 'rect1',
  13. parentId: 'page1',
  14. name: 'Rectangle',
  15. childIndex: 1,
  16. type: TLDrawShapeType.Rectangle,
  17. point: [32, 32],
  18. size: [100, 100],
  19. style: {
  20. dash: DashStyle.Draw,
  21. size: SizeStyle.Medium,
  22. color: ColorStyle.Blue,
  23. },
  24. },
  25. ellipse1: {
  26. id: 'ellipse1',
  27. parentId: 'page1',
  28. name: 'Ellipse',
  29. childIndex: 2,
  30. type: TLDrawShapeType.Ellipse,
  31. point: [132, 132],
  32. radius: [50, 50],
  33. style: {
  34. dash: DashStyle.Draw,
  35. size: SizeStyle.Medium,
  36. color: ColorStyle.Cyan,
  37. },
  38. },
  39. draw1: {
  40. id: 'draw1',
  41. parentId: 'page1',
  42. name: 'Draw',
  43. childIndex: 3,
  44. type: TLDrawShapeType.Draw,
  45. point: [232, 232],
  46. points: [
  47. [50, 0],
  48. [100, 100],
  49. [0, 100],
  50. [50, 0],
  51. [100, 100],
  52. [0, 100],
  53. [50, 0],
  54. [56, 5],
  55. ],
  56. style: {
  57. dash: DashStyle.Draw,
  58. size: SizeStyle.Medium,
  59. color: ColorStyle.Green,
  60. },
  61. },
  62. },
  63. bindings: {},
  64. },
  65. },
  66. pageStates: {
  67. page1: {
  68. id: 'page1',
  69. selectedIds: [],
  70. currentParentId: 'page1',
  71. camera: {
  72. point: [0, 0],
  73. zoom: 1,
  74. },
  75. },
  76. },
  77. }
  78. export default function Editor(): JSX.Element {
  79. const { value, setValue, status } = usePersistence('doc', initialDoc)
  80. const handleChange = React.useCallback(
  81. (tlstate: TLDrawState, reason: string) => {
  82. if (reason.startsWith('session')) {
  83. return
  84. }
  85. setValue(tlstate.document)
  86. },
  87. [setValue]
  88. )
  89. if (status === 'loading' || value === null) {
  90. return <div />
  91. }
  92. return <TLDraw document={value} onChange={handleChange} />
  93. }