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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
  2. import React from 'react'
  3. /* -------------------------------------------------- */
  4. /* Client State */
  5. /* -------------------------------------------------- */
  6. export interface Data {
  7. isReadOnly: boolean
  8. settings: {
  9. fontSize: number
  10. isDarkMode: boolean
  11. isCodeOpen: boolean
  12. isStyleOpen: boolean
  13. nudgeDistanceSmall: number
  14. nudgeDistanceLarge: number
  15. isToolLocked: boolean
  16. isPenLocked: boolean
  17. }
  18. currentStyle: ShapeStyles
  19. activeTool: ShapeType | 'select'
  20. brush?: Bounds
  21. boundsRotation: number
  22. pointedId?: string
  23. hoveredId?: string
  24. currentPageId: string
  25. currentParentId: string
  26. currentCodeFileId: string
  27. codeControls: Record<string, CodeControl>
  28. document: TLDocument
  29. pageStates: Record<string, PageState>
  30. }
  31. /* -------------------------------------------------- */
  32. /* Document */
  33. /* -------------------------------------------------- */
  34. export interface TLDocument {
  35. id: string
  36. name: string
  37. pages: Record<string, Page>
  38. code: Record<string, CodeFile>
  39. }
  40. export interface Page {
  41. id: string
  42. type: 'page'
  43. childIndex: number
  44. name: string
  45. shapes: Record<string, Shape>
  46. }
  47. export interface PageState {
  48. selectedIds: Set<string>
  49. camera: {
  50. point: number[]
  51. zoom: number
  52. }
  53. }
  54. export enum ShapeType {
  55. Dot = 'dot',
  56. Circle = 'circle',
  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 type ShapeStyles = {
  92. color: ColorStyle
  93. size: SizeStyle
  94. dash: DashStyle
  95. isFilled: boolean
  96. }
  97. export interface BaseShape {
  98. id: string
  99. seed: number
  100. type: ShapeType
  101. parentId: string
  102. childIndex: number
  103. isGenerated: boolean
  104. name: string
  105. point: number[]
  106. style: ShapeStyles
  107. rotation: number
  108. children?: string[]
  109. bindings?: Record<string, ShapeBinding>
  110. handles?: Record<string, ShapeHandle>
  111. isLocked: boolean
  112. isHidden: boolean
  113. isAspectRatioLocked: boolean
  114. }
  115. export interface DotShape extends BaseShape {
  116. type: ShapeType.Dot
  117. }
  118. export interface CircleShape extends BaseShape {
  119. type: ShapeType.Circle
  120. radius: number
  121. }
  122. export interface EllipseShape extends BaseShape {
  123. type: ShapeType.Ellipse
  124. radiusX: number
  125. radiusY: number
  126. }
  127. export interface LineShape extends BaseShape {
  128. type: ShapeType.Line
  129. direction: number[]
  130. }
  131. export interface RayShape extends BaseShape {
  132. type: ShapeType.Ray
  133. direction: number[]
  134. }
  135. export interface PolylineShape extends BaseShape {
  136. type: ShapeType.Polyline
  137. points: number[][]
  138. }
  139. export interface RectangleShape extends BaseShape {
  140. type: ShapeType.Rectangle
  141. size: number[]
  142. radius: number
  143. }
  144. export interface DrawShape extends BaseShape {
  145. type: ShapeType.Draw
  146. points: number[][]
  147. }
  148. export interface ArrowShape extends BaseShape {
  149. type: ShapeType.Arrow
  150. points: number[][]
  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. }
  163. export interface GroupShape extends BaseShape {
  164. type: ShapeType.Group
  165. children: string[]
  166. size: number[]
  167. }
  168. export type MutableShape =
  169. | DotShape
  170. | CircleShape
  171. | EllipseShape
  172. | LineShape
  173. | RayShape
  174. | PolylineShape
  175. | DrawShape
  176. | RectangleShape
  177. | ArrowShape
  178. | TextShape
  179. | GroupShape
  180. export interface Shapes {
  181. [ShapeType.Dot]: Readonly<DotShape>
  182. [ShapeType.Circle]: Readonly<CircleShape>
  183. [ShapeType.Ellipse]: Readonly<EllipseShape>
  184. [ShapeType.Line]: Readonly<LineShape>
  185. [ShapeType.Ray]: Readonly<RayShape>
  186. [ShapeType.Polyline]: Readonly<PolylineShape>
  187. [ShapeType.Draw]: Readonly<DrawShape>
  188. [ShapeType.Rectangle]: Readonly<RectangleShape>
  189. [ShapeType.Arrow]: Readonly<ArrowShape>
  190. [ShapeType.Text]: Readonly<TextShape>
  191. [ShapeType.Group]: Readonly<GroupShape>
  192. }
  193. export type Shape = Readonly<MutableShape>
  194. export type ShapeByType<T extends ShapeType> = Shapes[T]
  195. export interface CodeFile {
  196. id: string
  197. name: string
  198. code: string
  199. }
  200. export enum Decoration {
  201. Arrow = 'Arrow',
  202. }
  203. export interface ShapeBinding {
  204. id: string
  205. index: number
  206. point: number[]
  207. }
  208. export interface ShapeHandle {
  209. id: string
  210. index: number
  211. point: number[]
  212. }
  213. /* -------------------------------------------------- */
  214. /* Editor UI */
  215. /* -------------------------------------------------- */
  216. export interface PointerInfo {
  217. target: string
  218. pointerId: number
  219. origin: number[]
  220. point: number[]
  221. pressure: number
  222. shiftKey: boolean
  223. ctrlKey: boolean
  224. metaKey: boolean
  225. altKey: boolean
  226. }
  227. export enum Edge {
  228. Top = 'top_edge',
  229. Right = 'right_edge',
  230. Bottom = 'bottom_edge',
  231. Left = 'left_edge',
  232. }
  233. export enum Corner {
  234. TopLeft = 'top_left_corner',
  235. TopRight = 'top_right_corner',
  236. BottomRight = 'bottom_right_corner',
  237. BottomLeft = 'bottom_left_corner',
  238. }
  239. export interface Bounds {
  240. minX: number
  241. minY: number
  242. maxX: number
  243. maxY: number
  244. width: number
  245. height: number
  246. rotation?: number
  247. }
  248. export interface RotatedBounds extends Bounds {
  249. rotation: number
  250. }
  251. export interface ShapeBounds extends Bounds {
  252. id: string
  253. }
  254. export interface PointSnapshot extends Bounds {
  255. nx: number
  256. nmx: number
  257. ny: number
  258. nmy: number
  259. }
  260. export interface BoundsSnapshot extends PointSnapshot {
  261. nw: number
  262. nh: number
  263. }
  264. export type Difference<A, B> = A extends B ? never : A
  265. export type ShapeSpecificProps<T extends Shape> = Pick<
  266. T,
  267. Difference<keyof T, keyof BaseShape>
  268. >
  269. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  270. export type ShapeUtil<K extends Shape> = {
  271. create(props: Partial<K>): K
  272. getBounds(shape: K): Bounds
  273. hitTest(shape: K, test: number[]): boolean
  274. hitTestBounds(shape: K, bounds: Bounds): boolean
  275. rotate(shape: K): K
  276. translate(shape: K, delta: number[]): K
  277. scale(shape: K, scale: number): K
  278. stretch(shape: K, scaleX: number, scaleY: number): K
  279. render(shape: K): JSX.Element
  280. }
  281. export enum MoveType {
  282. Backward,
  283. Forward,
  284. ToFront,
  285. ToBack,
  286. }
  287. export enum AlignType {
  288. Top,
  289. CenterVertical,
  290. Bottom,
  291. Left,
  292. CenterHorizontal,
  293. Right,
  294. }
  295. export enum StretchType {
  296. Horizontal,
  297. Vertical,
  298. }
  299. export enum DistributeType {
  300. Horizontal,
  301. Vertical,
  302. }
  303. /* -------------------------------------------------- */
  304. /* Code Editor */
  305. /* -------------------------------------------------- */
  306. export type IMonaco = typeof monaco
  307. export type IMonacoEditor = monaco.editor.IStandaloneCodeEditor
  308. export enum ControlType {
  309. Number = 'number',
  310. Vector = 'vector',
  311. Text = 'text',
  312. Select = 'select',
  313. }
  314. export interface BaseCodeControl {
  315. id: string
  316. type: ControlType
  317. label: string
  318. }
  319. export interface NumberCodeControl extends BaseCodeControl {
  320. type: ControlType.Number
  321. min?: number
  322. max?: number
  323. value: number
  324. step: number
  325. format?: (value: number) => number
  326. }
  327. export interface VectorCodeControl extends BaseCodeControl {
  328. type: ControlType.Vector
  329. value: number[]
  330. isNormalized: boolean
  331. format?: (value: number[]) => number[]
  332. }
  333. export interface TextCodeControl extends BaseCodeControl {
  334. type: ControlType.Text
  335. value: string
  336. format?: (value: string) => string
  337. }
  338. export interface SelectCodeControl<T extends string = ''>
  339. extends BaseCodeControl {
  340. type: ControlType.Select
  341. value: T
  342. options: T[]
  343. format?: (string: T) => string
  344. }
  345. export type CodeControl =
  346. | NumberCodeControl
  347. | VectorCodeControl
  348. | TextCodeControl
  349. | SelectCodeControl
  350. export type PropsOfType<T extends object, K> = {
  351. [K in keyof T]: T[K] extends boolean ? K : never
  352. }[keyof T]
  353. export type Mutable<T extends Shape> = { -readonly [K in keyof T]: T[K] }