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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. }
  56. // Consider:
  57. // Glob = "glob",
  58. // Spline = "spline",
  59. // Cubic = "cubic",
  60. // Conic = "conic",
  61. export type ShapeStyles = Partial<React.SVGProps<SVGUseElement>>
  62. export interface BaseShape {
  63. id: string
  64. type: ShapeType
  65. parentId: string
  66. childIndex: number
  67. isGenerated: boolean
  68. name: string
  69. point: number[]
  70. rotation: number
  71. style: ShapeStyles
  72. }
  73. export interface DotShape extends BaseShape {
  74. type: ShapeType.Dot
  75. }
  76. export interface CircleShape extends BaseShape {
  77. type: ShapeType.Circle
  78. radius: number
  79. }
  80. export interface EllipseShape extends BaseShape {
  81. type: ShapeType.Ellipse
  82. radiusX: number
  83. radiusY: number
  84. }
  85. export interface LineShape extends BaseShape {
  86. type: ShapeType.Line
  87. direction: number[]
  88. }
  89. export interface RayShape extends BaseShape {
  90. type: ShapeType.Ray
  91. direction: number[]
  92. }
  93. export interface PolylineShape extends BaseShape {
  94. type: ShapeType.Polyline
  95. points: number[][]
  96. }
  97. export interface RectangleShape extends BaseShape {
  98. type: ShapeType.Rectangle
  99. size: number[]
  100. radius: number
  101. }
  102. export interface DrawShape extends BaseShape {
  103. type: ShapeType.Draw
  104. points: number[][]
  105. }
  106. export type MutableShape =
  107. | DotShape
  108. | CircleShape
  109. | EllipseShape
  110. | LineShape
  111. | RayShape
  112. | PolylineShape
  113. | DrawShape
  114. | RectangleShape
  115. export type Shape = Readonly<MutableShape>
  116. export interface Shapes {
  117. [ShapeType.Dot]: Readonly<DotShape>
  118. [ShapeType.Circle]: Readonly<CircleShape>
  119. [ShapeType.Ellipse]: Readonly<EllipseShape>
  120. [ShapeType.Line]: Readonly<LineShape>
  121. [ShapeType.Ray]: Readonly<RayShape>
  122. [ShapeType.Polyline]: Readonly<PolylineShape>
  123. [ShapeType.Draw]: Readonly<DrawShape>
  124. [ShapeType.Rectangle]: Readonly<RectangleShape>
  125. }
  126. export type ShapeByType<T extends ShapeType> = Shapes[T]
  127. export interface CodeFile {
  128. id: string
  129. name: string
  130. code: string
  131. }
  132. /* -------------------------------------------------- */
  133. /* Editor UI */
  134. /* -------------------------------------------------- */
  135. export interface PointerInfo {
  136. target: string
  137. pointerId: number
  138. origin: number[]
  139. point: number[]
  140. shiftKey: boolean
  141. ctrlKey: boolean
  142. metaKey: boolean
  143. altKey: boolean
  144. }
  145. export enum Edge {
  146. Top = 'top_edge',
  147. Right = 'right_edge',
  148. Bottom = 'bottom_edge',
  149. Left = 'left_edge',
  150. }
  151. export enum Corner {
  152. TopLeft = 'top_left_corner',
  153. TopRight = 'top_right_corner',
  154. BottomRight = 'bottom_right_corner',
  155. BottomLeft = 'bottom_left_corner',
  156. }
  157. export interface Bounds {
  158. minX: number
  159. minY: number
  160. maxX: number
  161. maxY: number
  162. width: number
  163. height: number
  164. }
  165. export interface RotatedBounds extends Bounds {
  166. rotation: number
  167. }
  168. export interface ShapeBounds extends Bounds {
  169. id: string
  170. }
  171. export interface PointSnapshot extends Bounds {
  172. nx: number
  173. nmx: number
  174. ny: number
  175. nmy: number
  176. }
  177. export interface BoundsSnapshot extends PointSnapshot {
  178. nw: number
  179. nh: number
  180. }
  181. export type Difference<A, B> = A extends B ? never : A
  182. export type ShapeSpecificProps<T extends Shape> = Pick<
  183. T,
  184. Difference<keyof T, keyof BaseShape>
  185. >
  186. export type ShapeIndicatorProps<T extends Shape> = ShapeSpecificProps<T>
  187. export type ShapeUtil<K extends Shape> = {
  188. create(props: Partial<K>): K
  189. getBounds(shape: K): Bounds
  190. hitTest(shape: K, test: number[]): boolean
  191. hitTestBounds(shape: K, bounds: Bounds): boolean
  192. rotate(shape: K): K
  193. translate(shape: K, delta: number[]): K
  194. scale(shape: K, scale: number): K
  195. stretch(shape: K, scaleX: number, scaleY: number): K
  196. render(shape: K): JSX.Element
  197. }
  198. export enum MoveType {
  199. Backward,
  200. Forward,
  201. ToFront,
  202. ToBack,
  203. }
  204. export enum AlignType {
  205. Top,
  206. CenterVertical,
  207. Bottom,
  208. Left,
  209. CenterHorizontal,
  210. Right,
  211. }
  212. export enum StretchType {
  213. Horizontal,
  214. Vertical,
  215. }
  216. export enum DistributeType {
  217. Horizontal,
  218. Vertical,
  219. }
  220. /* -------------------------------------------------- */
  221. /* Code Editor */
  222. /* -------------------------------------------------- */
  223. export type IMonaco = typeof monaco
  224. export type IMonacoEditor = monaco.editor.IStandaloneCodeEditor
  225. export enum ControlType {
  226. Number = 'number',
  227. Vector = 'vector',
  228. Text = 'text',
  229. Select = 'select',
  230. }
  231. export interface BaseCodeControl {
  232. id: string
  233. type: ControlType
  234. label: string
  235. }
  236. export interface NumberCodeControl extends BaseCodeControl {
  237. type: ControlType.Number
  238. min?: number
  239. max?: number
  240. value: number
  241. step: number
  242. format?: (value: number) => number
  243. }
  244. export interface VectorCodeControl extends BaseCodeControl {
  245. type: ControlType.Vector
  246. value: number[]
  247. isNormalized: boolean
  248. format?: (value: number[]) => number[]
  249. }
  250. export interface TextCodeControl extends BaseCodeControl {
  251. type: ControlType.Text
  252. value: string
  253. format?: (value: string) => string
  254. }
  255. export interface SelectCodeControl<T extends string = ''>
  256. extends BaseCodeControl {
  257. type: ControlType.Select
  258. value: T
  259. options: T[]
  260. format?: (string: T) => string
  261. }
  262. export type CodeControl =
  263. | NumberCodeControl
  264. | VectorCodeControl
  265. | TextCodeControl
  266. | SelectCodeControl