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

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