Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

types.ts 7.8KB

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