Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

controlled.tsx 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import * as React from 'react'
  2. import {
  3. TLDraw,
  4. ColorStyle,
  5. DashStyle,
  6. SizeStyle,
  7. TLDrawDocument,
  8. TLDrawShapeType,
  9. } from '@tldraw/tldraw'
  10. export default function Controlled() {
  11. const rDocument = React.useRef<TLDrawDocument>({
  12. id: 'doc',
  13. pages: {
  14. page1: {
  15. id: 'page1',
  16. shapes: {
  17. rect1: {
  18. id: 'rect1',
  19. type: TLDrawShapeType.Rectangle,
  20. parentId: 'page1',
  21. name: 'Rectangle',
  22. childIndex: 1,
  23. point: [100, 100],
  24. size: [100, 100],
  25. style: {
  26. dash: DashStyle.Draw,
  27. size: SizeStyle.Medium,
  28. color: ColorStyle.Blue,
  29. },
  30. },
  31. rect2: {
  32. id: 'rect2',
  33. type: TLDrawShapeType.Rectangle,
  34. parentId: 'page1',
  35. name: 'Rectangle',
  36. childIndex: 1,
  37. point: [200, 200],
  38. size: [100, 100],
  39. style: {
  40. dash: DashStyle.Draw,
  41. size: SizeStyle.Medium,
  42. color: ColorStyle.Blue,
  43. },
  44. },
  45. },
  46. bindings: {},
  47. },
  48. },
  49. pageStates: {
  50. page1: {
  51. id: 'page1',
  52. selectedIds: ['rect1'],
  53. camera: {
  54. point: [0, 0],
  55. zoom: 1,
  56. },
  57. },
  58. },
  59. })
  60. const [doc, setDoc] = React.useState<TLDrawDocument>(rDocument.current)
  61. React.useEffect(() => {
  62. let i = 0
  63. const interval = setInterval(() => {
  64. const currentDoc = rDocument.current
  65. const rect1 = currentDoc.pages.page1.shapes.rect1
  66. if (rect1) {
  67. i++
  68. const next = {
  69. ...currentDoc,
  70. pages: {
  71. ...currentDoc.pages,
  72. page1: {
  73. ...currentDoc.pages.page1,
  74. shapes: {
  75. ...currentDoc.pages.page1.shapes,
  76. rect1: {
  77. ...rect1,
  78. style: {
  79. ...rect1.style,
  80. color: i % 2 ? ColorStyle.Red : ColorStyle.Blue,
  81. },
  82. },
  83. },
  84. },
  85. },
  86. }
  87. rDocument.current = next
  88. setDoc(next)
  89. }
  90. }, 1000)
  91. return () => {
  92. clearInterval(interval)
  93. }
  94. }, [])
  95. const handleChange = React.useCallback((tlstate) => {
  96. rDocument.current = tlstate.document
  97. }, [])
  98. return <TLDraw document={doc} onChange={handleChange} />
  99. }