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

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