Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

types.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /* -------------------------------------------------- */
  2. /* Client State */
  3. /* -------------------------------------------------- */
  4. export interface Data {
  5. isReadOnly: boolean
  6. settings: {
  7. fontSize: number
  8. isDarkMode: boolean
  9. isCodeOpen: boolean
  10. isStyleOpen: boolean
  11. nudgeDistanceSmall: number
  12. nudgeDistanceLarge: number
  13. isToolLocked: boolean
  14. isPenLocked: boolean
  15. }
  16. currentStyle: ShapeStyles
  17. activeTool: ShapeType | 'select'
  18. brush?: Bounds
  19. boundsRotation: number
  20. pointedId?: string
  21. hoveredId?: string
  22. editingId?: string
  23. currentPageId: string
  24. currentParentId: string
  25. currentCodeFileId: string
  26. codeControls: Record<string, CodeControl>
  27. document: TLDocument
  28. pageStates: Record<string, PageState>
  29. }
  30. /* -------------------------------------------------- */
  31. /* Document */
  32. /* -------------------------------------------------- */
  33. export interface TLDocument {
  34. id: string
  35. name: string
  36. pages: Record<string, Page>
  37. code: Record<string, CodeFile>
  38. }
  39. export interface Page {
  40. id: string
  41. type: 'page'
  42. childIndex: number
  43. name: string
  44. shapes: Record<string, Shape>
  45. }
  46. export interface PageState {
  47. id: string
  48. selectedIds: Set<string>
  49. camera: {
  50. point: number[]
  51. zoom: number
  52. }
  53. }
  54. /* ----------------- Start Copy Here ---------------- */
  55. export enum ShapeType {
  56. Dot = 'dot',
  57. Ellipse = 'ellipse',
  58. Line = 'line',
  59. Ray = 'ray',
  60. Polyline = 'polyline',
  61. Rectangle = 'rectangle',
  62. Draw = 'draw',
  63. Arrow = 'arrow',
  64. Text = 'text',
  65. Group = 'group',
  66. }
  67. export enum ColorStyle {
  68. White = 'White',
  69. LightGray = 'LightGray',
  70. Gray = 'Gray',
  71. Black = 'Black',
  72. Green = 'Green',
  73. Cyan = 'Cyan',
  74. Blue = 'Blue',
  75. Indigo = 'Indigo',
  76. Violet = 'Violet',
  77. Red = 'Red',
  78. Orange = 'Orange',
  79. Yellow = 'Yellow',
  80. }
  81. export enum SizeStyle {
  82. Small = 'Small',
  83. Medium = 'Medium',
  84. Large = 'Large',
  85. }
  86. export enum DashStyle {
  87. Solid = 'Solid',
  88. Dashed = 'Dashed',
  89. Dotted = 'Dotted',
  90. }
  91. export enum FontSize {
  92. Small = 'Small',
  93. Medium = 'Medium',
  94. Large = 'Large',
  95. ExtraLarge = 'ExtraLarge',
  96. }
  97. export type ShapeStyles = {
  98. color: ColorStyle
  99. size: SizeStyle
  100. dash: DashStyle
  101. isFilled: boolean
  102. }
  103. export interface BaseShape {
  104. id: string
  105. type: ShapeType
  106. parentId: string
  107. childIndex: number
  108. isGenerated: boolean
  109. name: string
  110. point: number[]
  111. style: ShapeStyles
  112. rotation: number
  113. children?: string[]
  114. bindings?: Record<string, ShapeBinding>
  115. handles?: Record<string, ShapeHandle>
  116. isLocked: boolean
  117. isHidden: boolean
  118. isAspectRatioLocked: boolean
  119. }
  120. export interface DotShape extends BaseShape {
  121. type: ShapeType.Dot
  122. }
  123. export interface EllipseShape extends BaseShape {
  124. type: ShapeType.Ellipse
  125. radiusX: number
  126. radiusY: number
  127. }
  128. export interface LineShape extends BaseShape {
  129. type: ShapeType.Line
  130. direction: number[]
  131. }
  132. export interface RayShape extends BaseShape {
  133. type: ShapeType.Ray
  134. direction: number[]
  135. }
  136. export interface PolylineShape extends BaseShape {
  137. type: ShapeType.Polyline
  138. points: number[][]
  139. }
  140. export interface RectangleShape extends BaseShape {
  141. type: ShapeType.Rectangle
  142. size: number[]
  143. radius: number
  144. }
  145. export interface DrawShape extends BaseShape {
  146. type: ShapeType.Draw
  147. points: number[][]
  148. }
  149. export interface ArrowShape extends BaseShape {
  150. type: ShapeType.Arrow
  151. handles: Record<string, ShapeHandle>
  152. bend: number
  153. decorations?: {
  154. start: Decoration
  155. end: Decoration
  156. middle: Decoration
  157. }
  158. }
  159. export interface TextShape extends BaseShape {
  160. type: ShapeType.Text
  161. text: string
  162. size: number[] | 'auto'
  163. scale: number
  164. fontSize: FontSize
  165. }
  166. export interface GroupShape extends BaseShape {
  167. type: ShapeType.Group
  168. children: string[]
  169. size: number[]
  170. }
  171. export type ShapeProps<T extends Shape> = Partial<Omit<T, 'style'>> & {
  172. style?: Partial<ShapeStyles>
  173. }
  174. export type MutableShape =
  175. | DotShape
  176. | EllipseShape
  177. | LineShape
  178. | RayShape
  179. | PolylineShape
  180. | DrawShape
  181. | RectangleShape
  182. | ArrowShape
  183. | TextShape
  184. | GroupShape
  185. export interface Shapes {
  186. [ShapeType.Dot]: Readonly<DotShape>
  187. [ShapeType.Ellipse]: Readonly<EllipseShape>
  188. [ShapeType.Line]: Readonly<LineShape>
  189. [ShapeType.Ray]: Readonly<RayShape>
  190. [ShapeType.Polyline]: Readonly<PolylineShape>
  191. [ShapeType.Draw]: Readonly<DrawShape>
  192. [ShapeType.Rectangle]: Readonly<RectangleShape>
  193. [ShapeType.Arrow]: Readonly<ArrowShape>
  194. [ShapeType.Text]: Readonly<TextShape>
  195. [ShapeType.Group]: Readonly<GroupShape>
  196. }
  197. export type Shape = Readonly<MutableShape>
  198. export type ShapeByType<T extends ShapeType> = Shapes[T]
  199. export interface CodeFile {
  200. id: string
  201. name: string
  202. code: string
  203. }
  204. export enum Decoration {
  205. Arrow = 'Arrow',
  206. }
  207. export interface ShapeBinding {
  208. id: string
  209. index: number
  210. point: number[]
  211. }
  212. export interface ShapeHandle {
  213. id: string
  214. index: number
  215. point: number[]
  216. }
  217. /* -------------------------------------------------- */
  218. /* Editor UI */
  219. /* -------------------------------------------------- */
  220. export interface PointerInfo {
  221. target: string
  222. pointerId: number
  223. origin: number[]
  224. point: number[]
  225. pressure: number
  226. shiftKey: boolean
  227. ctrlKey: boolean
  228. metaKey: boolean
  229. altKey: boolean
  230. }
  231. export enum Edge {
  232. Top = 'top_edge',
  233. Right = 'right_edge',
  234. Bottom = 'bottom_edge',
  235. Left = 'left_edge',
  236. }
  237. export enum Corner {
  238. TopLeft = 'top_left_corner',
  239. TopRight = 'top_right_corner',
  240. BottomRight = 'bottom_right_corner',
  241. BottomLeft = 'bottom_left_corner',
  242. }
  243. export interface Bounds {
  244. minX: number
  245. minY: number
  246. maxX: number
  247. maxY: number
  248. width: number
  249. height: number
  250. rotation?: number
  251. }
  252. export interface RotatedBounds extends Bounds {
  253. rotation: number
  254. }
  255. export interface ShapeBounds extends Bounds {
  256. id: string
  257. }
  258. export interface PointSnapshot extends Bounds {
  259. nx: number
  260. nmx: number
  261. ny: number
  262. nmy: number
  263. }
  264. export interface BoundsSnapshot extends PointSnapshot {
  265. nw: number
  266. nh: number
  267. }
  268. export type Difference<A, B> = A extends B ? never : A
  269. export type ShapeSpecificProps<T extends Shape> = Pick<
  270. T,
  271. Difference<keyof T, keyof BaseShape>
  272. >
  273. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  274. export type ShapeUtil<K extends Shape> = {
  275. create(props: Partial<K>): K
  276. getBounds(shape: K): Bounds
  277. hitTest(shape: K, test: number[]): boolean
  278. hitTestBounds(shape: K, bounds: Bounds): boolean
  279. rotate(shape: K): K
  280. translate(shape: K, delta: number[]): K
  281. scale(shape: K, scale: number): K
  282. stretch(shape: K, scaleX: number, scaleY: number): K
  283. render(shape: K): JSX.Element
  284. }
  285. export enum MoveType {
  286. Backward,
  287. Forward,
  288. ToFront,
  289. ToBack,
  290. }
  291. export enum AlignType {
  292. Top,
  293. CenterVertical,
  294. Bottom,
  295. Left,
  296. CenterHorizontal,
  297. Right,
  298. }
  299. export enum StretchType {
  300. Horizontal,
  301. Vertical,
  302. }
  303. export enum DistributeType {
  304. Horizontal,
  305. Vertical,
  306. }
  307. export interface BezierCurveSegment {
  308. start: number[]
  309. tangentStart: number[]
  310. normalStart: number[]
  311. pressureStart: number
  312. end: number[]
  313. tangentEnd: number[]
  314. normalEnd: number[]
  315. pressureEnd: number
  316. }
  317. /* -------------------------------------------------- */
  318. /* Code Editor */
  319. /* -------------------------------------------------- */
  320. export enum ControlType {
  321. Number = 'number',
  322. Vector = 'vector',
  323. Text = 'text',
  324. Select = 'select',
  325. }
  326. export interface BaseCodeControl {
  327. id: string
  328. type: ControlType
  329. label: string
  330. }
  331. export interface NumberCodeControl extends BaseCodeControl {
  332. type: ControlType.Number
  333. value: number
  334. min?: number
  335. max?: number
  336. step?: number
  337. format?: (value: number) => number
  338. }
  339. export interface VectorCodeControl extends BaseCodeControl {
  340. type: ControlType.Vector
  341. value: number[]
  342. min?: number
  343. max?: number
  344. step?: number
  345. isNormalized?: boolean
  346. format?: (value: number[]) => number[]
  347. }
  348. export interface TextCodeControl extends BaseCodeControl {
  349. type: ControlType.Text
  350. value: string
  351. format?: (value: string) => string
  352. }
  353. export interface SelectCodeControl<T extends string = ''>
  354. extends BaseCodeControl {
  355. type: ControlType.Select
  356. value: T
  357. options: T[]
  358. format?: (string: T) => string
  359. }
  360. export type CodeControl =
  361. | NumberCodeControl
  362. | VectorCodeControl
  363. | TextCodeControl
  364. | SelectCodeControl
  365. export type PropsOfType<T extends Record<string, unknown>> = {
  366. [K in keyof T]: T[K] extends boolean ? K : never
  367. }[keyof T]
  368. export type Mutable<T extends Shape> = { -readonly [K in keyof T]: T[K] }
  369. export interface ShapeUtility<K extends Shape> {
  370. // A cache for the computed bounds of this kind of shape.
  371. boundsCache: WeakMap<K, Bounds>
  372. // Whether to show transform controls when this shape is selected.
  373. canTransform: boolean
  374. // Whether the shape's aspect ratio can change.
  375. canChangeAspectRatio: boolean
  376. // Whether the shape's style can be filled.
  377. canStyleFill: boolean
  378. // Whether the shape may be edited in an editing mode
  379. canEdit: boolean
  380. // Whether the shape is a foreign object.
  381. isForeignObject: boolean
  382. // Whether the shape can contain other shapes.
  383. isParent: boolean
  384. // Whether the shape is only shown when on hovered.
  385. isShy: boolean
  386. // Create a new shape.
  387. create(props: Partial<K>): K
  388. // Update a shape's styles
  389. applyStyles(
  390. this: ShapeUtility<K>,
  391. shape: Mutable<K>,
  392. style: Partial<ShapeStyles>
  393. ): ShapeUtility<K>
  394. translateBy(
  395. this: ShapeUtility<K>,
  396. shape: Mutable<K>,
  397. point: number[]
  398. ): ShapeUtility<K>
  399. translateTo(
  400. this: ShapeUtility<K>,
  401. shape: Mutable<K>,
  402. point: number[]
  403. ): ShapeUtility<K>
  404. rotateBy(
  405. this: ShapeUtility<K>,
  406. shape: Mutable<K>,
  407. rotation: number
  408. ): ShapeUtility<K>
  409. rotateTo(
  410. this: ShapeUtility<K>,
  411. shape: Mutable<K>,
  412. rotation: number,
  413. delta: number
  414. ): ShapeUtility<K>
  415. // Transform to fit a new bounding box when more than one shape is selected.
  416. transform(
  417. this: ShapeUtility<K>,
  418. shape: Mutable<K>,
  419. bounds: Bounds,
  420. info: {
  421. type: Edge | Corner
  422. initialShape: K
  423. scaleX: number
  424. scaleY: number
  425. transformOrigin: number[]
  426. }
  427. ): ShapeUtility<K>
  428. // Transform a single shape to fit a new bounding box.
  429. transformSingle(
  430. this: ShapeUtility<K>,
  431. shape: Mutable<K>,
  432. bounds: Bounds,
  433. info: {
  434. type: Edge | Corner
  435. initialShape: K
  436. scaleX: number
  437. scaleY: number
  438. transformOrigin: number[]
  439. }
  440. ): ShapeUtility<K>
  441. setProperty<P extends keyof K>(
  442. this: ShapeUtility<K>,
  443. shape: Mutable<K>,
  444. prop: P,
  445. value: K[P]
  446. ): ShapeUtility<K>
  447. // Respond when any child of this shape changes.
  448. onChildrenChange(
  449. this: ShapeUtility<K>,
  450. shape: Mutable<K>,
  451. children: Shape[]
  452. ): ShapeUtility<K>
  453. // Respond when a user moves one of the shape's bound elements.
  454. onBindingChange(
  455. this: ShapeUtility<K>,
  456. shape: Mutable<K>,
  457. bindings: Record<string, ShapeBinding>
  458. ): ShapeUtility<K>
  459. // Respond when a user moves one of the shape's handles.
  460. onHandleChange(
  461. this: ShapeUtility<K>,
  462. shape: Mutable<K>,
  463. handle: Partial<K['handles']>
  464. ): ShapeUtility<K>
  465. onDoublePointHandle(
  466. this: ShapeUtility<K>,
  467. shape: Mutable<K>,
  468. handle: keyof K['handles'],
  469. info: PointerInfo
  470. ): ShapeUtility<K>
  471. // Respond when a user double clicks the shape's bounds.
  472. onBoundsReset(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  473. // Respond when a user double clicks the center of the shape.
  474. onDoubleFocus(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  475. // Clean up changes when a session ends.
  476. onSessionComplete(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  477. // Render a shape to JSX.
  478. render(
  479. this: ShapeUtility<K>,
  480. shape: K,
  481. info: {
  482. isEditing: boolean
  483. ref?: React.MutableRefObject<HTMLTextAreaElement>
  484. }
  485. ): JSX.Element
  486. invalidate(this: ShapeUtility<K>, shape: K): ShapeUtility<K>
  487. // Get the bounds of the a shape.
  488. getBounds(this: ShapeUtility<K>, shape: K): Bounds
  489. // Get the routated bounds of the a shape.
  490. getRotatedBounds(this: ShapeUtility<K>, shape: K): Bounds
  491. // Get the center of the shape
  492. getCenter(this: ShapeUtility<K>, shape: K): number[]
  493. // Test whether a point lies within a shape.
  494. hitTest(this: ShapeUtility<K>, shape: K, test: number[]): boolean
  495. // Test whether bounds collide with or contain a shape.
  496. hitTestBounds(this: ShapeUtility<K>, shape: K, bounds: Bounds): boolean
  497. shouldDelete(this: ShapeUtility<K>, shape: K): boolean
  498. }