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 13KB

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