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.

types.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. seed: number
  106. type: ShapeType
  107. parentId: string
  108. childIndex: number
  109. isGenerated: boolean
  110. name: string
  111. point: number[]
  112. style: ShapeStyles
  113. rotation: number
  114. children?: string[]
  115. bindings?: Record<string, ShapeBinding>
  116. handles?: Record<string, ShapeHandle>
  117. isLocked: boolean
  118. isHidden: boolean
  119. isAspectRatioLocked: boolean
  120. }
  121. export interface DotShape extends BaseShape {
  122. type: ShapeType.Dot
  123. }
  124. export interface EllipseShape extends BaseShape {
  125. type: ShapeType.Ellipse
  126. radiusX: number
  127. radiusY: number
  128. }
  129. export interface LineShape extends BaseShape {
  130. type: ShapeType.Line
  131. direction: number[]
  132. }
  133. export interface RayShape extends BaseShape {
  134. type: ShapeType.Ray
  135. direction: number[]
  136. }
  137. export interface PolylineShape extends BaseShape {
  138. type: ShapeType.Polyline
  139. points: number[][]
  140. }
  141. export interface RectangleShape extends BaseShape {
  142. type: ShapeType.Rectangle
  143. size: number[]
  144. radius: number
  145. }
  146. export interface DrawShape extends BaseShape {
  147. type: ShapeType.Draw
  148. points: number[][]
  149. }
  150. export interface ArrowShape extends BaseShape {
  151. type: ShapeType.Arrow
  152. handles: Record<string, ShapeHandle>
  153. bend: number
  154. decorations?: {
  155. start: Decoration
  156. end: Decoration
  157. middle: Decoration
  158. }
  159. }
  160. export interface TextShape extends BaseShape {
  161. type: ShapeType.Text
  162. text: string
  163. size: number[] | 'auto'
  164. scale: number
  165. fontSize: FontSize
  166. }
  167. export interface GroupShape extends BaseShape {
  168. type: ShapeType.Group
  169. children: string[]
  170. size: number[]
  171. }
  172. export type ShapeProps<T extends Shape> = Partial<Omit<T, 'style'>> & {
  173. style?: Partial<ShapeStyles>
  174. }
  175. export type MutableShape =
  176. | DotShape
  177. | EllipseShape
  178. | LineShape
  179. | RayShape
  180. | PolylineShape
  181. | DrawShape
  182. | RectangleShape
  183. | ArrowShape
  184. | TextShape
  185. | GroupShape
  186. export interface Shapes {
  187. [ShapeType.Dot]: Readonly<DotShape>
  188. [ShapeType.Ellipse]: Readonly<EllipseShape>
  189. [ShapeType.Line]: Readonly<LineShape>
  190. [ShapeType.Ray]: Readonly<RayShape>
  191. [ShapeType.Polyline]: Readonly<PolylineShape>
  192. [ShapeType.Draw]: Readonly<DrawShape>
  193. [ShapeType.Rectangle]: Readonly<RectangleShape>
  194. [ShapeType.Arrow]: Readonly<ArrowShape>
  195. [ShapeType.Text]: Readonly<TextShape>
  196. [ShapeType.Group]: Readonly<GroupShape>
  197. }
  198. export type Shape = Readonly<MutableShape>
  199. export type ShapeByType<T extends ShapeType> = Shapes[T]
  200. export interface CodeFile {
  201. id: string
  202. name: string
  203. code: string
  204. }
  205. export enum Decoration {
  206. Arrow = 'Arrow',
  207. }
  208. export interface ShapeBinding {
  209. id: string
  210. index: number
  211. point: number[]
  212. }
  213. export interface ShapeHandle {
  214. id: string
  215. index: number
  216. point: number[]
  217. }
  218. /* -------------------------------------------------- */
  219. /* Editor UI */
  220. /* -------------------------------------------------- */
  221. export interface PointerInfo {
  222. target: string
  223. pointerId: number
  224. origin: number[]
  225. point: number[]
  226. pressure: number
  227. shiftKey: boolean
  228. ctrlKey: boolean
  229. metaKey: boolean
  230. altKey: boolean
  231. }
  232. export enum Edge {
  233. Top = 'top_edge',
  234. Right = 'right_edge',
  235. Bottom = 'bottom_edge',
  236. Left = 'left_edge',
  237. }
  238. export enum Corner {
  239. TopLeft = 'top_left_corner',
  240. TopRight = 'top_right_corner',
  241. BottomRight = 'bottom_right_corner',
  242. BottomLeft = 'bottom_left_corner',
  243. }
  244. export interface Bounds {
  245. minX: number
  246. minY: number
  247. maxX: number
  248. maxY: number
  249. width: number
  250. height: number
  251. rotation?: number
  252. }
  253. export interface RotatedBounds extends Bounds {
  254. rotation: number
  255. }
  256. export interface ShapeBounds extends Bounds {
  257. id: string
  258. }
  259. export interface PointSnapshot extends Bounds {
  260. nx: number
  261. nmx: number
  262. ny: number
  263. nmy: number
  264. }
  265. export interface BoundsSnapshot extends PointSnapshot {
  266. nw: number
  267. nh: number
  268. }
  269. export type Difference<A, B> = A extends B ? never : A
  270. export type ShapeSpecificProps<T extends Shape> = Pick<
  271. T,
  272. Difference<keyof T, keyof BaseShape>
  273. >
  274. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  275. export type ShapeUtil<K extends Shape> = {
  276. create(props: Partial<K>): K
  277. getBounds(shape: K): Bounds
  278. hitTest(shape: K, test: number[]): boolean
  279. hitTestBounds(shape: K, bounds: Bounds): boolean
  280. rotate(shape: K): K
  281. translate(shape: K, delta: number[]): K
  282. scale(shape: K, scale: number): K
  283. stretch(shape: K, scaleX: number, scaleY: number): K
  284. render(shape: K): JSX.Element
  285. }
  286. export enum MoveType {
  287. Backward,
  288. Forward,
  289. ToFront,
  290. ToBack,
  291. }
  292. export enum AlignType {
  293. Top,
  294. CenterVertical,
  295. Bottom,
  296. Left,
  297. CenterHorizontal,
  298. Right,
  299. }
  300. export enum StretchType {
  301. Horizontal,
  302. Vertical,
  303. }
  304. export enum DistributeType {
  305. Horizontal,
  306. Vertical,
  307. }
  308. export interface BezierCurveSegment {
  309. start: number[]
  310. tangentStart: number[]
  311. normalStart: number[]
  312. pressureStart: number
  313. end: number[]
  314. tangentEnd: number[]
  315. normalEnd: number[]
  316. pressureEnd: number
  317. }
  318. /* -------------------------------------------------- */
  319. /* Code Editor */
  320. /* -------------------------------------------------- */
  321. export enum ControlType {
  322. Number = 'number',
  323. Vector = 'vector',
  324. Text = 'text',
  325. Select = 'select',
  326. }
  327. export interface BaseCodeControl {
  328. id: string
  329. type: ControlType
  330. label: string
  331. }
  332. export interface NumberCodeControl extends BaseCodeControl {
  333. type: ControlType.Number
  334. value: number
  335. min?: number
  336. max?: number
  337. step?: number
  338. format?: (value: number) => number
  339. }
  340. export interface VectorCodeControl extends BaseCodeControl {
  341. type: ControlType.Vector
  342. value: number[]
  343. min?: number
  344. max?: number
  345. step?: number
  346. isNormalized?: boolean
  347. format?: (value: number[]) => number[]
  348. }
  349. export interface TextCodeControl extends BaseCodeControl {
  350. type: ControlType.Text
  351. value: string
  352. format?: (value: string) => string
  353. }
  354. export interface SelectCodeControl<T extends string = ''>
  355. extends BaseCodeControl {
  356. type: ControlType.Select
  357. value: T
  358. options: T[]
  359. format?: (string: T) => string
  360. }
  361. export type CodeControl =
  362. | NumberCodeControl
  363. | VectorCodeControl
  364. | TextCodeControl
  365. | SelectCodeControl
  366. export type PropsOfType<T extends Record<string, unknown>> = {
  367. [K in keyof T]: T[K] extends boolean ? K : never
  368. }[keyof T]
  369. export type Mutable<T extends Shape> = { -readonly [K in keyof T]: T[K] }
  370. export interface ShapeUtility<K extends Shape> {
  371. // A cache for the computed bounds of this kind of shape.
  372. boundsCache: WeakMap<K, Bounds>
  373. // Whether to show transform controls when this shape is selected.
  374. canTransform: boolean
  375. // Whether the shape's aspect ratio can change.
  376. canChangeAspectRatio: boolean
  377. // Whether the shape's style can be filled.
  378. canStyleFill: boolean
  379. // Whether the shape may be edited in an editing mode
  380. canEdit: boolean
  381. // Whether the shape is a foreign object.
  382. isForeignObject: boolean
  383. // Whether the shape can contain other shapes.
  384. isParent: boolean
  385. // Whether the shape is only shown when on hovered.
  386. isShy: boolean
  387. // Create a new shape.
  388. create(props: Partial<K>): K
  389. // Update a shape's styles
  390. applyStyles(
  391. this: ShapeUtility<K>,
  392. shape: Mutable<K>,
  393. style: Partial<ShapeStyles>
  394. ): ShapeUtility<K>
  395. translateBy(
  396. this: ShapeUtility<K>,
  397. shape: Mutable<K>,
  398. point: number[]
  399. ): ShapeUtility<K>
  400. translateTo(
  401. this: ShapeUtility<K>,
  402. shape: Mutable<K>,
  403. point: number[]
  404. ): ShapeUtility<K>
  405. rotateBy(
  406. this: ShapeUtility<K>,
  407. shape: Mutable<K>,
  408. rotation: number
  409. ): ShapeUtility<K>
  410. rotateTo(
  411. this: ShapeUtility<K>,
  412. shape: Mutable<K>,
  413. rotation: number,
  414. delta: number
  415. ): ShapeUtility<K>
  416. // Transform to fit a new bounding box when more than one shape is selected.
  417. transform(
  418. this: ShapeUtility<K>,
  419. shape: Mutable<K>,
  420. bounds: Bounds,
  421. info: {
  422. type: Edge | Corner
  423. initialShape: K
  424. scaleX: number
  425. scaleY: number
  426. transformOrigin: number[]
  427. }
  428. ): ShapeUtility<K>
  429. // Transform a single shape to fit a new bounding box.
  430. transformSingle(
  431. this: ShapeUtility<K>,
  432. shape: Mutable<K>,
  433. bounds: Bounds,
  434. info: {
  435. type: Edge | Corner
  436. initialShape: K
  437. scaleX: number
  438. scaleY: number
  439. transformOrigin: number[]
  440. }
  441. ): ShapeUtility<K>
  442. setProperty<P extends keyof K>(
  443. this: ShapeUtility<K>,
  444. shape: Mutable<K>,
  445. prop: P,
  446. value: K[P]
  447. ): ShapeUtility<K>
  448. // Respond when any child of this shape changes.
  449. onChildrenChange(
  450. this: ShapeUtility<K>,
  451. shape: Mutable<K>,
  452. children: Shape[]
  453. ): ShapeUtility<K>
  454. // Respond when a user moves one of the shape's bound elements.
  455. onBindingChange(
  456. this: ShapeUtility<K>,
  457. shape: Mutable<K>,
  458. bindings: Record<string, ShapeBinding>
  459. ): ShapeUtility<K>
  460. // Respond when a user moves one of the shape's handles.
  461. onHandleChange(
  462. this: ShapeUtility<K>,
  463. shape: Mutable<K>,
  464. handle: Partial<K['handles']>
  465. ): ShapeUtility<K>
  466. onDoublePointHandle(
  467. this: ShapeUtility<K>,
  468. shape: Mutable<K>,
  469. handle: keyof K['handles'],
  470. info: PointerInfo
  471. ): ShapeUtility<K>
  472. // Respond when a user double clicks the shape's bounds.
  473. onBoundsReset(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  474. // Respond when a user double clicks the center of the shape.
  475. onDoubleFocus(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  476. // Clean up changes when a session ends.
  477. onSessionComplete(this: ShapeUtility<K>, shape: Mutable<K>): ShapeUtility<K>
  478. // Render a shape to JSX.
  479. render(
  480. this: ShapeUtility<K>,
  481. shape: K,
  482. info: {
  483. isEditing: boolean
  484. ref?: React.MutableRefObject<HTMLTextAreaElement>
  485. }
  486. ): JSX.Element
  487. invalidate(this: ShapeUtility<K>, shape: K): ShapeUtility<K>
  488. // Get the bounds of the a shape.
  489. getBounds(this: ShapeUtility<K>, shape: K): Bounds
  490. // Get the routated bounds of the a shape.
  491. getRotatedBounds(this: ShapeUtility<K>, shape: K): Bounds
  492. // Get the center of the shape
  493. getCenter(this: ShapeUtility<K>, shape: K): number[]
  494. // Test whether a point lies within a shape.
  495. hitTest(this: ShapeUtility<K>, shape: K, test: number[]): boolean
  496. // Test whether bounds collide with or contain a shape.
  497. hitTestBounds(this: ShapeUtility<K>, shape: K, bounds: Bounds): boolean
  498. shouldDelete(this: ShapeUtility<K>, shape: K): boolean
  499. }