Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

editor.tsx 2.3KB

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