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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
  2. /* -------------------------------------------------- */
  3. /* Client State */
  4. /* -------------------------------------------------- */
  5. export interface Data {
  6. isReadOnly: boolean
  7. settings: {
  8. fontSize: number
  9. isDarkMode: boolean
  10. isCodeOpen: boolean
  11. isStyleOpen: boolean
  12. nudgeDistanceSmall: number
  13. nudgeDistanceLarge: number
  14. isToolLocked: boolean
  15. isPenLocked: boolean
  16. }
  17. currentStyle: ShapeStyles & TextStyles
  18. activeTool: ShapeType | 'select'
  19. brush?: Bounds
  20. boundsRotation: number
  21. pointedId?: string
  22. hoveredId?: string
  23. editingId?: 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. id: string
  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. scale: number
  174. fontSize: FontSize
  175. }
  176. export interface GroupShape extends BaseShape {
  177. type: ShapeType.Group
  178. children: string[]
  179. size: number[]
  180. }
  181. export type MutableShape =
  182. | DotShape
  183. | CircleShape
  184. | EllipseShape
  185. | LineShape
  186. | RayShape
  187. | PolylineShape
  188. | DrawShape
  189. | RectangleShape
  190. | ArrowShape
  191. | TextShape
  192. | GroupShape
  193. export interface Shapes {
  194. [ShapeType.Dot]: Readonly<DotShape>
  195. [ShapeType.Circle]: Readonly<CircleShape>
  196. [ShapeType.Ellipse]: Readonly<EllipseShape>
  197. [ShapeType.Line]: Readonly<LineShape>
  198. [ShapeType.Ray]: Readonly<RayShape>
  199. [ShapeType.Polyline]: Readonly<PolylineShape>
  200. [ShapeType.Draw]: Readonly<DrawShape>
  201. [ShapeType.Rectangle]: Readonly<RectangleShape>
  202. [ShapeType.Arrow]: Readonly<ArrowShape>
  203. [ShapeType.Text]: Readonly<TextShape>
  204. [ShapeType.Group]: Readonly<GroupShape>
  205. }
  206. export type Shape = Readonly<MutableShape>
  207. export type ShapeByType<T extends ShapeType> = Shapes[T]
  208. export interface CodeFile {
  209. id: string
  210. name: string
  211. code: string
  212. }
  213. export enum Decoration {
  214. Arrow = 'Arrow',
  215. }
  216. export interface ShapeBinding {
  217. id: string
  218. index: number
  219. point: number[]
  220. }
  221. export interface ShapeHandle {
  222. id: string
  223. index: number
  224. point: number[]
  225. }
  226. /* -------------------------------------------------- */
  227. /* Editor UI */
  228. /* -------------------------------------------------- */
  229. export interface PointerInfo {
  230. target: string
  231. pointerId: number
  232. origin: number[]
  233. point: number[]
  234. pressure: number
  235. shiftKey: boolean
  236. ctrlKey: boolean
  237. metaKey: boolean
  238. altKey: boolean
  239. }
  240. export enum Edge {
  241. Top = 'top_edge',
  242. Right = 'right_edge',
  243. Bottom = 'bottom_edge',
  244. Left = 'left_edge',
  245. }
  246. export enum Corner {
  247. TopLeft = 'top_left_corner',
  248. TopRight = 'top_right_corner',
  249. BottomRight = 'bottom_right_corner',
  250. BottomLeft = 'bottom_left_corner',
  251. }
  252. export interface Bounds {
  253. minX: number
  254. minY: number
  255. maxX: number
  256. maxY: number
  257. width: number
  258. height: number
  259. rotation?: number
  260. }
  261. export interface RotatedBounds extends Bounds {
  262. rotation: number
  263. }
  264. export interface ShapeBounds extends Bounds {
  265. id: string
  266. }
  267. export interface PointSnapshot extends Bounds {
  268. nx: number
  269. nmx: number
  270. ny: number
  271. nmy: number
  272. }
  273. export interface BoundsSnapshot extends PointSnapshot {
  274. nw: number
  275. nh: number
  276. }
  277. export type Difference<A, B> = A extends B ? never : A
  278. export type ShapeSpecificProps<T extends Shape> = Pick<
  279. T,
  280. Difference<keyof T, keyof BaseShape>
  281. >
  282. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  283. export type ShapeUtil<K extends Shape> = {
  284. create(props: Partial<K>): K
  285. getBounds(shape: K): Bounds
  286. hitTest(shape: K, test: number[]): boolean
  287. hitTestBounds(shape: K, bounds: Bounds): boolean
  288. rotate(shape: K): K
  289. translate(shape: K, delta: number[]): K
  290. scale(shape: K, scale: number): K
  291. stretch(shape: K, scaleX: number, scaleY: number): K
  292. render(shape: K): JSX.Element
  293. }
  294. export enum MoveType {
  295. Backward,
  296. Forward,
  297. ToFront,
  298. ToBack,
  299. }
  300. export enum AlignType {
  301. Top,
  302. CenterVertical,
  303. Bottom,
  304. Left,
  305. CenterHorizontal,
  306. Right,
  307. }
  308. export enum StretchType {
  309. Horizontal,
  310. Vertical,
  311. }
  312. export enum DistributeType {
  313. Horizontal,
  314. Vertical,
  315. }
  316. /* -------------------------------------------------- */
  317. /* Code Editor */
  318. /* -------------------------------------------------- */
  319. export type IMonaco = typeof monaco
  320. export type IMonacoEditor = monaco.editor.IStandaloneCodeEditor
  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. min?: number
  335. max?: number
  336. value: number
  337. step: number
  338. format?: (value: number) => number
  339. }
  340. export interface VectorCodeControl extends BaseCodeControl {
  341. type: ControlType.Vector
  342. value: number[]
  343. isNormalized: boolean
  344. format?: (value: number[]) => number[]
  345. }
  346. export interface TextCodeControl extends BaseCodeControl {
  347. type: ControlType.Text
  348. value: string
  349. format?: (value: string) => string
  350. }
  351. export interface SelectCodeControl<T extends string = ''>
  352. extends BaseCodeControl {
  353. type: ControlType.Select
  354. value: T
  355. options: T[]
  356. format?: (string: T) => string
  357. }
  358. export type CodeControl =
  359. | NumberCodeControl
  360. | VectorCodeControl
  361. | TextCodeControl
  362. | SelectCodeControl
  363. export type PropsOfType<T extends object, K> = {
  364. [K in keyof T]: T[K] extends boolean ? K : never
  365. }[keyof T]
  366. export type Mutable<T extends Shape> = { -readonly [K in keyof T]: T[K] }