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

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