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

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