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

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