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

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