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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 & TextStyles
  19. activeTool: ShapeType | 'select'
  20. brush?: Bounds
  21. boundsRotation: number
  22. pointedId?: string
  23. hoveredId?: string
  24. editingId?: string
  25. currentPageId: string
  26. currentParentId: string
  27. currentCodeFileId: string
  28. codeControls: Record<string, CodeControl>
  29. document: TLDocument
  30. pageStates: Record<string, PageState>
  31. }
  32. /* -------------------------------------------------- */
  33. /* Document */
  34. /* -------------------------------------------------- */
  35. export interface TLDocument {
  36. id: string
  37. name: string
  38. pages: Record<string, Page>
  39. code: Record<string, CodeFile>
  40. }
  41. export interface Page {
  42. id: string
  43. type: 'page'
  44. childIndex: number
  45. name: string
  46. shapes: Record<string, Shape>
  47. }
  48. export interface PageState {
  49. selectedIds: Set<string>
  50. camera: {
  51. point: number[]
  52. zoom: number
  53. }
  54. }
  55. export enum ShapeType {
  56. Dot = 'dot',
  57. Circle = 'circle',
  58. Ellipse = 'ellipse',
  59. Line = 'line',
  60. Ray = 'ray',
  61. Polyline = 'polyline',
  62. Rectangle = 'rectangle',
  63. Draw = 'draw',
  64. Arrow = 'arrow',
  65. Text = 'text',
  66. Group = 'group',
  67. }
  68. export enum ColorStyle {
  69. White = 'White',
  70. LightGray = 'LightGray',
  71. Gray = 'Gray',
  72. Black = 'Black',
  73. Green = 'Green',
  74. Cyan = 'Cyan',
  75. Blue = 'Blue',
  76. Indigo = 'Indigo',
  77. Violet = 'Violet',
  78. Red = 'Red',
  79. Orange = 'Orange',
  80. Yellow = 'Yellow',
  81. }
  82. export enum SizeStyle {
  83. Small = 'Small',
  84. Medium = 'Medium',
  85. Large = 'Large',
  86. }
  87. export enum DashStyle {
  88. Solid = 'Solid',
  89. Dashed = 'Dashed',
  90. Dotted = 'Dotted',
  91. }
  92. export enum FontSize {
  93. Small = 'Small',
  94. Medium = 'Medium',
  95. Large = 'Large',
  96. ExtraLarge = 'ExtraLarge',
  97. }
  98. export type ShapeStyles = {
  99. color: ColorStyle
  100. size: SizeStyle
  101. dash: DashStyle
  102. isFilled: boolean
  103. }
  104. export type TextStyles = {
  105. fontSize: FontSize
  106. }
  107. export interface BaseShape {
  108. id: string
  109. seed: number
  110. type: ShapeType
  111. parentId: string
  112. childIndex: number
  113. isGenerated: boolean
  114. name: string
  115. point: number[]
  116. style: ShapeStyles
  117. rotation: number
  118. children?: string[]
  119. bindings?: Record<string, ShapeBinding>
  120. handles?: Record<string, ShapeHandle>
  121. isLocked: boolean
  122. isHidden: boolean
  123. isAspectRatioLocked: boolean
  124. }
  125. export interface DotShape extends BaseShape {
  126. type: ShapeType.Dot
  127. }
  128. export interface CircleShape extends BaseShape {
  129. type: ShapeType.Circle
  130. radius: number
  131. }
  132. export interface EllipseShape extends BaseShape {
  133. type: ShapeType.Ellipse
  134. radiusX: number
  135. radiusY: number
  136. }
  137. export interface LineShape extends BaseShape {
  138. type: ShapeType.Line
  139. direction: number[]
  140. }
  141. export interface RayShape extends BaseShape {
  142. type: ShapeType.Ray
  143. direction: number[]
  144. }
  145. export interface PolylineShape extends BaseShape {
  146. type: ShapeType.Polyline
  147. points: number[][]
  148. }
  149. export interface RectangleShape extends BaseShape {
  150. type: ShapeType.Rectangle
  151. size: number[]
  152. radius: number
  153. }
  154. export interface DrawShape extends BaseShape {
  155. type: ShapeType.Draw
  156. points: number[][]
  157. }
  158. export interface ArrowShape extends BaseShape {
  159. type: ShapeType.Arrow
  160. points: number[][]
  161. handles: Record<string, ShapeHandle>
  162. bend: number
  163. decorations?: {
  164. start: Decoration
  165. end: Decoration
  166. middle: Decoration
  167. }
  168. }
  169. export interface TextShape extends BaseShape {
  170. type: ShapeType.Text
  171. text: string
  172. size: number[] | 'auto'
  173. fontSize: FontSize
  174. }
  175. export interface GroupShape extends BaseShape {
  176. type: ShapeType.Group
  177. children: string[]
  178. size: number[]
  179. }
  180. export type MutableShape =
  181. | DotShape
  182. | CircleShape
  183. | EllipseShape
  184. | LineShape
  185. | RayShape
  186. | PolylineShape
  187. | DrawShape
  188. | RectangleShape
  189. | ArrowShape
  190. | TextShape
  191. | GroupShape
  192. export interface Shapes {
  193. [ShapeType.Dot]: Readonly<DotShape>
  194. [ShapeType.Circle]: Readonly<CircleShape>
  195. [ShapeType.Ellipse]: Readonly<EllipseShape>
  196. [ShapeType.Line]: Readonly<LineShape>
  197. [ShapeType.Ray]: Readonly<RayShape>
  198. [ShapeType.Polyline]: Readonly<PolylineShape>
  199. [ShapeType.Draw]: Readonly<DrawShape>
  200. [ShapeType.Rectangle]: Readonly<RectangleShape>
  201. [ShapeType.Arrow]: Readonly<ArrowShape>
  202. [ShapeType.Text]: Readonly<TextShape>
  203. [ShapeType.Group]: Readonly<GroupShape>
  204. }
  205. export type Shape = Readonly<MutableShape>
  206. export type ShapeByType<T extends ShapeType> = Shapes[T]
  207. export interface CodeFile {
  208. id: string
  209. name: string
  210. code: string
  211. }
  212. export enum Decoration {
  213. Arrow = 'Arrow',
  214. }
  215. export interface ShapeBinding {
  216. id: string
  217. index: number
  218. point: number[]
  219. }
  220. export interface ShapeHandle {
  221. id: string
  222. index: number
  223. point: number[]
  224. }
  225. /* -------------------------------------------------- */
  226. /* Editor UI */
  227. /* -------------------------------------------------- */
  228. export interface PointerInfo {
  229. target: string
  230. pointerId: number
  231. origin: number[]
  232. point: number[]
  233. pressure: number
  234. shiftKey: boolean
  235. ctrlKey: boolean
  236. metaKey: boolean
  237. altKey: boolean
  238. }
  239. export enum Edge {
  240. Top = 'top_edge',
  241. Right = 'right_edge',
  242. Bottom = 'bottom_edge',
  243. Left = 'left_edge',
  244. }
  245. export enum Corner {
  246. TopLeft = 'top_left_corner',
  247. TopRight = 'top_right_corner',
  248. BottomRight = 'bottom_right_corner',
  249. BottomLeft = 'bottom_left_corner',
  250. }
  251. export interface Bounds {
  252. minX: number
  253. minY: number
  254. maxX: number
  255. maxY: number
  256. width: number
  257. height: number
  258. rotation?: number
  259. }
  260. export interface RotatedBounds extends Bounds {
  261. rotation: number
  262. }
  263. export interface ShapeBounds extends Bounds {
  264. id: string
  265. }
  266. export interface PointSnapshot extends Bounds {
  267. nx: number
  268. nmx: number
  269. ny: number
  270. nmy: number
  271. }
  272. export interface BoundsSnapshot extends PointSnapshot {
  273. nw: number
  274. nh: number
  275. }
  276. export type Difference<A, B> = A extends B ? never : A
  277. export type ShapeSpecificProps<T extends Shape> = Pick<
  278. T,
  279. Difference<keyof T, keyof BaseShape>
  280. >
  281. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  282. export type ShapeUtil<K extends Shape> = {
  283. create(props: Partial<K>): K
  284. getBounds(shape: K): Bounds
  285. hitTest(shape: K, test: number[]): boolean
  286. hitTestBounds(shape: K, bounds: Bounds): boolean
  287. rotate(shape: K): K
  288. translate(shape: K, delta: number[]): K
  289. scale(shape: K, scale: number): K
  290. stretch(shape: K, scaleX: number, scaleY: number): K
  291. render(shape: K): JSX.Element
  292. }
  293. export enum MoveType {
  294. Backward,
  295. Forward,
  296. ToFront,
  297. ToBack,
  298. }
  299. export enum AlignType {
  300. Top,
  301. CenterVertical,
  302. Bottom,
  303. Left,
  304. CenterHorizontal,
  305. Right,
  306. }
  307. export enum StretchType {
  308. Horizontal,
  309. Vertical,
  310. }
  311. export enum DistributeType {
  312. Horizontal,
  313. Vertical,
  314. }
  315. /* -------------------------------------------------- */
  316. /* Code Editor */
  317. /* -------------------------------------------------- */
  318. export type IMonaco = typeof monaco
  319. export type IMonacoEditor = monaco.editor.IStandaloneCodeEditor
  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. min?: number
  334. max?: number
  335. value: number
  336. step: number
  337. format?: (value: number) => number
  338. }
  339. export interface VectorCodeControl extends BaseCodeControl {
  340. type: ControlType.Vector
  341. value: number[]
  342. isNormalized: boolean
  343. format?: (value: number[]) => number[]
  344. }
  345. export interface TextCodeControl extends BaseCodeControl {
  346. type: ControlType.Text
  347. value: string
  348. format?: (value: string) => string
  349. }
  350. export interface SelectCodeControl<T extends string = ''>
  351. extends BaseCodeControl {
  352. type: ControlType.Select
  353. value: T
  354. options: T[]
  355. format?: (string: T) => string
  356. }
  357. export type CodeControl =
  358. | NumberCodeControl
  359. | VectorCodeControl
  360. | TextCodeControl
  361. | SelectCodeControl
  362. export type PropsOfType<T extends object, K> = {
  363. [K in keyof T]: T[K] extends boolean ? K : never
  364. }[keyof T]
  365. export type Mutable<T extends Shape> = { -readonly [K in keyof T]: T[K] }